From 9abb792df3b0375a3fada8726b3f1b7f9c3dd2c7 Mon Sep 17 00:00:00 2001 From: Aria Date: Sat, 19 Oct 2019 19:07:05 +0200 Subject: [PATCH] Added placeblock experience source --- .../source/PlaceBlockExperienceSource.java | 47 +++++++++++++++++++ .../mmocore/api/load/DefaultMMOLoader.java | 6 ++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/Indyuce/mmocore/api/experience/source/PlaceBlockExperienceSource.java diff --git a/src/main/java/net/Indyuce/mmocore/api/experience/source/PlaceBlockExperienceSource.java b/src/main/java/net/Indyuce/mmocore/api/experience/source/PlaceBlockExperienceSource.java new file mode 100644 index 00000000..072c9c5d --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/experience/source/PlaceBlockExperienceSource.java @@ -0,0 +1,47 @@ +package net.Indyuce.mmocore.api.experience.source; + +import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.BlockPlaceEvent; + +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 PlaceBlockExperienceSource extends SpecificExperienceSource { + public final Material material; + + public PlaceBlockExperienceSource(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.HIGHEST) + public void a(BlockPlaceEvent event) { + if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL) + return; + PlayerData data = PlayerData.get(event.getPlayer()); + + for (PlaceBlockExperienceSource source : getSources()) { + if (source.matches(data, event.getBlock().getType())) + source.giveExperience(data); + } + } + }; + } + + @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 d6d1e0a4..c1f17d40 100644 --- a/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java +++ b/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java @@ -16,6 +16,7 @@ import net.Indyuce.mmocore.api.experience.source.EnchantItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.FishItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.KillMobExperienceSource; import net.Indyuce.mmocore.api.experience.source.MineBlockExperienceSource; +import net.Indyuce.mmocore.api.experience.source.PlaceBlockExperienceSource; import net.Indyuce.mmocore.api.experience.source.RepairItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.SmeltItemExperienceSource; import net.Indyuce.mmocore.api.experience.source.type.ExperienceSource; @@ -88,7 +89,7 @@ public class DefaultMMOLoader implements MMOLoader { if (config.getKey().equals("mineblock")) return new MineBlockObjective(section, config); - + if (config.getKey().equals("killmob")) return new KillMobObjective(section, config); @@ -117,6 +118,9 @@ public class DefaultMMOLoader implements MMOLoader { if (config.getKey().equals("mineblock")) return new MineBlockExperienceSource(profession, config); + if (config.getKey().equals("placeblock")) + return new PlaceBlockExperienceSource(profession, config); + if (config.getKey().equals("brewpotion")) return new BrewPotionExperienceSource(profession, config);