From 1557de2bdf683e15f3b06bda5d91e2f77958c2e1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Wed, 4 Jan 2023 23:12:33 +0100 Subject: [PATCH] Access offline players inventory and enderchest. Also added various reflection wrapper API --- .../lib/paper/players/PaperOffPlayer.java | 74 ++++++++++++++++++- .../lib/paper/players/PaperOnlinePlayer.java | 36 +++++++++ .../paper/reflect/PandalibPaperReflect.java | 12 +++ .../wrapper/craftbukkit/CraftItemStack.java | 36 +++++++++ .../wrapper/minecraft/nbt/CollectionTag.java | 24 ++++++ .../wrapper/minecraft/nbt/CompoundTag.java | 16 +++- .../wrapper/minecraft/nbt/ListTag.java | 23 ++++++ .../reflect/wrapper/minecraft/nbt/NbtIo.java | 3 +- .../wrapper/minecraft/nbt/StringTag.java | 2 +- .../minecraft/server/DedicatedPlayerList.java | 2 +- .../minecraft/server/MinecraftServer.java | 6 ++ .../wrapper/minecraft/server/PlayerList.java | 22 ++++++ .../wrapper/minecraft/world/Entity.java | 8 +- .../wrapper/minecraft/world/ItemStack.java | 23 ++++++ .../minecraft/world/PlayerDataStorage.java | 29 ++++++++ 15 files changed, 309 insertions(+), 7 deletions(-) create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftItemStack.java create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CollectionTag.java create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/ListTag.java create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/PlayerList.java create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/ItemStack.java create mode 100644 pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/PlayerDataStorage.java diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java index 5f026ac..53b9324 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java @@ -1,12 +1,18 @@ package fr.pandacube.lib.paper.players; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.ListTag; +import fr.pandacube.lib.players.standalone.AbstractOffPlayer; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.ItemStack; import org.bukkit.scoreboard.Team; -import fr.pandacube.lib.players.standalone.AbstractOffPlayer; - import java.util.function.UnaryOperator; /** @@ -129,4 +135,68 @@ public interface PaperOffPlayer extends AbstractOffPlayer { PaperPlayerConfigStorage.unset(getUniqueId(), key); } + + + + /* + * Player data + */ + + default CompoundTag getPlayerData() { + return ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) + .getServer() + .getPlayerList() + .playerIo() + .getPlayerData(getUniqueId().toString()); + } + + default ItemStack[] getInventoryContent() { + ItemStack[] content = new ItemStack[InventoryType.PLAYER.getDefaultSize()]; + CompoundTag playerData = getPlayerData(); + if (playerData == null) + return content; + ListTag nbttaglist = playerData.getList("Inventory", 10); // type of list element 10 is CompoundTag + if (nbttaglist == null) + return content; + // cat nbEl NBTslot bukkitSlot NBT->Bukkit + // items 36el 0-35 == + // armor 4el start 100 36-39 -100 + 36 + // offhnd 1el start 150 40 -150 + 40 + for (int i = 0; i < nbttaglist.size(); i++) { + CompoundTag itemTag = nbttaglist.getCompound(i); + ItemStack is = CraftItemStack.asCraftMirror(itemTag); + if (is != null && !is.getType().isAir()) { + int nbtSlot = itemTag.getByte("Slot") & 255; + int bukkitSlot = nbtSlot < 36 ? nbtSlot + : (nbtSlot >= 100 && nbtSlot < 104) ? nbtSlot - 100 + 36 + : nbtSlot == 150 ? 40 + : -1; + if (bukkitSlot >= 0) + content[bukkitSlot] = is; + } + } + + return content; + } + + default ItemStack[] getEnderchestContent() { + ItemStack[] content = new ItemStack[InventoryType.ENDER_CHEST.getDefaultSize()]; + CompoundTag playerData = getPlayerData(); + if (playerData == null || !playerData.contains("EnderItems", 9)) // type 9 is list + return content; + ListTag nbtList = playerData.getList("EnderItems", 10); // type of list element 10 is CompoundTag + if (nbtList == null) + return content; + for (int i = 0; i < nbtList.size(); i++) { + CompoundTag itemTag = nbtList.getCompound(i); + int nbtSlot = itemTag.getByte("Slot") & 255; + ItemStack is = CraftItemStack.asCraftMirror(itemTag); + if (nbtSlot < content.length && is != null && !is.getType().isAir()) { + content[nbtSlot] = CraftItemStack.asCraftMirror(itemTag); + } + } + + return content; + } + } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java index 58bd6f7..c69fb86 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOnlinePlayer.java @@ -4,7 +4,10 @@ import com.destroystokyo.paper.ClientOption; import com.destroystokyo.paper.ClientOption.ChatVisibility; import com.destroystokyo.paper.SkinParts; import fr.pandacube.lib.paper.players.PlayerNonPersistentConfig.Expiration; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftPlayer; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag; import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; @@ -17,6 +20,7 @@ import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.Sound; import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MainHand; import java.util.Locale; @@ -53,6 +57,14 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer return getBukkitPlayer(); } + /** + * Returns the OBC.CraftPlayer instance wrapped into the {@link CraftPlayer} reflection wrapper. + * @return the OBC.CraftPlayer instance wrapped into the {@link CraftPlayer} reflection wrapper. + */ + default CraftPlayer getWrappedCraftPlayer() { + return ReflectWrapper.wrapTyped(getBukkitPlayer(), CraftPlayer.class); + } + @@ -305,4 +317,28 @@ public interface PaperOnlinePlayer extends PaperOffPlayer, AbstractOnlinePlayer } + + + /* + * Player data + */ + + @Override + default CompoundTag getPlayerData() { + CompoundTag tag = new CompoundTag(); + getWrappedCraftPlayer().getHandle().serializeEntity(tag); + return tag; + } + + @Override + default ItemStack[] getInventoryContent() { + return getBukkitPlayer().getInventory().getContents(); + } + + @Override + default ItemStack[] getEnderchestContent() { + return getBukkitPlayer().getEnderChest().getContents(); + } + + } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java index 9252be7..a711c0a 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/PandalibPaperReflect.java @@ -1,6 +1,7 @@ package fr.pandacube.lib.paper.reflect; import fr.pandacube.lib.paper.reflect.wrapper.brigadier.CommandNode; +import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftMapView; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftNamespacedKey; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftPlayer; @@ -24,7 +25,9 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.ResourceLocatio import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Vec3Argument; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.Vec3i; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CollectionTag; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.ListTag; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.NbtIo; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.StringTag; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.Tag; @@ -39,6 +42,7 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.DedicatedPlayerLi import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.DedicatedServer; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.DedicatedServerProperties; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.MinecraftServer; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.PlayerList; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerChunkCache; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerGamePacketListenerImpl; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerLevel; @@ -50,8 +54,10 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ChunkPos; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ChunkStorage; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DamageSource; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Level; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.MapItemSavedData; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.PlayerDataStorage; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.SavedData; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Vec3; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.VoxelShape; @@ -91,6 +97,7 @@ public class PandalibPaperReflect { initWrapper(CommandNode.class, CommandNode.REFLECT.get()); // craftbukkit + initWrapper(CraftItemStack.class, CraftItemStack.REFLECT.get()); initWrapper(CraftMapView.class, CraftMapView.REFLECT.get()); initWrapper(CraftNamespacedKey.class, CraftNamespacedKey.REFLECT.get()); initWrapper(CraftPlayer.class, CraftPlayer.REFLECT.get()); @@ -115,7 +122,9 @@ public class PandalibPaperReflect { initWrapper(BlockPos.class, BlockPos.MAPPING.runtimeClass()); initWrapper(Vec3i.class, Vec3i.MAPPING.runtimeClass()); // minecraft.nbt + initWrapper(CollectionTag.class, CollectionTag.MAPPING.runtimeClass()); initWrapper(CompoundTag.class, CompoundTag.MAPPING.runtimeClass()); + initWrapper(ListTag.class, ListTag.MAPPING.runtimeClass()); initWrapper(NbtIo.class, NbtIo.MAPPING.runtimeClass()); initWrapper(StringTag.class, StringTag.MAPPING.runtimeClass()); initWrapper(Tag.class, Tag.MAPPING.runtimeClass()); @@ -136,6 +145,7 @@ public class PandalibPaperReflect { initWrapper(DedicatedServer.class, DedicatedServer.MAPPING.runtimeClass()); initWrapper(DedicatedServerProperties.class, DedicatedServerProperties.MAPPING.runtimeClass()); initWrapper(MinecraftServer.class, MinecraftServer.MAPPING.runtimeClass()); + initWrapper(PlayerList.class, PlayerList.MAPPING.runtimeClass()); initWrapper(ServerChunkCache.class, ServerChunkCache.MAPPING.runtimeClass()); initWrapper(ServerGamePacketListenerImpl.class, ServerGamePacketListenerImpl.MAPPING.runtimeClass()); initWrapper(ServerLevel.class, ServerLevel.MAPPING.runtimeClass()); @@ -151,8 +161,10 @@ public class PandalibPaperReflect { initWrapper(ChunkStorage.class, ChunkStorage.MAPPING.runtimeClass()); initWrapper(DamageSource.class, DamageSource.MAPPING.runtimeClass()); initWrapper(Entity.class, Entity.MAPPING.runtimeClass()); + initWrapper(ItemStack.class, ItemStack.MAPPING.runtimeClass()); initWrapper(Level.class, Level.MAPPING.runtimeClass()); initWrapper(MapItemSavedData.class, MapItemSavedData.MAPPING.runtimeClass()); + initWrapper(PlayerDataStorage.class, PlayerDataStorage.MAPPING.runtimeClass()); initWrapper(SavedData.class, SavedData.MAPPING.runtimeClass()); initWrapper(Vec3.class, Vec3.MAPPING.runtimeClass()); initWrapper(VoxelShape.class, VoxelShape.MAPPING.runtimeClass()); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftItemStack.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftItemStack.java new file mode 100644 index 0000000..60b04b7 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/craftbukkit/CraftItemStack.java @@ -0,0 +1,36 @@ +package fr.pandacube.lib.paper.reflect.wrapper.craftbukkit; + +import fr.pandacube.lib.paper.reflect.OBCReflect; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.MapItemSavedData; +import fr.pandacube.lib.reflect.ReflectClass; +import fr.pandacube.lib.reflect.ReflectField; +import fr.pandacube.lib.reflect.ReflectMethod; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapperTyped; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.map.MapView; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; + +public class CraftItemStack extends ReflectWrapperTyped { + 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 ItemStack asCraftMirror(fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack original) { + return (ItemStack) wrapReflectEx(() -> asCraftMirror.invokeStatic(unwrap(original))); + } + + + public static ItemStack asCraftMirror(CompoundTag nbt) { + return asCraftMirror(fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.ItemStack.of(nbt)); + } + + + + + protected CraftItemStack(Object obj) { + super(obj); + } +} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CollectionTag.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CollectionTag.java new file mode 100644 index 0000000..8c5d307 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CollectionTag.java @@ -0,0 +1,24 @@ +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; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; + +public class CollectionTag extends ReflectWrapperTyped> implements Tag { + public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.CollectionTag")); + + + public int size() { + return __getRuntimeInstance().size(); + } + + protected CollectionTag(Object nms) { + super(nms); + } + +} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CompoundTag.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CompoundTag.java index 9a3d899..39d9849 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CompoundTag.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/CompoundTag.java @@ -8,6 +8,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import fr.pandacube.lib.reflect.ReflectConstructor; import fr.pandacube.lib.reflect.ReflectMethod; import fr.pandacube.lib.paper.reflect.NMSReflect; import fr.pandacube.lib.paper.reflect.NMSReflect.ClassMapping; @@ -15,6 +16,7 @@ import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; public class CompoundTag extends ReflectWrapper implements Tag { public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.CompoundTag")); + public static final ReflectConstructor CONSTRUCTOR = wrapEx(() -> MAPPING.runtimeReflect().constructor()); private static final ReflectMethod putBoolean = wrapEx(() -> MAPPING.mojMethod("putBoolean", String.class, boolean.class)); private static final ReflectMethod putByte = wrapEx(() -> MAPPING.mojMethod("putByte", String.class, byte.class)); private static final ReflectMethod putByteArray = wrapEx(() -> MAPPING.mojMethod("putByteArray", String.class, byte[].class)); @@ -45,14 +47,20 @@ public class CompoundTag extends ReflectWrapper implements Tag { private static final ReflectMethod getLongArray = wrapEx(() -> MAPPING.mojMethod("getLongArray", String.class)); private static final ReflectMethod getCompound = wrapEx(() -> MAPPING.mojMethod("getCompound", String.class)); private static final ReflectMethod getBoolean = wrapEx(() -> MAPPING.mojMethod("getBoolean", String.class)); + private static final ReflectMethod getList = wrapEx(() -> MAPPING.mojMethod("getList", String.class, int.class)); private static final ReflectMethod get = wrapEx(() -> MAPPING.mojMethod("get", String.class)); private static final ReflectMethod getAllKeys = wrapEx(() -> MAPPING.mojMethod("getAllKeys")); private static final ReflectMethod entries = wrapEx(() -> MAPPING.mojMethod("entries")); private static final ReflectMethod size = wrapEx(() -> MAPPING.mojMethod("size")); private static final ReflectMethod contains = wrapEx(() -> MAPPING.mojMethod("contains", String.class)); + private static final ReflectMethod containsStringInt = wrapEx(() -> MAPPING.mojMethod("contains", String.class, int.class)); - public CompoundTag(Object nms) { + public CompoundTag() { + this(wrapReflectEx(() -> CONSTRUCTOR.instanciate())); + } + + protected CompoundTag(Object nms) { super(nms); } @@ -143,6 +151,9 @@ public class CompoundTag extends ReflectWrapper implements Tag { 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 Tag get(String key) { return wrap(wrapReflectEx(() -> get.invoke(__getRuntimeInstance(), key)), Tag.class); } @@ -165,5 +176,8 @@ 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)); + } } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/ListTag.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/ListTag.java new file mode 100644 index 0000000..876a618 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/ListTag.java @@ -0,0 +1,23 @@ +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.ReflectMethod; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +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")); + 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); + } + + protected ListTag(Object nms) { + super(nms); + } + +} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/NbtIo.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/NbtIo.java index ee0b67f..82b7134 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/NbtIo.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/NbtIo.java @@ -21,8 +21,9 @@ public class NbtIo extends ReflectWrapper { public static CompoundTag readCompressed(File f) { - return new CompoundTag(wrapEx(() -> readCompressed.invokeStatic(f))); + return wrap(wrapEx(() -> readCompressed.invokeStatic(f)), CompoundTag.class); } + public static void writeCompressed(CompoundTag tag, File f) { Object nmsTag = ReflectWrapper.unwrap(tag); wrapEx(() -> writeCompressed.invokeStatic(nmsTag, f)); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/StringTag.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/StringTag.java index 526bd98..3a7f79a 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/StringTag.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/StringTag.java @@ -10,7 +10,7 @@ public class StringTag extends ReflectWrapper implements Tag { public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.StringTag")); - public StringTag(Object nms) { + protected StringTag(Object nms) { super(nms); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/DedicatedPlayerList.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/DedicatedPlayerList.java index ad93b88..c98492d 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/DedicatedPlayerList.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/DedicatedPlayerList.java @@ -5,7 +5,7 @@ import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; -public class DedicatedPlayerList extends ReflectWrapper { +public class DedicatedPlayerList extends PlayerList { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.dedicated.DedicatedPlayerList")); protected DedicatedPlayerList(Object obj) { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java index db4bec9..7882018 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/MinecraftServer.java @@ -1,6 +1,7 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; import fr.pandacube.lib.paper.reflect.NMSReflect; +import fr.pandacube.lib.reflect.ReflectMethod; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Commands; import fr.pandacube.lib.reflect.ReflectField; @@ -11,11 +12,16 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; public class MinecraftServer extends ReflectWrapper { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.MinecraftServer")); private static final ReflectField vanillaCommandDispatcher = wrapEx(() -> MAPPING.runtimeReflect().field("vanillaCommandDispatcher")); + private static final ReflectMethod getPlayerList = wrapEx(() -> MAPPING.mojMethod("getPlayerList")); public Commands vanillaCommandDispatcher() { return wrap(wrapReflectEx(() -> vanillaCommandDispatcher.getValue(__getRuntimeInstance())), Commands.class); } + public PlayerList getPlayerList() { + return wrap(wrapReflectEx(() -> getPlayerList.invoke(__getRuntimeInstance())), PlayerList.class); + } + protected MinecraftServer(Object obj) { super(obj); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/PlayerList.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/PlayerList.java new file mode 100644 index 0000000..e8c08b7 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/PlayerList.java @@ -0,0 +1,22 @@ +package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; + +import fr.pandacube.lib.paper.reflect.NMSReflect; +import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.PlayerDataStorage; +import fr.pandacube.lib.reflect.ReflectField; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; + +public class PlayerList extends ReflectWrapper { + public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.server.players.PlayerList")); + private static final ReflectField playerIo = wrapEx(() -> MAPPING.mojField("playerIo")); + + public PlayerDataStorage playerIo() { + return wrap(wrapReflectEx(() -> playerIo.getValue(__getRuntimeInstance())), PlayerDataStorage.class); + } + + protected PlayerList(Object obj) { + super(obj); + } +} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/Entity.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/Entity.java index eaed87c..16d4624 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/Entity.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/Entity.java @@ -1,6 +1,7 @@ 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.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.ReflectMethod; @@ -9,12 +10,17 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; public class Entity extends ReflectWrapper { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.entity.Entity")); - public static final ReflectMethod getBukkitEntity = wrapEx(() -> MAPPING.runtimeReflect().method("getBukkitEntity")); // spigot field + public static final ReflectMethod getBukkitEntity = wrapEx(() -> MAPPING.runtimeReflect().method("getBukkitEntity")); // spigot method + public static final ReflectMethod serializeEntity = wrapEx(() -> MAPPING.runtimeReflect().method("serializeEntity", CompoundTag.MAPPING.runtimeClass())); // paper method public org.bukkit.entity.Entity getBukkitEntity() { return (org.bukkit.entity.Entity) wrapReflectEx(() -> getBukkitEntity.invoke(__getRuntimeInstance())); } + public boolean serializeEntity(CompoundTag nbt) { + return wrapReflectEx(() -> (Boolean) serializeEntity.invoke(__getRuntimeInstance(), unwrap(nbt))); + } + protected Entity(Object obj) { super(obj); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/ItemStack.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/ItemStack.java new file mode 100644 index 0000000..a682c56 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/ItemStack.java @@ -0,0 +1,23 @@ +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; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +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.class)); + + public static ItemStack of(CompoundTag nbt) { + return wrap(wrapReflectEx(() -> of.invokeStatic(unwrap(nbt))), ItemStack.class); + } + + protected ItemStack(Object obj) { + super(obj); + } +} diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/PlayerDataStorage.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/PlayerDataStorage.java new file mode 100644 index 0000000..39c65c5 --- /dev/null +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/PlayerDataStorage.java @@ -0,0 +1,29 @@ +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.paper.reflect.wrapper.paper.configuration.WorldConfiguration; +import fr.pandacube.lib.reflect.ReflectField; +import fr.pandacube.lib.reflect.ReflectMethod; +import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; + +import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; +import static fr.pandacube.lib.util.ThrowableUtil.wrapReflectEx; + +public class PlayerDataStorage extends ReflectWrapper { + public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.level.storage.PlayerDataStorage")); + public static final ReflectMethod getPlayerData = wrapEx(() -> MAPPING.runtimeReflect().method("getPlayerData", String.class)); // Craftbukkit method + + /** + * @param playerId UUID of a player as it is used to name the player data file (UUID.toString()) + */ + public CompoundTag getPlayerData(String playerId) { + return wrap(wrapReflectEx(() -> getPlayerData.invoke(__getRuntimeInstance(), playerId)), CompoundTag.class); + } + + + + protected PlayerDataStorage(Object obj) { + super(obj); + } +}