!Improved give command

This commit is contained in:
Indyuce 2020-08-17 13:11:07 +02:00
parent b6eefa268b
commit 2d85f9a20b

View File

@ -1,5 +1,8 @@
package net.Indyuce.mmoitems.command.mmoitems;
import java.util.Arrays;
import java.util.Random;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -11,25 +14,31 @@ import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.MMOUtils;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.droptable.item.MMOItemDropItem;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.api.util.RandomAmount;
import net.Indyuce.mmoitems.api.util.message.Message;
import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot;
import net.Indyuce.mmoitems.stat.data.SoulboundData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.api.util.SmartGive;
import net.mmogroup.mmolib.command.api.CommandTreeNode;
import net.mmogroup.mmolib.command.api.Parameter;
public class GiveCommandTreeNode extends CommandTreeNode {
private static final Random random = new Random();
public GiveCommandTreeNode(CommandTreeNode parent) {
super(parent, "give");
addParameter(MMOItemsCommandTreeRoot.TYPE);
addParameter(MMOItemsCommandTreeRoot.ID_2);
addParameter(Parameter.PLAYER_OPTIONAL);
addParameter(new Parameter("(min-max)", (explore, list) -> list.add("1-3")));
addParameter(new Parameter("(unidentify-chance)", (explore, list) -> list.add("0")));
addParameter(new Parameter("(drop-chance)", (explore, list) -> list.add("1")));
addParameter(new Parameter("(min-max)", (explore, list) -> list.addAll(Arrays.asList("1-3", "1", "10", "32", "64"))));
addParameter(new Parameter("(unidentify-chance)", (explore, list) -> list.add("(unidentify-chance)")));
addParameter(new Parameter("(drop-chance)", (explore, list) -> list.add("(drop-chance)")));
addParameter(new Parameter("(soulbound-chance)", (explore, list) -> list.add("(soulbound-chance)")));
}
@Override
@ -46,14 +55,31 @@ public class GiveCommandTreeNode extends CommandTreeNode {
// item
Type type = MMOItems.plugin.getTypes().getOrThrow(args[1].toUpperCase().replace("-", "_"));
MMOItemDropItem dropItem = new MMOItemDropItem(type, args[2].toUpperCase(), args.length > 6 ? Double.parseDouble(args[6]) / 100 : 1,
args.length > 5 ? Double.parseDouble(args[5]) / 100 : 0, args.length > 4 ? new RandomAmount(args[4]) : new RandomAmount(1, 1));
if (!dropItem.rollDrop())
MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplateOrThrow(type, args[2].toUpperCase().replace("-", "_"));
RandomAmount amount = args.length > 4 ? new RandomAmount(args[4]) : new RandomAmount(1, 1);
double unidentify = args.length > 5 ? Double.parseDouble(args[5]) / 100 : 0;
double drop = args.length > 6 ? Double.parseDouble(args[6]) / 100 : 1;
double soulbound = args.length > 7 ? Double.parseDouble(args[7]) / 100 : 0;
// roll drop chance
if (random.nextDouble() > drop)
return CommandResult.SUCCESS;
ItemStack item = dropItem.getItem(PlayerData.get(target));
Validate.isTrue(item != null && item.getType() != Material.AIR, "Couldn't find/generate the item called '" + args[2].toUpperCase()
+ "'. Check your console for potential item generation issues.");
// generate mmoitem
MMOItem mmoitem = template.newBuilder(PlayerData.get(target).getRPG()).build();
// roll soulbound
if (random.nextDouble() < soulbound)
mmoitem.setData(ItemStat.SOULBOUND, new SoulboundData(target, 1));
// generate item
ItemStack item = random.nextDouble() < unidentify ? type.getUnidentifiedTemplate().newBuilder(mmoitem.newBuilder().buildNBT()).build()
: mmoitem.newBuilder().build();
// set amount
Validate.isTrue(item != null && item.getType() != Material.AIR,
"Couldn't find/generate the item called '" + template.getId() + "'. Check your console for potential item generation issues.");
item.setAmount(amount.getRandomAmount());
// message
if (!sender.equals(target))