From d8cdd8791bd81a3b6651fec3359afb09bdab90a0 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Tue, 20 Dec 2022 11:57:32 +0100 Subject: [PATCH] Repair item exp source cleanup --- .../source/RepairItemExperienceSource.java | 78 ++++++++++--------- .../source/type/ExperienceSource.java | 7 ++ .../mmocore/manager/ExperienceManager.java | 10 +-- 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/RepairItemExperienceSource.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/RepairItemExperienceSource.java index 20e3ebea..b308d3b7 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/RepairItemExperienceSource.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/RepairItemExperienceSource.java @@ -3,9 +3,9 @@ package net.Indyuce.mmocore.experience.source; import io.lumine.mythic.lib.api.MMOLineConfig; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.player.PlayerData; -import net.Indyuce.mmocore.experience.source.type.ExperienceSource; import net.Indyuce.mmocore.experience.EXPSource; import net.Indyuce.mmocore.experience.dispenser.ExperienceDispenser; +import net.Indyuce.mmocore.experience.source.type.ExperienceSource; import net.Indyuce.mmocore.manager.profession.ExperienceSourceManager; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -17,68 +17,70 @@ import org.bukkit.inventory.AnvilInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; +import javax.annotation.Nullable; + public class RepairItemExperienceSource extends ExperienceSource { + @Nullable private final Material material; public RepairItemExperienceSource(ExperienceDispenser dispenser, MMOLineConfig config) { super(dispenser); /* - * if material is null, the player can repair ANY material in order to - * get experience. + * If material is null, the player can repair + * ANY material in order to get experience. */ material = config.contains("type") ? Material.valueOf(config.getString("type").toUpperCase().replace("-", "_").replace(" ", "_")) : null; } @Override public boolean matchesParameter(PlayerData player, ItemStack item) { - return (material == null || item.getType() == material); + return material == null || item.getType() == material; } @Override public ExperienceSourceManager newManager() { - return new ExperienceSourceManager() { + return new CustomExperienceManager(); + } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void a(InventoryClickEvent event) { - if (event.getInventory() != null && event.getInventory().getType() == InventoryType.ANVIL && event.getSlot() == 2) { + private class CustomExperienceManager extends ExperienceSourceManager { - ItemStack item = event.getCurrentItem(); - PlayerData data = PlayerData.get((Player) event.getWhoClicked()); + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void a(InventoryClickEvent event) { + if (event.getInventory() == null || event.getInventory().getType() != InventoryType.ANVIL || event.getSlot() != 2) + return; - for (RepairItemExperienceSource source : getSources()) - if (source.matches(data, item)) { + // Check if there's exp associated to it + final ItemStack item = event.getCurrentItem(); + if (!MMOCore.plugin.smithingManager.hasExperience(item.getType())) + return; - if (!(event.getInventory() instanceof AnvilInventory)) - return; + final PlayerData data = PlayerData.get((Player) event.getWhoClicked()); + for (RepairItemExperienceSource source : getSources()) + if (source.matches(data, item)) { - if (((AnvilInventory) event.getInventory()).getRepairCost() > ((Player) event.getWhoClicked()).getLevel()) - return; + if (!(event.getInventory() instanceof AnvilInventory)) + return; - /* - * make sure the items can actually be repaired - * before getting the amount of durability repaired - */ - ItemStack old = event.getInventory().getItem(0); - if (old == null || old.getType() == Material.AIR) - return; - if (old.getType().getMaxDurability() < 30 || item.getType().getMaxDurability() < 10) - return; + if (((AnvilInventory) event.getInventory()).getRepairCost() > ((Player) event.getWhoClicked()).getLevel()) + return; - if (!MMOCore.plugin.smithingManager.hasExperience(item.getType())) - continue; + /* + * Make sure the items can actually be repaired + * before getting the amount of durability repaired + */ + final ItemStack old = event.getInventory().getItem(0); + if (old == null || old.getType() == Material.AIR || old.getType().getMaxDurability() < 30 || item.getType().getMaxDurability() < 10) + return; - /* - * calculate exp based on amount of durability which - * was repaired, substract damage from old item - * durability. - */ - double exp = MMOCore.plugin.smithingManager.getBaseExperience(item.getType()) - * Math.max(0, ((Damageable) old.getItemMeta()).getDamage() - ((Damageable) item.getItemMeta()).getDamage()) / 100; - getDispenser().giveExperience(data, exp, data.getPlayer().getLocation(), EXPSource.SOURCE); - } + /* + * Calculate exp based on amount of durability which was repaired, + * substract damage from old item durability. + */ + final double exp = MMOCore.plugin.smithingManager.getBaseExperience(item.getType()) + * Math.max(0, ((Damageable) old.getItemMeta()).getDamage() - ((Damageable) item.getItemMeta()).getDamage()) / 100; + getDispenser().giveExperience(data, exp, data.getPlayer().getLocation(), EXPSource.SOURCE); } - } - }; + } } } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/type/ExperienceSource.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/type/ExperienceSource.java index 66d3e7dd..777bef97 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/type/ExperienceSource.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/experience/source/type/ExperienceSource.java @@ -15,6 +15,13 @@ public abstract class ExperienceSource { return dispenser; } + /** + * This method is called ONCE per type of experience source. + * This instanciates the manager for all of the exp sources + * under that specific type. + * + * @return Instanciated experience source list + */ public abstract ExperienceSourceManager newManager(); public boolean matches(PlayerData player, T obj) { diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/ExperienceManager.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/ExperienceManager.java index d2a4d745..b778b0ed 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/ExperienceManager.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/ExperienceManager.java @@ -1,16 +1,17 @@ package net.Indyuce.mmocore.manager; import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.experience.ExpCurve; import net.Indyuce.mmocore.experience.droptable.ExperienceTable; import net.Indyuce.mmocore.experience.source.type.ExperienceSource; import net.Indyuce.mmocore.manager.profession.ExperienceSourceManager; -import net.Indyuce.mmocore.api.ConfigFile; import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.HandlerList; +import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.util.Collection; @@ -29,16 +30,15 @@ public class ExperienceManager implements MMOCoreManager { private final Map, ExperienceSourceManager> managers = new HashMap<>(); @SuppressWarnings("unchecked") + @Nullable public ExperienceSourceManager getManager(Class t) { return (ExperienceSourceManager) managers.get(t); } @SuppressWarnings("unchecked") public void registerSource(T source) { - Class path = (Class) source.getClass(); - - if (!managers.containsKey(path)) - managers.put(path, source.newManager()); + final Class path = (Class) source.getClass(); + managers.computeIfAbsent(path, unused -> source.newManager()); getManager(path).registerSource(source); }