diff --git a/pom.xml b/pom.xml index 8bc7472..81812aa 100644 --- a/pom.xml +++ b/pom.xml @@ -5,6 +5,13 @@ 0.0.1-SNAPSHOT + + org.spigotmc + spigot + 1.10-R0.1-SNAPSHOT + provided + v1_10_R1 + org.spigotmc spigot diff --git a/src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java b/src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java index 4ff24fa..eb516d7 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenEnderPluginCommand.java @@ -111,7 +111,7 @@ public class OpenEnderPluginCommand implements CommandExecutor { if (!online) { // Try loading the player's data - onlineTarget = plugin.getPlayerLoader().loadPlayer(target); + onlineTarget = plugin.getPlayerLoader().loadPlayer(plugin, target); if (onlineTarget == null) { player.sendMessage(ChatColor.RED + "Player not found!"); diff --git a/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java b/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java index 2a97a07..548aa19 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenInvPluginCommand.java @@ -112,7 +112,7 @@ public class OpenInvPluginCommand implements CommandExecutor { if (!online) { // Try loading the player's data - onlineTarget = plugin.getPlayerLoader().loadPlayer(target); + onlineTarget = plugin.getPlayerLoader().loadPlayer(plugin, target); if (onlineTarget == null) { player.sendMessage(ChatColor.RED + "Player not found!"); diff --git a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java index d61cb4a..b7efbfc 100644 --- a/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java +++ b/src/main/java/com/lishid/openinv/internal/PlayerDataManager.java @@ -16,15 +16,55 @@ package com.lishid.openinv.internal; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; public abstract class PlayerDataManager { - public final Player loadPlayer(OfflinePlayer offline) { + public final Player loadPlayer(final Plugin plugin, final OfflinePlayer offline) { if (offline.isOnline()) { return offline.getPlayer(); } - return this.loadOfflinePlayer(offline); + if (Bukkit.isPrimaryThread()) { + return this.loadOfflinePlayer(offline); + } + + + Future future = Bukkit.getScheduler().callSyncMethod(plugin, + new Callable() { + @Override + public Player call() throws Exception { + return loadOfflinePlayer(offline); + } + }); + + int ticks = 0; + while (!future.isDone() && !future.isCancelled() && ticks < 10) { + ++ticks; + try { + Thread.sleep(50L); + } catch (InterruptedException e) { + e.printStackTrace(); + return null; + } + } + if (!future.isDone() || future.isCancelled()) { + return null; + } + try { + return future.get(); + } catch (InterruptedException e) { + e.printStackTrace(); + return null; + } catch (ExecutionException e) { + e.printStackTrace(); + return null; + } } protected abstract Player loadOfflinePlayer(OfflinePlayer offline); diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/AnySilentChest.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/AnySilentChest.java new file mode 100644 index 0000000..15e3d17 --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/AnySilentChest.java @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +import com.lishid.openinv.internal.IAnySilentChest; + +// Volatile +import net.minecraft.server.v1_10_R1.AxisAlignedBB; +import net.minecraft.server.v1_10_R1.Block; +import net.minecraft.server.v1_10_R1.BlockPosition; +import net.minecraft.server.v1_10_R1.Entity; +import net.minecraft.server.v1_10_R1.EntityOcelot; +import net.minecraft.server.v1_10_R1.EntityPlayer; +import net.minecraft.server.v1_10_R1.IInventory; +import net.minecraft.server.v1_10_R1.ITileInventory; +import net.minecraft.server.v1_10_R1.InventoryLargeChest; +import net.minecraft.server.v1_10_R1.PacketPlayOutOpenWindow; +import net.minecraft.server.v1_10_R1.TileEntityChest; +import net.minecraft.server.v1_10_R1.World; + +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer; + +public class AnySilentChest implements IAnySilentChest { + @Override + public boolean isAnyChestNeeded(Player p, int x, int y, int z) { + // FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest + EntityPlayer player = ((CraftPlayer) p).getHandle(); + World world = player.world; + // If block or ocelot on top + if (world.getType(new BlockPosition(x, y + 1, z)).l() || hasOcelotOnTop(world, x, y, z)) + return true; + + int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock()); + + // If block next to chest is chest and has a block or ocelot on top + if (isBlockedChest(world, id, x - 1, y, z)) + return true; + if (isBlockedChest(world, id, x + 1, y, z)) + return true; + if (isBlockedChest(world, id, x, y, z - 1)) + return true; + if (isBlockedChest(world, id, x, y, z + 1)) + return true; + + return false; + } + + private boolean isBlockedChest(World world, int id, int x, int y, int z) { + BlockPosition position = new BlockPosition(x, y, z); + if (Block.getId(world.getType(position).getBlock()) != id) { + return false; + } + + if (world.getType(position).l()) { + return true; + } + + return hasOcelotOnTop(world, x, y, z); + } + + private boolean hasOcelotOnTop(World world, int x, int y, int z) { + for (Entity localEntity : world.a(EntityOcelot.class, + new AxisAlignedBB(x, y + 1, z, x + 1, y + 2, z + 1))) { + EntityOcelot localEntityOcelot = (EntityOcelot) localEntity; + if (localEntityOcelot.isSitting()) { + return true; + } + } + + return false; + } + + @Override + public boolean activateChest(Player p, boolean anychest, boolean silentchest, int x, int y, int z) { + EntityPlayer player = ((CraftPlayer) p).getHandle(); + World world = player.world; + Object chest = world.getTileEntity(new BlockPosition(x, y, z)); + if (chest == null) + return true; + + int id = Block.getId(world.getType(new BlockPosition(x, y, z)).getBlock()); + + if (!anychest) { + if (world.getType(new BlockPosition(x, y + 1, z)).l()) + return true; + if ((Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x - 1, y + 1, z)).l())) + return true; + if ((Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id) && (world.getType(new BlockPosition(x + 1, y + 1, z)).l())) + return true; + if ((Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z - 1)).l())) + return true; + if ((Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id) && (world.getType(new BlockPosition(x, y + 1, z + 1)).l())) + return true; + } + + if (Block.getId(world.getType(new BlockPosition(x - 1, y, z)).getBlock()) == id) + chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x - 1, y, z)), (ITileInventory) chest); + if (Block.getId(world.getType(new BlockPosition(x + 1, y, z)).getBlock()) == id) + chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x + 1, y, z))); + if (Block.getId(world.getType(new BlockPosition(x, y, z - 1)).getBlock()) == id) + chest = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z - 1)), (ITileInventory) chest); + if (Block.getId(world.getType(new BlockPosition(x, y, z + 1)).getBlock()) == id) + chest = new InventoryLargeChest("Large chest", (ITileInventory) chest, (TileEntityChest) world.getTileEntity(new BlockPosition(x, y, z + 1))); + + boolean returnValue = true; + if (!silentchest) { + player.openContainer((IInventory) chest); + } + else { + try { + SilentContainerChest silentContainerChest = new SilentContainerChest(player.inventory, ((IInventory) chest), player); + int windowId = player.nextContainerCounter(); + player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, "minecraft:chest", ((IInventory) chest).getScoreboardDisplayName(), ((IInventory) chest).getSize())); + player.activeContainer = silentContainerChest; + player.activeContainer.windowId = windowId; + player.activeContainer.addSlotListener(player); + returnValue = false; + } + catch (Exception e) { + e.printStackTrace(); + p.sendMessage(ChatColor.RED + "Error while sending silent chest."); + } + } + + return returnValue; + } +} diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/InventoryAccess.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/InventoryAccess.java new file mode 100644 index 0000000..7d67f87 --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/InventoryAccess.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +import java.lang.reflect.Field; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.Permissions; +import com.lishid.openinv.internal.IInventoryAccess; + +// Volatile +import net.minecraft.server.v1_10_R1.IInventory; + +import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory; + +public class InventoryAccess implements IInventoryAccess { + @Override + public boolean check(Inventory inventory, HumanEntity player) { + IInventory inv = grabInventory(inventory); + + if (inv instanceof SpecialPlayerInventory) { + if (!OpenInv.hasPermission(player, Permissions.PERM_EDITINV)) { + return false; + } + } + + else if (inv instanceof SpecialEnderChest) { + if (!OpenInv.hasPermission(player, Permissions.PERM_EDITENDER)) { + return false; + } + } + + return true; + } + + private IInventory grabInventory(Inventory inventory) { + if(inventory instanceof CraftInventory) { + return ((CraftInventory) inventory).getInventory(); + } + + //Use reflection to find the iinventory + Class clazz = inventory.getClass(); + IInventory result = null; + for(Field f : clazz.getDeclaredFields()) { + f.setAccessible(true); + if(IInventory.class.isAssignableFrom(f.getDeclaringClass())) { + try { + result = (IInventory) f.get(inventory); + } + catch (Exception e) { + e.printStackTrace(); + } + } + } + return result; + } +} diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/PlayerDataManager.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/PlayerDataManager.java new file mode 100644 index 0000000..9dfeb16 --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/PlayerDataManager.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import com.mojang.authlib.GameProfile; + +// Volatile +import net.minecraft.server.v1_10_R1.EntityPlayer; +import net.minecraft.server.v1_10_R1.MinecraftServer; +import net.minecraft.server.v1_10_R1.PlayerInteractManager; + +import org.bukkit.craftbukkit.v1_10_R1.CraftServer; + +public class PlayerDataManager extends com.lishid.openinv.internal.PlayerDataManager { + + @Override + public Player loadOfflinePlayer(OfflinePlayer offline) { + if (offline == null || !offline.hasPlayedBefore()) { + return null; + } + GameProfile profile = new GameProfile(offline.getUniqueId(), offline.getName()); + MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer(); + // Create an entity to load the player data + 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; + } + return null; + } + + @Override + public String getPlayerDataID(OfflinePlayer player) { + return player.getUniqueId().toString(); + } +} diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/SilentContainerChest.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SilentContainerChest.java new file mode 100644 index 0000000..3bc1d2e --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SilentContainerChest.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +// Volatile +import net.minecraft.server.v1_10_R1.ContainerChest; +import net.minecraft.server.v1_10_R1.EntityHuman; +import net.minecraft.server.v1_10_R1.IInventory; +import net.minecraft.server.v1_10_R1.PlayerInventory; + +public class SilentContainerChest extends ContainerChest { + public IInventory inv; + + public SilentContainerChest(IInventory i1, IInventory i2, EntityHuman e1) { + super(i1, i2, e1); + inv = i2; + // close signal + inv.closeContainer(e1); + } + + @Override + public void b(EntityHuman entityHuman) { + // Don't send close signal twice, might screw up + PlayerInventory playerinventory = entityHuman.inventory; + + if (playerinventory.getCarried() != null) { + entityHuman.drop(playerinventory.getCarried(), false); + playerinventory.setCarried(null); + } + } +} diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialEnderChest.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialEnderChest.java new file mode 100644 index 0000000..62ceee7 --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialEnderChest.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.ISpecialEnderChest; + +// Volatile +import net.minecraft.server.v1_10_R1.EntityHuman; +import net.minecraft.server.v1_10_R1.IInventory; +import net.minecraft.server.v1_10_R1.InventoryEnderChest; +import net.minecraft.server.v1_10_R1.InventorySubcontainer; +import net.minecraft.server.v1_10_R1.ItemStack; + +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory; + +public class SpecialEnderChest extends InventorySubcontainer implements IInventory, ISpecialEnderChest { + + private final OpenInv plugin; + private final InventoryEnderChest enderChest; + private final CraftInventory inventory = new CraftInventory(this); + public List transaction = new ArrayList(); + public boolean playerOnline = false; + private CraftPlayer owner; + private int maxStack = MAX_STACK; + + public SpecialEnderChest(OpenInv plugin, Player p, Boolean online) { + super(((CraftPlayer) p).getHandle().getEnderChest().getName(), ((CraftPlayer) p).getHandle().getEnderChest().hasCustomName(), ((CraftPlayer) p).getHandle().getEnderChest().getSize()); + this.plugin = plugin; + CraftPlayer player = (CraftPlayer) p; + this.enderChest = player.getHandle().getEnderChest(); + this.owner = player; + setItemArrays(this, enderChest.getContents()); + } + + private void setItemArrays(InventorySubcontainer subcontainer, ItemStack[] items) { + try { + // Prepare to remove final modifier + Field modifiers; + 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(inventory, items); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + @Override + public Inventory getBukkitInventory() { + return inventory; + } + + @Override + public boolean inventoryRemovalCheck(boolean save) { + boolean offline = transaction.isEmpty() && !playerOnline; + if (offline && save && !plugin.disableSaving()) { + owner.saveData(); + } + return offline; + } + + @Override + public void setPlayerOnline(Player player) { + if (!playerOnline) { + try { + owner = (CraftPlayer) player; + setItemArrays(owner.getHandle().getEnderChest(), this.items); + } + catch (Exception e) {} + playerOnline = true; + } + } + + @Override + public boolean setPlayerOffline() { + playerOnline = false; + return inventoryRemovalCheck(false); + } + + @Override + public ItemStack[] getContents() { + return this.items; + } + + @Override + public void onOpen(CraftHumanEntity who) { + transaction.add(who); + } + + @Override + public void onClose(CraftHumanEntity who) { + transaction.remove(who); + this.inventoryRemovalCheck(true); + } + + @Override + public List getViewers() { + return transaction; + } + + @Override + public InventoryHolder getOwner() { + return this.owner; + } + + @Override + public void setMaxStackSize(int size) { + maxStack = size; + } + + @Override + public int getMaxStackSize() { + return maxStack; + } + + @Override + public boolean a(EntityHuman entityhuman) { + return true; + } + + public void startOpen() { + + } + + public void f() { + + } + + @Override + public void update() { + enderChest.update(); + } +} diff --git a/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialPlayerInventory.java new file mode 100644 index 0000000..c21e5ec --- /dev/null +++ b/src/main/java/com/lishid/openinv/internal/v1_10_R1/SpecialPlayerInventory.java @@ -0,0 +1,321 @@ +/* + * Copyright (C) 2011-2014 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_10_R1; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +import com.lishid.openinv.OpenInv; +import com.lishid.openinv.internal.ISpecialPlayerInventory; + +// Volatile +import net.minecraft.server.v1_10_R1.EntityHuman; +import net.minecraft.server.v1_10_R1.ItemStack; +import net.minecraft.server.v1_10_R1.PlayerInventory; + +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory; + +public class SpecialPlayerInventory extends PlayerInventory implements ISpecialPlayerInventory { + + private final OpenInv plugin; + private final ItemStack[] extra = new ItemStack[4]; + private final CraftInventory inventory = new CraftInventory(this); + private CraftPlayer owner; + private boolean playerOnline = false; + + public SpecialPlayerInventory(OpenInv plugin, Player p, Boolean online) { + super(((CraftPlayer) p).getHandle()); + this.plugin = plugin; + this.owner = ((CraftPlayer) p); + this.playerOnline = online; + setItemArrays(this, player.inventory.items, player.inventory.armor, player.inventory.extraSlots); + } + + private void setItemArrays(PlayerInventory inventory, ItemStack[] items, ItemStack[] armor, + ItemStack[] extraSlots) { + try { + // Prepare to remove final modifier + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + + // Access and replace main inventory array + Field field = PlayerInventory.class.getField("items"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, items); + + // Access and replace armor inventory array + field = PlayerInventory.class.getField("armor"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, armor); + + // Access and replace offhand inventory array + field = PlayerInventory.class.getField("extraSlots"); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, extraSlots); + + // Access and replace array containing all inventory arrays + field = PlayerInventory.class.getDeclaredField("g"); + field.setAccessible(true); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(inventory, new ItemStack[][] { items, armor, extraSlots }); + } catch (NoSuchFieldException e) { + // Unable to set final fields to item arrays, we're screwed. Noisily fail. + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + @Override + public Inventory getBukkitInventory() { + return inventory; + } + + @Override + public boolean inventoryRemovalCheck(boolean save) { + boolean offline = transaction.isEmpty() && !playerOnline; + if (offline && save && !plugin.disableSaving()) { + owner.saveData(); + } + return offline; + } + + @Override + public void setPlayerOnline(Player player) { + if (!playerOnline) { + owner = (CraftPlayer) player; + this.player = owner.getHandle(); + setItemArrays(this.player.inventory, items, armor, extraSlots); + playerOnline = true; + } + } + + @Override + public boolean setPlayerOffline() { + playerOnline = false; + return this.inventoryRemovalCheck(false); + } + + @Override + public void onClose(CraftHumanEntity who) { + super.onClose(who); + this.inventoryRemovalCheck(true); + } + + @Override + public ItemStack[] getContents() { + ItemStack[] C = new ItemStack[getSize()]; + System.arraycopy(items, 0, C, 0, items.length); + System.arraycopy(armor, 0, C, items.length, armor.length); + System.arraycopy(extraSlots, 0, C, items.length + armor.length, extraSlots.length); + return C; + } + + @Override + public int getSize() { + return super.getSize() + 5; + } + + @Override + public ItemStack getItem(int i) { + ItemStack[] is = this.items; + + if (i >= is.length) { + i -= is.length; + is = this.armor; + } + else { + i = getReversedItemSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extraSlots; + } + else if (is == this.armor) { + i = getReversedArmorSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extra; + } + + // extraSlots is, for now, just an array with length 1. No need for special handling. + + return is[i]; + } + + @Override + public ItemStack splitStack(int i, int j) { + ItemStack[] is = this.items; + + if (i >= is.length) { + i -= is.length; + is = this.armor; + } + else { + i = getReversedItemSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extraSlots; + } + else if (is == this.armor) { + i = getReversedArmorSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extra; + } + + if (is[i] != null) { + ItemStack itemstack; + + if (is[i].count <= j) { + itemstack = is[i]; + is[i] = null; + return itemstack; + } + else { + itemstack = is[i].cloneAndSubtract(j); + if (is[i].count == 0) { + is[i] = null; + } + + return itemstack; + } + } + else { + return null; + } + } + + @Override + public ItemStack splitWithoutUpdate(int i) { + ItemStack[] is = this.items; + + if (i >= is.length) { + i -= is.length; + is = this.armor; + } + else { + i = getReversedItemSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extraSlots; + } + else if (is == this.armor) { + i = getReversedArmorSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extra; + } + + if (is[i] != null) { + ItemStack itemstack = is[i]; + + is[i] = null; + return itemstack; + } + else { + return null; + } + } + + @Override + public void setItem(int i, ItemStack itemstack) { + ItemStack[] is = this.items; + + if (i >= is.length) { + i -= is.length; + is = this.armor; + } + else { + i = getReversedItemSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extraSlots; + } + else if (is == this.armor) { + i = getReversedArmorSlotNum(i); + } + + if (i >= is.length) { + i -= is.length; + is = this.extra; + } + + // Effects + if (is == this.extra) { + owner.getHandle().drop(itemstack, true); + itemstack = null; + } + + is[i] = itemstack; + + owner.getHandle().defaultContainer.b(); + } + + private int getReversedItemSlotNum(int i) { + if (i >= 27) + return i - 27; + else + return i + 9; + } + + private int getReversedArmorSlotNum(int i) { + if (i == 0) + return 3; + if (i == 1) + return 2; + if (i == 2) + return 1; + if (i == 3) + return 0; + else + return i; + } + + @Override + public String getName() { + if (player.getName().length() > 16) { + return player.getName().substring(0, 16); + } + return player.getName(); + } + + @Override + public boolean a(EntityHuman entityhuman) { + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0b8dadf..82f04fa 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: com.lishid.openinv.OpenInv -version: 2.4.4 +version: 2.4.6 author: lishid authors: [Jikoo] description: >