Skip to content

Commit

Permalink
Properly update PlayerInventory owner when they come online
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
Jikoo committed Apr 11, 2016
1 parent e39ab10 commit 0ebe6fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
9 changes: 0 additions & 9 deletions src/main/java/com/lishid/openinv/OpenInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/com/lishid/openinv/OpenInvPlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 0ebe6fe

Please sign in to comment.