!Fixed MMOCore drop items

This commit is contained in:
Indyuce 2020-08-16 16:12:05 +02:00
parent 5dbc4b53b1
commit a70d8ec786
9 changed files with 114 additions and 100 deletions

View File

@ -140,10 +140,25 @@ public class MMOItemTemplate implements ItemReference {
public enum TemplateOption {
/*
* when the item is being generated, modifiers are rolled in a random
* order so you never the same modifiers again and again
/**
* When the item is being generated, modifiers are rolled in a random
* order so you never the same modifiers again and again.
*/
ROLL_MODIFIER_CHECK_ORDER;
ROLL_MODIFIER_CHECK_ORDER,
/**
* When building an item based on this template, if no tier is
* specified, a random tier will be chosen for the item. By default, the
* item has no tier OR has the tier given in the config file.
*/
TIERED,
/**
* When building an item based on this template, if no level is
* specified, a random level will be rolled based on the player's level
* or 0 if no player is specified. By default, items are generated with
* level 0
*/
LEVEL_ITEM;
}
}

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmoitems.api.item.template.loot;
package net.Indyuce.mmoitems.api.item.template.explorer;
import java.util.function.Predicate;

View File

@ -0,0 +1,57 @@
package net.Indyuce.mmoitems.api.item.template.explorer;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
/**
* Used to explore currently registered templates and randomly pick a template
* given specific restrictions like type/class restrictions. Warning, this
* caches in a collection all currently registered templates and applies filters
* so this can be performance heavy
*
* @author cympe
*
*/
public class TemplateExplorer {
/*
* Not defined at the beginning to save extra performance, if there are 100+
* templates this be pretty violent. TODO make type reference necessary?
*/
private final Collection<MMOItemTemplate> all = MMOItems.plugin.getTemplates().collectTemplates();
public int count() {
return all.size();
}
public TemplateExplorer applyFilter(Predicate<MMOItemTemplate> filter) {
all.removeIf(not(filter));
return this;
}
public Optional<MMOItemTemplate> rollLoot() {
return all.stream().findAny();
}
/**
* Util method to easily generate random MI loot
*
* @param player
* The player
* @return Random item with random tier and item level which matches the
* player's level
*/
public Optional<MMOItem> rollItem(RPGPlayer player) {
return rollLoot().map(template -> template.newBuilder(player).build());
}
private <T> Predicate<T> not(Predicate<T> predicate) {
return t -> !predicate.test(t);
}
}

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmoitems.api.item.template.loot;
package net.Indyuce.mmoitems.api.item.template.explorer;
import java.util.function.Predicate;

View File

@ -1,62 +0,0 @@
package net.Indyuce.mmoitems.api.item.template.loot;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
public class LootBuilder {
/*
* not defined at the beginning to save extra performance, if there are 100+
* templates this be pretty violent. TODO make type reference necessary?
*/
private final Collection<MMOItemTemplate> all;
/*
* options required to generate random loot
*/
private final ItemTier itemTier;
private final int itemLevel;
public LootBuilder(RPGPlayer player, ItemTier itemTier) {
itemLevel = MMOItems.plugin.getTemplates().rollLevel(player.getLevel());
this.itemTier = itemTier;
all = MMOItems.plugin.getTemplates().collectTemplates();
}
public LootBuilder(RPGPlayer player) {
itemLevel = MMOItems.plugin.getTemplates().rollLevel(player.getLevel());
itemTier = MMOItems.plugin.getTemplates().rollTier();
all = MMOItems.plugin.getTemplates().collectTemplates();
}
public LootBuilder(int itemLevel, ItemTier itemTier) {
this.itemLevel = itemLevel;
this.itemTier = itemTier;
all = MMOItems.plugin.getTemplates().collectTemplates();
}
public int count() {
return all.size();
}
public LootBuilder applyFilter(Predicate<MMOItemTemplate> filter) {
all.removeIf(not(filter));
return this;
}
public MMOItem rollLoot() {
Optional<MMOItemTemplate> found = all.stream().findAny();
return found.isPresent() ? found.get().newBuilder(itemLevel, itemTier).build() : null;
}
private <T> Predicate<T> not(Predicate<T> predicate) {
return t -> !predicate.test(t);
}
}

View File

