Add /npc command itemcost

This commit is contained in:
fullwall 2021-02-01 22:35:28 +08:00
parent f776638508
commit e03d65aeb4
3 changed files with 77 additions and 0 deletions

View File

@ -125,6 +125,7 @@ public class Settings {
NPC_ATTACK_DISTANCE("npc.pathfinding.attack-range", 1.75 * 1.75),
NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE("npc.commands.error-messages.maximum-times-used",
"You have reached the maximum number of uses ({0})."),
NPC_COMMAND_MISSING_ITEM_MESSAGE("npc.commands.error-messages.missing-item", "Missing {1} {0}"),
NPC_COMMAND_NO_PERMISSION_MESSAGE("npc.commands.error-messages.no-permission",
"You don't have permission to do that."),
NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE("npc.commands.error-messages.not-enough-money", "You need at least ${0}."),

View File

@ -67,6 +67,7 @@ import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.PlayerCloneNPCEvent;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.gui.InventoryMenu;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
@ -87,6 +88,7 @@ import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.CommandTrait.ExecutionMode;
import net.citizensnpcs.trait.CommandTrait.ItemRequirementGUI;
import net.citizensnpcs.trait.CommandTrait.NPCCommandBuilder;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
@ -365,6 +367,10 @@ public class NPCCommands {
commands.getExecutionMode() == ExecutionMode.RANDOM ? ExecutionMode.LINEAR : ExecutionMode.RANDOM);
Messaging.sendTr(sender, commands.getExecutionMode() == ExecutionMode.RANDOM ? Messages.COMMANDS_RANDOM_SET
: Messages.COMMANDS_RANDOM_UNSET);
} else if (args.getString(1).equalsIgnoreCase("itemcost")) {
if (!(sender instanceof Player))
throw new CommandException(Messages.COMMAND_MUST_BE_INGAME);
InventoryMenu.create(new ItemRequirementGUI(commands)).present(((Player) sender));
} else {
throw new CommandUsageException();
}

View File

@ -9,8 +9,13 @@ import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.RegisteredServiceProvider;
@ -26,6 +31,9 @@ import com.google.common.io.ByteStreams;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCCommandDispatchEvent;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.Menu;
import net.citizensnpcs.api.gui.MenuContext;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.DelegatePersistence;
import net.citizensnpcs.api.persistence.Persist;
@ -58,6 +66,8 @@ public class CommandTrait extends Trait {
@Persist
private final Map<String, Long> globalCooldowns = Maps.newHashMap();
@Persist
private List<ItemStack> itemRequirements = Lists.newArrayList();
@Persist
private final List<String> temporaryPermissions = Lists.newArrayList();
public CommandTrait() {
@ -87,6 +97,25 @@ public class CommandTrait extends Trait {
Messaging.severe("Unable to find Vault when checking command cost - is it installed?");
}
}
if (itemRequirements.size() > 0) {
List<ItemStack> req = Lists.newArrayList(itemRequirements);
Inventory tempInventory = Bukkit.createInventory(null, 54);
for (int i = 0; i < player.getInventory().getSize(); i++) {
tempInventory.setItem(i, player.getInventory().getItem(i));
}
for (ItemStack stack : req) {
if (tempInventory.containsAtLeast(stack, stack.getAmount())) {
tempInventory.removeItem(stack);
} else {
sendErrorMessage(player, CommandTraitMessages.MISSING_ITEM, Util.prettyEnum(stack.getType()),
stack.getAmount());
return false;
}
}
for (int i = 0; i < player.getInventory().getSize(); i++) {
player.getInventory().setItem(i, tempInventory.getItem(i));
}
}
return true;
}
@ -277,6 +306,7 @@ public class CommandTrait extends Trait {
private enum CommandTraitMessages {
MAXIMUM_TIMES_USED(Setting.NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE),
MISSING_ITEM(Setting.NPC_COMMAND_MISSING_ITEM_MESSAGE),
MISSING_MONEY(Setting.NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE),
NO_PERMISSION(Setting.NPC_COMMAND_NO_PERMISSION_MESSAGE),
ON_COOLDOWN(Setting.NPC_COMMAND_ON_COOLDOWN_MESSAGE),
@ -301,6 +331,46 @@ public class CommandTrait extends Trait {
RIGHT;
}
@Menu(title = "Drag items for requirements", type = InventoryType.CHEST, dimensions = { 5, 9 })
public static class ItemRequirementGUI extends InventoryMenuPage {
private Inventory inventory;
private int taskId;
private CommandTrait trait;
private ItemRequirementGUI() {
throw new UnsupportedOperationException();
}
public ItemRequirementGUI(CommandTrait trait) {
this.trait = trait;
}
@Override
public void initialise(MenuContext ctx) {
this.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(CitizensAPI.getPlugin(), this, 0, 1);
this.inventory = ctx.getInventory();
for (ItemStack stack : trait.itemRequirements) {
inventory.addItem(stack.clone());
}
}
@Override
public void onClose(HumanEntity player) {
Bukkit.getScheduler().cancelTask(taskId);
}
@Override
public void run() {
List<ItemStack> requirements = Lists.newArrayList();
for (ItemStack stack : inventory.getContents()) {
if (stack != null && stack.getType() != Material.AIR) {
requirements.add(stack);
}
}
this.trait.itemRequirements = requirements;
}
}
private static class NPCCommand {
String bungeeServer;
String command;