Refactor offline player data access

- Class that handle all Bukkit/NBT conversion of player data
- Ability to read and save the player inventory (more to come later)
This commit is contained in:
2023-02-18 21:32:12 +01:00
parent fb4c62a0bc
commit bf59617e19
8 changed files with 613 additions and 70 deletions

View File

@@ -17,6 +17,7 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
public class CraftItemStack extends ReflectWrapperTyped<ItemStack> {
public static final ReflectClass<?> REFLECT = wrapEx(() -> OBCReflect.ofClass("inventory.CraftItemStack"));
public static final ReflectMethod<?> asCraftMirror = wrapEx(() -> REFLECT.method("asCraftMirror", fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack.MAPPING.runtimeClass()));
public static final ReflectMethod<?> asNMSCopy = wrapEx(() -> REFLECT.method("asNMSCopy", ItemStack.class));
public static ItemStack asCraftMirror(fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack original) {
return (ItemStack) wrapReflectEx(() -> asCraftMirror.invokeStatic(unwrap(original)));
@@ -28,6 +29,11 @@ public class CraftItemStack extends ReflectWrapperTyped<ItemStack> {
}
public static fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack asNMSCopy(ItemStack original) {
return wrap(wrapReflectEx(() -> asNMSCopy.invokeStatic(original)), fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack.class);
}
protected CraftItemStack(Object obj) {

View File

@@ -2,7 +2,6 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt;
import fr.pandacube.lib.paper.reflect.NMSReflect;
import fr.pandacube.lib.paper.reflect.NMSReflect.ClassMapping;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped;
import java.util.AbstractList;
@@ -17,6 +16,22 @@ public class CollectionTag extends ReflectWrapperTyped<AbstractList<?>> implemen
return __getRuntimeInstance().size();
}
public Tag get(int i) {
return wrap(__getRuntimeInstance().get(i), Tag.class);
}
public Tag set(int i, Tag t) {
return wrap(((AbstractList<Object>)__getRuntimeInstance()).set(i, unwrap(t)), Tag.class);
}
public void add(int i, Tag t) {
((AbstractList<Object>)__getRuntimeInstance()).add(i, unwrap(t));
}
public Tag remove(int i) {
return wrap(__getRuntimeInstance().remove(i), Tag.class);
}
protected CollectionTag(Object nms) {
super(nms);
}

View File

@@ -2,6 +2,7 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt;
import fr.pandacube.lib.paper.reflect.NMSReflect;
import fr.pandacube.lib.paper.reflect.NMSReflect.ClassMapping;
import fr.pandacube.lib.reflect.ReflectConstructor;
import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
@@ -10,12 +11,17 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
public class ListTag extends CollectionTag {
public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.ListTag"));
public static final ReflectConstructor<?> CONSTRUCTOR = wrapEx(() -> MAPPING.runtimeReflect().constructor());
private static final ReflectMethod<?> getCompound = wrapEx(() -> MAPPING.mojMethod("getCompound", int.class));
public CompoundTag getCompound(int index) {
return wrap(wrapReflectEx(() -> getCompound.invoke(__getRuntimeInstance(), index)), CompoundTag.class);
}
public ListTag() {
this(wrapReflectEx(() -> CONSTRUCTOR.instanciate()));
}
protected ListTag(Object nms) {
super(nms);
}

View File

@@ -2,7 +2,6 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.world;
import fr.pandacube.lib.paper.reflect.NMSReflect;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag;
import fr.pandacube.lib.reflect.ReflectConstructor;
import fr.pandacube.lib.reflect.ReflectMethod;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
@@ -12,12 +11,19 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx;
public class ItemStack extends ReflectWrapper {
public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.item.ItemStack"));
private static final ReflectMethod<?> of = wrapEx(() -> MAPPING.mojMethod("of", CompoundTag.MAPPING));
private static final ReflectMethod<?> save = wrapEx(() -> MAPPING.mojMethod("save", CompoundTag.MAPPING));
public static ItemStack of(CompoundTag nbt) {
return wrap(wrapReflectEx(() -> of.invokeStatic(unwrap(nbt))), ItemStack.class);
}
protected ItemStack(Object obj) {
super(obj);
}
public CompoundTag save(CompoundTag nbt) {
return wrap(wrapReflectEx(() -> save.invoke(__getRuntimeInstance(), unwrap(nbt))), CompoundTag.class);
}
}