From a4b28597afae7386885079ce1e76b508643334c8 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 3 Sep 2020 01:54:00 -0400 Subject: [PATCH] added condition comp for all drop tables added level condition: - 'level{profession=PROFESSION_NAME_HERE;amount=LEVEL_HERE}' for main level: - 'level{amount=LEVEL_HERE}' --- .../mmocore/api/droptable/DropTable.java | 30 +++++++++++++++++-- .../droptable/condition/LevelCondition.java | 30 +++++++++++++++++++ .../droptable/dropitem/DropTableDropItem.java | 7 +++-- .../api/event/CustomBlockMineEvent.java | 5 +++- .../mmocore/api/load/DefaultMMOLoader.java | 4 +++ 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmocore/api/droptable/condition/LevelCondition.java diff --git a/src/main/java/net/Indyuce/mmocore/api/droptable/DropTable.java b/src/main/java/net/Indyuce/mmocore/api/droptable/DropTable.java index fec437c2..aa468b46 100644 --- a/src/main/java/net/Indyuce/mmocore/api/droptable/DropTable.java +++ b/src/main/java/net/Indyuce/mmocore/api/droptable/DropTable.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.Set; import java.util.logging.Level; +import net.Indyuce.mmocore.api.droptable.condition.Condition; +import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance; import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; @@ -18,6 +20,7 @@ import net.mmogroup.mmolib.api.MMOLineConfig; public class DropTable extends PostLoadObject { private final String id; private final Set drops = new LinkedHashSet<>(); + private final Set conditions = new LinkedHashSet<>(); public DropTable(ConfigurationSection config) { super(config); @@ -37,16 +40,24 @@ public class DropTable extends PostLoadObject { */ @Override protected void whenPostLoaded(ConfigurationSection config) { - List list = config.getStringList("items"); - Validate.notNull(list, "Could not find drop item list"); + List itemsList = config.getStringList("items"); + List conditionsList = config.getStringList("conditions"); + Validate.notNull(itemsList, "Could not find drop item list"); - for (String key : list) + for (String key : itemsList) try { drops.add(MMOCore.plugin.loadManager.loadDropItem(new MMOLineConfig(key))); } catch (IllegalArgumentException exception) { MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load drop item '" + key + "' from table '" + id + "': " + exception.getMessage()); } + for (String key : conditionsList) + try { + conditions.add(MMOCore.plugin.loadManager.loadCondition(new MMOLineConfig(key))); + } catch (IllegalArgumentException exception) { + MMOCore.plugin.getLogger().log(Level.WARNING, + "Could not load condition '" + key + "' from table '" + id + "': " + exception.getMessage()); + } } public String getId() { @@ -69,4 +80,17 @@ public class DropTable extends PostLoadObject { return builder.getLoot(); } + + public Set getConditions() { + return conditions; + } + + public boolean areConditionsMet(ConditionInstance entity) { + for (Condition condition : this.getConditions()) { + if (!condition.isMet(entity)) { + return false; + } + } + return true; + } } \ No newline at end of file diff --git a/src/main/java/net/Indyuce/mmocore/api/droptable/condition/LevelCondition.java b/src/main/java/net/Indyuce/mmocore/api/droptable/condition/LevelCondition.java new file mode 100644 index 00000000..498007f5 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/droptable/condition/LevelCondition.java @@ -0,0 +1,30 @@ +package net.Indyuce.mmocore.api.droptable.condition; + +import net.mmogroup.mmolib.api.MMOLineConfig; +import net.mmogroup.mmolib.api.player.MMOPlayerData; +import org.bukkit.entity.Player; + +public class LevelCondition extends Condition { + private final int amount; + + private final String profession; + + public LevelCondition(MMOLineConfig config) { + super(config); + + config.validate("amount"); + + amount = config.getInt("amount"); + profession = config.contains("profession") ? config.getString("profession") : null; + } + + @Override + public boolean isMet(ConditionInstance entity) { + if (entity.getEntity() instanceof Player) { + int level = (profession != null) ? MMOPlayerData.get((Player) entity.getEntity()).getMMOCore().getCollectionSkills().getLevel(profession) + : MMOPlayerData.get((Player) entity.getEntity()).getMMOCore().getLevel(); + return level >= amount; + } + return false; + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/droptable/dropitem/DropTableDropItem.java b/src/main/java/net/Indyuce/mmocore/api/droptable/dropitem/DropTableDropItem.java index d664ba0d..a71133bd 100644 --- a/src/main/java/net/Indyuce/mmocore/api/droptable/dropitem/DropTableDropItem.java +++ b/src/main/java/net/Indyuce/mmocore/api/droptable/dropitem/DropTableDropItem.java @@ -1,5 +1,7 @@ package net.Indyuce.mmocore.api.droptable.dropitem; +import net.Indyuce.mmocore.api.droptable.condition.Condition; +import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance; import org.apache.commons.lang.Validate; import net.Indyuce.mmocore.MMOCore; @@ -22,7 +24,8 @@ public class DropTableDropItem extends DropItem { @Override public void collect(LootBuilder builder) { - for (int j = 0; j < rollAmount(); j++) - builder.addLoot(dropTable.collect(builder)); + if (dropTable.areConditionsMet(new ConditionInstance(builder.getEntity().getPlayer()))) + for (int j = 0; j < rollAmount(); j++) + builder.addLoot(dropTable.collect(builder)); } } diff --git a/src/main/java/net/Indyuce/mmocore/api/event/CustomBlockMineEvent.java b/src/main/java/net/Indyuce/mmocore/api/event/CustomBlockMineEvent.java index f73bb1ea..0bc336bc 100644 --- a/src/main/java/net/Indyuce/mmocore/api/event/CustomBlockMineEvent.java +++ b/src/main/java/net/Indyuce/mmocore/api/event/CustomBlockMineEvent.java @@ -1,7 +1,9 @@ package net.Indyuce.mmocore.api.event; +import java.util.ArrayList; import java.util.List; +import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance; import org.bukkit.block.Block; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -27,7 +29,8 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable this.block = block; this.info = info; - this.drops = info.collectDrops(new LootBuilder(player, 0)); + this.drops = (info.getDropTable().areConditionsMet(new ConditionInstance(player.getPlayer()))) + ? info.collectDrops(new LootBuilder(player, 0)) : new ArrayList<>(); this.experience = info.hasExperience() ? info.getExperience().newInfo() : null; } 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 fc502ae8..cfdb4957 100644 --- a/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java +++ b/src/main/java/net/Indyuce/mmocore/api/load/DefaultMMOLoader.java @@ -1,5 +1,6 @@ package net.Indyuce.mmocore.api.load; +import net.Indyuce.mmocore.api.droptable.condition.LevelCondition; import org.bukkit.configuration.ConfigurationSection; import net.Indyuce.mmocore.api.block.BlockType; @@ -113,6 +114,9 @@ public class DefaultMMOLoader extends MMOLoader { if (config.getKey().equals("biome")) return new BiomeCondition(config); + if (config.getKey().equals("level")) + return new LevelCondition(config); + return null; }