Compare commits

...

8 Commits

9 changed files with 94 additions and 57 deletions

View File

@@ -3,6 +3,7 @@ package fr.pandacube.lib.core.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.Strictness;
import com.google.gson.ToNumberStrategy;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
@@ -74,21 +75,21 @@ public class Json {
public static final Gson gson = build(Function.identity());
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#setPrettyPrinting()} and support
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#setPrettyPrinting()} and support
* for Java records and additional {@link TypeAdapterFactory} provided with
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting);
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()} and support for
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#serializeNulls()} and support for
* Java records and additional {@link TypeAdapterFactory} provided with
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls);
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()},
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#serializeNulls()},
* {@link GsonBuilder#setPrettyPrinting()} and support for Java records and additional {@link TypeAdapterFactory}
* provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
@@ -105,7 +106,7 @@ public class Json {
.registerTypeAdapterFactory(new CustomAdapterFactory())
.disableHtmlEscaping()
.setObjectToNumberStrategy(YAML_EQUIVALENT_NUMBER_STRATEGY)
.setLenient();
.setStrictness(Strictness.LENIENT);
return builderModifier.apply(base).create();
}

View File

@@ -18,6 +18,9 @@ import java.util.Objects;
*/
public class DummyPlayerInventory extends InventoryWrapper implements PlayerInventory {
/**
* Total number of item slots in the player inventory.
*/
public static final int PLAYER_INVENTORY_SIZE = 43; // 36 base inventory + 4 armor slots + 1 off hand + 2 hidden slots (body and saddle)
private int heldItemSlot;
@@ -116,18 +119,34 @@ public class DummyPlayerInventory extends InventoryWrapper implements PlayerInve
setItem(36, boots);
}
/**
* Gets the item stack in the SADDLE {@link EquipmentSlot}.
* @return the SADDLE item stack.
*/
public ItemStack getSaddle() {
return getItem(42);
}
/**
* Puts the provided item stack in the SADDLE {@link EquipmentSlot}.
* @param saddle the item.
*/
public void setSaddle(@Nullable ItemStack saddle) {
setItem(42, saddle);
}
/**
* Gets the item stack in the BODY {@link EquipmentSlot}.
* @return the BODY item stack.
*/
public ItemStack getBody() {
return getItem(41);
}
/**
* Puts the provided item stack in the BODY {@link EquipmentSlot}.
* @param body the item.
*/
public void setBody(@Nullable ItemStack body) {
setItem(41, body);
}

View File

@@ -9,10 +9,13 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.Strictness;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.bind.TreeTypeAdapter;
import com.google.gson.reflect.TypeToken;
import net.kyori.adventure.key.Key;
import org.bukkit.Bukkit;
import org.bukkit.Registry;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.inventory.ItemStack;
@@ -28,16 +31,25 @@ import java.util.Map;
private static final TypeToken<Map<String, Object>> MAP_STR_OBJ_TYPE = new TypeToken<>() { };
/** Gson instance with no custom type adapter */
private static final Gson vanillaGson = new GsonBuilder().setLenient().create();
private static final Gson vanillaGson = new GsonBuilder().setStrictness(Strictness.LENIENT).create();
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonObject jsonObj))
throw new JsonParseException("Unable to deserialize a ConfigurationSerializable from the provided json structure.");
// if it contains both old and new data, delete the old one introduced for compatibility
if (jsonObj.has("DataVersion") || jsonObj.has("id")) {
jsonObj.remove("v");
jsonObj.remove("type");
}
// format used when using ConfigurationSerialization
if (jsonObj.has(ConfigurationSerialization.SERIALIZED_TYPE_KEY))
return context.deserialize(jsonObj, ConfigurationSerializable.class);
if (jsonObj.has("meta")
&& jsonObj.get("meta") instanceof JsonObject metaJson
&& !metaJson.has(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
@@ -47,6 +59,8 @@ import java.util.Map;
Map<String, Object> map = context.deserialize(jsonObj, MAP_STR_OBJ_TYPE.getType());
fixDeserializationVersion(map);
map.remove("meta");
ItemStack is = ItemStack.deserialize(map);
Class<? extends ItemMeta> metaClass = is.getItemMeta().getClass();

View File

@@ -179,9 +179,7 @@ public record PlayerDataWrapper(CompoundTag data) {
private int getHeldItemSlot() {
if (!data.contains("SelectedItemSlot"))
return 0;
return data.getInt("SelectedItemSlot");
return data.getInt("SelectedItemSlot").orElse(0);
}
private void setHeldItemSlot(int slot) {
@@ -194,9 +192,7 @@ public record PlayerDataWrapper(CompoundTag data) {
* @return the value of Score.
*/
public int getScore() {
if (!data.contains("Score"))
return 0;
return data.getInt("Score");
return data.getInt("Score").orElse(0);
}
@@ -214,9 +210,7 @@ public record PlayerDataWrapper(CompoundTag data) {
* @return the value of XpTotal.
*/
public int getTotalExperience() {
if (!data.contains("XpTotal"))
return 0;
return data.getInt("XpTotal");
return data.getInt("XpTotal").orElse(0);
}
/**

View File

@@ -7,8 +7,8 @@ import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
@@ -24,13 +24,11 @@ public class CompoundTag extends ReflectWrapper implements Tag {
private static final ReflectMethod<?> putInt = wrapEx(() -> REFLECT.method("putInt", String.class, int.class));
private static final ReflectMethod<?> putIntArray = wrapEx(() -> REFLECT.method("putIntArray", String.class, int[].class));
private static final ReflectMethod<?> putString = wrapEx(() -> REFLECT.method("putString", String.class, String.class));
private static final ReflectMethod<?> putUUID = wrapEx(() -> REFLECT.method("putUUID", String.class, UUID.class));
private static final ReflectMethod<?> putLong = wrapEx(() -> REFLECT.method("putLong", String.class, long.class));
private static final ReflectMethod<?> putLongArray = wrapEx(() -> REFLECT.method("putLongArray", String.class, long[].class));
private static final ReflectMethod<?> putShort = wrapEx(() -> REFLECT.method("putShort", String.class, short.class));
private static final ReflectMethod<?> put = wrapEx(() -> REFLECT.method("put", String.class, Tag.REFLECT.get()));
private static final ReflectMethod<?> getTagType = wrapEx(() -> REFLECT.method("getTagType", String.class));
private static final ReflectMethod<?> getByte = wrapEx(() -> REFLECT.method("getByte", String.class));
private static final ReflectMethod<?> getShort = wrapEx(() -> REFLECT.method("getShort", String.class));
private static final ReflectMethod<?> getInt = wrapEx(() -> REFLECT.method("getInt", String.class));
@@ -43,14 +41,13 @@ public class CompoundTag extends ReflectWrapper implements Tag {
private static final ReflectMethod<?> getLongArray = wrapEx(() -> REFLECT.method("getLongArray", String.class));
private static final ReflectMethod<?> getCompound = wrapEx(() -> REFLECT.method("getCompound", String.class));
private static final ReflectMethod<?> getBoolean = wrapEx(() -> REFLECT.method("getBoolean", String.class));
private static final ReflectMethod<?> getList = wrapEx(() -> REFLECT.method("getList", String.class, int.class));
private static final ReflectMethod<?> getList = wrapEx(() -> REFLECT.method("getList", String.class));
private static final ReflectMethod<?> get = wrapEx(() -> REFLECT.method("get", String.class));
private static final ReflectMethod<?> getAllKeys = wrapEx(() -> REFLECT.method("getAllKeys"));
private static final ReflectMethod<?> keySet = wrapEx(() -> REFLECT.method("keySet"));
private static final ReflectMethod<?> entrySet = wrapEx(() -> REFLECT.method("entrySet"));
private static final ReflectMethod<?> size = wrapEx(() -> REFLECT.method("size"));
private static final ReflectMethod<?> contains = wrapEx(() -> REFLECT.method("contains", String.class));
private static final ReflectMethod<?> containsStringInt = wrapEx(() -> REFLECT.method("contains", String.class, int.class));
public CompoundTag() {
this(wrapReflectEx(() -> CONSTRUCTOR.instantiate()));
@@ -84,9 +81,6 @@ public class CompoundTag extends ReflectWrapper implements Tag {
public void putString(String key, String value) {
wrapReflectEx(() -> putString.invoke(__getRuntimeInstance(), key, value));
}
public void putUUID(String key, UUID value) {
wrapReflectEx(() -> putUUID.invoke(__getRuntimeInstance(), key, value));
}
public void putLong(String key, long value) {
wrapReflectEx(() -> putLong.invoke(__getRuntimeInstance(), key, value));
}
@@ -99,54 +93,64 @@ public class CompoundTag extends ReflectWrapper implements Tag {
public void put(String key, Tag value) {
wrapReflectEx(() -> put.invoke(__getRuntimeInstance(), key, unwrap(value)));
}
public byte getTagType(String key) {
return (byte) wrapReflectEx(() -> getTagType.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Byte> getByte(String key) {
return (Optional<Byte>) wrapReflectEx(() -> getByte.invoke(__getRuntimeInstance(), key));
}
public byte getByte(String key) {
return (byte) wrapReflectEx(() -> getByte.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Short> getShort(String key) {
return (Optional<Short>) wrapReflectEx(() -> getShort.invoke(__getRuntimeInstance(), key));
}
public short getShort(String key) {
return (short) wrapReflectEx(() -> getShort.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Integer> getInt(String key) {
return (Optional<Integer>) wrapReflectEx(() -> getInt.invoke(__getRuntimeInstance(), key));
}
public int getInt(String key) {
return (int) wrapReflectEx(() -> getInt.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Long> getLong(String key) {
return (Optional<Long>) wrapReflectEx(() -> getLong.invoke(__getRuntimeInstance(), key));
}
public long getLong(String key) {
return (long) wrapReflectEx(() -> getLong.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Float> getFloat(String key) {
return (Optional<Float>) wrapReflectEx(() -> getFloat.invoke(__getRuntimeInstance(), key));
}
public float getFloat(String key) {
return (float) wrapReflectEx(() -> getFloat.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<Double> getDouble(String key) {
return (Optional<Double>) wrapReflectEx(() -> getDouble.invoke(__getRuntimeInstance(), key));
}
public double getDouble(String key) {
return (double) wrapReflectEx(() -> getDouble.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<String> getString(String key) {
return (Optional<String>) wrapReflectEx(() -> getString.invoke(__getRuntimeInstance(), key));
}
public String getString(String key) {
return (String) wrapReflectEx(() -> getString.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<byte[]> getByteArray(String key) {
return (Optional<byte[]>) wrapReflectEx(() -> getByteArray.invoke(__getRuntimeInstance(), key));
}
public byte[] getByteArray(String key) {
return (byte[]) wrapReflectEx(() -> getByteArray.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<int[]> getIntArray(String key) {
return (Optional<int[]>) wrapReflectEx(() -> getIntArray.invoke(__getRuntimeInstance(), key));
}
public int[] getIntArray(String key) {
return (int[]) wrapReflectEx(() -> getIntArray.invoke(__getRuntimeInstance(), key));
@SuppressWarnings("unchecked")
public Optional<long[]> getLongArray(String key) {
return (Optional<long[]>) wrapReflectEx(() -> getLongArray.invoke(__getRuntimeInstance(), key));
}
public long[] getLongArray(String key) {
return (long[]) wrapReflectEx(() -> getLongArray.invoke(__getRuntimeInstance(), key));
public Optional<CompoundTag> getCompound(String key) {
return ((Optional<?>) wrapReflectEx(() -> getCompound.invoke(__getRuntimeInstance(), key)))
.map(u -> wrap(u, CompoundTag.class));
}
public CompoundTag getCompound(String key) {
return wrap(wrapReflectEx(() -> getCompound.invoke(__getRuntimeInstance(), key)), CompoundTag.class);
@SuppressWarnings("unchecked")
public Optional<Boolean> getBoolean(String key) {
return (Optional<Boolean>) wrapReflectEx(() -> getBoolean.invoke(__getRuntimeInstance(), key));
}
public boolean getBoolean(String key) {
return (boolean) wrapReflectEx(() -> getBoolean.invoke(__getRuntimeInstance(), key));
}
public ListTag getList(String key, int type) {
return wrap(wrapReflectEx(() -> getList.invoke(__getRuntimeInstance(), key, type)), ListTag.class);
public Optional<ListTag> getList(String key) {
return ((Optional<?>) wrapReflectEx(() -> getList.invoke(__getRuntimeInstance(), key)))
.map(u -> wrap(u, ListTag.class));
}
public Tag get(String key) {
return wrap(wrapReflectEx(() -> get.invoke(__getRuntimeInstance(), key)), Tag.class);
}
@SuppressWarnings("unchecked")
public Set<String> getAllKeys() {
return (Set<String>) wrapReflectEx(() -> getAllKeys.invoke(__getRuntimeInstance()));
public Set<String> keySet() {
return (Set<String>) wrapReflectEx(() -> keySet.invoke(__getRuntimeInstance()));
}
/**
@@ -163,8 +167,5 @@ public class CompoundTag extends ReflectWrapper implements Tag {
public boolean contains(String key) {
return (boolean) wrapReflectEx(() -> contains.invoke(__getRuntimeInstance(), key));
}
public boolean contains(String key, int type) {
return (boolean) wrapReflectEx(() -> containsStringInt.invoke(__getRuntimeInstance(), key, type));
}
}

View File

@@ -4,6 +4,7 @@ import com.mojang.serialization.Codec;
import fr.pandacube.lib.reflect.Reflect;
import fr.pandacube.lib.reflect.ReflectClass;
import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ConcreteWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperI;
@@ -11,6 +12,7 @@ import static fr.pandacube.lib.reflect.wrapper.ReflectWrapper.wrap;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
@ConcreteWrapper(ValueInput.__concrete.class)
public interface ValueInput extends ReflectWrapperI {
ReflectClass<?> REFLECT = wrapEx(() -> Reflect.ofClass("net.minecraft.world.level.storage.ValueInput"));
ReflectMethod<?> listOrEmpty = wrapEx(() -> REFLECT.method("listOrEmpty", String.class, Codec.class));

View File

@@ -2,11 +2,13 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.world;
import fr.pandacube.lib.reflect.Reflect;
import fr.pandacube.lib.reflect.ReflectClass;
import fr.pandacube.lib.reflect.wrapper.ConcreteWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTypedI;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
@ConcreteWrapper(ValueInputTypedInputList.__concrete.class)
public interface ValueInputTypedInputList extends ReflectWrapperTypedI<Iterable<?>> {
ReflectClass<?> REFLECT = wrapEx(() -> Reflect.ofClass("net.minecraft.world.level.storage.ValueInput$TypedInputList"));

View File

@@ -4,6 +4,7 @@ import com.mojang.serialization.Codec;
import fr.pandacube.lib.reflect.Reflect;
import fr.pandacube.lib.reflect.ReflectClass;
import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ConcreteWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperI;
@@ -11,6 +12,7 @@ import static fr.pandacube.lib.reflect.wrapper.ReflectWrapper.wrap;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
@ConcreteWrapper(ValueOutput.__concrete.class)
public interface ValueOutput extends ReflectWrapperI {
ReflectClass<?> REFLECT = wrapEx(() -> Reflect.ofClass("net.minecraft.world.level.storage.ValueOutput"));
ReflectMethod<?> list = wrapEx(() -> REFLECT.method("list", String.class, Codec.class));

View File

@@ -3,12 +3,14 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.world;
import fr.pandacube.lib.reflect.Reflect;
import fr.pandacube.lib.reflect.ReflectClass;
import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ConcreteWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperI;
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
@ConcreteWrapper(ValueOutputTypedOutputList.__concrete.class)
public interface ValueOutputTypedOutputList extends ReflectWrapperI {
ReflectClass<?> REFLECT = wrapEx(() -> Reflect.ofClass("net.minecraft.world.level.storage.ValueOutput$TypedOutputList"));
ReflectMethod<?> add = wrapEx(() -> REFLECT.method("add", Object.class));