This commit is contained in:
ASangarin 2020-09-04 12:28:25 +02:00
commit 8bf73754de
5 changed files with 70 additions and 6 deletions

View File

@ -5,6 +5,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; 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.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -18,6 +20,7 @@ import net.mmogroup.mmolib.api.MMOLineConfig;
public class DropTable extends PostLoadObject { public class DropTable extends PostLoadObject {
private final String id; private final String id;
private final Set<DropItem> drops = new LinkedHashSet<>(); private final Set<DropItem> drops = new LinkedHashSet<>();
private final Set<Condition> conditions = new LinkedHashSet<>();
public DropTable(ConfigurationSection config) { public DropTable(ConfigurationSection config) {
super(config); super(config);
@ -37,16 +40,24 @@ public class DropTable extends PostLoadObject {
*/ */
@Override @Override
protected void whenPostLoaded(ConfigurationSection config) { protected void whenPostLoaded(ConfigurationSection config) {
List<String> list = config.getStringList("items"); List<String> itemsList = config.getStringList("items");
Validate.notNull(list, "Could not find drop item list"); List<String> conditionsList = config.getStringList("conditions");
Validate.notNull(itemsList, "Could not find drop item list");
for (String key : list) for (String key : itemsList)
try { try {
drops.add(MMOCore.plugin.loadManager.loadDropItem(new MMOLineConfig(key))); drops.add(MMOCore.plugin.loadManager.loadDropItem(new MMOLineConfig(key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.plugin.getLogger().log(Level.WARNING, MMOCore.plugin.getLogger().log(Level.WARNING,
"Could not load drop item '" + key + "' from table '" + id + "': " + exception.getMessage()); "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() { public String getId() {
@ -69,4 +80,17 @@ public class DropTable extends PostLoadObject {
return builder.getLoot(); return builder.getLoot();
} }
public Set<Condition> getConditions() {
return conditions;
}
public boolean areConditionsMet(ConditionInstance entity) {
for (Condition condition : this.getConditions()) {
if (!condition.isMet(entity)) {
return false;
}
}
return true;
}
} }

View File

@ -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;
}
}

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.api.droptable.dropitem; 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 org.apache.commons.lang.Validate;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
@ -22,7 +24,8 @@ public class DropTableDropItem extends DropItem {
@Override @Override
public void collect(LootBuilder builder) { public void collect(LootBuilder builder) {
for (int j = 0; j < rollAmount(); j++) if (dropTable.areConditionsMet(new ConditionInstance(builder.getEntity().getPlayer())))
builder.addLoot(dropTable.collect(builder)); for (int j = 0; j < rollAmount(); j++)
builder.addLoot(dropTable.collect(builder));
} }
} }

View File

@ -1,7 +1,9 @@
package net.Indyuce.mmocore.api.event; package net.Indyuce.mmocore.api.event;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.Indyuce.mmocore.api.droptable.condition.ConditionInstance;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -27,7 +29,8 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
this.block = block; this.block = block;
this.info = info; 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; this.experience = info.hasExperience() ? info.getExperience().newInfo() : null;
} }

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.api.load; package net.Indyuce.mmocore.api.load;
import net.Indyuce.mmocore.api.droptable.condition.LevelCondition;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import net.Indyuce.mmocore.api.block.BlockType; import net.Indyuce.mmocore.api.block.BlockType;
@ -113,6 +114,9 @@ public class DefaultMMOLoader extends MMOLoader {
if (config.getKey().equals("biome")) if (config.getKey().equals("biome"))
return new BiomeCondition(config); return new BiomeCondition(config);
if (config.getKey().equals("level"))
return new LevelCondition(config);
return null; return null;
} }