feat: save region selector to session store (#2621)

* feat: save region selector to session store
 - closes #2397

* Move new gson adapters to FAWE packages
This commit is contained in:
Jordan 2024-03-17 20:25:43 +01:00 committed by GitHub
parent b93f01c5b3
commit b512182e1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 79 additions and 11 deletions

View File

@ -1,4 +1,4 @@
package com.sk89q.worldedit.util.gson;
package com.fastasyncworldedit.core.util.gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;

View File

@ -0,0 +1,33 @@
package com.fastasyncworldedit.core.util.gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.RegionSelectorType;
import java.lang.reflect.Type;
public class RegionSelectorAdapter implements JsonDeserializer<RegionSelector>, JsonSerializer<RegionSelector> {
@Override
public RegionSelector deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
RegionSelectorType regionType = RegionSelectorType.valueOf(json.getAsString());
return regionType.createSelector();
}
@Override
public JsonElement serialize(RegionSelector selector, Type type, JsonSerializationContext context) {
RegionSelectorType regionType = RegionSelectorType.getForSelector(selector);
// Cannot nicely deserialize Fuzzy region type
if (regionType == null || regionType == RegionSelectorType.FUZZY) {
return null;
}
return new JsonPrimitive(regionType.toString());
}
}

View File

@ -125,7 +125,9 @@ public class LocalSession implements TextureHolder {
private transient int cuiVersion = CUI_VERSION_UNINITIALIZED;
// Session related
private transient RegionSelector selector = new CuboidRegionSelector();
//FAWE start - allow saving to session store
private RegionSelector selector = new CuboidRegionSelector();
//FAWE end
private transient boolean placeAtPos1 = false;
//FAWE start
private final transient List<Object> history = Collections.synchronizedList(new LinkedList<>() {
@ -771,6 +773,7 @@ public class LocalSession implements TextureHolder {
checkNotNull(selector);
selector.setWorld(world);
this.selector = selector;
setDirty();
if (hasWorldOverride() && !world.equals(getWorldOverride())) {
setWorldOverride(null);
}

View File

@ -755,13 +755,7 @@ public class SelectionCommands {
}
if (setDefaultSelector) {
RegionSelectorType found = null;
for (RegionSelectorType type : RegionSelectorType.values()) {
if (type.getSelectorClass() == newSelector.getClass()) {
found = type;
break;
}
}
RegionSelectorType found = RegionSelectorType.getForSelector(newSelector);
if (found != null) {
session.setDefaultRegionSelector(found);

View File

@ -19,8 +19,14 @@
package com.sk89q.worldedit.regions.selector;
import com.fastasyncworldedit.core.regions.selector.FuzzyRegionSelector;
import com.fastasyncworldedit.core.regions.selector.PolyhedralRegionSelector;
import com.sk89q.worldedit.regions.RegionSelector;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* An enum of default region selector types.
*/
@ -32,7 +38,21 @@ public enum RegionSelectorType {
SPHERE(SphereRegionSelector.class),
ELLIPSOID(EllipsoidRegionSelector.class),
POLYGON(Polygonal2DRegionSelector.class),
CONVEX_POLYHEDRON(ConvexPolyhedralRegionSelector.class);
CONVEX_POLYHEDRON(ConvexPolyhedralRegionSelector.class),
//FAWE start
POLYHEDRAL(PolyhedralRegionSelector.class),
FUZZY(FuzzyRegionSelector.class);
//FAWE end
//FAWE start
private static final Map<Class<? extends RegionSelector>, RegionSelectorType> VALUE_MAP = new HashMap<>();
static {
for (RegionSelectorType type : values()) {
VALUE_MAP.put(type.getSelectorClass(), type);
}
}
//FAWE end
private final Class<? extends RegionSelector> selectorClass;
@ -40,6 +60,19 @@ public enum RegionSelectorType {
this.selectorClass = selectorClass;
}
//FAWE start
/**
* Get a {@link RegionSelectorType} for the given {@link RegionSelector}
*
* @param selector Region selector to get type enum for
* @since TODO
*/
@Nullable
public static RegionSelectorType getForSelector(RegionSelector selector) {
return VALUE_MAP.get(selector.getClass());
}
//FAWE end
/**
* Get the selector class.
*

View File

@ -19,12 +19,14 @@
package com.sk89q.worldedit.util.gson;
import com.fastasyncworldedit.core.util.gson.ItemTypeAdapter;
import com.fastasyncworldedit.core.util.gson.RegionSelectorAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
/**
* Utility methods for Google's GSON library.
@ -43,7 +45,10 @@ public final class GsonUtil {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
gsonBuilder.registerTypeAdapter(BlockVector3.class, new BlockVectorAdapter());
//FAWE start
gsonBuilder.registerTypeAdapter(RegionSelector.class, new RegionSelectorAdapter());
gsonBuilder.registerTypeAdapter(ItemType.class, new ItemTypeAdapter());
//FAWE end
return gsonBuilder;
}