Repair item exp source cleanup

This commit is contained in:
Indyuce 2022-12-20 11:57:32 +01:00
parent d20ef20e40
commit d8cdd8791b
3 changed files with 52 additions and 43 deletions

View File

@ -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<ItemStack> {
@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<RepairItemExperienceSource> newManager() {
return new ExperienceSourceManager<RepairItemExperienceSource>() {
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<RepairItemExperienceSource> {
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);
}
}
};
}
}
}

View File

@ -15,6 +15,13 @@ public abstract class ExperienceSource<T> {
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) {

View File

@ -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<Class<?>, ExperienceSourceManager<?>> managers = new HashMap<>();
@SuppressWarnings("unchecked")
@Nullable
public <T extends ExperienceSource> ExperienceSourceManager<T> getManager(Class<T> t) {
return (ExperienceSourceManager<T>) managers.get(t);
}
@SuppressWarnings("unchecked")
public <T extends ExperienceSource> void registerSource(T source) {
Class<T> path = (Class<T>) source.getClass();
if (!managers.containsKey(path))
managers.put(path, source.newManager());
final Class<T> path = (Class<T>) source.getClass();
managers.computeIfAbsent(path, unused -> source.newManager());
getManager(path).registerSource(source);
}