This commit is contained in:
YuTian 2025-01-24 22:41:52 +08:00
parent 601f3e2ea5
commit db6c29f103
46 changed files with 469 additions and 250 deletions

View File

@ -26,7 +26,7 @@ public final class ElementOriginLib extends JavaPlugin {
public void onEnable() {
instance = this;
loadLibraries();
// loadLibraries();
new GuiHandlerListener(this);
new PlayerChatInputListener(this);
@ -49,7 +49,7 @@ public final class ElementOriginLib extends JavaPlugin {
LibraryManager libraryManager = new BukkitLibraryManager(this);
libraryManager.addMavenCentral();
libraryManager.setLogLevel(LogLevel.WARN);
libraryManager.loadLibrary(Library.builder().groupId("org{}apache{}commons").artifactId("commons-compress").version("1.27.1").build());
// libraryManager.loadLibrary(Library.builder().groupId("org{}apache{}commons").artifactId("commons-compress").version("1.27.1").build());
// libraryManager.loadLibrary(Library.builder().groupId("com{}github{}luben").artifactId("zstd-jni").version("1.5.6-9").build());
// libraryManager.loadLibrary(Library.builder().groupId("com{}zaxxer").artifactId("HikariCP").version("6.2.1").build());
// libraryManager.loadLibrary(Library.builder().groupId("org{}xerial").artifactId("sqlite-jdbc").version("3.46.0.0").build());

View File

@ -3,13 +3,13 @@ package com.io.yutian.elementoriginlib.command;
import com.io.yutian.elementoriginlib.command.argument.Argument;
import com.io.yutian.elementoriginlib.command.argument.ArgumentType;
import com.io.yutian.elementoriginlib.command.interfaces.Command;
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
import com.io.yutian.elementoriginlib.command.interfaces.Parameter;
import com.io.yutian.elementoriginlib.command.interfaces.SubCommand;
import com.io.yutian.elementoriginlib.exception.command.CommandRegisterException;
import com.io.yutian.elementoriginlib.exception.command.CommandParseException;
import com.io.yutian.elementoriginlib.logger.Logger;
import org.bukkit.command.CommandSender;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
public class CommandEntity {
@ -50,23 +50,37 @@ public class CommandEntity {
return childrens;
}
public static CommandEntity parseFromClass(Class clazz) {
if (clazz == null) {
LOGGER.warn("Class is null");
return null;
public boolean canInvoke(CommandSender sender) {
if (childrens.size() == 1 && childrens.get(0).isNodal()) {
CommandEntry child = childrens.get(0);
return child.canInvoke(sender);
}
if (!clazz.isAnnotationPresent(Command.class)) {
LOGGER.warn("Class " + clazz.getName() + " is not annotated with @Command");
return null;
return true;
}
Object instance;
try {
instance = clazz.getConstructor(null).newInstance();
} catch (Exception e) {
LOGGER.warn("无法实例化类 " + clazz);
return null;
public boolean hasPermission(CommandSender sender) {
if (childrens.size() == 1 && childrens.get(0).isNodal()) {
CommandEntry child = childrens.get(0);
return child.hasPermission(sender);
}
return sender.isOp() || sender.hasPermission(permission);
}
public static CommandEntity parseFromClass(Object instance, Class clazz) {
if (clazz == null) {
throw new NullPointerException("Class is null");
}
if (!clazz.isAnnotationPresent(Command.class)) {
throw new CommandParseException("" + clazz + " 未标注 @Command 注解");
}
// Object instance;
// try {
// instance = clazz.getConstructor(null).newInstance();
// } catch (Exception e) {
// e.printStackTrace();
// throw new CommandParseException("无法实例化类 "+clazz);
// }
Command commandAnnotation = (Command) clazz.getAnnotation(Command.class);
String command = commandAnnotation.value();
@ -76,30 +90,41 @@ public class CommandEntity {
Map<String, CommandEntry> pathToEntryMap = new HashMap<>();
List<CommandEntry> rootEntries = new ArrayList<>();
List<String> allPaths = new ArrayList<>();
boolean hasNodal = false;
try {
for (Method method : clazz.getDeclaredMethods()) {
if (!method.isAnnotationPresent(SubCommand.class)) {
continue;
}
CommandEntry commandEntry = parseFromMethod(method);
CommandEntry commandEntry = parseFromMethod(clazz, method);
if (commandEntry.isNodal()) {
if (hasNodal) {
throwParseException(clazz, "命令路径冲突: 命令 '" + command + "' 不能同时出现多个无节点命令(nodal=true)");
}
hasNodal = true;
commandEntry.setName(command);
}
if (commandEntry != null) {
allEntries.add(commandEntry);
String path = commandEntry.getFullName();
allPaths.add(path);
allEntries.add(commandEntry);
pathToEntryMap.put(path, commandEntry);
}
}
if (hasNodal && allEntries.size() > 1) {
throwParseException(clazz, "命令路径冲突: 命令 '" + command + "' 不能同时包含子命令和无节点命令(nodal=true)");
}
for (String path : allPaths) {
String parentPath = getParentPath(path);
if (pathToEntryMap.containsKey(path)) {
LOGGER.warn("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
throw new CommandRegisterException("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
}
if (!parentPath.isEmpty() && pathToEntryMap.containsKey(parentPath)) {
LOGGER.warn("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
throw new CommandRegisterException("命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
}
pathToEntryMap.put(commandEntry.getFullName(), commandEntry);
throwParseException(clazz, "命令路径冲突: 子命令 '" + path + "' 已经定义, 不能再定义父命令 '" + parentPath + "'");
}
}
@ -122,6 +147,7 @@ public class CommandEntity {
parentEntry.getChildrens().add(entry);
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
@ -130,6 +156,7 @@ public class CommandEntity {
return new CommandEntity(instance, command, permission, rootEntries);
}
private static String getParentPath(String path) {
int lastDotIndex = path.lastIndexOf('.');
if (lastDotIndex == -1) {
@ -159,17 +186,17 @@ public class CommandEntity {
}
private static CommandEntry parseFromMethod(Method method) {
private static CommandEntry parseFromMethod(Class clazz, Method method) {
SubCommand subCommand = method.getAnnotation(SubCommand.class);
String path = subCommand.value();
String permission = subCommand.permission();
boolean nodal = subCommand.nodal();
String[] senderRequireArray = subCommand.senderRequire();
List<SenderRequire> senderRequireList = new ArrayList<>();
for (String senderRequire : senderRequireArray) {
SenderRequire senderRequire1 = SenderRequires.get(senderRequire);
if (senderRequire1 == null) {
LOGGER.warn("Sender require " + senderRequire + " is null");
continue;
throwParseException(clazz, "使用者权限 " + senderRequire + " 不存在");
}
senderRequireList.add(SenderRequires.get(senderRequire));
}
@ -179,39 +206,54 @@ public class CommandEntity {
if (path.contains(".")) {
int lastDotIndex = path.lastIndexOf('.');
if (lastDotIndex == -1 || lastDotIndex > path.length() - 1) {
LOGGER.warn("Invalid command name '" + path + "'");
return null;
throwParseException(clazz, "命令名 '" + path + "' 无效");
}
subName = path.substring(path.lastIndexOf('.') + 1);
}
List<Argument> arguments = new ArrayList<>();
for (Parameter parameter : method.getParameters()) {
if (!parameter.isAnnotationPresent(CommandArgument.class)) {
int allArgumentsCount = (int) Arrays.stream(method.getParameters())
.filter(parameter -> parameter.isAnnotationPresent(Parameter.class))
.count();
int index = 0;
for (java.lang.reflect.Parameter parameter : method.getParameters()) {
if (!parameter.isAnnotationPresent(Parameter.class)) {
continue;
}
CommandArgument commandArgument = parameter.getAnnotation(CommandArgument.class);
Parameter commandArgument = parameter.getAnnotation(Parameter.class);
String name = commandArgument.name();
String argumentName = name.isEmpty() ? parameter.getName() : name;
boolean required = commandArgument.required();
if (!required && index < allArgumentsCount - 1) {
throwParseException(clazz, "可选参数仅能在最后一个位置中使用");
}
String defaultValue = commandArgument.defaultValue();
String suggestionType = commandArgument.suggestionType();
Class<?> type = parameter.getType();
ArgumentType argumentType = ArgumentType.get(parameter.getType());
if (argumentType == null) {
LOGGER.warn("Argument type " + parameter.getType().getName() + " is null");
continue;
throwParseException(clazz, "参数类型 " + parameter.getType().getName() + " 不存在");
}
Argument argument = new Argument(argumentName, argumentType);
if (!suggestionType.isEmpty()) {
Suggest suggest = Suggests.getSuggest(suggestionType);
if (suggest == null) {
LOGGER.warn("Suggest type " + suggestionType + " is null");
throwParseException(clazz, "建议类型 " + parameter.getType().getName() + " 不存在");
} else {
argument.suggest(suggest);
}
}
arguments.add(argument);
if (!required) {
argument.optional(defaultValue);
}
return new CommandEntry(path, subName, permission, senderRequireList, arguments, method);
arguments.add(argument);
index++;
}
return new CommandEntry(path, subName, permission, nodal, senderRequireList, arguments, method);
}
private static void throwParseException(Class clazz, String message) {
LOGGER.warn("解析命令 "+clazz+" 时出现错误");
throw new CommandParseException(message);
}
@Override
@ -222,4 +264,5 @@ public class CommandEntity {
", childrens=" + childrens +
'}';
}
}

View File

@ -2,15 +2,15 @@ package com.io.yutian.elementoriginlib.command;
import com.io.yutian.elementoriginlib.command.argument.Argument;
import com.io.yutian.elementoriginlib.command.argument.ArgumentValue;
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
import com.io.yutian.elementoriginlib.command.interfaces.Parameter;
import com.io.yutian.elementoriginlib.logger.Logger;
import org.bukkit.command.CommandSender;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CommandEntry {
@ -22,6 +22,8 @@ public class CommandEntry {
private String permission;
private List<SenderRequire> senderRequires = new ArrayList<>();
private boolean nodal;
private List<Argument> arguments = new ArrayList<>();
private List<CommandEntry> childrens = new ArrayList<>();
@ -33,10 +35,11 @@ public class CommandEntry {
private Class<?>[] parameterTypes;
public CommandEntry(String fullName, String name, String permission, List<SenderRequire> senderRequires, List<Argument> arguments, Method method) {
public CommandEntry(String fullName, String name, String permission, boolean nodal, List<SenderRequire> senderRequires, List<Argument> arguments, Method method) {
this.fullName = fullName;
this.name = name;
this.permission = permission;
this.nodal = nodal;
this.senderRequires = senderRequires;
this.arguments = arguments;
this.method = method;
@ -75,8 +78,8 @@ public class CommandEntry {
int argumentIndex = 0;
for (int i = 0; i < parameterTypes.length; i++) {
Parameter parameter = method.getParameters()[i];
if (parameter.isAnnotationPresent(CommandArgument.class)) {
java.lang.reflect.Parameter parameter = method.getParameters()[i];
if (parameter.isAnnotationPresent(Parameter.class)) {
String paramName = arguments.get(argumentIndex).getName();
ArgumentValue argumentValue = commandContext.getArgumentsValue(paramName);
if (argumentValue != null) {
@ -205,6 +208,10 @@ public class CommandEntry {
return arguments;
}
public boolean isNodal() {
return nodal;
}
public List<CommandEntry> getChildrens() {
return childrens;
}
@ -213,15 +220,24 @@ public class CommandEntry {
return depth;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "CommandEntry{" +
"fullName='" + fullName + '\'' +
", name='" + name + '\'' +
", permission='" + permission + '\'' +
", senderRequires=" + senderRequires +
", nodal=" + nodal +
", arguments=" + arguments +
", childrens=" + childrens +
", depth=" + depth +
", method=" + method +
", methodHandle=" + methodHandle +
", parameterTypes=" + Arrays.toString(parameterTypes) +
'}';
}
}

View File

@ -6,6 +6,6 @@ public interface ICommandManager {
String getName();
List<CommandEntity> getCommands();
List<CommandEntity> getCommandEntities();
}

View File

@ -22,35 +22,32 @@ public class SimpleCommandManager implements ICommandManager {
private final String name;
@NotNull
private List<Class<?>> commandClasses = new ArrayList<>();
private Map<Class<?>, Object> commands = new HashMap<>();
@NotNull
private List<CommandEntity> commands = new ArrayList<>();
private List<CommandEntity> commandEntities = new ArrayList<>();
public SimpleCommandManager(Plugin plugin, String name) {
this(plugin, name, new ArrayList<>());
}
public SimpleCommandManager(Plugin plugin, String name, @NotNull List<Class<?>> commandClasses) {
public SimpleCommandManager(@NotNull Plugin plugin, @NotNull String name) {
this.plugin = plugin;
this.name = name;
this.commandClasses = commandClasses;
}
public void register(@NotNull Class<?> commandClass) {
if (commandClass == null) {
return;
public <T> void register(@NotNull Object instance) {
if (instance == null) {
throw new NullPointerException("instance is null");
}
CommandEntity command = CommandEntity.parseFromClass(commandClass);
Class<?> instanceClass = instance.getClass();
CommandEntity command = CommandEntity.parseFromClass(instance, instanceClass);
if (command == null) {
return;
}
commandClasses.add(commandClass);
commands.add(command);
commands.put(instanceClass, instance);
commandEntities.add(command);
}
public void unregisterAll() {
commands.clear();
commandEntities.clear();
}
public void unregister(Class<?> commandClass) {
@ -58,6 +55,7 @@ public class SimpleCommandManager implements ICommandManager {
return;
}
commands.remove(commandClass);
commandEntities.clear();
}
public void registerPluginCommand(@NotNull String commandName) {
@ -76,8 +74,8 @@ public class SimpleCommandManager implements ICommandManager {
@NotNull
@Override
public List<CommandEntity> getCommands() {
return commands;
public List<CommandEntity> getCommandEntities() {
return commandEntities;
}
static {

View File

@ -11,14 +11,14 @@ public class Argument {
private Suggest suggest;
private boolean optional = false;
private Object defaultValue = null;
private String defaultValue = null;
public Argument(String name, ArgumentType argumentsType) {
this.name = name;
this.argumentsType = argumentsType;
}
public Argument optional(Object defaultValue) {
public Argument optional(String defaultValue) {
optional = true;
this.defaultValue = defaultValue;
return this;
@ -36,7 +36,7 @@ public class Argument {
return optional;
}
public Object getDefaultValue() {
public String getDefaultValue() {
return defaultValue;
}

View File

@ -1,12 +0,0 @@
package com.io.yutian.elementoriginlib.command.argument;
public class ArgumentNode extends Argument {
private static final ArgumentType<Object> NODE = new ArgumentType<>("node", null, null);
public ArgumentNode(String name) {
super(name, NODE);
}
}

View File

@ -26,7 +26,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
return;
}
String commandName = args[0];
Optional<CommandEntity> entityOptional = commandManager.getCommands().stream()
Optional<CommandEntity> entityOptional = commandManager.getCommandEntities().stream()
.filter(entity -> entity.getCommand().equalsIgnoreCase(commandName))
.findFirst();
@ -36,7 +36,11 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
}
CommandEntity commandEntity = entityOptional.get();
handleCommand(commandEntity, sender, label, args, commandEntity.getChildrens(), 1, new StringBuilder());
int index = 1;
if (commandEntity.getChildrens().size() == 1 && commandEntity.getChildrens().get(0).isNodal()) {
index = 0;
}
handleCommand(commandEntity, sender, label, args, commandEntity.getChildrens(), index, new StringBuilder());
}
private void handleCommand(CommandEntity commandEntity, CommandSender sender, String label, String[] args, List<CommandEntry> entries, int index, StringBuilder fullCommand) {
@ -109,16 +113,18 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
int argIndex = startIndex + arguments.indexOf(argument);
if (argIndex >= args.length) {
if (argument.isOptional()) {
parsedArguments.put(argument.getName(), new ArgumentValue(argument.getDefaultValue()));
} else {
if (!argument.isOptional()) {
sender.sendMessage(Lang.get("command.short-arg", argument.getName()));
return null;
}
continue;
}
String rawValue = args[argIndex];
String rawValue = null;
if (argument.isOptional() && argIndex >= args.length) {
rawValue = argument.getDefaultValue();
} else {
rawValue = args[argIndex];
}
if (!argument.getArgumentsType().test(rawValue)) {
sender.sendMessage(Lang.get("command.error-arg", argIndex + 1, rawValue));
return null;
@ -141,13 +147,13 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 1) {
return commandManager.getCommands().stream()
return commandManager.getCommandEntities().stream()
.map(CommandEntity::getCommand)
.collect(Collectors.toList());
}
String commandName = args[0];
Optional<CommandEntity> entityOptional = commandManager.getCommands().stream()
Optional<CommandEntity> entityOptional = commandManager.getCommandEntities().stream()
.filter(entity -> entity.getCommand().equalsIgnoreCase(commandName))
.findFirst();
@ -158,15 +164,24 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
CommandEntity commandEntity = entityOptional.get();
List<CommandEntry> entries = commandEntity.getChildrens();
if (entries.size() == 0) {
return Collections.emptyList();
}
int depth = args.length;
String currentArg = args[depth - 1];
return getSuggestions(sender, entries, args, 1, depth - 1, currentArg);
// int index = depth - 1;
// if (commandEntity.getChildrens().size() == 1 && commandEntity.getChildrens().get(0).isNodal()) {
// index = depth - 2;
// }
return getSuggestions(sender, entries, args, 0, depth - 1, currentArg);
}
private List<String> getSuggestions(CommandSender sender, List<CommandEntry> entries, String[] args, int start, int currentDepth, String currentArg) {
int maxDepth = -1;
CommandEntry commandEntry = null;
for (int i = start; i < currentDepth; i++) {
String arg = args[i];
Optional<CommandEntry> optionalEntry = entries.stream()
@ -180,15 +195,11 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
commandEntry = optionalEntry.get();
entries = commandEntry.getChildrens();
if (entries.size() > 0) {
maxDepth = i + 1;
}
}
if (entries.size() == 0) {
List<Argument> arguments = commandEntry.getArguments();
int index = (int) (currentDepth - commandEntry.getDepth() - 2);
int index = (int) (currentDepth - commandEntry.getDepth() - 1);
if (index >= arguments.size()) {
return Collections.emptyList();
}

View File

@ -7,12 +7,14 @@ import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface CommandArgument {
public @interface Parameter {
String name() default "";
boolean required() default true;
String defaultValue() default "";
String suggestionType() default "";
}

View File

@ -15,4 +15,6 @@ public @interface SubCommand {
String[] senderRequire() default { "console", "player" };
boolean nodal() default false;
}

View File

@ -0,0 +1,126 @@
package com.io.yutian.elementoriginlib.command.list;
import com.io.yutian.elementoriginlib.command.CommandContext;
import com.io.yutian.elementoriginlib.command.CommandEntity;
import com.io.yutian.elementoriginlib.command.CommandEntry;
import com.io.yutian.elementoriginlib.command.interfaces.Command;
import com.io.yutian.elementoriginlib.command.interfaces.Parameter;
import com.io.yutian.elementoriginlib.command.interfaces.SubCommand;
import com.io.yutian.elementoriginlib.lang.Lang;
import com.io.yutian.elementoriginlib.list.PageList;
import com.io.yutian.elementoriginlib.manager.CommandManager;
import com.io.yutian.elementoriginlib.util.ComponentBuilder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.command.CommandSender;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Command("help")
public class CommandHelp {
private final static int MAX_PAGE_SIZE = 8;
private final CommandManager commandManager;
private final String alias;
public CommandHelp(CommandManager commandManager, String alias) {
this.commandManager = commandManager;
this.alias = alias;
}
@SubCommand(nodal = true)
public void help(CommandContext commandContext, @Parameter(required = false, defaultValue = "1") int page) {
String commandAlias = alias != null ? alias : commandContext.getLabel();
CommandSender sender = commandContext.getSender();
if (page <= 0) {
sender.sendMessage(Lang.get("command.help.page-error"));
return;
}
List<CommandEntity> commands = commandManager.getCommandEntities();
Stream<CommandEntity> stream = commands.stream().filter((c) -> c.hasPermission(sender) && c.canInvoke(sender));
List<CommandEntity> list = stream.collect(Collectors.toList());
PageList<CommandEntity> pageList = new PageList<>(list, MAX_PAGE_SIZE);
if (page > pageList.size()) {
sender.sendMessage(Lang.get("command.help.page-error"));
return;
}
sender.sendMessage(" ");
List<CommandEntity> commandList = pageList.getList(page);
sender.sendMessage("§7======[ §e§l"+commandManager.getName()+" §7]======");
for (CommandEntity command : commandList) {
StringBuilder stringBuilder = getCommandInfo(command, commandAlias);
sender.sendMessage(stringBuilder.toString());
}
ComponentBuilder componentBuilder = new ComponentBuilder();
boolean hasUpPage = page > 1;
boolean hasNextPage = page < pageList.size();
componentBuilder.add("§7=====");
if (hasUpPage) {
componentBuilder.add(" §7["+getColor(true)+"◀§7] ", ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, "/"+commandAlias+" help "+(page-1)), HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("§f上一页")));
} else {
componentBuilder.add(" §7["+getColor(false)+"◀§7] ");
}
componentBuilder.add("§7====");
componentBuilder.add("(§a"+page+"§f/§e"+pageList.size()+"§7)");
componentBuilder.add("§7====");
if (hasNextPage) {
componentBuilder.add(" §7["+getColor(true)+"▶§7] ", ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, "/"+commandAlias+" help "+(page+1)), HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text("§f下一页")));
} else {
componentBuilder.add(" §7["+getColor(false)+"▶§7] ");
}
componentBuilder.add("§7=====");
sender.sendMessage(componentBuilder.build());
}
private StringBuilder getCommandInfo(CommandEntity command, String commandAlias) {
StringBuilder stringBuilder = new StringBuilder("§6/"+ commandAlias +" "+ command.getCommand());
stringBuilder.append("§f");
System.out.println(commandAlias);
if (command.getChildrens().size() > 0) {
} else {
}
Optional<String> optional = Lang.getOptional("command."+command.getCommand()+".description");
if (optional.isPresent()) {
stringBuilder.append(" ");
stringBuilder.append("§7- §f"+ optional.get());
}
// if (command.getChildrens().size() > 0) {
// StringBuilder sb = new StringBuilder();
// sb.append(" [");
// int i = 0;
// for (CommandNode node : command.getCommandNodes()) {
// sb.append(node.getName());
// if (i + 1 < command.getCommandNodes().size()) {
// sb.append("/");
// }
// i++;
// }
// sb.append("]");
// stringBuilder.append(sb);
// } else {
// for (Argument argument : command.getArguments()) {
// stringBuilder.append(" ");
// stringBuilder.append("<"+argument.getName()+">");
// }
// }
// Optional<String> optional = Lang.getOptional("command."+command.getName()+".description");
// if (optional.isPresent()) {
// stringBuilder.append(" ");
// stringBuilder.append("§7- §f"+ optional.get());
// }
return stringBuilder;
}
private String getColor(boolean hasPage) {
return hasPage ? "§a" : "§c";
}
}

View File

@ -3,32 +3,46 @@ package com.io.yutian.elementoriginlib.command.list;
import com.io.yutian.elementoriginlib.command.CommandContext;
import com.io.yutian.elementoriginlib.command.interfaces.Command;
import com.io.yutian.elementoriginlib.command.interfaces.SubCommand;
import com.io.yutian.elementoriginlib.command.interfaces.CommandArgument;
import com.io.yutian.elementoriginlib.command.interfaces.Parameter;
import com.io.yutian.elementoriginlib.item.OriginItem;
import com.io.yutian.elementoriginlib.item.stat.ItemStats;
import com.io.yutian.elementoriginlib.item.stat.data.StringData;
import com.io.yutian.elementoriginlib.item.stat.list.IdStat;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@Command("test")
public class CommandTest {
@SubCommand("list")
public void list(CommandContext context, @CommandArgument int page) {
public void list(CommandContext context, @Parameter(required = false, defaultValue = "1") int page) {
System.out.println(context.getSender());
System.out.println("list:"+page);
}
@SubCommand("item.load")
public void item_load(@CommandArgument(suggestionType = "world_list") String id, int page, double k, CommandContext context) {
System.out.println(context.getSender());
System.out.println("load:"+id);
System.out.println(page);
System.out.println(k);
@SubCommand("item.get")
public void item_get(CommandContext context) {
Player player = (Player) context.getSender();
OriginItem originItem = new OriginItem(new ItemStack(Material.IRON_AXE));
originItem.setStatData(ItemStats.getItemStat(IdStat.class), new StringData("test"));
player.getInventory().addItem(originItem.getItemStack());
}
@SubCommand("item.save")
public void item_save(@CommandArgument String id) {
System.out.println("save:"+id);
@SubCommand("item.load")
public void item_load(CommandContext context) {
Player player = (Player) context.getSender();
ItemStack itemStack = player.getInventory().getItemInMainHand();
if (itemStack == null || itemStack.getType() == Material.AIR) {
return;
}
OriginItem originItem = new OriginItem(itemStack);
System.out.println(originItem.getStats());
}
@SubCommand("item.test")
public void item_test(@CommandArgument String id) {
public void item_test(@Parameter String id) {
System.out.println(id);
}
}

View File

@ -0,0 +1,21 @@
package com.io.yutian.elementoriginlib.exception.command;
public class CommandParseException extends RuntimeException {
public CommandParseException() {
super();
}
public CommandParseException(String s) {
super(s);
}
public CommandParseException(String message, Throwable cause) {
super(message, cause);
}
public CommandParseException(Throwable cause) {
super(cause);
}
}

View File

@ -1,21 +0,0 @@
package com.io.yutian.elementoriginlib.exception.command;
public class CommandRegisterException extends RuntimeException {
public CommandRegisterException() {
super();
}
public CommandRegisterException(String s) {
super(s);
}
public CommandRegisterException(String message, Throwable cause) {
super(message, cause);
}
public CommandRegisterException(Throwable cause) {
super(cause);
}
}

View File

@ -1,7 +1,7 @@
package com.io.yutian.elementoriginlib.gui.button;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.nbt.TagString;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import com.io.yutian.elementoriginlib.tag.TagString;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -20,11 +20,11 @@ public class ItemButton extends Button {
public ItemButton(ItemStack itemStack) {
super(itemStack);
NBTItem nbtItem = new NBTItem(itemStack);
nbtItem.editTag((nbtCompound -> {
ItemProxy itemProxy = new ItemProxy(itemStack);
itemProxy.editTag((nbtCompound -> {
nbtCompound.putString("gui_meta", "item_button");
}));
setItemStack(nbtItem.getItemStack());
setItemStack(itemProxy.getItemStack());
}
public ItemButton setItem(ItemStack item) {
@ -51,8 +51,8 @@ public class ItemButton extends Button {
}
public boolean isItem(ItemStack item) {
NBTItem nbtItem = new NBTItem(item);
return nbtItem.has("gui_meta", TagString.TYPE_ID) && ((TagString) nbtItem.get("gui_meta")).getString().equals("item_button");
ItemProxy itemProxy = new ItemProxy(item);
return itemProxy.has("gui_meta", TagString.TYPE_ID) && ((TagString) itemProxy.get("gui_meta")).getString().equals("item_button");
}
public ItemButton clickItem(BiConsumer<Player, ItemStack> consumer) {

View File

@ -1,33 +1,37 @@
package com.io.yutian.elementoriginlib.item;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.ItemStats;
import com.io.yutian.elementoriginlib.item.stat.StatData;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class OriginItem {
private ItemStack itemStack;
private NBTItem nbtItem;
private ItemProxy itemProxy;
private Map<ItemStat, StatData> stats = new HashMap<>();
private OriginItemStackBuilder builder;
public OriginItem(ItemStack itemStack) {
this.itemStack = itemStack;
this.nbtItem = new NBTItem(itemStack);
this.itemProxy = new ItemProxy(itemStack);
this.builder = new OriginItemStackBuilder(this);
}
public void setStatData(@NotNull ItemStat stat, @NotNull StatData data) {
this.stats.put(stat, data);
this.itemStack = builder().build();
}
public void removeStatData(@NotNull ItemStat stat) {
this.stats.remove(stat);
this.itemStack = builder().build();
}
public <S extends StatData> S getStatData(@NotNull ItemStat<S> stat) {
@ -50,20 +54,34 @@ public class OriginItem {
@NotNull
public OriginItemStackBuilder builder() {
return new OriginItemStackBuilder(this);
return this.builder;
}
@NotNull
public Set<ItemStat> getStats() {
return this.stats.keySet();
public Set<ItemStat> getStatTypes() {
return Collections.unmodifiableSet(this.stats.keySet());
}
@NotNull
public Map<ItemStat, StatData> getStats() {
for (ItemStat stat : ItemStats.getItemStats()) {
if (!hasStatData(stat)) {
try {
stat.load(this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return Collections.unmodifiableMap(stats);
}
public ItemStack getItemStack() {
return itemStack;
}
public NBTItem getNBTItem() {
return nbtItem;
public ItemProxy getItemProxy() {
return itemProxy;
}
@Override

View File

@ -1,7 +1,7 @@
package com.io.yutian.elementoriginlib.item;
import com.io.yutian.elementoriginlib.item.stat.StatData;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
@ -16,18 +16,18 @@ public class OriginItemStackBuilder {
protected ItemStack itemStack;
protected ItemMeta itemMeta;
protected NBTItem nbtItem;
protected ItemProxy itemProxy;
public OriginItemStackBuilder(OriginItem originItem) {
this.originItem = originItem;
this.itemStack = originItem.getItemStack();
this.itemMeta = itemStack.getItemMeta();
this.nbtItem = originItem.getNBTItem();
this.itemProxy = originItem.getItemProxy();
}
@NotNull
public ItemStack build() {
buildNBT();
buildCompounds();
return itemStack;
}
@ -36,29 +36,20 @@ public class OriginItemStackBuilder {
return itemMeta;
}
private void buildNBT() {
originItem.getStats().forEach(stat -> {
StatData statData = originItem.getStatData(stat);
stat.whenApplied(this, statData);
});
itemStack = nbtItem.getItemStack();
private void buildCompounds() {
originItem.getStats().forEach((stat, statData) -> stat.whenApplied(this, statData));
itemStack = itemProxy.getItemStack();
itemMeta = itemStack.getItemMeta();
originItem.getStats().forEach(stat -> {
StatData statData = originItem.getStatData(stat);
stat.applyMeta(this, statData);
});
originItem.getStats().forEach((stat, statData) -> stat.applyMeta(this, statData));
itemStack.setItemMeta(itemMeta);
List<String> lores = itemMeta.hasLore() ? itemMeta.getLore() : new ArrayList<>();
originItem.getStats().forEach(stat -> {
StatData statData = originItem.getStatData(stat);
stat.whenApplyLore(this, statData, lores);
});
originItem.getStats().forEach((stat, statData) -> stat.whenApplyLore(this, statData, lores));
itemStack.setLore(lores);
}
@NotNull
public NBTItem getNBTItem() {
return nbtItem;
public ItemProxy getNBTItem() {
return itemProxy;
}
public OriginItem getOriginItem() {

View File

@ -2,7 +2,7 @@ package com.io.yutian.elementoriginlib.item.stat;
import com.io.yutian.elementoriginlib.item.OriginItem;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -29,10 +29,10 @@ public abstract class ItemStat<S extends StatData> {
}
@Nullable
public abstract S getLoadedNBT(@NotNull NBTItem nbtItem);
public abstract S getLoadedNBT(@NotNull ItemProxy itemProxy);
public void load(@NotNull OriginItem originItem) {
S loadedNBT = getLoadedNBT(originItem.getNBTItem());
S loadedNBT = getLoadedNBT(originItem.getItemProxy());
if (loadedNBT != null) {
originItem.setStatData(this, loadedNBT);
}

View File

@ -1,25 +1,30 @@
package com.io.yutian.elementoriginlib.item.stat;
import com.io.yutian.elementoriginlib.item.stat.list.ItemStatId;
import com.io.yutian.elementoriginlib.item.stat.list.IdStat;
import com.io.yutian.elementoriginlib.logger.Logger;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ItemStats {
private static final Logger LOGGER = Logger.getLogger(ItemStats.class);
private static Map<Class<? extends ItemStat>, ItemStat<?>> itemStats = new HashMap<>();
static {
register(ItemStatId.class, new ItemStatId());
register(IdStat.class, new IdStat());
}
public static <I extends ItemStat> I register(Class<I> clazz, I itemStat) {
public static <I extends ItemStat> void register(Class<I> clazz, I itemStat) {
if (itemStats.containsKey(itemStat.getId())) {
return itemStat;
LOGGER.warn("ItemStat "+clazz.getName()+" 已经注册过了");
return;
}
itemStats.put(clazz, itemStat);
return itemStat;
}
public static void unregister(ItemStat itemStat) {
@ -41,4 +46,8 @@ public class ItemStats {
return null;
}
public static Collection<ItemStat> getItemStats() {
return Collections.unmodifiableCollection(itemStats.values());
}
}

View File

@ -2,9 +2,9 @@ package com.io.yutian.elementoriginlib.item.stat.list;
import com.io.yutian.elementoriginlib.item.stat.type.StringStat;
public class ItemStatId extends StringStat {
public class IdStat extends StringStat {
public ItemStatId() {
public IdStat() {
super("id", "Id");
}

View File

@ -3,8 +3,8 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.BooleanData;
import com.io.yutian.elementoriginlib.nbt.TagByte;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.TagByte;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,14 +16,14 @@ public abstract class BooleanStat extends ItemStat<BooleanData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull BooleanData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
nbtItem.editTag(nbtCompound -> nbtCompound.putByte(getNBTPath(), (byte) (statData.getValue() ? 1 : 0)));
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
itemProxy.editTag(nbtCompound -> nbtCompound.putByte(getNBTPath(), (byte) (statData.getValue() ? 1 : 0)));
}
@Nullable
@Override
public BooleanData getLoadedNBT(@NotNull NBTItem nbtItem) {
return new BooleanData(((TagByte) nbtItem.get(getNBTPath())).getByte() == 1);
public BooleanData getLoadedNBT(@NotNull ItemProxy itemProxy) {
return new BooleanData(((TagByte) itemProxy.get(getNBTPath())).getByte() == 1);
}
}

View File

@ -3,8 +3,8 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.DoubleData;
import com.io.yutian.elementoriginlib.nbt.TagDouble;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.TagDouble;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,14 +16,14 @@ public abstract class DoubleStat extends ItemStat<DoubleData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull DoubleData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
nbtItem.editTag(nbtCompound -> nbtCompound.putDouble(getNBTPath(), statData.getValue()));
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
itemProxy.editTag(nbtCompound -> nbtCompound.putDouble(getNBTPath(), statData.getValue()));
}
@Nullable
@Override
public DoubleData getLoadedNBT(@NotNull NBTItem nbtItem) {
return new DoubleData(((TagDouble) nbtItem.get(getNBTPath())).getDouble());
public DoubleData getLoadedNBT(@NotNull ItemProxy itemProxy) {
return new DoubleData(((TagDouble) itemProxy.get(getNBTPath())).getDouble());
}
}

View File

@ -3,8 +3,8 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.IntData;
import com.io.yutian.elementoriginlib.nbt.TagInt;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.TagInt;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,14 +16,14 @@ public abstract class IntStat extends ItemStat<IntData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull IntData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
nbtItem.editTag(nbtCompound -> nbtCompound.putInt(getNBTPath(), statData.getInt()));
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
itemProxy.editTag(nbtCompound -> nbtCompound.putInt(getNBTPath(), statData.getInt()));
}
@Nullable
@Override
public IntData getLoadedNBT(@NotNull NBTItem nbtItem) {
return new IntData(((TagInt)nbtItem.get(getNBTPath())).getInt());
public IntData getLoadedNBT(@NotNull ItemProxy itemProxy) {
return new IntData(((TagInt) itemProxy.get(getNBTPath())).getInt());
}
}

View File

@ -3,9 +3,9 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.StringListData;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.nbt.TagList;
import com.io.yutian.elementoriginlib.nbt.TagString;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import com.io.yutian.elementoriginlib.tag.TagList;
import com.io.yutian.elementoriginlib.tag.TagString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -20,19 +20,19 @@ public abstract class StringListStat extends ItemStat<StringListData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull StringListData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
TagList<TagString> tagList = new TagList<>();
statData.getList().forEach(s -> tagList.add(new TagString(s)));
nbtItem.editTag(nbtCompound -> nbtCompound.put(getNBTPath(), tagList));
itemProxy.editTag(nbtCompound -> nbtCompound.put(getNBTPath(), tagList));
}
@Nullable
@Override
public StringListData getLoadedNBT(@NotNull NBTItem nbtItem) {
if (!nbtItem.has(getNBTPath(), TagList.TYPE_ID)) {
public StringListData getLoadedNBT(@NotNull ItemProxy itemProxy) {
if (!itemProxy.has(getNBTPath(), TagList.TYPE_ID)) {
return new StringListData(new ArrayList<>());
}
TagList<TagString> tagList = (TagList<TagString>) nbtItem.get(getNBTPath());
TagList<TagString> tagList = (TagList<TagString>) itemProxy.get(getNBTPath());
List<String> list = new ArrayList<>();
for (int i = 0; i < tagList.size(); i++) {
list.add(tagList.get(i).getString());

View File

@ -3,8 +3,8 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.StringData;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.nbt.TagString;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import com.io.yutian.elementoriginlib.tag.TagString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,15 +16,15 @@ public abstract class StringStat extends ItemStat<StringData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull StringData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
nbtItem.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getString()));
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
itemProxy.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getString()));
}
@Nullable
@Override
public StringData getLoadedNBT(@NotNull NBTItem nbtItem) {
return new StringData(((TagString) nbtItem.get(getNBTPath())).getString());
public StringData getLoadedNBT(@NotNull ItemProxy itemProxy) {
return new StringData(((TagString) itemProxy.get(getNBTPath())).getString());
}
}

View File

@ -3,8 +3,8 @@ package com.io.yutian.elementoriginlib.item.stat.type;
import com.io.yutian.elementoriginlib.item.OriginItemStackBuilder;
import com.io.yutian.elementoriginlib.item.stat.ItemStat;
import com.io.yutian.elementoriginlib.item.stat.data.UUIDData;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.nbt.TagString;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import com.io.yutian.elementoriginlib.tag.TagString;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -18,14 +18,14 @@ public abstract class UUIDStat extends ItemStat<UUIDData> {
@Override
public void whenApplied(@NotNull OriginItemStackBuilder itemStackBuilder, @NotNull UUIDData statData) {
NBTItem nbtItem = itemStackBuilder.getNBTItem();
nbtItem.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getUUID().toString()));
ItemProxy itemProxy = itemStackBuilder.getNBTItem();
itemProxy.editTag(nbtCompound -> nbtCompound.putString(getNBTPath(), statData.getUUID().toString()));
}
@Nullable
@Override
public UUIDData getLoadedNBT(@NotNull NBTItem nbtItem) {
return new UUIDData(UUID.fromString(((TagString) nbtItem.get(getNBTPath())).getString()));
public UUIDData getLoadedNBT(@NotNull ItemProxy itemProxy) {
return new UUIDData(UUID.fromString(((TagString) itemProxy.get(getNBTPath())).getString()));
}
}

View File

@ -1,6 +1,7 @@
package com.io.yutian.elementoriginlib.manager;
import com.io.yutian.elementoriginlib.command.SimpleCommandManager;
import com.io.yutian.elementoriginlib.command.list.CommandHelp;
import com.io.yutian.elementoriginlib.command.list.CommandTest;
import org.bukkit.plugin.Plugin;
@ -8,8 +9,8 @@ public class CommandManager extends SimpleCommandManager {
public CommandManager(Plugin plugin) {
super(plugin, "elementoriginlib");
// register(new CommandHelp(this, "help"));
register(CommandTest.class);
register(new CommandHelp(this, "help"));
register(new CommandTest());
}
}

View File

@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.io.yutian.elementoriginlib.nbt.TagHelper;
import com.io.yutian.elementoriginlib.tag.TagHelper;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;

View File

@ -3,7 +3,7 @@ package com.io.yutian.elementoriginlib.serialize.serializers;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.io.yutian.elementoriginlib.nbt.TagHelper;
import com.io.yutian.elementoriginlib.tag.TagHelper;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import net.minecraft.nbt.*;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import com.io.yutian.elementoriginlib.serialize.serializers.ItemStackDeserializer;
import net.minecraft.core.component.DataComponents;
@ -11,23 +11,23 @@ import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.function.Consumer;
public class NBTItem {
public class ItemProxy {
private ItemStack originalItemStack;
private net.minecraft.world.item.ItemStack nmsItemStack;
protected ItemStack resultItemStack;
public NBTItem(ItemStack itemStack) {
public ItemProxy(ItemStack itemStack) {
this.originalItemStack = itemStack;
this.nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
}
@NotNull
public TagCompound getTag() {
CompoundTag tag = nmsItemStack.getComponents().get(DataComponents.CUSTOM_DATA).getUnsafe();
if (tag == null) {
tag = new CompoundTag();
CompoundTag tag = new CompoundTag();
if (nmsItemStack.getComponents().has(DataComponents.CUSTOM_DATA)) {
tag = nmsItemStack.getComponents().get(DataComponents.CUSTOM_DATA).getUnsafe();
}
return (TagCompound) ITag.as(tag);
}
@ -71,13 +71,13 @@ public class NBTItem {
return nmsItemStack;
}
public static NBTItem build(ItemStack itemStack) {
return new NBTItem(itemStack);
public static ItemProxy build(ItemStack itemStack) {
return new ItemProxy(itemStack);
}
public static NBTItem build(TagCompound tagCompound) {
public static ItemProxy build(TagCompound tagCompound) {
net.minecraft.world.item.ItemStack nmsItemStack = net.minecraft.world.item.ItemStack.parse(ItemStackDeserializer.getRegistry(), ITag.asNMS(tagCompound)).get();
return new NBTItem(CraftItemStack.asBukkitCopy(nmsItemStack));
return new ItemProxy(CraftItemStack.asBukkitCopy(nmsItemStack));
}
}

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagByte extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.*;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagDouble extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagFloat extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import net.minecraft.nbt.*;
import net.minecraft.nbt.Tag;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagInt extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagLong extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public abstract class TagNumber implements ITag {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
public class TagShort extends TagNumber {

View File

@ -1,4 +1,4 @@
package com.io.yutian.elementoriginlib.nbt;
package com.io.yutian.elementoriginlib.tag;
import java.util.Objects;

View File

@ -1,7 +1,7 @@
package com.io.yutian.elementoriginlib.util;
import com.io.yutian.elementoriginlib.nbt.TagCompound;
import com.io.yutian.elementoriginlib.nbt.NBTItem;
import com.io.yutian.elementoriginlib.tag.TagCompound;
import com.io.yutian.elementoriginlib.tag.ItemProxy;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
@ -45,8 +45,8 @@ public class ItemStackUtil {
if (!flag) {
return false;
}
NBTItem nbtOriginal = new NBTItem(original);
NBTItem nbtTester = new NBTItem(tester);
ItemProxy nbtOriginal = new ItemProxy(original);
ItemProxy nbtTester = new ItemProxy(tester);
TagCompound tagCompound0 = nbtOriginal.getTag();
TagCompound tagCompound1 = nbtTester.getTag();
return tagCompound0.equals(tagCompound1);