This commit is contained in:
parent
d12bf53972
commit
c64032034c
7 changed files with 461 additions and 26 deletions
|
@ -7,6 +7,7 @@ import me.unurled.sacredrealms.sr.SR;
|
|||
import me.unurled.sacredrealms.sr.commands.admin.AttributeCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.ClientBuildCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.EntityTypeCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.ItemCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.admin.LevelCommand;
|
||||
import me.unurled.sacredrealms.sr.commands.player.ResetAdventureCommand;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
|
@ -40,6 +41,7 @@ public class CommandManager extends Manager {
|
|||
registerCommand("clientbuild", ClientBuildCommand.class);
|
||||
registerCommand("level", LevelCommand.class);
|
||||
registerCommand("entitytype", EntityTypeCommand.class);
|
||||
registerCommand("item", ItemCommand.class);
|
||||
|
||||
registerCommand("resetadventure", ResetAdventureCommand.class);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,435 @@
|
|||
package me.unurled.sacredrealms.sr.commands.admin;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Component.comp;
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.components.attributes.Attribute;
|
||||
import me.unurled.sacredrealms.sr.components.item.Item;
|
||||
import me.unurled.sacredrealms.sr.components.item.ItemManager;
|
||||
import me.unurled.sacredrealms.sr.components.item.ItemType;
|
||||
import me.unurled.sacredrealms.sr.components.item.Rarity;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ItemCommand implements TabExecutor {
|
||||
|
||||
/**
|
||||
* Executes the given command, returning its success. <br>
|
||||
* If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be
|
||||
* sent to the player.
|
||||
*
|
||||
* @param sender Source of the command
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args Passed command arguments
|
||||
* @return true if a valid command, otherwise false
|
||||
*/
|
||||
@Override
|
||||
public boolean onCommand(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (!(sender instanceof Player p)) {
|
||||
sender.sendMessage("You must be a player to use this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!p.hasPermission("sr.admin.item")) {
|
||||
p.sendMessage("You do not have permission to use this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
p.sendMessage("Usage: /item <list|create|delete|give|modify>");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("list")) {
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
if (im.getItemIDs().isEmpty()) {
|
||||
p.sendMessage(comp("<red>There are no items."));
|
||||
return true;
|
||||
}
|
||||
p.sendMessage("Items:");
|
||||
im.getItemIDs().forEach(p::sendMessage);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("create")) {
|
||||
if (args.length < 2) {
|
||||
p.sendMessage("Usage: /item create <id>");
|
||||
return true;
|
||||
}
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
if (im.getItemIDs().contains(args[1])) {
|
||||
p.sendMessage(comp("<red>An item with that ID already exists."));
|
||||
return true;
|
||||
}
|
||||
Item item = new Item();
|
||||
item.setID(args[1]);
|
||||
im.addItem(item);
|
||||
p.sendMessage(
|
||||
comp(
|
||||
"<green>Item created. You can now modify it using "
|
||||
+ "<click:suggest_command:/item "
|
||||
+ "modify "
|
||||
+ args[1]
|
||||
+ "> /item modify "
|
||||
+ args[1]
|
||||
+ "</click>"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("delete")) {
|
||||
if (args.length < 2) {
|
||||
p.sendMessage("Usage: /item delete <id>");
|
||||
return true;
|
||||
}
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
if (!im.getItemIDs().contains(args[1])) {
|
||||
p.sendMessage(comp("<red>An item with that ID does not exist."));
|
||||
return true;
|
||||
}
|
||||
im.removeItem(args[1]);
|
||||
p.sendMessage(comp("<green>Item deleted."));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("give")) {
|
||||
if (args.length < 2) {
|
||||
p.sendMessage("Usage: /item give <id>");
|
||||
return true;
|
||||
}
|
||||
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
if (!im.getItemIDs().contains(args[1])) {
|
||||
p.sendMessage(comp("<red>An item with that ID does not exist."));
|
||||
return true;
|
||||
}
|
||||
|
||||
Item item = im.getItem(args[1]);
|
||||
|
||||
if (args.length == 2) {
|
||||
p.getInventory().addItem(item.toItemStack());
|
||||
p.sendMessage(comp("<green>Item given."));
|
||||
return true;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[2]);
|
||||
if (target == null) {
|
||||
p.sendMessage(comp("<red>Player not found."));
|
||||
return true;
|
||||
}
|
||||
|
||||
target.getInventory().addItem(item.toItemStack());
|
||||
p.sendMessage(comp("<green>Item given."));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("modify")) {
|
||||
if (args.length < 2) {
|
||||
p.sendMessage(
|
||||
"Usage: /item modify <id> <name|material|enchantments|attributes|abilities|custommodeldata|rarity|type|description> [value]");
|
||||
return true;
|
||||
}
|
||||
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
if (!im.getItemIDs().contains(args[1])) {
|
||||
p.sendMessage(comp("<red>An item with that ID does not exist."));
|
||||
return true;
|
||||
}
|
||||
|
||||
Item item = im.getItem(args[1]);
|
||||
|
||||
if (args.length == 2) {
|
||||
p.sendMessage(
|
||||
"Usage: /item modify <id> <name|material|enchantments|attributes|abilities|custommodeldata|rarity|type|description> [value]");
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (args[2].toLowerCase()) {
|
||||
case "name" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> name <value>");
|
||||
return true;
|
||||
}
|
||||
String name = String.join(" ", Arrays.copyOfRange(args, 3, args.length));
|
||||
item.setName(name);
|
||||
p.sendMessage(comp("<green>Item name set."));
|
||||
// TODO: update item in player inventory
|
||||
return true;
|
||||
}
|
||||
case "material" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> material <value>");
|
||||
return true;
|
||||
}
|
||||
Material material = Material.matchMaterial(args[3]);
|
||||
if (material == null) {
|
||||
p.sendMessage(comp("<red>Material not found."));
|
||||
return true;
|
||||
}
|
||||
item.setMaterial(material);
|
||||
p.sendMessage(comp("<green>Item material set."));
|
||||
}
|
||||
case "enchantments" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> enchantments <add|remove> [enchantment]");
|
||||
return true;
|
||||
}
|
||||
if (args[3].equalsIgnoreCase("add")) {
|
||||
if (args.length < 5) {
|
||||
p.sendMessage("Usage: /item modify <id> enchantments add <enchantment>");
|
||||
return true;
|
||||
}
|
||||
// TODO: add enchantment
|
||||
p.sendMessage("Enchantments not working for now.");
|
||||
return true;
|
||||
}
|
||||
if (args[3].equalsIgnoreCase("remove")) {
|
||||
if (args.length < 5) {
|
||||
p.sendMessage("Usage: /item modify <id> enchantments remove <enchantment>");
|
||||
return true;
|
||||
}
|
||||
// TODO: remove enchantment
|
||||
p.sendMessage("Enchantments not working for now.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case "attributes" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> attributes <add|remove> [attribute] [value]");
|
||||
return true;
|
||||
}
|
||||
if (args[3].equalsIgnoreCase("add")) {
|
||||
if (args.length < 6) {
|
||||
p.sendMessage("Usage: /item modify <id> attributes add <attribute> <value>");
|
||||
return true;
|
||||
}
|
||||
Attribute attribute = null;
|
||||
try {
|
||||
attribute = Attribute.valueOf(args[4]);
|
||||
} catch (Exception e) {
|
||||
p.sendMessage(comp("<red>Attribute not found."));
|
||||
error("Error on attribute affectation on item: " + args[1] + "\n" + e);
|
||||
return true;
|
||||
}
|
||||
double value;
|
||||
try {
|
||||
value = Double.parseDouble(args[5]);
|
||||
} catch (NumberFormatException e) {
|
||||
p.sendMessage(comp("<red>Invalid value."));
|
||||
return true;
|
||||
}
|
||||
item.addAttribute(attribute, value);
|
||||
p.sendMessage(comp("<green>Attribute added."));
|
||||
return true;
|
||||
}
|
||||
if (args[3].equalsIgnoreCase("remove")) {
|
||||
if (args.length < 5) {
|
||||
p.sendMessage("Usage: /item modify <id> attributes remove <attribute>");
|
||||
return true;
|
||||
}
|
||||
Attribute attribute = null;
|
||||
try {
|
||||
attribute = Attribute.valueOf(args[4]);
|
||||
} catch (Exception e) {
|
||||
p.sendMessage(comp("<red>Attribute not found."));
|
||||
error("Error on attribute affectation on item: " + args[1] + "\n" + e);
|
||||
return true;
|
||||
}
|
||||
item.removeAttribute(attribute);
|
||||
p.sendMessage(comp("<green>Attribute removed."));
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case "abilities" -> {
|
||||
if (args.length < 5) {
|
||||
p.sendMessage("Usage: /item modify <id> abilities add <ability>");
|
||||
return true;
|
||||
}
|
||||
if (args[3].equalsIgnoreCase("add")) {
|
||||
p.sendMessage("Abilities not working for now.");
|
||||
// TODO: add ability
|
||||
} else if (args[3].equalsIgnoreCase("remove")) {
|
||||
p.sendMessage("Abilities not working for now.");
|
||||
// TODO: remove ability
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case "custommodeldata" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> custommodeldata <value>");
|
||||
return true;
|
||||
}
|
||||
int customModelData;
|
||||
try {
|
||||
customModelData = Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException e) {
|
||||
p.sendMessage(comp("<red>Invalid value."));
|
||||
return true;
|
||||
}
|
||||
item.setCustomModelData(customModelData);
|
||||
p.sendMessage(comp("<green>Custom model data set."));
|
||||
return true;
|
||||
}
|
||||
case "rarity" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> rarity <value>");
|
||||
return true;
|
||||
}
|
||||
Rarity rarity;
|
||||
try {
|
||||
rarity = Rarity.valueOf(args[3]);
|
||||
} catch (Exception e) {
|
||||
p.sendMessage(comp("<red>Rarity not found."));
|
||||
return true;
|
||||
}
|
||||
item.setRarity(rarity);
|
||||
p.sendMessage(comp("<green>Rarity set."));
|
||||
return true;
|
||||
}
|
||||
case "type" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> type <value>");
|
||||
return true;
|
||||
}
|
||||
ItemType type;
|
||||
try {
|
||||
type = ItemType.valueOf(args[3]);
|
||||
} catch (Exception e) {
|
||||
p.sendMessage(comp("<red>Type not found."));
|
||||
return true;
|
||||
}
|
||||
item.setType(type);
|
||||
p.sendMessage(comp("<green>Type set."));
|
||||
return true;
|
||||
}
|
||||
case "description" -> {
|
||||
if (args.length < 4) {
|
||||
p.sendMessage("Usage: /item modify <id> description <value>");
|
||||
return true;
|
||||
}
|
||||
String description = String.join(" ", Arrays.copyOfRange(args, 3, args.length));
|
||||
item.setDescription(description);
|
||||
p.sendMessage(comp("<green>Description set."));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a list of possible completions for a command argument.
|
||||
*
|
||||
* @param sender Source of the command. For players tab-completing a command inside of a command
|
||||
* block, this will be the player, not the command block.
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args The arguments passed to the command, including final partial argument to be
|
||||
* completed
|
||||
* @return A List of possible completions for the final argument, or null to default to the
|
||||
* command executor
|
||||
*/
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (!(sender instanceof Player) || !sender.hasPermission("sr.admin.item")) {
|
||||
return List.of("");
|
||||
}
|
||||
|
||||
if (args.length == 1) {
|
||||
return List.of("list", "create", "delete", "give", "modify");
|
||||
}
|
||||
|
||||
if (args.length == 2) {
|
||||
if (args[0].equalsIgnoreCase("delete")
|
||||
|| args[0].equalsIgnoreCase("give")
|
||||
|| args[0].equalsIgnoreCase("modify")) {
|
||||
ItemManager im = ItemManager.getInstance(ItemManager.class);
|
||||
return im.getItemIDs();
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 3) {
|
||||
if (args[0].equalsIgnoreCase("modify")) {
|
||||
return List.of(
|
||||
"name",
|
||||
"material",
|
||||
"enchantments",
|
||||
"attributes",
|
||||
"abilities",
|
||||
"custommodeldata",
|
||||
"rarity",
|
||||
"type",
|
||||
"description");
|
||||
} else if (args[0].equalsIgnoreCase("give")) {
|
||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 4) {
|
||||
if (args[0].equalsIgnoreCase("modify")) {
|
||||
if (args[2].equalsIgnoreCase("rarity")) {
|
||||
return Arrays.stream(Rarity.values()).map(Enum::name).toList();
|
||||
} else if (args[2].equalsIgnoreCase("type")) {
|
||||
return Arrays.stream(ItemType.values()).map(Enum::name).toList();
|
||||
} else if (args[2].equalsIgnoreCase("material")) {
|
||||
return Arrays.stream(Material.values()).map(Enum::name).toList();
|
||||
} else if (args[2].equalsIgnoreCase("enchantments")) {
|
||||
return List.of("add", "remove");
|
||||
} else if (args[2].equalsIgnoreCase("abilities")) {
|
||||
return List.of("add", "remove");
|
||||
} else if (args[2].equalsIgnoreCase("attributes")) {
|
||||
return List.of("add", "remove");
|
||||
} else if (args[2].equalsIgnoreCase("custommodeldata")) {
|
||||
try {
|
||||
Integer.parseInt(args[3]);
|
||||
} catch (NumberFormatException e) {
|
||||
return List.of("1");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 5) {
|
||||
if (args[0].equalsIgnoreCase("modify")) {
|
||||
/*if (args[2].equalsIgnoreCase("enchantments")) { // TODO: Enchantments in completion
|
||||
return Arrays.stream(Enchantment.values()).map(Enum::name).toList();
|
||||
} else*/ if (args[2].equalsIgnoreCase("attributes")) {
|
||||
return Arrays.stream(Attribute.values()).map(Enum::name).toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 6) {
|
||||
if (args[0].equalsIgnoreCase("modify")) {
|
||||
if (args[2].equalsIgnoreCase("attributes")) {
|
||||
try {
|
||||
Double.parseDouble(args[5]);
|
||||
} catch (NumberFormatException e) {
|
||||
return List.of("1.0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return List.of("");
|
||||
}
|
||||
}
|
|
@ -127,6 +127,14 @@ public class Item {
|
|||
this.rarity = rarity;
|
||||
}
|
||||
|
||||
public ItemType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(ItemType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public HashMap<Attribute, Double> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
@ -263,4 +271,8 @@ public class Item {
|
|||
error("Failed to parse item from string: " + item + "\n" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setID(String arg) {
|
||||
this.ID = arg;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package me.unurled.sacredrealms.sr.components.item;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import me.unurled.sacredrealms.sr.data.gson.ItemDeserializer;
|
||||
|
@ -12,7 +13,6 @@ import org.bukkit.NamespacedKey;
|
|||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
@SuppressWarnings("EmptyMethod")
|
||||
public class ItemManager extends Manager {
|
||||
public static final NamespacedKey ID = new NamespacedKey("sr", "id");
|
||||
|
||||
|
@ -85,4 +85,8 @@ public class ItemManager extends Manager {
|
|||
public boolean isItem(String id) {
|
||||
return items.containsKey(id);
|
||||
}
|
||||
|
||||
public List<String> getItemIDs() {
|
||||
return List.copyOf(items.keySet());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,3 @@
|
|||
package me.unurled.sacredrealms.sr.components.item.enchantments;
|
||||
|
||||
public class Enchantment {
|
||||
private final String name;
|
||||
private final String ID;
|
||||
private final Integer maxLevel;
|
||||
|
||||
public Enchantment(String name, String ID, Integer maxLevel) {
|
||||
this.name = name;
|
||||
this.ID = ID;
|
||||
this.maxLevel = maxLevel;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public Integer getMaxLevel() {
|
||||
return maxLevel;
|
||||
}
|
||||
}
|
||||
public record Enchantment(String name, String ID, Integer maxLevel) {}
|
||||
|
|
|
@ -112,8 +112,7 @@ public class Items {
|
|||
lore.add(comp("<dark_purple>Enchantments:"));
|
||||
item.getEnchantments()
|
||||
.forEach(
|
||||
(k, v) ->
|
||||
lore.add(comp(" <blue>➤ " + k.getName() + " <if " + "max level gold>" + v)));
|
||||
(k, v) -> lore.add(comp(" <blue>➤ " + k.name() + " <if " + "max level gold>" + v)));
|
||||
}
|
||||
|
||||
stack.lore(lore);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue