From 09d22c52a129d7b5cfb01c70f028bce36ee0639b Mon Sep 17 00:00:00 2001 From: Madeline Miller Date: Sat, 22 Jul 2023 14:45:58 +1000 Subject: [PATCH] Preserve player-set side effects --- .../com/sk89q/worldedit/LocalSession.java | 5 +-- .../session/storage/JsonFileSessionStore.java | 5 ++- .../sk89q/worldedit/util/SideEffectSet.java | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index e727756075..cc6afed5aa 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -107,7 +107,6 @@ public class LocalSession { private transient boolean useInventory; private transient com.sk89q.worldedit.world.snapshot.Snapshot snapshot; private transient Snapshot snapshotExperimental; - private transient SideEffectSet sideEffectSet = SideEffectSet.defaults(); private transient Mask mask; private transient ZoneId timezone = ZoneId.systemDefault(); private transient BlockVector3 cuiTemporaryBlock; @@ -127,6 +126,7 @@ public class LocalSession { private Boolean wandItemDefault; private String navWandItem; private Boolean navWandItemDefault; + private SideEffectSet sideEffectSet = SideEffectSet.defaults(); /** * Construct the object. @@ -1159,6 +1159,7 @@ public SideEffectSet getSideEffectSet() { */ public void setSideEffectSet(SideEffectSet sideEffectSet) { this.sideEffectSet = sideEffectSet; + setDirty(); } /** @@ -1178,7 +1179,7 @@ public boolean hasFastMode() { */ @Deprecated public void setFastMode(boolean fastMode) { - this.sideEffectSet = fastMode ? SideEffectSet.none() : SideEffectSet.defaults(); + setSideEffectSet(fastMode ? SideEffectSet.none() : SideEffectSet.defaults()); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java index 781de3b9ce..bdc8464b13 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java @@ -25,6 +25,7 @@ import com.google.gson.JsonParseException; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.internal.util.LogManagerCompat; +import com.sk89q.worldedit.util.SideEffectSet; import com.sk89q.worldedit.util.gson.GsonUtil; import org.apache.logging.log4j.Logger; @@ -77,7 +78,9 @@ public JsonFileSessionStore(Path dir) { this.dir = dir; GsonBuilder builder = GsonUtil.createBuilder(); - gson = builder.create(); + gson = builder + .registerTypeAdapter(SideEffectSet.class, new SideEffectSet.GsonSerializer()) + .create(); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java index e57ee500d6..8b23df61ea 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java @@ -21,7 +21,16 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import java.lang.reflect.Type; import java.util.Arrays; import java.util.EnumMap; import java.util.Map; @@ -91,4 +100,28 @@ public static SideEffectSet defaults() { public static SideEffectSet none() { return NONE; } + + public static class GsonSerializer implements JsonSerializer, JsonDeserializer { + + @Override + public SideEffectSet deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + Map sideEffects = Maps.newEnumMap(SideEffect.class); + JsonObject obj = json.getAsJsonObject(); + for (Map.Entry stringJsonElementEntry : obj.entrySet()) { + SideEffect sideEffect = SideEffect.valueOf(stringJsonElementEntry.getKey()); + SideEffect.State state = SideEffect.State.valueOf(stringJsonElementEntry.getValue().getAsString()); + sideEffects.put(sideEffect, state); + } + return new SideEffectSet(sideEffects); + } + + @Override + public JsonElement serialize(SideEffectSet src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject obj = new JsonObject(); + for (Map.Entry entry : src.sideEffects.entrySet()) { + obj.add(entry.getKey().name(), new JsonPrimitive(entry.getValue().name())); + } + return obj; + } + } }