diff --git a/src/main/java/net/Indyuce/mmocore/api/experience/source/CraftItemExperienceSource.java b/src/main/java/net/Indyuce/mmocore/api/experience/source/CraftItemExperienceSource.java new file mode 100644 index 00000000..63641f50 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/experience/source/CraftItemExperienceSource.java @@ -0,0 +1,46 @@ +package net.Indyuce.mmocore.api.experience.source; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.inventory.CraftItemEvent; + +import net.Indyuce.mmocore.api.experience.Profession; +import net.Indyuce.mmocore.api.experience.source.type.SpecificExperienceSource; +import net.Indyuce.mmocore.api.load.MMOLineConfig; +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.manager.profession.ExperienceManager; + +public class CraftItemExperienceSource extends SpecificExperienceSource { + public final Material material; + + public CraftItemExperienceSource(Profession profession, MMOLineConfig config) { + super(profession, config); + + config.validate("type"); + material = Material.valueOf(config.getString("type").toUpperCase().replace("-", "_").replace(" ", "_")); + } + + @Override + public ExperienceManager newManager() { + return new ExperienceManager() { + + @EventHandler(priority = EventPriority.HIGH) + public void a(CraftItemEvent event) { + if (event.isCancelled()) + return; + + PlayerData data = PlayerData.get((Player) event.getWhoClicked()); + for (CraftItemExperienceSource source : getSources()) + if (source.matches(data, event.getInventory().getResult().getType())) + source.giveExperience(data, event.getInventory().getLocation()); + } + }; + } + + @Override + public boolean matches(PlayerData player, Material obj) { + return material == obj && hasRightClass(player); + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java b/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java index 8d730b3c..37fd86fa 100644 --- a/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java +++ b/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java @@ -12,6 +12,7 @@ import net.Indyuce.mmocore.api.droptable.dropitem.NoteDropItem; import net.Indyuce.mmocore.api.droptable.dropitem.VanillaDropItem; import net.Indyuce.mmocore.api.experience.Profession; import net.Indyuce.mmocore.api.experience.source.BrewPotionExperienceSource; +import net.Indyuce.mmocore.api.experience.source.CraftItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.EnchantItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.FishItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.KillMobExperienceSource; @@ -133,6 +134,9 @@ public class DefaultMMOLoader implements MMOLoader { if (config.getKey().equals("repairitem")) return new RepairItemExperienceSource(profession, config); + if (config.getKey().equals("craftitem")) + return new CraftItemExperienceSource(profession, config); + return null; } }