From 3725711b9440d6d6d7a1d2ff64e83f5917733b00 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Sun, 26 Aug 2018 14:43:21 -0400 Subject: [PATCH] Support Spigot 1.13.1 --- internal/pom.xml | 3 +- internal/v1_13_R2/pom.xml | 57 ++++ .../internal/v1_13_R2/AnySilentContainer.java | 297 ++++++++++++++++++ .../internal/v1_13_R2/InventoryAccess.java | 77 +++++ .../internal/v1_13_R2/PlayerDataManager.java | 106 +++++++ .../internal/v1_13_R2/SpecialEnderChest.java | 96 ++++++ .../v1_13_R2/SpecialPlayerInventory.java | 255 +++++++++++++++ 7 files changed, 890 insertions(+), 1 deletion(-) create mode 100644 internal/v1_13_R2/pom.xml create mode 100644 internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/AnySilentContainer.java create mode 100644 internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/InventoryAccess.java create mode 100644 internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/PlayerDataManager.java create mode 100644 internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialEnderChest.java create mode 100644 internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialPlayerInventory.java diff --git a/internal/pom.xml b/internal/pom.xml index 2959b13..06875b5 100644 --- a/internal/pom.xml +++ b/internal/pom.xml @@ -41,8 +41,8 @@ recent - v1_12_R1 v1_13_R1 + v1_13_R2 @@ -72,6 +72,7 @@ v1_11_R1 v1_12_R1 v1_13_R1 + v1_13_R2 diff --git a/internal/v1_13_R2/pom.xml b/internal/v1_13_R2/pom.xml new file mode 100644 index 0000000..40b6667 --- /dev/null +++ b/internal/v1_13_R2/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + com.lishid + openinvinternal + 3.3.6-SNAPSHOT + + + openinvadapter1_13_R2 + OpenInvAdapter1_13_R2 + + + + org.spigotmc + spigot + 1.13.1-R0.1-SNAPSHOT + provided + + + com.lishid + openinvcommon + 3.3.6-SNAPSHOT + + + + + + + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + diff --git a/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/AnySilentContainer.java b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/AnySilentContainer.java new file mode 100644 index 0000000..b2a9fd3 --- /dev/null +++ b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/AnySilentContainer.java @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2011-2018 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_13_R2; + +import com.lishid.openinv.internal.IAnySilentContainer; +import net.minecraft.server.v1_13_R2.AxisAlignedBB; +import net.minecraft.server.v1_13_R2.Block; +import net.minecraft.server.v1_13_R2.BlockChest; +import net.minecraft.server.v1_13_R2.BlockChestTrapped; +import net.minecraft.server.v1_13_R2.BlockEnderChest; +import net.minecraft.server.v1_13_R2.BlockPosition; +import net.minecraft.server.v1_13_R2.BlockPropertyChestType; +import net.minecraft.server.v1_13_R2.BlockShulkerBox; +import net.minecraft.server.v1_13_R2.ChatMessage; +import net.minecraft.server.v1_13_R2.Entity; +import net.minecraft.server.v1_13_R2.EntityOcelot; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.EnumDirection; +import net.minecraft.server.v1_13_R2.EnumGamemode; +import net.minecraft.server.v1_13_R2.IBlockData; +import net.minecraft.server.v1_13_R2.ITileInventory; +import net.minecraft.server.v1_13_R2.InventoryEnderChest; +import net.minecraft.server.v1_13_R2.InventoryLargeChest; +import net.minecraft.server.v1_13_R2.PlayerInteractManager; +import net.minecraft.server.v1_13_R2.TileEntity; +import net.minecraft.server.v1_13_R2.TileEntityChest; +import net.minecraft.server.v1_13_R2.TileEntityEnderChest; +import net.minecraft.server.v1_13_R2.TileEntityShulkerBox; +import net.minecraft.server.v1_13_R2.VoxelShapes; +import net.minecraft.server.v1_13_R2.World; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; + +import java.lang.reflect.Field; + +public class AnySilentContainer implements IAnySilentContainer { + + private Field playerInteractManagerGamemode; + + public AnySilentContainer() { + try { + this.playerInteractManagerGamemode = PlayerInteractManager.class.getDeclaredField("gamemode"); + this.playerInteractManagerGamemode.setAccessible(true); + } catch (NoSuchFieldException | SecurityException e) { + System.err.println("[OpenInv] Unable to directly write player gamemode! SilentChest will fail."); + e.printStackTrace(); + } + } + + @Override + public boolean isAnySilentContainer(final org.bukkit.block.Block block) { + if (block.getType() == Material.ENDER_CHEST) { + return true; + } + BlockState state = block.getState(); + return state instanceof org.bukkit.block.Chest + || state instanceof org.bukkit.block.ShulkerBox; + } + + @Override + public boolean isAnyContainerNeeded(final Player p, final org.bukkit.block.Block b) { + EntityPlayer player = PlayerDataManager.getHandle(p); + World world = player.world; + BlockPosition blockPosition = new BlockPosition(b.getX(), b.getY(), b.getZ()); + IBlockData blockData = world.getType(blockPosition); + Block block = blockData.getBlock(); + + if (block instanceof BlockShulkerBox) { + return this.isBlockedShulkerBox(world, blockPosition, blockData); + } + + if (block instanceof BlockEnderChest) { + // Ender chests are not blocked by ocelots. + return world.getType(blockPosition.up()).isOccluding(); + } + + // Check if chest is blocked or has an ocelot on top + if (this.isBlockedChest(world, blockPosition)) { + return true; + } + + // Check for matching adjacent chests that are blocked or have an ocelot on top + BlockPropertyChestType chestType = blockData.get(BlockChest.b); + + if (chestType == BlockPropertyChestType.SINGLE) { + return false; + } + + BlockPosition adjacentBlockPosition = blockPosition.shift(BlockChest.k(blockData)); + IBlockData adjacentBlockData = world.getType(adjacentBlockPosition); + + if (adjacentBlockData.getBlock() == block) { + + BlockPropertyChestType adjacentChestType = adjacentBlockData.get(BlockChest.b); + + if (adjacentChestType != BlockPropertyChestType.SINGLE && chestType != adjacentChestType + && adjacentBlockData.get(BlockChest.FACING) == blockData.get(BlockChest.FACING)) { + + return this.isBlockedChest(world, adjacentBlockPosition); + } + } + + return false; + } + + private boolean isBlockedShulkerBox(final World world, final BlockPosition blockPosition, + final IBlockData blockData) { + // For reference, look at net.minecraft.server.BlockShulkerBox + TileEntity tile = world.getTileEntity(blockPosition); + + if (!(tile instanceof TileEntityShulkerBox)) { + return false; + } + + EnumDirection enumDirection = blockData.get(BlockShulkerBox.a); + if (((TileEntityShulkerBox) tile).r() == TileEntityShulkerBox.AnimationPhase.CLOSED) { + AxisAlignedBB axisAlignedBB = VoxelShapes.b().a() + .b(0.5F * enumDirection.getAdjacentX(), 0.5F * enumDirection.getAdjacentY(), 0.5F * enumDirection.getAdjacentZ()) + .a(enumDirection.getAdjacentX(), enumDirection.getAdjacentY(), enumDirection.getAdjacentZ()); + return !world.getCubes(null, axisAlignedBB.a(blockPosition.shift(enumDirection))); + } + + return false; + } + + private boolean isBlockedChest(final World world, final BlockPosition blockPosition) { + // For reference, loot at net.minecraft.server.BlockChest + return world.getType(blockPosition.up()).isOccluding() || this.hasOcelotOnTop(world, blockPosition); + } + + private boolean hasOcelotOnTop(final World world, final BlockPosition blockPosition) { + for (Entity entity : world.a(EntityOcelot.class, + new AxisAlignedBB(blockPosition.getX(), blockPosition.getY() + 1, + blockPosition.getZ(), blockPosition.getX() + 1, blockPosition.getY() + 2, + blockPosition.getZ() + 1))) { + EntityOcelot entityOcelot = (EntityOcelot) entity; + if (entityOcelot.isSitting()) { + return true; + } + } + + return false; + } + + @Override + public boolean activateContainer(final Player bukkitPlayer, final boolean silentchest, + final org.bukkit.block.Block bukkitBlock) { + + // Silent ender chest is API-only + if (silentchest && bukkitBlock.getType() == Material.ENDER_CHEST) { + bukkitPlayer.openInventory(bukkitPlayer.getEnderChest()); + bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); + return true; + } + + EntityPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + + final World world = player.world; + final BlockPosition blockPosition = new BlockPosition(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ()); + final Object tile = world.getTileEntity(blockPosition); + + if (tile == null) { + return false; + } + + if (tile instanceof TileEntityEnderChest) { + // Anychest ender chest. See net.minecraft.server.BlockEnderChest + InventoryEnderChest enderChest = player.getEnderChest(); + enderChest.a((TileEntityEnderChest) tile); + player.openContainer(enderChest); + bukkitPlayer.incrementStatistic(Statistic.ENDERCHEST_OPENED); + return true; + } + + if (!(tile instanceof ITileInventory)) { + return false; + } + + ITileInventory tileInventory = (ITileInventory) tile; + IBlockData blockData = world.getType(blockPosition); + Block block = blockData.getBlock(); + + if (block instanceof BlockChest) { + + BlockPropertyChestType chestType = blockData.get(BlockChest.b); + + if (chestType != BlockPropertyChestType.SINGLE) { + + BlockPosition adjacentBlockPosition = blockPosition.shift(BlockChest.k(blockData)); + IBlockData adjacentBlockData = world.getType(adjacentBlockPosition); + + if (adjacentBlockData.getBlock() == block) { + + BlockPropertyChestType adjacentChestType = adjacentBlockData.get(BlockChest.b); + + if (adjacentChestType != BlockPropertyChestType.SINGLE && chestType != adjacentChestType + && adjacentBlockData.get(BlockChest.FACING) == blockData.get(BlockChest.FACING)) { + + TileEntity adjacentTile = world.getTileEntity(adjacentBlockPosition); + + if (adjacentTile instanceof TileEntityChest) { + ITileInventory rightChest = chestType == BlockPropertyChestType.RIGHT ? tileInventory : (ITileInventory) adjacentTile; + ITileInventory leftChest = chestType == BlockPropertyChestType.RIGHT ? (ITileInventory) adjacentTile : tileInventory; + tileInventory = new InventoryLargeChest(new ChatMessage("container.chestDouble"), rightChest, leftChest); + } + } + } + } + + if (block instanceof BlockChestTrapped) { + bukkitPlayer.incrementStatistic(Statistic.TRAPPED_CHEST_TRIGGERED); + } else { + bukkitPlayer.incrementStatistic(Statistic.CHEST_OPENED); + } + } + + if (block instanceof BlockShulkerBox) { + bukkitPlayer.incrementStatistic(Statistic.SHULKER_BOX_OPENED); + } + + // AnyChest only - SilentChest not active, container unsupported, or unnecessary. + if (!silentchest || player.playerInteractManager.getGameMode() == EnumGamemode.SPECTATOR) { + player.openContainer(tileInventory); + return true; + } + + // SilentChest requires access to setting players' gamemode directly. + if (this.playerInteractManagerGamemode == null) { + return false; + } + + EnumGamemode gamemode = player.playerInteractManager.getGameMode(); + this.forceGameMode(player, EnumGamemode.SPECTATOR); + player.openContainer(tileInventory); + this.forceGameMode(player, gamemode); + return true; + } + + @Override + public void deactivateContainer(final Player bukkitPlayer) { + if (this.playerInteractManagerGamemode == null) { + return; + } + + InventoryView view = bukkitPlayer.getOpenInventory(); + switch (view.getType()) { + case CHEST: + case ENDER_CHEST: + case SHULKER_BOX: + break; + default: + return; + } + + EntityPlayer player = PlayerDataManager.getHandle(bukkitPlayer); + + EnumGamemode gamemode = player.playerInteractManager.getGameMode(); + this.forceGameMode(player, EnumGamemode.SPECTATOR); + player.activeContainer.b(player); + player.activeContainer = player.defaultContainer; + this.forceGameMode(player, gamemode); + } + + private void forceGameMode(final EntityPlayer player, final EnumGamemode gameMode) { + if (this.playerInteractManagerGamemode == null) { + // No need to warn repeatedly, error on startup and lack of function should be enough. + return; + } + try { + if (!this.playerInteractManagerGamemode.isAccessible()) { + // Just in case, ensure accessible. + this.playerInteractManagerGamemode.setAccessible(true); + } + this.playerInteractManagerGamemode.set(player.playerInteractManager, gameMode); + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + } + +} diff --git a/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/InventoryAccess.java b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/InventoryAccess.java new file mode 100644 index 0000000..eca054c --- /dev/null +++ b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/InventoryAccess.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2011-2018 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_13_R2; + +import com.lishid.openinv.internal.IInventoryAccess; +import com.lishid.openinv.internal.ISpecialEnderChest; +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import com.lishid.openinv.util.InternalAccessor; +import net.minecraft.server.v1_13_R2.IInventory; +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftInventory; +import org.bukkit.inventory.Inventory; + +public class InventoryAccess implements IInventoryAccess { + + @Override + public ISpecialEnderChest getSpecialEnderChest(final Inventory inventory) { + IInventory inv; + if (inventory instanceof CraftInventory) { + inv = ((CraftInventory) inventory).getInventory(); + } else { + inv = InternalAccessor.grabFieldOfTypeFromObject(IInventory.class, inventory); + } + + if (inv instanceof SpecialEnderChest) { + return (SpecialEnderChest) inv; + } + return null; + } + + @Override + public ISpecialPlayerInventory getSpecialPlayerInventory(final Inventory inventory) { + IInventory inv; + if (inventory instanceof CraftInventory) { + inv = ((CraftInventory) inventory).getInventory(); + } else { + inv = InternalAccessor.grabFieldOfTypeFromObject(IInventory.class, inventory); + } + + if (inv instanceof SpecialPlayerInventory) { + return (SpecialPlayerInventory) inv; + } + return null; + } + + @Override + public boolean isSpecialEnderChest(final Inventory inventory) { + if (inventory instanceof CraftInventory) { + return ((CraftInventory) inventory).getInventory() instanceof ISpecialEnderChest; + } + return InternalAccessor.grabFieldOfTypeFromObject(IInventory.class, + inventory) instanceof ISpecialEnderChest; + } + + @Override + public boolean isSpecialPlayerInventory(final Inventory inventory) { + if (inventory instanceof CraftInventory) { + return ((CraftInventory) inventory).getInventory() instanceof ISpecialPlayerInventory; + } + return InternalAccessor.grabFieldOfTypeFromObject(IInventory.class, + inventory) instanceof ISpecialPlayerInventory; + } + +} diff --git a/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/PlayerDataManager.java b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/PlayerDataManager.java new file mode 100644 index 0000000..8db7a45 --- /dev/null +++ b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/PlayerDataManager.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2011-2018 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_13_R2; + +import com.lishid.openinv.internal.IPlayerDataManager; +import com.mojang.authlib.GameProfile; +import net.minecraft.server.v1_13_R2.DimensionManager; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.MinecraftServer; +import net.minecraft.server.v1_13_R2.PlayerInteractManager; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.craftbukkit.v1_13_R2.CraftServer; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.util.Collection; +import java.util.UUID; + +public class PlayerDataManager implements IPlayerDataManager { + + public static EntityPlayer getHandle(final Player player) { + if (player instanceof CraftPlayer) { + return ((CraftPlayer) player).getHandle(); + } + + Server server = player.getServer(); + EntityPlayer nmsPlayer = null; + + if (server instanceof CraftServer) { + nmsPlayer = ((CraftServer) server).getHandle().getPlayer(player.getName()); + } + + if (nmsPlayer == null) { + // Could use reflection to examine fields, but it's honestly not worth the bother. + throw new RuntimeException("Unable to fetch EntityPlayer from provided Player implementation"); + } + + return nmsPlayer; + } + + @Override + public Collection getOnlinePlayers() { + return Bukkit.getOnlinePlayers(); + } + + @Override + public OfflinePlayer getPlayerByID(final String identifier) { + try { + UUID uuid = UUID.fromString(identifier); + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + // Ensure player is a real player, otherwise return null + if (player == null || !player.hasPlayedBefore() && !player.isOnline()) { + return null; + } + return player; + } catch (IllegalArgumentException e) { + // Not a UUID + return null; + } + } + + @Override + public String getPlayerDataID(final OfflinePlayer offline) { + return offline.getUniqueId().toString(); + } + + @Override + public Player loadPlayer(final OfflinePlayer offline) { + // Ensure player has data + if (offline == null || !offline.hasPlayedBefore()) { + return null; + } + + // Create a profile and entity to load the player data + GameProfile profile = new GameProfile(offline.getUniqueId(), offline.getName()); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(DimensionManager.OVERWORLD), profile, + new PlayerInteractManager(server.getWorldServer(DimensionManager.OVERWORLD))); + + // Get the bukkit entity + Player target = entity.getBukkitEntity(); + if (target != null) { + // Load data + target.loadData(); + } + // Return the entity + return target; + } + +} diff --git a/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialEnderChest.java b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialEnderChest.java new file mode 100644 index 0000000..8414323 --- /dev/null +++ b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialEnderChest.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2011-2018 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_13_R2; + +import com.lishid.openinv.internal.ISpecialEnderChest; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.IInventory; +import net.minecraft.server.v1_13_R2.InventoryEnderChest; +import net.minecraft.server.v1_13_R2.InventorySubcontainer; +import net.minecraft.server.v1_13_R2.ItemStack; +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.List; + +public class SpecialEnderChest extends InventorySubcontainer + implements IInventory, ISpecialEnderChest { + + private final InventoryEnderChest enderChest; + private final CraftInventory inventory = new CraftInventory(this); + private boolean playerOnline; + + public SpecialEnderChest(final Player player, final Boolean online) { + super(PlayerDataManager.getHandle(player).getEnderChest().getDisplayName(), + PlayerDataManager.getHandle(player).getEnderChest().getSize(), player); + this.playerOnline = online; + this.enderChest = PlayerDataManager.getHandle(player).getEnderChest(); + this.setItemLists(this, this.enderChest.getContents()); + } + + @Override + public Inventory getBukkitInventory() { + return this.inventory; + } + + @Override + public boolean isInUse() { + return !this.getViewers().isEmpty(); + } + + private void setItemLists(final InventorySubcontainer subcontainer, final List list) { + try { + // Prepare to remove final modifier + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + // Access and replace main inventory array + Field field = InventorySubcontainer.class.getField("items"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(subcontainer, list); + } catch (NoSuchFieldException | SecurityException | IllegalArgumentException + | IllegalAccessException e) { + e.printStackTrace(); + } + } + + @Override + public void setPlayerOffline() { + this.playerOnline = false; + } + + @Override + public void setPlayerOnline(final Player player) { + if (!this.playerOnline) { + try { + EntityPlayer nmsPlayer = PlayerDataManager.getHandle(player); + this.bukkitOwner = nmsPlayer.getBukkitEntity(); + this.setItemLists(nmsPlayer.getEnderChest(), this.items); + } catch (Exception e) {} + this.playerOnline = true; + } + } + + @Override + public void update() { + super.update(); + this.enderChest.update(); + } + +} diff --git a/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialPlayerInventory.java b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialPlayerInventory.java new file mode 100644 index 0000000..5fa8548 --- /dev/null +++ b/internal/v1_13_R2/src/main/java/com/lishid/openinv/internal/v1_13_R2/SpecialPlayerInventory.java @@ -0,0 +1,255 @@ +/* + * Copyright (C) 2011-2018 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_13_R2; + +import com.lishid.openinv.internal.ISpecialPlayerInventory; +import net.minecraft.server.v1_13_R2.ChatMessage; +import net.minecraft.server.v1_13_R2.ContainerUtil; +import net.minecraft.server.v1_13_R2.EntityHuman; +import net.minecraft.server.v1_13_R2.IChatBaseComponent; +import net.minecraft.server.v1_13_R2.ItemStack; +import net.minecraft.server.v1_13_R2.NonNullList; +import net.minecraft.server.v1_13_R2.PlayerInventory; +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftInventory; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; + +public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory { + + private final CraftInventory inventory = new CraftInventory(this); + private boolean playerOnline; + + public SpecialPlayerInventory(final Player bukkitPlayer, final Boolean online) { + super(PlayerDataManager.getHandle(bukkitPlayer)); + this.playerOnline = online; + this.setItemArrays(this, this.player.inventory.items, this.player.inventory.armor, + this.player.inventory.extraSlots); + } + + @Override + public boolean a(final EntityHuman entityhuman) { + return true; + } + + @Override + public Inventory getBukkitInventory() { + return this.inventory; + } + + @Override + public ItemStack getItem(int i) { + NonNullList list = this.items; + + if (i >= list.size()) { + i -= list.size(); + list = this.armor; + } else { + i = this.getReversedItemSlotNum(i); + } + + if (i >= list.size()) { + i -= list.size(); + list = this.extraSlots; + } else if (list == this.armor) { + i = this.getReversedArmorSlotNum(i); + } + + if (i >= list.size()) { + return ItemStack.a; + } + + return list.get(i); + } + + @Override + public IChatBaseComponent getDisplayName() { + return new ChatMessage(this.player.getName()); + } + + @Override + public boolean hasCustomName() { + return true; + } + + private int getReversedArmorSlotNum(final int i) { + if (i == 0) { + return 3; + } + if (i == 1) { + return 2; + } + if (i == 2) { + return 1; + } + if (i == 3) { + return 0; + } + return i; + } + + private int getReversedItemSlotNum(final int i) { + if (i >= 27) { + return i - 27; + } + return i + 9; + } + + @Override + public int getSize() { + return super.getSize() + 4; + } + + @Override + public boolean isInUse() { + return !this.getViewers().isEmpty(); + } + + @Override + public void setItem(int i, final ItemStack itemstack) { + NonNullList list = this.items; + + if (i >= list.size()) { + i -= list.size(); + list = this.armor; + } else { + i = this.getReversedItemSlotNum(i); + } + + if (i >= list.size()) { + i -= list.size(); + list = this.extraSlots; + } else if (list == this.armor) { + i = this.getReversedArmorSlotNum(i); + } + + if (i >= list.size()) { + this.player.drop(itemstack, true); + return; + } + + list.set(i, itemstack); + } + + private void setItemArrays(final PlayerInventory inventory, final NonNullList items, + final NonNullList armor, final NonNullList extraSlots) { + try { + // Prepare to remove final modifier + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + + // Access and replace main inventory list + Field field = PlayerInventory.class.getField("items"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, items); + + // Access and replace armor inventory list + field = PlayerInventory.class.getField("armor"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, armor); + + // Access and replace offhand inventory list + field = PlayerInventory.class.getField("extraSlots"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, extraSlots); + + // Access and replace list containing all inventory lists + field = PlayerInventory.class.getDeclaredField("f"); + field.setAccessible(true); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, Arrays.asList(items, armor, extraSlots)); + } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { + // Unable to set final fields to item lists, we're screwed. Noisily fail. + e.printStackTrace(); + } + } + + @Override + public void setPlayerOffline() { + this.playerOnline = false; + } + + @Override + public void setPlayerOnline(final Player player) { + if (!this.playerOnline) { + this.player = PlayerDataManager.getHandle(player); + this.setItemArrays(this.player.inventory, this.items, this.armor, this.extraSlots); + this.playerOnline = true; + } + } + + @Override + public ItemStack splitStack(int i, final int j) { + NonNullList list = this.items; + + if (i >= list.size()) { + i -= list.size(); + list = this.armor; + } else { + i = this.getReversedItemSlotNum(i); + } + + if (i >= list.size()) { + i -= list.size(); + list = this.extraSlots; + } else if (list == this.armor) { + i = this.getReversedArmorSlotNum(i); + } + + if (i >= list.size()) { + return ItemStack.a; + } + + return list.get(i).isEmpty() ? ItemStack.a : ContainerUtil.a(list, i, j); + } + + @Override + public ItemStack splitWithoutUpdate(int i) { + NonNullList list = this.items; + + if (i >= list.size()) { + i -= list.size(); + list = this.armor; + } else { + i = this.getReversedItemSlotNum(i); + } + + if (i >= list.size()) { + i -= list.size(); + list = this.extraSlots; + } else if (list == this.armor) { + i = this.getReversedArmorSlotNum(i); + } + + if (i >= list.size()) { + return ItemStack.a; + } + + if (!list.get(i).isEmpty()) { + ItemStack itemstack = list.get(i); + + list.set(i, ItemStack.a); + return itemstack; + } + + return ItemStack.a; + } + +}