public static boolean equals(Inventory one, Inventory two) { if (one == null) return two == null; if (two == null) return false; if (!equals(one.getContents(), two.getContents())) return false; if (one instanceof PlayerInventory) { PlayerInventory pone = (PlayerInventory) one; if (two instanceof PlayerInventory) { PlayerInventory ptwo = (PlayerInventory) two; return equals(pone.getArmorContents(), ptwo.getArmorContents()); } else { return false; } } return true; }
public static int fromPlayerInventory(PlayerInventory inv) { int armorPoints = 0; for (ItemStack item : inv.getArmorContents()) if (item != null) { armorPoints += ArmorPoints.fromItemStack(item); } return armorPoints; }
// Armor contents has always been implemented the same way and can be used directly. public static ItemStack[] getContentsArmor(Inventory inventory) { PlayerInventory playerInventory = asPlayerInventory(inventory); if (playerInventory == null) return null; ItemStack[] ret = playerInventory.getArmorContents(); clean(ret); return ret; }
// All content varies over versions. // Before 1.9 it was getContents() + getArmorContents() + new ItemStack[1]. // From and including 1.9 it's just getContents(). public static ItemStack[] getContentsAll(Inventory inventory) { if (inventory == null) return null; ItemStack[] contents = inventory.getContents(); ItemStack[] ret = contents; PlayerInventory playerInventory = asPlayerInventory(inventory); if (playerInventory != null && contents.length == SIZE_PLAYER_STORAGE) { ret = concat(contents, playerInventory.getArmorContents(), new ItemStack[SIZE_PLAYER_EXTRA]); } clean(ret); return ret; }
@Override public void run() { if (Bukkit.getOnlinePlayers() == null) return; for (int ps = 0; ps < Bukkit.getOnlinePlayers().length; ps++) { Player p = Bukkit.getOnlinePlayers()[ps]; PlayerInventory inv = p.getInventory(); for (int a = 0; a < inv.getArmorContents().length; a++) { if (inv.getArmorContents()[a] != null) { CustomItemStack is = new CustomItemStack(inv.getArmorContents()[a]); if (!is.getEnchantments().isEmpty()) { for (Entry<org.bukkit.enchantments.Enchantment, Integer> enchs : is.getEnchantments().entrySet()) { if (enchs.getKey() instanceof EnchantmentCustom) { ((EnchantmentCustom) enchs.getKey()).onTick(p, is, enchs.getValue()); } } } } } } }
// called when a player spawns, applies protection for that player if necessary public void checkPvpProtectionNeeded(Player player) { WorldConfig wc = GriefPrevention.instance.getWorldCfg(player.getWorld()); // if pvp is disabled, do nothing if (!player.getWorld().getPVP()) return; // if player is in creative mode, do nothing if (player.getGameMode() == GameMode.CREATIVE) return; // if anti spawn camping feature is not enabled, do nothing if (!wc.getProtectFreshSpawns()) return; // if the player has the damage any player permission enabled, do nothing if (player.hasPermission("griefprevention.nopvpimmunity")) return; // check inventory for well, anything PlayerInventory inventory = player.getInventory(); ItemStack[] armorStacks = inventory.getArmorContents(); // check armor slots, stop if any items are found for (int i = 0; i < armorStacks.length; i++) { if (!(armorStacks[i] == null || armorStacks[i].getType() == Material.AIR)) return; } // check other slots, stop if any items are found ItemStack[] generalStacks = inventory.getContents(); for (int i = 0; i < generalStacks.length; i++) { if (!(generalStacks[i] == null || generalStacks[i].getType() == Material.AIR)) return; } // otherwise, apply immunity PlayerData playerData = this.dataStore.getPlayerData(player.getName()); playerData.pvpImmune = true; // inform the player GriefPrevention.sendMessage(player, TextMode.Success, Messages.PvPImmunityStart); }