Access offline players inventory and enderchest.

Also added various reflection wrapper API
This commit is contained in:
2023-01-04 23:12:33 +01:00
parent a36b8ec1f2
commit 1557de2bdf
15 changed files with 309 additions and 7 deletions

View File

@@ -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;
}
}

View File

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