!Clean up player inv calculation

This commit is contained in:
Indyuce 2019-10-21 12:29:22 +02:00
parent d9def4d4ea
commit eb20aa2230
6 changed files with 85 additions and 54 deletions

View File

@ -30,6 +30,7 @@ import net.Indyuce.mmoitems.comp.holograms.HologramSupport;
import net.Indyuce.mmoitems.comp.holograms.HologramsPlugin; import net.Indyuce.mmoitems.comp.holograms.HologramsPlugin;
import net.Indyuce.mmoitems.comp.holograms.HolographicDisplaysPlugin; import net.Indyuce.mmoitems.comp.holograms.HolographicDisplaysPlugin;
import net.Indyuce.mmoitems.comp.inventory.DefaultPlayerInventory; import net.Indyuce.mmoitems.comp.inventory.DefaultPlayerInventory;
import net.Indyuce.mmoitems.comp.inventory.OrnamentPlayerInventory;
import net.Indyuce.mmoitems.comp.inventory.PlayerInventory; import net.Indyuce.mmoitems.comp.inventory.PlayerInventory;
import net.Indyuce.mmoitems.comp.inventory.RPGInventoryHook; import net.Indyuce.mmoitems.comp.inventory.RPGInventoryHook;
import net.Indyuce.mmoitems.comp.itemglow.ItemGlowListener; import net.Indyuce.mmoitems.comp.itemglow.ItemGlowListener;
@ -51,7 +52,6 @@ import net.Indyuce.mmoitems.listener.DisableInteractions;
import net.Indyuce.mmoitems.listener.ElementListener; import net.Indyuce.mmoitems.listener.ElementListener;
import net.Indyuce.mmoitems.listener.ItemUse; import net.Indyuce.mmoitems.listener.ItemUse;
import net.Indyuce.mmoitems.listener.MitigationListener; import net.Indyuce.mmoitems.listener.MitigationListener;
import net.Indyuce.mmoitems.listener.OrnamentTypeListener;
import net.Indyuce.mmoitems.listener.PlayerListener; import net.Indyuce.mmoitems.listener.PlayerListener;
import net.Indyuce.mmoitems.listener.version.Listener_v1_13; import net.Indyuce.mmoitems.listener.version.Listener_v1_13;
import net.Indyuce.mmoitems.manager.AbilityManager; import net.Indyuce.mmoitems.manager.AbilityManager;
@ -172,8 +172,6 @@ public class MMOItems extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(new DisableInteractions(), this); Bukkit.getPluginManager().registerEvents(new DisableInteractions(), this);
Bukkit.getPluginManager().registerEvents(new GuiListener(), this); Bukkit.getPluginManager().registerEvents(new GuiListener(), this);
Bukkit.getPluginManager().registerEvents(new ElementListener(), this); Bukkit.getPluginManager().registerEvents(new ElementListener(), this);
if(configManager.iterateWholeInventory)
Bukkit.getPluginManager().registerEvents(new OrnamentTypeListener(), this);
if (version.isStrictlyHigher(1, 12)) if (version.isStrictlyHigher(1, 12))
Bukkit.getPluginManager().registerEvents(new Listener_v1_13(), this); Bukkit.getPluginManager().registerEvents(new Listener_v1_13(), this);
@ -201,9 +199,10 @@ public class MMOItems extends JavaPlugin {
} }
if (Bukkit.getPluginManager().getPlugin("RPGInventory") != null) { if (Bukkit.getPluginManager().getPlugin("RPGInventory") != null) {
inventory = new RPGInventoryHook(this); inventory = new RPGInventoryHook();
getLogger().log(Level.INFO, "Hooked onto RPGInventory"); getLogger().log(Level.INFO, "Hooked onto RPGInventory");
} } else if (MMOItems.plugin.getConfig().getBoolean("iterate-whole-inventory"))
inventory = new OrnamentPlayerInventory();
if (Bukkit.getPluginManager().getPlugin("AdvancedEnchantments") != null) { if (Bukkit.getPluginManager().getPlugin("AdvancedEnchantments") != null) {
Bukkit.getPluginManager().registerEvents(new AdvancedEnchantmentsHook(), this); Bukkit.getPluginManager().registerEvents(new AdvancedEnchantmentsHook(), this);

View File

@ -6,9 +6,7 @@ import java.util.List;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type.EquipmentSlot; import net.Indyuce.mmoitems.api.Type.EquipmentSlot;
import net.Indyuce.mmoitems.api.item.NBTItem;
public class DefaultPlayerInventory implements PlayerInventory { public class DefaultPlayerInventory implements PlayerInventory {
@Override @Override
@ -20,13 +18,6 @@ public class DefaultPlayerInventory implements PlayerInventory {
for (ItemStack armor : player.getInventory().getArmorContents()) for (ItemStack armor : player.getInventory().getArmorContents())
list.add(new EquippedItem(armor, EquipmentSlot.ARMOR)); list.add(new EquippedItem(armor, EquipmentSlot.ARMOR));
if (MMOItems.plugin.getLanguage().iterateWholeInventory)
for (ItemStack item : player.getInventory().getContents()) {
NBTItem nbtItem;
if (item != null && (nbtItem = MMOItems.plugin.getNMS().getNBTItem(item)).hasType() && nbtItem.getType().getEquipmentType() == EquipmentSlot.ANY)
list.add(new EquippedItem(nbtItem, EquipmentSlot.ANY));
}
return list; return list;
} }
} }

View File

@ -0,0 +1,58 @@
package net.Indyuce.mmoitems.comp.inventory;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type.EquipmentSlot;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
public class OrnamentPlayerInventory implements PlayerInventory, Listener {
public OrnamentPlayerInventory() {
Bukkit.getPluginManager().registerEvents(this, MMOItems.plugin);
}
@Override
public List<EquippedItem> getInventory(Player player) {
List<EquippedItem> list = new ArrayList<>();
list.add(new EquippedItem(player.getEquipment().getItemInMainHand(), EquipmentSlot.MAIN_HAND));
list.add(new EquippedItem(player.getEquipment().getItemInOffHand(), EquipmentSlot.OFF_HAND));
for (ItemStack armor : player.getInventory().getArmorContents())
list.add(new EquippedItem(armor, EquipmentSlot.ARMOR));
for (ItemStack item : player.getInventory().getContents()) {
NBTItem nbtItem;
if (item != null && (nbtItem = MMOItems.plugin.getNMS().getNBTItem(item)).hasType() && nbtItem.getType().getEquipmentType() == EquipmentSlot.ANY)
list.add(new EquippedItem(nbtItem, EquipmentSlot.ANY));
}
return list;
}
@EventHandler(ignoreCancelled = true)
public void a(EntityPickupItemEvent event) {
if (event.getEntityType() == EntityType.PLAYER) {
NBTItem nbt = NBTItem.get(event.getItem().getItemStack());
if (nbt.hasType() && nbt.getType().getEquipmentType() == EquipmentSlot.ANY)
PlayerData.get((Player) event.getEntity()).updateInventory();
}
}
@EventHandler(ignoreCancelled = true)
public void b(PlayerDropItemEvent event) {
NBTItem nbt = NBTItem.get(event.getItemDrop().getItemStack());
if (nbt.hasType() && nbt.getType().getEquipmentType() == EquipmentSlot.ANY)
PlayerData.get(event.getPlayer()).updateInventory();
}
}

View File

@ -9,15 +9,25 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type.EquipmentSlot; import net.Indyuce.mmoitems.api.Type.EquipmentSlot;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
import ru.endlesscode.rpginventory.api.InventoryAPI; import ru.endlesscode.rpginventory.api.InventoryAPI;
public class RPGInventoryHook implements PlayerInventory, Listener { public class RPGInventoryHook implements PlayerInventory, Listener {
public RPGInventoryHook(Plugin plugin) { private final boolean ornaments;
Bukkit.getPluginManager().registerEvents(this, plugin);
/*
* RPGInventory is outdated. MI still supports it but it shall NEVER be
* considered a priority to keep MI compatible OR performance efficient with
* RPGInventory
*/
public RPGInventoryHook() {
Bukkit.getPluginManager().registerEvents(this, MMOItems.plugin);
if (ornaments = MMOItems.plugin.getConfig().getBoolean("iterate-whole-inventory"))
Bukkit.getPluginManager().registerEvents(new OrnamentPlayerInventory(), MMOItems.plugin);
} }
@Override @Override
@ -31,6 +41,13 @@ public class RPGInventoryHook implements PlayerInventory, Listener {
for (ItemStack armor : player.getInventory().getArmorContents()) for (ItemStack armor : player.getInventory().getArmorContents())
list.add(new EquippedItem(armor, EquipmentSlot.ARMOR)); list.add(new EquippedItem(armor, EquipmentSlot.ARMOR));
if (ornaments)
for (ItemStack item : player.getInventory().getContents()) {
NBTItem nbtItem;
if (item != null && (nbtItem = MMOItems.plugin.getNMS().getNBTItem(item)).hasType() && nbtItem.getType().getEquipmentType() == EquipmentSlot.ANY)
list.add(new EquippedItem(nbtItem, EquipmentSlot.ANY));
}
return list; return list;
} }

View File

@ -1,34 +0,0 @@
package net.Indyuce.mmoitems.listener;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import net.Indyuce.mmoitems.api.Type.EquipmentSlot;
import net.Indyuce.mmoitems.api.item.NBTItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
public class OrnamentTypeListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void a(EntityPickupItemEvent event) {
if(event.getEntityType().equals(EntityType.PLAYER)) {
NBTItem nbt = NBTItem.get(event.getItem().getItemStack());
if(!nbt.hasType()) return;
if(nbt.getType().getEquipmentType() == EquipmentSlot.ANY)
PlayerData.get((Player) event.getEntity()).updateInventory();
}
}
@EventHandler(ignoreCancelled = true)
public void b(PlayerDropItemEvent event) {
NBTItem nbt = NBTItem.get(event.getItemDrop().getItemStack());
if(!nbt.hasType()) return;
if(nbt.getType().getEquipmentType() == EquipmentSlot.ANY)
PlayerData.get(event.getPlayer()).updateInventory();
}
}

View File

@ -42,7 +42,7 @@ public class ConfigManager {
private ConfigFile abilities, items, loreFormat, messages, potionEffects, stats, attackEffects, namePlaceholders, durabilityStatesConfig; private ConfigFile abilities, items, loreFormat, messages, potionEffects, stats, attackEffects, namePlaceholders, durabilityStatesConfig;
// cached config options // cached config options
public boolean abilityPlayerDamage, dodgeKnockbackEnabled, iterateWholeInventory, replaceMushroomDrops, worldGenEnabled; public boolean abilityPlayerDamage, dodgeKnockbackEnabled, replaceMushroomDrops, worldGenEnabled;
public String healIndicatorFormat, damageIndicatorFormat, abilitySplitter; public String healIndicatorFormat, damageIndicatorFormat, abilitySplitter;
public DecimalFormat healIndicatorDecimalFormat, damageIndicatorDecimalFormat; public DecimalFormat healIndicatorDecimalFormat, damageIndicatorDecimalFormat;
@ -88,7 +88,8 @@ public class ConfigManager {
for (String fileName : fileNames) for (String fileName : fileNames)
if (!new File(MMOItems.plugin.getDataFolder() + "/language/" + language, fileName + ".yml").exists()) { if (!new File(MMOItems.plugin.getDataFolder() + "/language/" + language, fileName + ".yml").exists()) {
try { try {
Files.copy(MMOItems.plugin.getResource("language/" + language + "/" + fileName + ".yml"), new File(MMOItems.plugin.getDataFolder() + "/language/" + language, fileName + ".yml").getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(MMOItems.plugin.getResource("language/" + language + "/" + fileName + ".yml"),
new File(MMOItems.plugin.getDataFolder() + "/language/" + language, fileName + ".yml").getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -188,7 +189,6 @@ public class ConfigManager {
* reload cached config options for quicker access - these options are * reload cached config options for quicker access - these options are
* used in runnables, it is thus better to cache them * used in runnables, it is thus better to cache them
*/ */
iterateWholeInventory = MMOItems.plugin.getConfig().getBoolean("iterate-whole-inventory");
replaceMushroomDrops = MMOItems.plugin.getConfig().getBoolean("custom-blocks.replace-mushroom-drops"); replaceMushroomDrops = MMOItems.plugin.getConfig().getBoolean("custom-blocks.replace-mushroom-drops");
worldGenEnabled = MMOItems.plugin.getConfig().getBoolean("custom-blocks.enable-world-gen"); worldGenEnabled = MMOItems.plugin.getConfig().getBoolean("custom-blocks.enable-world-gen");
abilityPlayerDamage = MMOItems.plugin.getConfig().getBoolean("ability-player-damage"); abilityPlayerDamage = MMOItems.plugin.getConfig().getBoolean("ability-player-damage");