Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve player-set side effects #2367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -127,6 +126,7 @@ public class LocalSession {
private Boolean wandItemDefault;
private String navWandItem;
private Boolean navWandItemDefault;
private SideEffectSet sideEffectSet = SideEffectSet.defaults();

/**
* Construct the object.
Expand Down Expand Up @@ -1159,6 +1159,7 @@ public SideEffectSet getSideEffectSet() {
*/
public void setSideEffectSet(SideEffectSet sideEffectSet) {
this.sideEffectSet = sideEffectSet;
setDirty();
}

/**
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,4 +100,28 @@ public static SideEffectSet defaults() {
public static SideEffectSet none() {
return NONE;
}

public static class GsonSerializer implements JsonSerializer<SideEffectSet>, JsonDeserializer<SideEffectSet> {

@Override
public SideEffectSet deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Map<SideEffect, SideEffect.State> sideEffects = Maps.newEnumMap(SideEffect.class);
JsonObject obj = json.getAsJsonObject();
for (Map.Entry<String, JsonElement> 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<SideEffect, SideEffect.State> entry : src.sideEffects.entrySet()) {
obj.add(entry.getKey().name(), new JsonPrimitive(entry.getValue().name()));
}
return obj;
}
}
}