Persist default state of selwand and navwand in session (#1600)

* Persist default state of selwand and navwand in session

* Fixed some issues with the way it retained info

* Fixed it setting the wrong item

* A little cleaner

* I somewhat misunderstood the way this system worked - This is cleaner and works a lot better

* Replace with octy-approved UX

* Improve logic for default wand loading

Co-authored-by: Octavia Togami <octavia.togami@gmail.com>
This commit is contained in:
Matthew Miller 2020-12-09 15:03:42 +10:00 committed by GitHub
parent 3637d94aef
commit 15cba009f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -65,6 +65,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
@ -116,7 +117,9 @@ public class LocalSession {
private RegionSelectorType defaultSelector;
private boolean useServerCUI = false; // Save this to not annoy players.
private String wandItem;
private Boolean wandItemDefault;
private String navWandItem;
private Boolean navWandItemDefault;
/**
* Construct the object.
@ -677,9 +680,15 @@ public class LocalSession {
throw new InvalidToolBindException(item, TranslatableComponent.of("worldedit.tool.error.item-only"));
}
if (tool instanceof SelectionWand) {
setSingleItemTool(id -> this.wandItem = id, this.wandItem, item);
setSingleItemTool(id -> {
this.wandItem = id;
this.wandItemDefault = id.equals(config.wandItem);
}, this.wandItem, item);
} else if (tool instanceof NavigationWand) {
setSingleItemTool(id -> this.navWandItem = id, this.navWandItem, item);
setSingleItemTool(id -> {
this.navWandItem = id;
this.navWandItemDefault = id.equals(config.navigationWand);
}, this.navWandItem, item);
} else if (tool == null) {
// Check if un-setting sel/nav
String id = item.getId();
@ -1106,6 +1115,19 @@ public class LocalSession {
return wandItem;
}
/**
* Get if the selection wand item should use the default, or null if unknown.
*
* @return if it should use the default
*/
public boolean isWandItemDefault() {
if (wandItemDefault == null) {
wandItemDefault = Objects.equals(wandItem, config.wandItem);
setDirty();
}
return wandItemDefault;
}
/**
* Get the preferred navigation wand item for this user, or {@code null} to use the default.
* @return item id of nav wand item, or {@code null}
@ -1114,6 +1136,19 @@ public class LocalSession {
return navWandItem;
}
/**
* Get if the navigation wand item should use the default, or null if unknown.
*
* @return if it should use the default
*/
public boolean isNavWandItemDefault() {
if (navWandItemDefault == null) {
navWandItemDefault = Objects.equals(navWandItem, config.navigationWand);
setDirty();
}
return navWandItemDefault;
}
/**
* Get the last block distribution stored in this session.
*

View File

@ -171,14 +171,16 @@ public class SessionManager {
session.setBlockChangeLimit(config.defaultChangeLimit);
session.setTimeout(config.calculationTimeout);
try {
setDefaultWand(session.getWandItem(), config.wandItem, session, new SelectionWand());
String sessionItem = session.isWandItemDefault() ? null : session.getWandItem();
setDefaultWand(sessionItem, config.wandItem, session, new SelectionWand());
} catch (InvalidToolBindException e) {
if (warnedInvalidTool.add("selwand")) {
log.warn("Invalid selection wand tool set in config. Tool will not be assigned: " + e.getItemType());
}
}
try {
setDefaultWand(session.getNavWandItem(), config.navigationWand, session, new NavigationWand());
String sessionItem = session.isNavWandItemDefault() ? null : session.getNavWandItem();
setDefaultWand(sessionItem, config.navigationWand, session, new NavigationWand());
} catch (InvalidToolBindException e) {
if (warnedInvalidTool.add("navwand")) {
log.warn("Invalid navigation wand tool set in config. Tool will not be assigned: " + e.getItemType());