@ -1,6 +1,7 @@
package net.Indyuce.mmoitems.command.mmoitems;
import java.util.Arrays;
import java.util.Optional;
import java.util.Random;
import org.apache.commons.lang.Validate;
@ -14,10 +15,10 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.loot.ClassFilter;
import net.Indyuce.mmoitems.api.item.template.loot.LootBuilder;
import net.Indyuce.mmoitems.api.item.template.loot.TypeFilter;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.item.template.explorer.ClassFilter;
import net.Indyuce.mmoitems.api.item.template.explorer.TemplateExplorer;
import net.Indyuce.mmoitems.api.item.template.explorer.TypeFilter;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.mmogroup.mmolib.api.util.SmartGive;
@ -54,7 +55,7 @@ public class GenerateCommandTreeNode extends CommandTreeNode {
? MMOItems.plugin.getTiers().getOrThrow(handler.getValue("tier").toUpperCase().replace("-", "_"))
: MMOItems.plugin.getTemplates().rollTier();
LootBuilder builder = new LootBuilder(itemLevel, itemTier);
TemplateExplorer builder = new TemplateExplorer();
if (handler.hasArgument("matchclass"))
builder.applyFilter(new ClassFilter(rpgPlayer));
if (handler.hasArgument("class"))
@ -65,11 +66,11 @@ public class GenerateCommandTreeNode extends CommandTreeNode {
builder.applyFilter(new TypeFilter(Type.get(format)));
}
MMOItem mmoitem = builder.rollLoot();
Validate.notNull(mmoitem, "No item matched your criterias.");
Optional<MMOItemTemplate> optional = builder.rollLoot();
Validate.isTrue(optional.isPresent(), "No item matched your criterias.");
ItemStack item = mmoitem.newBuilder().build();
Validate.isTrue(item != null && item.getType() != Material.AIR, "Could not generate item with ID '" + mmoitem.getId() + "'");
ItemStack item = optional.get().newBuilder(itemLevel, itemTier).build().newBuilder().build();
Validate.isTrue(item != null && item.getType() != Material.AIR, "Could not generate item with ID '" + optional.get().getId() + "'");
new SmartGive(give).give(item);
return CommandResult.SUCCESS;

View File

@ -6,11 +6,14 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmocore.api.droptable.dropitem.DropItem;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate.TemplateOption;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.mmogroup.mmolib.api.MMOLineConfig;
public abstract class ItemGenerationDropItem extends DropItem {
protected final boolean matchLevel, tiered;
protected final int level;
protected final ItemTier tier;
@ -20,9 +23,7 @@ public abstract class ItemGenerationDropItem extends DropItem {
public ItemGenerationDropItem(MMOLineConfig config) {
super(config);
matchLevel = config.getBoolean("match-level", false);
level = config.getInt("level", 0);
tiered = config.getBoolean("tiered", false);
if (config.contains("tier")) {
String format = config.getString("tier").toUpperCase().replace("-", "_").replace(" ", "_");
@ -35,6 +36,13 @@ public abstract class ItemGenerationDropItem extends DropItem {
soulbound = config.getDouble("soulbound", 0);
}
public MMOItem rollMMOItem(MMOItemTemplate template, RPGPlayer rpgPlayer) {
int itemLevel = level > 0 ? level
: template.hasOption(TemplateOption.LEVEL_ITEM) ? MMOItems.plugin.getTemplates().rollLevel(rpgPlayer.getLevel()) : 0;
ItemTier itemTier = tier != null ? tier : template.hasOption(TemplateOption.TIERED) ? MMOItems.plugin.getTemplates().rollTier() : null;
return new MMOItemBuilder(template, itemLevel, itemTier).build();
}
public ItemStack rollUnidentification(MMOItem mmoitem) {
return random.nextDouble() < unidentified ? mmoitem.getType().getUnidentifiedTemplate().newBuilder(mmoitem.newBuilder().buildNBT()).build()
: mmoitem.newBuilder().build();

View File

@ -5,7 +5,6 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmocore.api.loot.LootBuilder;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
@ -34,11 +33,7 @@ public class ItemTemplateDropItem extends ItemGenerationDropItem {
@Override
public void collect(LootBuilder builder) {
RPGPlayer rpgPlayer = builder.getEntity().getMMOPlayerData().getMMOItems().getRPG();
int itemLevel = matchLevel ? MMOItems.plugin.getTemplates().rollLevel(rpgPlayer.getLevel()) : this.level;
ItemTier itemTier = this.tier != null ? this.tier : this.tiered ? MMOItems.plugin.getTemplates().rollTier() : null;
MMOItem mmoitem = template.newBuilder(itemLevel, itemTier).build();
MMOItem mmoitem = rollMMOItem(template, rpgPlayer);
if (rollSoulbound())
mmoitem.setData(ItemStat.SOULBOUND, new SoulboundData(rpgPlayer.getPlayer(), 1));

View File

@ -1,15 +1,18 @@
package net.Indyuce.mmoitems.comp.mmocore.load;
import java.util.Optional;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmocore.api.loot.LootBuilder;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.loot.ClassFilter;
import net.Indyuce.mmoitems.api.item.template.loot.TypeFilter;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.item.template.explorer.ClassFilter;
import net.Indyuce.mmoitems.api.item.template.explorer.TemplateExplorer;
import net.Indyuce.mmoitems.api.item.template.explorer.TypeFilter;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.stat.data.SoulboundData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
@ -40,24 +43,21 @@ public class RandomItemDropItem extends ItemGenerationDropItem {
public void collect(LootBuilder builder) {
RPGPlayer rpgPlayer = builder.getEntity().getMMOPlayerData().getMMOItems().getRPG();
int itemLevel = matchLevel ? MMOItems.plugin.getTemplates().rollLevel(rpgPlayer.getLevel()) : this.level;
ItemTier itemTier = this.tier != null ? this.tier : this.tiered ? MMOItems.plugin.getTemplates().rollTier() : null;
net.Indyuce.mmoitems.api.item.template.loot.LootBuilder loot = new net.Indyuce.mmoitems.api.item.template.loot.LootBuilder(itemLevel,
itemTier);
TemplateExplorer explorer = new TemplateExplorer();
if (matchClass)
loot.applyFilter(new ClassFilter(rpgPlayer));
explorer.applyFilter(new ClassFilter(rpgPlayer));
else if (!profess.isEmpty())
loot.applyFilter(new ClassFilter(profess));
explorer.applyFilter(new ClassFilter(profess));
if (type != null)
loot.applyFilter(new TypeFilter(type));
explorer.applyFilter(new TypeFilter(type));
MMOItem rolled = loot.rollLoot();
if (rolled == null)
Optional<MMOItemTemplate> optional = explorer.rollLoot();
if (!optional.isPresent())
return;
MMOItem rolled = rollMMOItem(optional.get(), rpgPlayer);
if (rollSoulbound())
rolled.setData(ItemStat.SOULBOUND, new SoulboundData(rpgPlayer.getPlayer(), 1));