diff --git a/internal/pom.xml b/internal/pom.xml index e3793bf..f84facc 100644 --- a/internal/pom.xml +++ b/internal/pom.xml @@ -41,8 +41,8 @@ recent - v1_11_R1 v1_12_R1 + v1_13_R1 @@ -71,6 +71,7 @@ v1_10_R1 v1_11_R1 v1_12_R1 + v1_13_R1 diff --git a/internal/v1_13_R1/pom.xml b/internal/v1_13_R1/pom.xml new file mode 100644 index 0000000..1ce8784 --- /dev/null +++ b/internal/v1_13_R1/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + com.lishid + openinvinternal + 3.3.4-SNAPSHOT + + + openinvadapter1_13_R1 + OpenInvAdapter1_13_R1 + + + + org.spigotmc + spigot + 1.13-pre7-R0.1-SNAPSHOT + provided + + + com.lishid + openinvcommon + 3.3.4-SNAPSHOT + + + + + + + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + diff --git a/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/AnySilentContainer.java b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/AnySilentContainer.java new file mode 100644 index 0000000..f4681f1 --- /dev/null +++ b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/AnySilentContainer.java @@ -0,0 +1,293 @@ +/* + * 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_R1; + +import java.lang.reflect.Field; + +import com.lishid.openinv.internal.IAnySilentContainer; + +import net.minecraft.server.v1_13_R1.BlockChestTrapped; +import net.minecraft.server.v1_13_R1.ChatMessage; +import net.minecraft.server.v1_13_R1.VoxelShapes; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; + +import net.minecraft.server.v1_13_R1.AxisAlignedBB; +import net.minecraft.server.v1_13_R1.Block; +import net.minecraft.server.v1_13_R1.BlockChest; +import net.minecraft.server.v1_13_R1.BlockEnderChest; +import net.minecraft.server.v1_13_R1.BlockPosition; +import net.minecraft.server.v1_13_R1.BlockShulkerBox; +import net.minecraft.server.v1_13_R1.Entity; +import net.minecraft.server.v1_13_R1.EntityOcelot; +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.EnumDirection; +import net.minecraft.server.v1_13_R1.EnumGamemode; +import net.minecraft.server.v1_13_R1.IBlockData; +import net.minecraft.server.v1_13_R1.ITileInventory; +import net.minecraft.server.v1_13_R1.InventoryEnderChest; +import net.minecraft.server.v1_13_R1.InventoryLargeChest; +import net.minecraft.server.v1_13_R1.PlayerInteractManager; +import net.minecraft.server.v1_13_R1.TileEntity; +import net.minecraft.server.v1_13_R1.TileEntityChest; +import net.minecraft.server.v1_13_R1.TileEntityEnderChest; +import net.minecraft.server.v1_13_R1.TileEntityShulkerBox; +import net.minecraft.server.v1_13_R1.World; + +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 = com.lishid.openinv.internal.v1_13_R1.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 + for (EnumDirection localEnumDirection : EnumDirection.EnumDirectionLimit.HORIZONTAL) { + BlockPosition localBlockPosition = blockPosition.shift(localEnumDirection); + Block localBlock = world.getType(localBlockPosition).getBlock(); + + if (localBlock != block) { + continue; + } + + TileEntity localTileEntity = world.getTileEntity(localBlockPosition); + if (!(localTileEntity instanceof TileEntityChest)) { + continue; + } + + if (this.isBlockedChest(world, localBlockPosition)) { + return true; + } + } + + 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 = com.lishid.openinv.internal.v1_13_R1.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; + Block block = world.getType(blockPosition).getBlock(); + + if (block instanceof BlockChest) { + for (EnumDirection localEnumDirection : EnumDirection.EnumDirectionLimit.HORIZONTAL) { + BlockPosition localBlockPosition = blockPosition.shift(localEnumDirection); + Block localBlock = world.getType(localBlockPosition).getBlock(); + + if (localBlock != block) { + continue; + } + + TileEntity localTileEntity = world.getTileEntity(localBlockPosition); + if (!(localTileEntity instanceof TileEntityChest)) { + continue; + } + + if (localEnumDirection == EnumDirection.WEST + || localEnumDirection == EnumDirection.NORTH) { + tileInventory = new InventoryLargeChest(new ChatMessage("container.chestDouble"), + (TileEntityChest) localTileEntity, tileInventory); + } else { + tileInventory = new InventoryLargeChest(new ChatMessage("container.chestDouble"), + tileInventory, (TileEntityChest) localTileEntity); + } + break; + } + + 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 = com.lishid.openinv.internal.v1_13_R1.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_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/InventoryAccess.java b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/InventoryAccess.java new file mode 100644 index 0000000..fc5acc7 --- /dev/null +++ b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/InventoryAccess.java @@ -0,0 +1,80 @@ +/* + * 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_R1; + +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 org.bukkit.inventory.Inventory; + +import net.minecraft.server.v1_13_R1.IInventory; + +import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftInventory; + +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 com.lishid.openinv.internal.v1_13_R1.SpecialEnderChest) { + return (com.lishid.openinv.internal.v1_13_R1.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 com.lishid.openinv.internal.v1_13_R1.SpecialPlayerInventory) { + return (com.lishid.openinv.internal.v1_13_R1.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_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/PlayerDataManager.java b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/PlayerDataManager.java new file mode 100644 index 0000000..079a3ff --- /dev/null +++ b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/PlayerDataManager.java @@ -0,0 +1,109 @@ +/* + * 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_R1; + +import java.util.Collection; +import java.util.UUID; + +import com.lishid.openinv.internal.IPlayerDataManager; + +import com.mojang.authlib.GameProfile; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.entity.Player; + +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.MinecraftServer; +import net.minecraft.server.v1_13_R1.PlayerInteractManager; + +import org.bukkit.craftbukkit.v1_13_R1.CraftServer; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftPlayer; + +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(0), profile, + new PlayerInteractManager(server.getWorldServer(0))); + + // Get the bukkit entity + Player target = entity == null ? null : entity.getBukkitEntity(); + if (target != null) { + // Load data + target.loadData(); + } + // Return the entity + return target; + } + +} diff --git a/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialEnderChest.java b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialEnderChest.java new file mode 100644 index 0000000..7d3e41e --- /dev/null +++ b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialEnderChest.java @@ -0,0 +1,99 @@ +/* + * 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_R1; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.List; + +import com.lishid.openinv.internal.ISpecialEnderChest; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.IInventory; +import net.minecraft.server.v1_13_R1.InventoryEnderChest; +import net.minecraft.server.v1_13_R1.InventorySubcontainer; +import net.minecraft.server.v1_13_R1.ItemStack; + +import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftInventory; + +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_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialPlayerInventory.java b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialPlayerInventory.java new file mode 100644 index 0000000..5857f0b --- /dev/null +++ b/internal/v1_13_R1/src/main/java/com/lishid/openinv/internal/v1_13_R1/SpecialPlayerInventory.java @@ -0,0 +1,258 @@ +/* + * 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_R1; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; + +import com.lishid.openinv.internal.ISpecialPlayerInventory; + +import net.minecraft.server.v1_13_R1.ChatMessage; +import net.minecraft.server.v1_13_R1.IChatBaseComponent; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import net.minecraft.server.v1_13_R1.ContainerUtil; +import net.minecraft.server.v1_13_R1.EntityHuman; +import net.minecraft.server.v1_13_R1.ItemStack; +import net.minecraft.server.v1_13_R1.NonNullList; +import net.minecraft.server.v1_13_R1.PlayerInventory; + +import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftInventory; + +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; + } + +}