diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/Chat.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/Chat.java index db10974..4852985 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/Chat.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/Chat.java @@ -5,6 +5,8 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentBuilder; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.TranslationArgument; +import net.kyori.adventure.text.TranslationArgumentLike; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; @@ -972,21 +974,38 @@ public abstract sealed class Chat extends ChatStatic implements HoverEventSource + /* package */ static ComponentLike filterObjToComponentLike(Object v) { + return switch (v) { + case BaseComponent[] baseComponents -> toAdventure(baseComponents); + case BaseComponent baseComponent -> toAdventure(baseComponent); + case ComponentLike componentLike -> componentLike; + case null, default -> Component.text(Objects.toString(v)); + }; + } /* package */ static ComponentLike[] filterObjToComponentLike(Object[] values) { if (values == null) return null; ComponentLike[] ret = new ComponentLike[values.length]; + for (int i = 0; i < values.length; i++) { + ret[i] = filterObjToComponentLike(values[i]); + } + return ret; + } + + + /* package */ static TranslationArgumentLike[] filterObjToTranslationArgumentLike(Object[] values) { + if (values == null) + return null; + TranslationArgumentLike[] ret = new TranslationArgumentLike[values.length]; for (int i = 0; i < values.length; i++) { Object v = values[i]; - if (v instanceof BaseComponent[]) - ret[i] = toAdventure((BaseComponent[]) v); - else if (v instanceof BaseComponent) - ret[i] = toAdventure((BaseComponent) v); - else if (v instanceof ComponentLike) - ret[i] = (ComponentLike) v; + if (v instanceof Number n) + ret[i] = TranslationArgument.numeric(n); + else if (v instanceof Boolean b) + ret[i] = TranslationArgument.bool(b); else - ret[i] = Component.text(Objects.toString(v)); + ret[i] = TranslationArgument.component(filterObjToComponentLike(values[i])); } return ret; } diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatColorGradient.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatColorGradient.java index 764730b..d0f204f 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatColorGradient.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatColorGradient.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; * A custom gradient with at least 2 colors in it. */ public class ChatColorGradient { + private record GradientColor( float location, TextColor color @@ -48,7 +49,7 @@ public class ChatColorGradient { if (colors.isEmpty()) throw new IllegalStateException("Must define at least one color in this ChatColorGradient instance."); if (colors.size() == 1) - return colors.get(0).color(); + return colors.getFirst().color(); int i = 0; for (; i < colors.size(); i++) { @@ -59,7 +60,7 @@ public class ChatColorGradient { if (i == 0) return colors.get(i).color(); if (i == colors.size()) - return colors.get(colors.size() - 1).color(); + return colors.getLast().color(); int p = i - 1; float pLoc = colors.get(p).location(); diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java index e5e0bb7..ce4606d 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatStatic.java @@ -264,7 +264,7 @@ public abstract class ChatStatic { * @return a new {@link FormatableChat} with the provided translation key and parameters. */ public static FormatableChat translation(String key, Object... with) { - return new FormatableChat(Component.translatable().key(key).args(Chat.filterObjToComponentLike(with))); + return new FormatableChat(Component.translatable().key(key).arguments(Chat.filterObjToTranslationArgumentLike(with))); } /** @@ -632,50 +632,32 @@ public abstract class ChatStatic { private static ComponentBuilder componentToBuilder(Component c) { - ComponentBuilder builder; - if (c instanceof TextComponent) { - builder = Component.text() - .content(((TextComponent) c).content()); - } - else if (c instanceof TranslatableComponent) { - builder = Component.translatable() - .key(((TranslatableComponent) c).key()) - .args(((TranslatableComponent) c).args()); - } - else if (c instanceof SelectorComponent) { - builder = Component.selector() - .pattern(((SelectorComponent) c).pattern()); - } - else if (c instanceof ScoreComponent) { - builder = Component.score() - .name(((ScoreComponent) c).name()) - .objective(((ScoreComponent) c).objective()); - } - else if (c instanceof KeybindComponent) { - builder = Component.keybind() - .keybind(((KeybindComponent) c).keybind()); - } - else if (c instanceof BlockNBTComponent) { - builder = Component.blockNBT() - .interpret(((BlockNBTComponent) c).interpret()) - .nbtPath(((BlockNBTComponent) c).nbtPath()) - .pos(((BlockNBTComponent) c).pos()); - } - else if (c instanceof EntityNBTComponent) { - builder = Component.entityNBT() - .interpret(((EntityNBTComponent) c).interpret()) - .nbtPath(((EntityNBTComponent) c).nbtPath()) - .selector(((EntityNBTComponent) c).selector()); - } - else if (c instanceof StorageNBTComponent) { - builder = Component.storageNBT() - .interpret(((StorageNBTComponent) c).interpret()) - .nbtPath(((StorageNBTComponent) c).nbtPath()) - .storage(((StorageNBTComponent) c).storage()); - } - else { - throw new IllegalArgumentException("Unknown component type " + c.getClass()); - } + ComponentBuilder builder = switch (c) { + case TextComponent textComponent -> Component.text() + .content(textComponent.content()); + case TranslatableComponent translatableComponent -> Component.translatable() + .key(translatableComponent.key()).arguments(translatableComponent.arguments()); + case SelectorComponent selectorComponent -> Component.selector() + .pattern(selectorComponent.pattern()); + case ScoreComponent scoreComponent -> Component.score() + .name(scoreComponent.name()) + .objective(scoreComponent.objective()); + case KeybindComponent keybindComponent -> Component.keybind() + .keybind(keybindComponent.keybind()); + case BlockNBTComponent blockNBTComponent -> Component.blockNBT() + .interpret(blockNBTComponent.interpret()) + .nbtPath(blockNBTComponent.nbtPath()) + .pos(blockNBTComponent.pos()); + case EntityNBTComponent entityNBTComponent -> Component.entityNBT() + .interpret(entityNBTComponent.interpret()) + .nbtPath(entityNBTComponent.nbtPath()) + .selector(entityNBTComponent.selector()); + case StorageNBTComponent storageNBTComponent -> Component.storageNBT() + .interpret(storageNBTComponent.interpret()) + .nbtPath(storageNBTComponent.nbtPath()) + .storage(storageNBTComponent.storage()); + case null, default -> throw new IllegalArgumentException("Unknown component type " + (c == null ? "null" : c.getClass())); + }; return builder.style(c.style()).append(c.children()); } diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java index f104695..cf90146 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java @@ -14,6 +14,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.TranslationArgument; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; @@ -265,8 +266,8 @@ public class ChatUtil { count += strWidth(((TextComponent)component).content(), console, actuallyBold); } else if (component instanceof TranslatableComponent) { - for (Component c : ((TranslatableComponent)component).args()) - count += componentWidth(c, console, actuallyBold); + for (TranslationArgument c : ((TranslatableComponent)component).arguments()) + count += componentWidth(c.asComponent(), console, actuallyBold); } for (Component c : component.children()) @@ -480,7 +481,7 @@ public class ChatUtil { spacedRow.thenText(space); } if (!row.isEmpty()) - spacedRow.then(row.get(row.size() - 1)); + spacedRow.then(row.getLast()); spacedRows.add(spacedRow.getAdv()); } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java index 709ff27..a0d0fca 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/commands/PaperBrigadierCommand.java @@ -2,38 +2,26 @@ package fr.pandacube.lib.paper.commands; import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; -import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.RootCommandNode; import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.commands.BadCommandUsage; import fr.pandacube.lib.commands.BrigadierCommand; import fr.pandacube.lib.commands.SuggestionsSupplier; -import fr.pandacube.lib.paper.permissions.PandalibPaperPermissions; import fr.pandacube.lib.paper.reflect.PandalibPaperReflect; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; -import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftVector; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.VanillaCommandWrapper; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.BlockPosArgument; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Commands; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.ComponentArgument; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Coordinates; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.EntityArgument; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.EntitySelector; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Vec3Argument; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos; -import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerPlayer; -import fr.pandacube.lib.paper.reflect.wrapper.paper.PaperAdventure; import fr.pandacube.lib.players.standalone.AbstractOffPlayer; import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer; import fr.pandacube.lib.players.standalone.AbstractPlayerManager; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.util.log.Log; -import net.kyori.adventure.text.Component; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.Command; @@ -41,18 +29,10 @@ import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; -import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerCommandSendEvent; -import org.bukkit.event.server.ServerLoadEvent; import org.bukkit.plugin.Plugin; -import org.bukkit.util.BlockVector; -import org.bukkit.util.Vector; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -64,7 +44,9 @@ import static fr.pandacube.lib.util.ThrowableUtil.wrapEx; /** * Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher. */ -public abstract class PaperBrigadierCommand extends BrigadierCommand implements Listener { +@SuppressWarnings("UnstableApiUsage") +public abstract class PaperBrigadierCommand extends BrigadierCommand implements Listener { + private static final Commands vanillaCommandDispatcher; private static final CommandDispatcher nmsDispatcher; @@ -91,6 +73,7 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand commandNode; + protected final LiteralCommandNode commandNode; + /** + * The command requested aliases. + */ + protected final String[] aliases; + + protected final String description; private final RegistrationPolicy registrationPolicy; @@ -147,12 +136,14 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand { - String[] aliases = getAliases(); - if (aliases == null) - aliases = new String[0]; + registeredAliases = new HashSet<>(event.registrar().register(commandNode, description, List.of(aliases))); - String pluginName = plugin.getName().toLowerCase(); + if (registrationPolicy == RegistrationPolicy.ALL) { + // enforce registration of aliases + for (String alias : aliases) { + if (!registeredAliases.contains(alias)) + registeredAliases.addAll(event.registrar().register(getAliasNode(alias), description)); + } + } - registeredAliases = new HashSet<>(); - registerNode(commandNode, false); - registerAlias(pluginName + ":" + commandNode.getLiteral(), true); - - for (String alias : aliases) { - registerAlias(alias, false); - registerAlias(pluginName + ":" + alias, true); - } + }); } - private void registerAlias(String alias, boolean prefixed) { - LiteralCommandNode node = literal(alias) + private LiteralCommandNode getAliasNode(String alias) { + return literal(alias) .requires(commandNode.getRequirement()) .executes(commandNode.getCommand()) .redirect(commandNode) .build(); - registerNode(node, prefixed); - } - - - private void registerNode(LiteralCommandNode node, boolean prefixed) { - RootCommandNode root = getRootNode(); - String name = node.getLiteral(); - boolean isAlias = node.getRedirect() == commandNode; - boolean forceRegistration = switch (registrationPolicy) { - case NONE -> false; - case ONLY_BASE_COMMAND -> prefixed || !isAlias; - case ALL -> true; - }; - - // nmsDispatcher integration and conflit resolution - boolean nmsRegister = false, nmsRegistered = false; - CommandNode nmsConflicted = root.getChild(name); - if (nmsConflicted != null) { - - if (isFromThisCommand(nmsConflicted)) { - // this command is already registered in NMS. Don’t need to register again - nmsRegistered = true; - } - else if (forceRegistration) { - nmsRegister = true; - Log.info("Overwriting Brigadier command /" + name); - } - else if (prefixed || !isAlias) { - Log.severe("/" + name + " already in NMS Brigadier instance." - + " Wont replace it because registration is not forced for prefixed or initial name of a command."); - } - else { // conflict, won't replace, not forced but only an alias anyway - Log.info("/" + name + " already in NMS Brigadier instance." - + " Wont replace it because registration is not forced for a non-prefixed alias."); - } - } - else { - nmsRegister = true; - } - - if (nmsRegister) { - @SuppressWarnings("unchecked") - var rCommandNode = ReflectWrapper.wrapTyped(root, fr.pandacube.lib.paper.reflect.wrapper.brigadier.CommandNode.class); - rCommandNode.removeCommand(name); - root.addChild(node); - nmsRegistered = true; - } - - if (!nmsRegistered) { - return; - } - - registeredAliases.add(name); - - // bukkit dispatcher conflict resolution - boolean bukkitRegister = false; - CommandMap bukkitCmdMap = Bukkit.getCommandMap(); - Command bukkitConflicted = bukkitCmdMap.getCommand(name); - if (bukkitConflicted != null) { - if (!isFromThisCommand(bukkitConflicted)) { - if (forceRegistration) { - bukkitRegister = true; - Log.info("Overwriting Bukkit command /" + name - + " (" + getCommandIdentity(bukkitConflicted) + ")"); - } - else if (prefixed || !isAlias) { - Log.severe("/" + name + " already in Bukkit dispatcher (" + getCommandIdentity(bukkitConflicted) + ")." + - " Wont replace it because registration is not forced for prefixed or initial name of a command."); - } - else { - Log.info("/" + name + " already in Bukkit dispatcher (" + getCommandIdentity(bukkitConflicted) + ")." + - " Wont replace it because registration is not forced for a non-prefixed alias."); - } - } - } - else { - bukkitRegister = true; - } - - if (bukkitRegister) { - bukkitCmdMap.getKnownCommands().remove(name.toLowerCase()); - if (bukkitConflicted != null) - bukkitConflicted.unregister(bukkitCmdMap); - - Command newCommand = new VanillaCommandWrapper(vanillaCommandDispatcher, node).__getRuntimeInstance(); - bukkitCmdMap.getKnownCommands().put(name.toLowerCase(), newCommand); - newCommand.register(bukkitCmdMap); - } - - } - - private boolean isFromThisCommand(CommandNode node) { - return node == commandNode || node.getRedirect() == commandNode; - } - - private boolean isFromThisCommand(Command bukkitCmd) { - if (VanillaCommandWrapper.REFLECT.get().isInstance(bukkitCmd)) { - return isFromThisCommand(ReflectWrapper.wrapTyped((BukkitCommand) bukkitCmd, VanillaCommandWrapper.class).vanillaCommand()); - } - return false; } private static String getCommandIdentity(Command bukkitCmd) { @@ -305,25 +194,6 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand "minecraft:" + s).toList()); - } - - - /** - * Server load event handler. - * @param event the event. - */ - @EventHandler - public void onServerLoad(ServerLoadEvent event) { - register(); - } - @@ -342,6 +212,15 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand hasPermission(String permission) { + public Predicate hasPermission(String permission) { return wrapper -> getCommandSender(wrapper).hasPermission(permission); } @@ -395,7 +275,7 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand context) { + public static CommandSender getCommandSender(CommandContext context) { return getCommandSender(context.getSource()); } @@ -404,8 +284,8 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand wrapSuggestions(SuggestionsSupplier suggestions) { + public SuggestionProvider wrapSuggestions(SuggestionsSupplier suggestions) { return wrapSuggestions(suggestions, PaperBrigadierCommand::getCommandSender); } @@ -456,7 +336,7 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand wrapCommand(com.mojang.brigadier.Command cmd) { + protected static com.mojang.brigadier.Command wrapCommand(com.mojang.brigadier.Command cmd) { return context -> { try { return cmd.run(context); @@ -485,181 +365,13 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand argumentMinecraftEntity(boolean singleTarget, boolean playersOnly) { - if (playersOnly) { - return singleTarget ? EntityArgument.player() : EntityArgument.players(); - } - else { - return singleTarget ? EntityArgument.entity() : EntityArgument.entities(); - } - } - /** - * Gets the value of the provided argument of type {@code minecraft:entity} (list of entities), from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @return the value of the argument, or null if not found. - */ - public List tryGetMinecraftEntityArgument(CommandContext context, String argument) { - EntitySelector es = ReflectWrapper.wrap(tryGetArgument(context, argument, EntitySelector.MAPPING.runtimeClass()), EntitySelector.class); - if (es == null) - return null; - List nmsEntityList = es.findEntities(context.getSource()); - List entityList = new ArrayList<>(nmsEntityList.size()); - for (fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity nmsEntity : nmsEntityList) { - entityList.add(nmsEntity.getBukkitEntity()); - } - return entityList; - } - - /** - * Gets the value of the provided argument of type {@code minecraft:entity} (list of players), from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @return the value of the argument, or null if not found. - */ - public List tryGetMinecraftEntityArgumentPlayers(CommandContext context, String argument) { - EntitySelector es = ReflectWrapper.wrap(tryGetArgument(context, argument, EntitySelector.MAPPING.runtimeClass()), EntitySelector.class); - if (es == null) - return null; - List nmsPlayerList = es.findPlayers(context.getSource()); - List playerList = new ArrayList<>(nmsPlayerList.size()); - for (ServerPlayer nmsPlayer : nmsPlayerList) { - playerList.add(nmsPlayer.getBukkitEntity()); - } - return playerList; - } - - /** - * Gets the value of the provided argument of type {@code minecraft:entity} (one entity), from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @return the value of the argument, or null if not found. - */ - public Entity tryGetMinecraftEntityArgumentOneEntity(CommandContext context, String argument) { - EntitySelector es = ReflectWrapper.wrap(tryGetArgument(context, argument, EntitySelector.MAPPING.runtimeClass()), EntitySelector.class); - if (es == null) - return null; - fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.Entity nmsEntity = es.findSingleEntity(context.getSource()); - return nmsEntity == null ? null : nmsEntity.getBukkitEntity(); - } - - /** - * Gets the value of the provided argument of type {@code minecraft:entity} (one player), from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @return the value of the argument, or null if not found. - */ - public Player tryGetMinecraftEntityArgumentOnePlayer(CommandContext context, String argument) { - EntitySelector es = ReflectWrapper.wrap(tryGetArgument(context, argument, EntitySelector.MAPPING.runtimeClass()), EntitySelector.class); - if (es == null) - return null; - ServerPlayer nmsPlayer = es.findSinglePlayer(context.getSource()); - return nmsPlayer == null ? null : nmsPlayer.getBukkitEntity(); - } - - - - - - - /** - * Creates a new instance of the Brigadier argument type {@code minecraft:block_pos}. - * @return the {@code minecraft:block_pos} argument type. - */ - public static ArgumentType argumentMinecraftBlockPosition() { - return BlockPosArgument.blockPos(); - } - - /** - * Gets the value of the provided argument of type {@code minecraft:block_pos}, from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @param deflt a default value if the argument is not found. - * @return the value of the argument. - */ - public BlockVector tryGetMinecraftBlockPositionArgument(CommandContext context, - String argument, BlockVector deflt) { - return tryGetArgument(context, argument, Coordinates.MAPPING.runtimeClass(), nmsCoordinate -> { - BlockPos bp = ReflectWrapper.wrap(nmsCoordinate, Coordinates.class).getBlockPos(context.getSource()); - return new BlockVector(bp.getX(), bp.getY(), bp.getZ()); - }, deflt); - } - - - - - /** - * Creates a new instance of the Brigadier argument type {@code minecraft:vec3}. - * @return the {@code minecraft:vec3} argument type. - */ - public static ArgumentType argumentMinecraftVec3() { - return Vec3Argument.vec3(true); - } - - /** - * Gets the value of the provided argument of type {@code minecraft:vec3}, from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @param deflt a default value if the argument is not found. - * @return the value of the argument. - */ - public Vector tryGetMinecraftVec3Argument(CommandContext context, String argument, - Vector deflt) { - return tryGetArgument(context, argument, Coordinates.MAPPING.runtimeClass(), - nmsCoordinate -> CraftVector.toBukkit( - ReflectWrapper.wrap(nmsCoordinate, Coordinates.class).getPosition(context.getSource()) - ), - deflt); - } - - - - - /** - * Creates a new instance of the Brigadier argument type {@code minecraft:component}. - * @return the {@code minecraft:component} argument type. - */ - public static ArgumentType argumentMinecraftChatComponent() { - return ComponentArgument.textComponent(); - } - - /** - * Gets the value of the provided argument of type {@code minecraft:component}, from the provided context. - * @param context the command execution context. - * @param argument the argument name. - * @param deflt a default value if the argument is not found. - * @return the value of the argument. - */ - public Component tryGetMinecraftChatComponentArgument(CommandContext context, - String argument, Component deflt) { - return tryGetArgument(context, argument, - fr.pandacube.lib.paper.reflect.wrapper.minecraft.network.chat.Component.MAPPING.runtimeClass(), - nmsComp -> PaperAdventure.asAdventure( - ReflectWrapper.wrap(nmsComp, - fr.pandacube.lib.paper.reflect.wrapper.minecraft.network.chat.Component.class) - ), - deflt); - - } /** * All possible choices on how to force the registration of a command, based on certain conditions. */ public enum RegistrationPolicy { - /** - * Do not force to register a command node or an alias if there is already a command with that name in the - * vanilla Brigadier dispatcher. - * Note that all plugin-name-prefixed aliases will be registered anyway. - */ - NONE, /** * Force only the base command (but not the aliases) to be registered, even if a command with that name already * exists in the vanilla Brigadier dispatcher. diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java index e7f9fc3..11a10e6 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java @@ -1,6 +1,5 @@ package fr.pandacube.lib.paper.util; -import com.destroystokyo.paper.Namespaced; import com.google.common.collect.Streams; import fr.pandacube.lib.chat.Chat; import net.kyori.adventure.text.Component; @@ -13,10 +12,8 @@ import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Set; import java.util.function.Consumer; import static fr.pandacube.lib.chat.ChatStatic.chatComponent; @@ -183,7 +180,7 @@ public class ItemStackBuilder { public ItemStackBuilder fakeEnchant(boolean apply) { if (apply) { - enchant(Enchantment.DURABILITY, 1); + enchant(Enchantment.UNBREAKING, 1); return hideEnchants(); } return this; @@ -196,22 +193,6 @@ public class ItemStackBuilder { public ItemStackBuilder unbreakable(boolean unbreakable) { return meta(m -> m.setUnbreakable(unbreakable)); } - - public ItemStackBuilder canDestroy(Set destroyable) { - return canDestroy(destroyable.stream().map(m -> (Namespaced) m.getKey()).toList()); - } - - public ItemStackBuilder canPlaceOn(Set placeOn) { - return canPlaceOn(placeOn.stream().map(m -> (Namespaced) m.getKey()).toList()); - } - - public ItemStackBuilder canDestroy(Collection destroyable) { - return meta(m -> m.setDestroyableKeys(destroyable)); - } - - public ItemStackBuilder canPlaceOn(Collection placeOn) { - return meta(m -> m.setPlaceableKeys(placeOn)); - } public ItemStackBuilder damage(int d) { return meta(m -> m.setDamage(d), Damageable.class); diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java index bc35784..54596e2 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java @@ -18,6 +18,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.TreeMap; import java.util.function.IntUnaryOperator; @@ -98,7 +99,7 @@ public record PlayerDataWrapper(CompoundTag data) { Inventory inv = Bukkit.createInventory(null, bukkitType); if (stacks.isEmpty()) return inv; - for (Map.Entry is : stacks.entrySet()) { + for (Entry is : stacks.entrySet()) { inv.setItem(nbtToBukkitSlotConverter.applyAsInt(is.getKey()), is.getValue()); } return inv; @@ -142,7 +143,7 @@ public record PlayerDataWrapper(CompoundTag data) { private void setRawInventoryContent(String key, Map stacks) { ListTag list = new ListTag(); - for (Map.Entry is : stacks.entrySet()) { + for (Entry is : stacks.entrySet()) { ItemStack stack = filterStack(is.getValue()); if (stack == null) continue; @@ -312,6 +313,7 @@ public record PlayerDataWrapper(CompoundTag data) { case LEGS -> Objects.requireNonNullElseGet(this.getLeggings(), () -> new ItemStack(Material.AIR)); case CHEST -> Objects.requireNonNullElseGet(this.getChestplate(), () -> new ItemStack(Material.AIR)); case HEAD -> Objects.requireNonNullElseGet(this.getHelmet(), () -> new ItemStack(Material.AIR)); + case BODY -> new ItemStack(Material.AIR); // for horses/wolves armor }; } diff --git a/pom.xml b/pom.xml index b786663..45dd127 100644 --- a/pom.xml +++ b/pom.xml @@ -56,8 +56,8 @@ UTF-8 1.20-R0.3-SNAPSHOT - 1.20.4-R0.1 - 1.20.4 + 1.20.6-R0.1 + 1.20.6