From 0ebe6fe1323a99feaf49d2ad791724ed182f6761 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Mon, 11 Apr 2016 17:59:28 -0400 Subject: [PATCH] Properly update PlayerInventory owner when they come online Closes #5 --- src/main/java/com/lishid/openinv/OpenInv.java | 9 ----- .../lishid/openinv/OpenInvPlayerListener.java | 33 +++++++++++-------- .../v1_9_R1/SpecialPlayerInventory.java | 26 +++++++++++---- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index c27cd90f..8af86d49 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -165,15 +165,6 @@ public boolean notifyAnyChest() { return getConfig().getBoolean("NotifyAnyChest", true); } - public boolean getPlayerItemOpenInvStatus(OfflinePlayer player) { - return getConfig().getBoolean("ItemOpenInv." + getPlayerLoader().getPlayerDataID(player) + ".toggle", false); - } - - public void setPlayerItemOpenInvStatus(OfflinePlayer player, boolean status) { - getConfig().set("ItemOpenInv." + getPlayerLoader().getPlayerDataID(player) + ".toggle", status); - saveConfig(); - } - public boolean getPlayerSilentChestStatus(OfflinePlayer player) { return getConfig().getBoolean("SilentChest." + getPlayerLoader().getPlayerDataID(player) + ".toggle", false); } diff --git a/src/main/java/com/lishid/openinv/OpenInvPlayerListener.java b/src/main/java/com/lishid/openinv/OpenInvPlayerListener.java index 97d5050e..d0eb93fa 100644 --- a/src/main/java/com/lishid/openinv/OpenInvPlayerListener.java +++ b/src/main/java/com/lishid/openinv/OpenInvPlayerListener.java @@ -27,6 +27,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.scheduler.BukkitRunnable; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialPlayerInventory; @@ -39,19 +40,25 @@ public OpenInvPlayerListener(OpenInv plugin) { this.plugin = plugin; } - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerJoin(PlayerJoinEvent event) { - ISpecialPlayerInventory inventory = plugin.getInventoryFor(event.getPlayer()); - - if (inventory != null) { - inventory.setPlayerOnline(event.getPlayer()); - } - - ISpecialEnderChest chest = plugin.getEnderChestFor(event.getPlayer()); - - if (chest != null) { - chest.setPlayerOnline(event.getPlayer()); - } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(final PlayerJoinEvent event) { + new BukkitRunnable() { + @Override + public void run() { + if (!event.getPlayer().isOnline()) { + return; + } + ISpecialPlayerInventory inventory = plugin.getInventoryFor(event.getPlayer()); + if (inventory != null) { + inventory.setPlayerOnline(event.getPlayer()); + event.getPlayer().updateInventory(); + } + ISpecialEnderChest chest = plugin.getEnderChestFor(event.getPlayer()); + if (chest != null) { + chest.setPlayerOnline(event.getPlayer()); + } + } + }.runTask(plugin); } @EventHandler(priority = EventPriority.MONITOR) diff --git a/src/main/java/com/lishid/openinv/internal/v1_9_R1/SpecialPlayerInventory.java b/src/main/java/com/lishid/openinv/internal/v1_9_R1/SpecialPlayerInventory.java index 31dcb011..b6369b93 100644 --- a/src/main/java/com/lishid/openinv/internal/v1_9_R1/SpecialPlayerInventory.java +++ b/src/main/java/com/lishid/openinv/internal/v1_9_R1/SpecialPlayerInventory.java @@ -49,17 +49,30 @@ public SpecialPlayerInventory(Player p, Boolean online) { private void setItemArrays(PlayerInventory inventory, ItemStack[] items, ItemStack[] armor, ItemStack[] extraSlots) { try { - Field field = inventory.getClass().getField("items"); + // 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); - field = inventory.getClass().getField("armor"); + + // Access and replace armor inventory array + field = PlayerInventory.class.getField("armor"); modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(inventory, armor); - field = inventory.getClass().getField("extraSlots"); + + // 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(); @@ -84,9 +97,10 @@ public boolean inventoryRemovalCheck() { @Override public void setPlayerOnline(Player player) { if (!playerOnline) { - CraftPlayer p = (CraftPlayer) player; - setItemArrays(p.getHandle().inventory, items, armor, extraSlots); - p.saveData(); + owner = (CraftPlayer) player; + this.player = owner.getHandle(); + setItemArrays(this.player.inventory, items, armor, extraSlots); + owner.saveData(); playerOnline = true; } }