diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7ad5a875b9..2f27dd07ee 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,35 +40,10 @@ jobs: - name: Build the CommandAPI (Bukkit+Velocity) using maven run: mvn clean install --batch-mode -P Platform.Bukkit,Platform.Velocity - - name: Check NMS_Common compiles against all compatible Minecraft versions - run: | - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_17_R1 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_R1 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_2_R2 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_R1 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_3_R2 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_20_R1 - mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_20_2_R2; - # Not Spigot_1_20_4_R3. This isn't compatible with NMS_CommonWithFunctions - # mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_20_4_R3; - - # - name: validate version-specific dependencies (nms-common) - # run: | - # nms_common_path="commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/target/classes/dev/jorel/commandapi/nms/NMS_Common.class" - # if ! command -v javap &> /dev/null - # then - # echo "javap could not be found, skipping NMS Common validation check" - # exit - # else - # javapcommand=$(javap -v $nms_common_path | grep -E '#[0-9]+ = Class ' | cut -c 46- | sort | grep 'org/bukkit/craftbukkit') - # if [[ -n "$javapcommand" ]]; then - # echo "NMS Common has version-specific dependencies!" - # echo $javapcommand - # exit 1 - # else - # echo "NMS Common has no version-specific dependencies :)" - # fi - # fi + ### Check NMS_Common compiles against all compatible Minecraft versions + + - name: Check NMS_Common compiles identically for all versions + run: ./compileNMSCommon.sh ### Bukkit tests across all NMS versions ### # Dev note: Yes, I know I could put this under one run section, but it's a million times easier diff --git a/README.md b/README.md index 1bee9080d8..02a8bcb3f1 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,7 @@ This is the current roadmap for the CommandAPI (as of 1st April 2025): Bug Fixes: diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R2.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R2.java index 0b5737ff94..d3dc99b46f 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R2.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R2.java @@ -20,63 +20,6 @@ *******************************************************************************/ package dev.jorel.commandapi.nms; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.lang.reflect.Field; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.function.Predicate; -import java.util.function.ToIntFunction; - -import dev.jorel.commandapi.*; -import dev.jorel.commandapi.wrappers.*; -import net.minecraft.advancements.critereon.MinMaxBounds; -import net.minecraft.commands.arguments.*; -import org.bukkit.Bukkit; -import org.bukkit.Color; -import org.bukkit.Location; -import org.bukkit.NamespacedKey; -import org.bukkit.Particle; -import org.bukkit.Particle.DustOptions; -import org.bukkit.Particle.DustTransition; -import org.bukkit.Vibration; -import org.bukkit.Vibration.Destination; -import org.bukkit.Vibration.Destination.BlockDestination; -import org.bukkit.World; -import org.bukkit.advancement.Advancement; -import org.bukkit.block.Biome; -import org.bukkit.block.Block; -import org.bukkit.block.data.BlockData; -import org.bukkit.command.CommandSender; -import org.bukkit.command.SimpleCommandMap; -import org.bukkit.craftbukkit.v1_20_R2.CraftLootTable; -import org.bukkit.craftbukkit.v1_20_R2.CraftParticle; -import org.bukkit.craftbukkit.v1_20_R2.CraftServer; -import org.bukkit.craftbukkit.v1_20_R2.CraftSound; -import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData; -import org.bukkit.craftbukkit.v1_20_R2.command.BukkitCommandWrapper; -import org.bukkit.craftbukkit.v1_20_R2.command.VanillaCommandWrapper; -import org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_20_R2.help.CustomHelpTopic; -import org.bukkit.craftbukkit.v1_20_R2.help.SimpleHelpMap; -import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack; -import org.bukkit.craftbukkit.v1_20_R2.potion.CraftPotionEffectType; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.help.HelpTopic; -import org.bukkit.inventory.Recipe; - import com.google.common.collect.ImmutableList; import com.google.common.io.Files; import com.google.gson.GsonBuilder; @@ -88,7 +31,11 @@ import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.logging.LogUtils; - +import dev.jorel.commandapi.CommandAPI; +import dev.jorel.commandapi.CommandAPIHandler; +import dev.jorel.commandapi.CommandRegistrationStrategy; +import dev.jorel.commandapi.SafeVarHandle; +import dev.jorel.commandapi.SpigotCommandRegistration; import dev.jorel.commandapi.arguments.ArgumentSubType; import dev.jorel.commandapi.arguments.SuggestionProviders; import dev.jorel.commandapi.commandsenders.AbstractCommandSender; @@ -97,19 +44,45 @@ import dev.jorel.commandapi.preprocessor.Differs; import dev.jorel.commandapi.preprocessor.NMSMeta; import dev.jorel.commandapi.preprocessor.RequireField; +import dev.jorel.commandapi.wrappers.ComplexRecipeImpl; +import dev.jorel.commandapi.wrappers.FloatRange; +import dev.jorel.commandapi.wrappers.FunctionWrapper; +import dev.jorel.commandapi.wrappers.IntegerRange; +import dev.jorel.commandapi.wrappers.Location2D; +import dev.jorel.commandapi.wrappers.NativeProxyCommandSender; +import dev.jorel.commandapi.wrappers.ParticleData; +import dev.jorel.commandapi.wrappers.Rotation; +import dev.jorel.commandapi.wrappers.ScoreboardSlot; +import dev.jorel.commandapi.wrappers.SimpleFunctionWrapper; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; import net.minecraft.advancements.AdvancementHolder; +import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandFunction; -import net.minecraft.commands.CommandFunction.Entry; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; +import net.minecraft.commands.arguments.ColorArgument; +import net.minecraft.commands.arguments.ComponentArgument; +import net.minecraft.commands.arguments.DimensionArgument; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.commands.arguments.MessageArgument; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.ParticleArgument; +import net.minecraft.commands.arguments.RangeArgument; +import net.minecraft.commands.arguments.ResourceArgument; +import net.minecraft.commands.arguments.ResourceLocationArgument; +import net.minecraft.commands.arguments.ScoreHolderArgument; +import net.minecraft.commands.arguments.ScoreboardSlotArgument; +import net.minecraft.commands.arguments.TeamArgument; import net.minecraft.commands.arguments.blocks.BlockPredicateArgument; import net.minecraft.commands.arguments.blocks.BlockStateArgument; import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.coordinates.ColumnPosArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import net.minecraft.commands.arguments.coordinates.Vec2Argument; import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.item.FunctionArgument; @@ -159,6 +132,61 @@ import net.minecraft.world.level.storage.loot.LootDataType; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.Particle; +import org.bukkit.Particle.DustOptions; +import org.bukkit.Particle.DustTransition; +import org.bukkit.Vibration; +import org.bukkit.Vibration.Destination; +import org.bukkit.Vibration.Destination.BlockDestination; +import org.bukkit.World; +import org.bukkit.advancement.Advancement; +import org.bukkit.block.Biome; +import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; +import org.bukkit.command.CommandSender; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.craftbukkit.v1_20_R2.CraftLootTable; +import org.bukkit.craftbukkit.v1_20_R2.CraftParticle; +import org.bukkit.craftbukkit.v1_20_R2.CraftServer; +import org.bukkit.craftbukkit.v1_20_R2.CraftSound; +import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData; +import org.bukkit.craftbukkit.v1_20_R2.command.BukkitCommandWrapper; +import org.bukkit.craftbukkit.v1_20_R2.command.VanillaCommandWrapper; +import org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_20_R2.help.CustomHelpTopic; +import org.bukkit.craftbukkit.v1_20_R2.help.SimpleHelpMap; +import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_20_R2.potion.CraftPotionEffectType; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.help.HelpTopic; +import org.bukkit.inventory.Recipe; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.function.Predicate; +import java.util.function.ToIntFunction; // Mojang-Mapped reflection /** @@ -169,7 +197,7 @@ @RequireField(in = EntitySelector.class, name = "usesSelector", ofType = boolean.class) @RequireField(in = ItemInput.class, name = "tag", ofType = CompoundTag.class) @RequireField(in = ServerFunctionLibrary.class, name = "dispatcher", ofType = CommandDispatcher.class) -public class NMS_1_20_R2 extends NMS_CommonWithFunctions { +public class NMS_1_20_R2 extends NMS_Common { private static final SafeVarHandle> helpMapTopics; private static final Field entitySelectorUsesSelector; @@ -196,18 +224,19 @@ public class NMS_1_20_R2 extends NMS_CommonWithFunctions { serverFunctionLibraryDispatcher = CommandAPIHandler.getField(ServerFunctionLibrary.class, "g", "dispatcher"); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentAdvancement() { + return ResourceLocationArgument.id(); } @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentChatComponent() { + return ComponentArgument.textComponent(); } @Override @@ -216,29 +245,8 @@ public final ArgumentType _ArgumentEnchantment() { } @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentRecipe() { + return ResourceLocationArgument.id(); } @Override @@ -267,10 +275,10 @@ public final String convert(ParticleData particle) { } // Converts NMS function to SimpleFunctionWrapper - private final SimpleFunctionWrapper convertFunction(CommandFunction commandFunction) { + private SimpleFunctionWrapper convertFunction(CommandFunction commandFunction) { ToIntFunction appliedObj = (CommandSourceStack css) -> this.getMinecraftServer().getFunctions().execute(commandFunction, css); - Entry[] cArr = commandFunction.getEntries(); + CommandFunction.Entry[] cArr = commandFunction.getEntries(); String[] result = new String[cArr.length]; for (int i = 0, size = cArr.length; i < size; i++) { result[i] = cArr[i].toString(); @@ -278,12 +286,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction commandFunct return new SimpleFunctionWrapper(fromResourceLocation(commandFunction.getId()), appliedObj, result); } - @Override - public final void createDispatcherFile(File file, CommandDispatcher dispatcher) throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { return new CustomHelpTopic(commandName, shortDescription, fullDescription, permission); @@ -294,6 +296,11 @@ public Advancement getAdvancement(CommandContext cmdCtx, Str return ResourceLocationArgument.getAdvancement(cmdCtx, key).toBukkit(); } + @Override + public Component getAdventureChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return GsonComponentSerializer.gson().deserialize(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + @Override public NamedTextColor getAdventureChatColor(CommandContext cmdCtx, String key) { final Integer color = ColorArgument.getColor(cmdCtx, key).getColor(); @@ -323,13 +330,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -340,6 +340,16 @@ public CommandSourceStack getBrigadierSourceFromCommandSender(AbstractCommandSen return VanillaCommandWrapper.getListener(sender.getSource()); } + @Override + public BaseComponent[] getChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key))); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { return DimensionArgument.getDimension(cmdCtx, key).getWorld(); @@ -406,12 +416,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType.getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -434,6 +438,28 @@ public final FunctionWrapper[] getFunction(CommandContext cm return result.toArray(new FunctionWrapper[0]); } + @Override + public SimpleFunctionWrapper getFunction(NamespacedKey key) { + final ResourceLocation resourceLocation = new ResourceLocation(key.getNamespace(), key.getKey()); + Optional commandFunctionOptional = this.getMinecraftServer().getFunctions().get(resourceLocation); + if(commandFunctionOptional.isPresent()) { + return convertFunction(commandFunctionOptional.get()); + } else { + throw new IllegalStateException("Failed to get defined function " + key + + "! This should never happen - please report this to the CommandAPI" + + "developers, we'd love to know how you got this error message!"); + } + } + + @Override + public Set getFunctions() { + Set result = new HashSet<>(); + for (ResourceLocation resourceLocation : this.getMinecraftServer().getFunctions().getFunctionNames()) { + result.add(fromResourceLocation(resourceLocation)); + } + return result; + } + @Override public IntegerRange getIntRange(CommandContext cmdCtx, String key) { MinMaxBounds.Ints range = RangeArgument.Ints.getRange(cmdCtx, key); @@ -472,12 +498,6 @@ public final Predicate getItemStackPredicate(Com return item -> predicate.test(CraftItemStack.asNMSCopy(item)); } - @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - @Override public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); @@ -485,21 +505,20 @@ public final Location2D getLocation2DPrecise(CommandContext } @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); + public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { + ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); + return new CraftLootTable(fromResourceLocation(resourceLocation), this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); } @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); + public NamespacedKey getMinecraftKey(CommandContext cmdCtx, String key) { + return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } @Override - public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { - ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); - return new CraftLootTable(fromResourceLocation(resourceLocation), this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); } @Differs(from = "1.20.1", by = "Uses CraftParticle#minecraftToBukkit instead of CraftParticle#toBukkit") @@ -586,11 +605,28 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); } + @Override + public Collection getScoreHolderMultiple(CommandContext cmdCtx, String key) + throws CommandSyntaxException { + return ScoreHolderArgument.getNames(cmdCtx, key); + } + + @Override + public String getScoreHolderSingle(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ScoreHolderArgument.getName(cmdCtx, key); + } + @Override public BukkitCommandSender getSenderForCommand(CommandContext cmdCtx, boolean isNative) { CommandSourceStack css = cmdCtx.getSource(); @@ -715,6 +751,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R3.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R3.java index d7222fb040..92f6fb07ff 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R3.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R3.java @@ -40,6 +40,13 @@ import java.util.function.ToIntFunction; import dev.jorel.commandapi.*; +import dev.jorel.commandapi.wrappers.Rotation; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; +import net.minecraft.commands.arguments.MessageArgument; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -184,6 +191,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -222,18 +231,19 @@ public class NMS_1_20_R3 extends NMS_Common { serverFunctionLibraryDispatcher = CommandAPIHandler.getField(ServerFunctionLibrary.class, "g", "dispatcher"); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentAdvancement() { + return ResourceLocationArgument.id(); } @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentChatComponent() { + return ComponentArgument.textComponent(); } @Override @@ -242,29 +252,8 @@ public final ArgumentType _ArgumentEnchantment() { } @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentRecipe() { + return ResourceLocationArgument.id(); } @Override @@ -346,13 +335,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -365,6 +347,11 @@ public Advancement getAdvancement(CommandContext cmdCtx, Str return ResourceLocationArgument.getAdvancement(cmdCtx, key).toBukkit(); } + @Override + public Component getAdventureChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return GsonComponentSerializer.gson().deserialize(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + @Override public NamedTextColor getAdventureChatColor(CommandContext cmdCtx, String key) { final Integer color = ColorArgument.getColor(cmdCtx, key).getColor(); @@ -397,14 +384,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -416,6 +395,16 @@ public CommandSourceStack getBrigadierSourceFromCommandSender( return VanillaCommandWrapper.getListener(sender.getSource()); } + @Override + public BaseComponent[] getChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key))); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -490,14 +479,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -586,38 +567,27 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); + public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { + ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); + return new CraftLootTable(fromResourceLocation(resourceLocation), + this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); } @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); + public NamespacedKey getMinecraftKey(CommandContext cmdCtx, String key) { + return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } @Override - public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { - ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); - return new CraftLootTable(fromResourceLocation(resourceLocation), - this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); } @Differs(from = "1.20.1", by = "Uses CraftParticle#minecraftToBukkit instead of CraftParticle#toBukkit") @@ -697,6 +667,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -848,6 +824,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java index c666e479d0..44b17dce9d 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java @@ -40,6 +40,10 @@ import java.util.function.ToIntFunction; import dev.jorel.commandapi.*; +import dev.jorel.commandapi.wrappers.Rotation; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -191,6 +195,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -240,18 +246,14 @@ public class NMS_1_20_R4 extends NMS_Common { vanillaCommandDispatcherFieldExists = fieldExists; } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); - } - @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentAdvancement() { + return ResourceLocationArgument.id(); } @Differs(from = "1.20.4", by = "Now needs a command build context") @@ -266,29 +268,8 @@ public final ArgumentType _ArgumentEnchantment() { } @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentRecipe() { + return ResourceLocationArgument.id(); } @Override @@ -388,13 +369,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -446,14 +420,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -471,6 +437,11 @@ public final BaseComponent[] getChat(CommandContext cmdCtx, return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key), COMMAND_BUILD_CONTEXT)); } + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key), COMMAND_BUILD_CONTEXT)); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -545,14 +516,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -629,33 +592,11 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } - @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); - } - - @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); - } - @Override public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { return CraftLootTable.minecraftToBukkit(ResourceLocationArgument.getId(cmdCtx, key)); @@ -666,6 +607,12 @@ public NamespacedKey getMinecraftKey(CommandContext cmdCtx, return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } + @Override + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); + } + @Differs(from = "1.20.4", by = "New particle option ColorParticleOption") @Override public final ParticleData getParticle(CommandContext cmdCtx, String key) { @@ -771,6 +718,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -917,6 +870,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R1.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R1.java index 1c7e4465ff..cd70bf71ff 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R1.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R1.java @@ -31,7 +31,11 @@ import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.logging.LogUtils; -import dev.jorel.commandapi.*; +import dev.jorel.commandapi.CommandAPI; +import dev.jorel.commandapi.CommandAPIHandler; +import dev.jorel.commandapi.CommandRegistrationStrategy; +import dev.jorel.commandapi.SafeVarHandle; +import dev.jorel.commandapi.SpigotCommandRegistration; import dev.jorel.commandapi.arguments.ArgumentSubType; import dev.jorel.commandapi.arguments.SuggestionProviders; import dev.jorel.commandapi.commandsenders.AbstractCommandSender; @@ -40,21 +44,44 @@ import dev.jorel.commandapi.preprocessor.Differs; import dev.jorel.commandapi.preprocessor.NMSMeta; import dev.jorel.commandapi.preprocessor.RequireField; -import dev.jorel.commandapi.wrappers.*; +import dev.jorel.commandapi.wrappers.ComplexRecipeImpl; +import dev.jorel.commandapi.wrappers.FloatRange; +import dev.jorel.commandapi.wrappers.FunctionWrapper; +import dev.jorel.commandapi.wrappers.IntegerRange; +import dev.jorel.commandapi.wrappers.Location2D; +import dev.jorel.commandapi.wrappers.NativeProxyCommandSender; +import dev.jorel.commandapi.wrappers.ParticleData; +import dev.jorel.commandapi.wrappers.Rotation; +import dev.jorel.commandapi.wrappers.ScoreboardSlot; +import dev.jorel.commandapi.wrappers.SimpleFunctionWrapper; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandFunction; -import net.minecraft.commands.CommandFunction.Entry; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.SharedSuggestionProvider; -import net.minecraft.commands.arguments.*; +import net.minecraft.commands.arguments.ColorArgument; +import net.minecraft.commands.arguments.ComponentArgument; +import net.minecraft.commands.arguments.DimensionArgument; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.commands.arguments.MessageArgument; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.ParticleArgument; +import net.minecraft.commands.arguments.RangeArgument; +import net.minecraft.commands.arguments.ResourceArgument; +import net.minecraft.commands.arguments.ResourceLocationArgument; +import net.minecraft.commands.arguments.ScoreHolderArgument; +import net.minecraft.commands.arguments.ScoreboardSlotArgument; +import net.minecraft.commands.arguments.TeamArgument; import net.minecraft.commands.arguments.blocks.BlockPredicateArgument; import net.minecraft.commands.arguments.blocks.BlockStateArgument; import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.coordinates.ColumnPosArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import net.minecraft.commands.arguments.coordinates.Vec2Argument; import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.item.FunctionArgument; @@ -65,7 +92,15 @@ import net.minecraft.commands.synchronization.ArgumentUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.RegistryAccess.Frozen; -import net.minecraft.core.particles.*; +import net.minecraft.core.particles.BlockParticleOption; +import net.minecraft.core.particles.DustColorTransitionOptions; +import net.minecraft.core.particles.DustParticleOptions; +import net.minecraft.core.particles.ItemParticleOption; +import net.minecraft.core.particles.ParticleOptions; +import net.minecraft.core.particles.SculkChargeParticleOptions; +import net.minecraft.core.particles.ShriekParticleOption; +import net.minecraft.core.particles.SimpleParticleType; +import net.minecraft.core.particles.VibrationParticleOption; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; @@ -95,12 +130,17 @@ import net.minecraft.world.level.storage.loot.LootDataType; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; -import org.bukkit.*; +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; import org.bukkit.Particle; import org.bukkit.Particle.DustOptions; import org.bukkit.Particle.DustTransition; +import org.bukkit.Vibration; import org.bukkit.Vibration.Destination; import org.bukkit.Vibration.Destination.BlockDestination; +import org.bukkit.World; import org.bukkit.advancement.Advancement; import org.bukkit.block.Biome; import org.bukkit.block.Block; @@ -125,6 +165,8 @@ import org.bukkit.entity.Player; import org.bukkit.help.HelpTopic; import org.bukkit.inventory.Recipe; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; import java.io.File; import java.io.IOException; @@ -132,7 +174,14 @@ import java.io.StringWriter; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; import java.util.function.ToIntFunction; @@ -146,7 +195,7 @@ @RequireField(in = EntitySelector.class, name = "usesSelector", ofType = boolean.class) @RequireField(in = ItemInput.class, name = "tag", ofType = CompoundTag.class) @RequireField(in = ServerFunctionLibrary.class, name = "dispatcher", ofType = CommandDispatcher.class) -public class NMS_1_20_R1 extends NMS_CommonWithFunctions { +public class NMS_1_20_R1 extends NMS_Common { private static final SafeVarHandle> helpMapTopics; private static final Field entitySelectorUsesSelector; @@ -173,18 +222,19 @@ public class NMS_1_20_R1 extends NMS_CommonWithFunctions { serverFunctionLibraryDispatcher = CommandAPIHandler.getField(ServerFunctionLibrary.class, "g", "dispatcher"); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentAdvancement() { + return ResourceLocationArgument.id(); } @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentChatComponent() { + return ComponentArgument.textComponent(); } @Override @@ -193,29 +243,8 @@ public final ArgumentType _ArgumentEnchantment() { } @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentRecipe() { + return ResourceLocationArgument.id(); } @Override @@ -244,10 +273,10 @@ public final String convert(ParticleData particle) { } // Converts NMS function to SimpleFunctionWrapper - private final SimpleFunctionWrapper convertFunction(CommandFunction commandFunction) { + private SimpleFunctionWrapper convertFunction(CommandFunction commandFunction) { ToIntFunction appliedObj = (CommandSourceStack css) -> this.getMinecraftServer().getFunctions().execute(commandFunction, css); - Entry[] cArr = commandFunction.getEntries(); + CommandFunction.Entry[] cArr = commandFunction.getEntries(); String[] result = new String[cArr.length]; for (int i = 0, size = cArr.length; i < size; i++) { result[i] = cArr[i].toString(); @@ -255,12 +284,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction commandFunct return new SimpleFunctionWrapper(fromResourceLocation(commandFunction.getId()), appliedObj, result); } - @Override - public final void createDispatcherFile(File file, CommandDispatcher dispatcher) throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { return new CustomHelpTopic(commandName, shortDescription, fullDescription, permission); @@ -271,6 +294,11 @@ public Advancement getAdvancement(CommandContext cmdCtx, Str return ResourceLocationArgument.getAdvancement(cmdCtx, key).bukkit; } + @Override + public Component getAdventureChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return GsonComponentSerializer.gson().deserialize(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + @Override public NamedTextColor getAdventureChatColor(CommandContext cmdCtx, String key) { final Integer color = ColorArgument.getColor(cmdCtx, key).getColor(); @@ -300,13 +328,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -317,6 +338,16 @@ public CommandSourceStack getBrigadierSourceFromCommandSender(AbstractCommandSen return VanillaCommandWrapper.getListener(sender.getSource()); } + @Override + public BaseComponent[] getChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + } + + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key))); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { return DimensionArgument.getDimension(cmdCtx, key).getWorld(); @@ -387,12 +418,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType.getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -415,6 +440,28 @@ public final FunctionWrapper[] getFunction(CommandContext cm return result.toArray(new FunctionWrapper[0]); } + @Override + public SimpleFunctionWrapper getFunction(NamespacedKey key) { + final ResourceLocation resourceLocation = new ResourceLocation(key.getNamespace(), key.getKey()); + Optional commandFunctionOptional = this.getMinecraftServer().getFunctions().get(resourceLocation); + if(commandFunctionOptional.isPresent()) { + return convertFunction(commandFunctionOptional.get()); + } else { + throw new IllegalStateException("Failed to get defined function " + key + + "! This should never happen - please report this to the CommandAPI" + + "developers, we'd love to know how you got this error message!"); + } + } + + @Override + public Set getFunctions() { + Set result = new HashSet<>(); + for (ResourceLocation resourceLocation : this.getMinecraftServer().getFunctions().getFunctionNames()) { + result.add(fromResourceLocation(resourceLocation)); + } + return result; + } + @Override public IntegerRange getIntRange(CommandContext cmdCtx, String key) { MinMaxBounds.Ints range = RangeArgument.Ints.getRange(cmdCtx, key); @@ -453,35 +500,28 @@ public final Predicate getItemStackPredicate(Com return item -> predicate.test(CraftItemStack.asNMSCopy(item)); } - @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - @Override public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } + @Differs(from = "1.19.4", by = "Uses MinecraftServer#getLootData instead of MinecraftServer#getLootTables") @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); + public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { + ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); + return new CraftLootTable(fromResourceLocation(resourceLocation), this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); } @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); + public NamespacedKey getMinecraftKey(CommandContext cmdCtx, String key) { + return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } - @Differs(from = "1.19.4", by = "Uses MinecraftServer#getLootData instead of MinecraftServer#getLootTables") @Override - public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { - ResourceLocation resourceLocation = ResourceLocationArgument.getId(cmdCtx, key); - return new CraftLootTable(fromResourceLocation(resourceLocation), this.getMinecraftServer().getLootData().getLootTable(resourceLocation)); + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); } @Override @@ -566,11 +606,28 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.getId()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key)); } + @Override + public Collection getScoreHolderMultiple(CommandContext cmdCtx, String key) + throws CommandSyntaxException { + return ScoreHolderArgument.getNames(cmdCtx, key); + } + + @Override + public String getScoreHolderSingle(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ScoreHolderArgument.getName(cmdCtx, key); + } + @Override public BukkitCommandSender getSenderForCommand(CommandContext cmdCtx, boolean isNative) { CommandSourceStack css = cmdCtx.getSource(); @@ -694,6 +751,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R2.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R2.java index 4738c84f56..bf2595c610 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R2.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R2.java @@ -39,6 +39,10 @@ import java.util.function.Predicate; import java.util.function.ToIntFunction; +import dev.jorel.commandapi.wrappers.Rotation; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -199,6 +203,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -252,8 +258,9 @@ public class NMS_1_21_R2 extends NMS_Common { minecraftServerFuelValues = SafeVarHandle.ofOrNull(MinecraftServer.class, "aE", "fuelValues", FuelValues.class); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Differs(from = "1.21.1", by = "New advancement argument implementation") @@ -261,16 +268,6 @@ private static NamespacedKey fromResourceLocation(ResourceLocation key) { public ArgumentType _ArgumentAdvancement() { return ResourceKeyArgument.key(Registries.ADVANCEMENT); } - - @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); - } @Override public ArgumentType _ArgumentChatComponent() { @@ -282,32 +279,6 @@ public final ArgumentType _ArgumentEnchantment() { return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENCHANTMENT); } - @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); - } - @Differs(from = "1.21.1", by = "New recipe argument implementation") @Override public ArgumentType _ArgumentRecipe() { @@ -410,13 +381,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -467,14 +431,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -491,6 +447,11 @@ public final BaseComponent[] getChat(CommandContext cmdCtx, return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key), COMMAND_BUILD_CONTEXT)); } + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key), COMMAND_BUILD_CONTEXT)); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -566,14 +527,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -651,33 +604,11 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } - @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); - } - - @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); - } - @Override public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { return CraftLootTable.minecraftToBukkit(ResourceLocationArgument.getId(cmdCtx, key)); @@ -688,6 +619,12 @@ public NamespacedKey getMinecraftKey(CommandContext cmdCtx, return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } + @Override + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); + } + @Override public final ParticleData getParticle(CommandContext cmdCtx, String key) { final ParticleOptions particleOptions = ParticleArgument.getParticle(cmdCtx, key); @@ -791,6 +728,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id().registry()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -941,6 +884,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R3.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R3.java index 4b3670d27c..89e7d0036a 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R3.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R3.java @@ -42,6 +42,10 @@ import java.util.function.Predicate; import java.util.function.ToIntFunction; +import dev.jorel.commandapi.wrappers.Rotation; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -204,6 +208,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -268,8 +274,9 @@ public class NMS_1_21_R3 extends NMS_Common { minecraftServerFuelValues = SafeVarHandle.ofOrNull(MinecraftServer.class, "aE", "fuelValues", FuelValues.class); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Differs(from = "1.21.1", by = "New advancement argument implementation") @@ -277,16 +284,6 @@ private static NamespacedKey fromResourceLocation(ResourceLocation key) { public ArgumentType _ArgumentAdvancement() { return ResourceKeyArgument.key(Registries.ADVANCEMENT); } - - @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); - } @Override public ArgumentType _ArgumentChatComponent() { @@ -298,32 +295,6 @@ public final ArgumentType _ArgumentEnchantment() { return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENCHANTMENT); } - @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); - } - @Differs(from = "1.21.1", by = "New recipe argument implementation") @Override public ArgumentType _ArgumentRecipe() { @@ -426,13 +397,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -483,14 +447,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -507,6 +463,11 @@ public final BaseComponent[] getChat(CommandContext cmdCtx, return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key), COMMAND_BUILD_CONTEXT)); } + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key), COMMAND_BUILD_CONTEXT)); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -582,14 +543,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -667,33 +620,11 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } - @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); - } - - @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); - } - @Override public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { return CraftLootTable.minecraftToBukkit(ResourceLocationArgument.getId(cmdCtx, key)); @@ -704,6 +635,12 @@ public NamespacedKey getMinecraftKey(CommandContext cmdCtx, return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } + @Override + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); + } + @Override public final ParticleData getParticle(CommandContext cmdCtx, String key) { final ParticleOptions particleOptions = ParticleArgument.getParticle(cmdCtx, key); @@ -818,6 +755,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id().registry()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -968,6 +911,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R4.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R4.java index 7e46106f60..8869a4551b 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R4.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R4.java @@ -42,6 +42,10 @@ import java.util.function.Predicate; import java.util.function.ToIntFunction; +import dev.jorel.commandapi.wrappers.Rotation; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -204,6 +208,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -268,24 +274,15 @@ public class NMS_1_21_R4 extends NMS_Common { minecraftServerFuelValues = SafeVarHandle.ofOrNull(MinecraftServer.class, "aE", "fuelValues", FuelValues.class); } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); + @Override + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override public ArgumentType _ArgumentAdvancement() { return ResourceKeyArgument.key(Registries.ADVANCEMENT); } - - @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); - } @Override public ArgumentType _ArgumentChatComponent() { @@ -297,32 +294,6 @@ public final ArgumentType _ArgumentEnchantment() { return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENCHANTMENT); } - @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); - } - @Override public ArgumentType _ArgumentRecipe() { return ResourceKeyArgument.key(Registries.RECIPE); @@ -423,13 +394,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -480,14 +444,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -584,14 +540,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -668,33 +616,11 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } - @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); - } - - @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); - } - @Override public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { return CraftLootTable.minecraftToBukkit(ResourceLocationArgument.getId(cmdCtx, key)); @@ -705,6 +631,12 @@ public NamespacedKey getMinecraftKey(CommandContext cmdCtx, return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } + @Override + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); + } + @Override public final ParticleData getParticle(CommandContext cmdCtx, String key) { final ParticleOptions particleOptions = ParticleArgument.getParticle(cmdCtx, key); @@ -818,6 +750,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id().registry()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -967,6 +905,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R1.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R1.java index d94c804170..781b8ad19c 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R1.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R1.java @@ -39,6 +39,10 @@ import java.util.function.Predicate; import java.util.function.ToIntFunction; +import dev.jorel.commandapi.wrappers.Rotation; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -194,6 +198,8 @@ import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.ScoreHolder; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; // Mojang-Mapped reflection /** @@ -243,18 +249,14 @@ public class NMS_1_21_R1 extends NMS_Common { vanillaCommandDispatcherFieldExists = fieldExists; } - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); - } - @Override - public final ArgumentType _ArgumentBlockPredicate() { - return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT); + protected CommandBuildContext getCommandBuildContext() { + return COMMAND_BUILD_CONTEXT; } @Override - public final ArgumentType _ArgumentBlockState() { - return BlockStateArgument.block(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentAdvancement() { + return ResourceLocationArgument.id(); } @Override @@ -268,29 +270,8 @@ public final ArgumentType _ArgumentEnchantment() { } @Override - public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { - return switch (subType) { - case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); - case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); - case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); - case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); - default -> throw new IllegalArgumentException("Unexpected value: " + subType); - }; - } - - @Override - public final ArgumentType _ArgumentItemPredicate() { - return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentItemStack() { - return ItemArgument.item(COMMAND_BUILD_CONTEXT); - } - - @Override - public final ArgumentType _ArgumentParticle() { - return ParticleArgument.particle(COMMAND_BUILD_CONTEXT); + public ArgumentType _ArgumentRecipe() { + return ResourceLocationArgument.id(); } @Override @@ -388,13 +369,6 @@ private final SimpleFunctionWrapper convertFunction(CommandFunction dispatcher) - throws IOException { - Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() - .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); - } - @Override public final HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, String permission) { @@ -445,14 +419,6 @@ public final Object getBiome(CommandContext cmdCtx, String k }; } - @Override - public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); - return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), - new BlockPos(block.getX(), block.getY(), block.getZ()), true)); - } - @Override public final BlockData getBlockState(CommandContext cmdCtx, String key) { return CraftBlockData.fromData(BlockStateArgument.getBlock(cmdCtx, key).getState()); @@ -469,6 +435,11 @@ public final BaseComponent[] getChat(CommandContext cmdCtx, return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key), COMMAND_BUILD_CONTEXT)); } + @Override + public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key), COMMAND_BUILD_CONTEXT)); + } + @Override public final World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -544,14 +515,6 @@ public final Object getEntitySelector(CommandContext cmdCtx, }; } - @SuppressWarnings("deprecation") - @Override - public final EntityType getEntityType(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return EntityType.fromName(net.minecraft.world.entity.EntityType - .getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); - } - @Override public FloatRange getFloatRange(CommandContext cmdCtx, String key) { MinMaxBounds.Doubles range = RangeArgument.Floats.getRange(cmdCtx, key); @@ -629,33 +592,11 @@ public final Predicate getItemStackPredicate( } @Override - public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); - } - - @Override - public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { + public final Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); } - @Override - public final Location getLocationBlock(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); - return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); - } - - @Override - public final Location getLocationPrecise(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); - return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); - } - @Override public final org.bukkit.loot.LootTable getLootTable(CommandContext cmdCtx, String key) { return CraftLootTable.minecraftToBukkit(ResourceLocationArgument.getId(cmdCtx, key)); @@ -666,6 +607,12 @@ public NamespacedKey getMinecraftKey(CommandContext cmdCtx, return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); } + @Override + public final Objective getObjective(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); + } + @Override public final ParticleData getParticle(CommandContext cmdCtx, String key) { final ParticleOptions particleOptions = ParticleArgument.getParticle(cmdCtx, key); @@ -768,6 +715,12 @@ public final Recipe getRecipe(CommandContext cmdCtx, String return new ComplexRecipeImpl(fromResourceLocation(recipe.id()), recipe.toBukkitRecipe()); } + @Override + public final Rotation getRotation(CommandContext cmdCtx, String key) { + Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); + return new Rotation(rotation.y, rotation.x); + } + @Override public ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key) { return ScoreboardSlot.ofMinecraft(ScoreboardSlotArgument.getDisplaySlot(cmdCtx, key).id()); @@ -915,6 +868,12 @@ public Set getTags() { return result; } + @Override + public final Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { + String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); + return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); + } + @Override public World getWorldForCSS(CommandSourceStack css) { return (css.getLevel() == null) ? null : css.getLevel().getWorld(); diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/pom.xml b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/pom.xml index de1489d2e9..d6d1f05335 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/pom.xml +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/pom.xml @@ -99,7 +99,7 @@ net.md-5 specialsource-maven-plugin - 1.2.4 + 2.0.4 package @@ -134,43 +134,71 @@ - Spigot_1_20_4_R3 - - - org.spigotmc - spigot - 1.20.4-R0.1-SNAPSHOT - remapped-mojang - provided - - + Spigot_1_21_R4 + + 1.21.5-R0.1-SNAPSHOT + 1.21.5-R0.1-SNAPSHOT + 1.21.5-R0.1-SNAPSHOT + - Spigot_1_20_2_R2 - - - org.spigotmc - spigot - 1.20.2-R0.1-SNAPSHOT - remapped-mojang - provided - - + Spigot_1_21_R3 + + 1.21.4-R0.1-SNAPSHOT + 1.21.4-R0.1-SNAPSHOT + 1.21.4-R0.1-SNAPSHOT + + + + Spigot_1_21_R2 + + 1.21.3-R0.1-SNAPSHOT + 1.21.3-R0.1-SNAPSHOT + 1.21.3-R0.1-SNAPSHOT + + + + Spigot_1_21_R1 + + 1.21.1-R0.1-SNAPSHOT + 1.21.1-R0.1-SNAPSHOT + 1.21.1-R0.1-SNAPSHOT + + + + Spigot_1_20_R4 + + 1.20.6-R0.1-SNAPSHOT + 1.20.6-R0.1-SNAPSHOT + 1.20.6-R0.1-SNAPSHOT + + + + Spigot_1_20_R3 + + 1.20.4-R0.1-SNAPSHOT + 1.20.4-R0.1-SNAPSHOT + 1.20.4-R0.1-SNAPSHOT + + + + Spigot_1_20_R2 + + 1.20.2-R0.1-SNAPSHOT + 1.20.2-R0.1-SNAPSHOT + 1.20.2-R0.1-SNAPSHOT + Spigot_1_20_R1 + + 1.20.1-R0.1-SNAPSHOT + 1.20.1-R0.1-SNAPSHOT + 1.20.1-R0.1-SNAPSHOT + true - - - org.spigotmc - spigot - 1.20-R0.1-SNAPSHOT - remapped-mojang - provided - - \ No newline at end of file diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java index 100351b9f1..4b76e13c23 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java @@ -20,83 +20,97 @@ *******************************************************************************/ package dev.jorel.commandapi.nms; +import com.google.common.io.Files; +import com.google.gson.GsonBuilder; 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 dev.jorel.commandapi.CommandAPIBukkit; import dev.jorel.commandapi.CommandAPIHandler; -import dev.jorel.commandapi.CommandRegistrationStrategy; import dev.jorel.commandapi.arguments.ArgumentSubType; -import dev.jorel.commandapi.arguments.SuggestionProviders; -import dev.jorel.commandapi.commandsenders.AbstractCommandSender; import dev.jorel.commandapi.commandsenders.BukkitCommandSender; -import dev.jorel.commandapi.preprocessor.Differs; -import dev.jorel.commandapi.preprocessor.Overridden; -import dev.jorel.commandapi.preprocessor.Unimplemented; -import dev.jorel.commandapi.wrappers.Rotation; -import dev.jorel.commandapi.wrappers.*; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.chat.ComponentSerializer; +import dev.jorel.commandapi.wrappers.Location2D; +import dev.jorel.commandapi.wrappers.MathOperation; +import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.arguments.*; -import net.minecraft.commands.arguments.coordinates.*; +import net.minecraft.commands.arguments.AngleArgument; +import net.minecraft.commands.arguments.ColorArgument; +import net.minecraft.commands.arguments.CompoundTagArgument; +import net.minecraft.commands.arguments.DimensionArgument; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.commands.arguments.GameProfileArgument; +import net.minecraft.commands.arguments.MessageArgument; +import net.minecraft.commands.arguments.ObjectiveArgument; +import net.minecraft.commands.arguments.ObjectiveCriteriaArgument; +import net.minecraft.commands.arguments.OperationArgument; +import net.minecraft.commands.arguments.ParticleArgument; +import net.minecraft.commands.arguments.RangeArgument; +import net.minecraft.commands.arguments.ResourceArgument; +import net.minecraft.commands.arguments.ResourceLocationArgument; +import net.minecraft.commands.arguments.ScoreHolderArgument; +import net.minecraft.commands.arguments.ScoreboardSlotArgument; +import net.minecraft.commands.arguments.TeamArgument; +import net.minecraft.commands.arguments.TimeArgument; +import net.minecraft.commands.arguments.UuidArgument; +import net.minecraft.commands.arguments.blocks.BlockPredicateArgument; +import net.minecraft.commands.arguments.blocks.BlockStateArgument; +import net.minecraft.commands.arguments.coordinates.BlockPosArgument; +import net.minecraft.commands.arguments.coordinates.ColumnPosArgument; +import net.minecraft.commands.arguments.coordinates.RotationArgument; +import net.minecraft.commands.arguments.coordinates.SwizzleArgument; +import net.minecraft.commands.arguments.coordinates.Vec2Argument; +import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.item.FunctionArgument; -import net.minecraft.network.chat.Component.Serializer; +import net.minecraft.commands.arguments.item.ItemArgument; +import net.minecraft.commands.arguments.item.ItemPredicateArgument; +import net.minecraft.commands.synchronization.ArgumentUtils; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.phys.Vec2; -import org.bukkit.*; +import net.minecraft.server.level.ColumnPos; +import net.minecraft.world.level.block.state.pattern.BlockInWorld; +import net.minecraft.world.phys.Vec3; +import org.bukkit.Axis; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.OfflinePlayer; +import org.bukkit.Sound; import org.bukkit.block.Block; -import org.bukkit.block.data.BlockData; import org.bukkit.command.CommandSender; -import org.bukkit.command.SimpleCommandMap; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; -import org.bukkit.help.HelpTopic; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.Recipe; -import org.bukkit.loot.LootTable; import org.bukkit.potion.PotionEffectType; -import org.bukkit.scoreboard.Objective; -import org.bukkit.scoreboard.Team; import java.io.File; import java.io.IOException; -import java.util.*; +import java.nio.charset.StandardCharsets; +import java.util.EnumSet; +import java.util.Locale; +import java.util.UUID; import java.util.function.Function; import java.util.function.Predicate; -import static dev.jorel.commandapi.preprocessor.Unimplemented.REASON.*; - /** * Common NMS code To ensure that this code actually works across all versions - * of Minecraft that this is supposed to support (1.17+), you should be + * of Minecraft that this is supposed to support (1.20+), you should be * compiling this code against all of the declared Maven profiles specified in - * this submodule's pom.xml file, by running the following commands: - *
    - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_3_R2
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_R1
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_2_R2
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_R1
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_17_R1
  • - *
- * Any of these that do not work should be removed or implemented otherwise - * (introducing another NMS_Common module perhaps? + * this submodule's pom.xml file, by running the following command: + * ./compileNMSCommon + *

+ * In case that fails, the method that causes the failure should be implemented in all + * NMS classes to maintain an error-free NMS_Common module that is applicable to all + * versions. */ public abstract class NMS_Common extends CommandAPIBukkit { - private static NamespacedKey fromResourceLocation(ResourceLocation key) { + + public final NamespacedKey fromResourceLocation(ResourceLocation key) { return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); } - @Override - public ArgumentType _ArgumentAdvancement() { - return ResourceLocationArgument.id(); - } + protected abstract CommandBuildContext getCommandBuildContext(); @Override public final ArgumentType _ArgumentAngle() { @@ -109,49 +123,40 @@ public final ArgumentType _ArgumentAxis() { } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19") - public abstract ArgumentType _ArgumentBlockPredicate(); + public final ArgumentType _ArgumentBlockPredicate() { + return BlockPredicateArgument.blockPredicate(getCommandBuildContext()); + } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19") - public abstract ArgumentType _ArgumentBlockState(); + public final ArgumentType _ArgumentBlockState() { + return BlockStateArgument.block(getCommandBuildContext()); + } @Override public final ArgumentType _ArgumentChat() { return MessageArgument.message(); } - @Override - @Overridden(in = "1.20.5", because = "This now takes in a CommandBuildContext") - public ArgumentType _ArgumentChatComponent() { - return ComponentArgument.textComponent(); - } - @Override public final ArgumentType _ArgumentChatFormat() { return ColorArgument.color(); } @Override - public ArgumentType _ArgumentDimension() { + public final ArgumentType _ArgumentDimension() { return DimensionArgument.dimension(); } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19.3") - public abstract ArgumentType _ArgumentEnchantment(); - - @Override - // I mean... really? Why? - @Unimplemented(because = NAME_CHANGED, info = "a (1.17) -> entity (1.18) -> a (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "multipleEntities (1.17) -> entities (1.18) -> b (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "c (1.17) -> player (1.18) -> c (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "d (1.17) -> players (1.18) -> d (1.19)") - public abstract ArgumentType _ArgumentEntity(ArgumentSubType subType); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19.3") - public abstract ArgumentType _ArgumentEntitySummon(); + public final ArgumentType _ArgumentEntity(ArgumentSubType subType) { + return switch (subType) { + case ENTITYSELECTOR_MANY_ENTITIES -> EntityArgument.entities(); + case ENTITYSELECTOR_MANY_PLAYERS -> EntityArgument.players(); + case ENTITYSELECTOR_ONE_ENTITY -> EntityArgument.entity(); + case ENTITYSELECTOR_ONE_PLAYER -> EntityArgument.player(); + default -> throw new IllegalArgumentException("Unexpected value: " + subType); + }; + } @Override public final ArgumentType _ArgumentFloatRange() { @@ -164,12 +169,14 @@ public final ArgumentType _ArgumentIntRange() { } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19") - public abstract ArgumentType _ArgumentItemPredicate(); + public final ArgumentType _ArgumentItemPredicate() { + return ItemPredicateArgument.itemPredicate(getCommandBuildContext()); + } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19") - public abstract ArgumentType _ArgumentItemStack(); + public final ArgumentType _ArgumentItemStack() { + return ItemArgument.item(getCommandBuildContext()); + } @Override public final ArgumentType _ArgumentMathOperation() { @@ -181,22 +188,14 @@ public final ArgumentType _ArgumentMinecraftKeyRegistered() { return ResourceLocationArgument.id(); } - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19.3") - public abstract ArgumentType _ArgumentMobEffect(); - @Override public final ArgumentType _ArgumentNBTCompound() { return CompoundTagArgument.compoundTag(); } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19.3") - public abstract ArgumentType _ArgumentParticle(); - - @Override - public ArgumentType _ArgumentRecipe() { - return ResourceLocationArgument.id(); + public final ArgumentType _ArgumentParticle() { + return ParticleArgument.particle(getCommandBuildContext()); } @Override @@ -248,10 +247,6 @@ public final ArgumentType _ArgumentScoreholder(ArgumentSubType subType) { }; } - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.18.2") - public abstract ArgumentType _ArgumentSyntheticBiome(); - @Override public final ArgumentType _ArgumentTag() { return FunctionArgument.functions(); @@ -277,22 +272,6 @@ public final ArgumentType _ArgumentVec3(boolean centerPosition) { return Vec3Argument.vec3(centerPosition); } - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "SimpleHelpMap") - public abstract Map getHelpMap(); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION) - public abstract String[] compatibleVersions(); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftItemStack") - public abstract String convert(ItemStack is); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION) - public abstract String convert(ParticleData particle); - @Override public final String convert(PotionEffectType potion) { return potion.getName().toLowerCase(Locale.ENGLISH); @@ -304,29 +283,11 @@ public final String convert(Sound sound) { } @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19") - public abstract void createDispatcherFile(File file, CommandDispatcher dispatcher) throws IOException; - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CustomHelpTopic") - public abstract HelpTopic generateHelpTopic(String commandName, String shortDescription, String fullDescription, - String permission); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.20.2") - public abstract org.bukkit.advancement.Advancement getAdvancement(CommandContext cmdCtx, String key) - throws CommandSyntaxException; - - @Override - @Overridden(in = "1.20.5", because = "Serializer.toJson now needs a Provider") - public Component getAdventureChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return GsonComponentSerializer.gson().deserialize(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + public final void createDispatcherFile(File file, CommandDispatcher dispatcher) throws IOException { + Files.asCharSink(file, StandardCharsets.UTF_8).write(new GsonBuilder().setPrettyPrinting().create() + .toJson(ArgumentUtils.serializeNodeToJson(dispatcher, dispatcher.getRoot()))); } - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, from = "ofExact", to = "namedColor", in = "NamedTextColor", introducedIn = "Adventure 4.10.0", info = "1.18 uses Adventure 4.9.3. 1.18.2 uses Adventure 4.11.0") - public abstract NamedTextColor getAdventureChatColor(CommandContext cmdCtx, String key); - @Override public final float getAngle(CommandContext cmdCtx, String key) { return AngleArgument.getAngle(cmdCtx, key); @@ -335,8 +296,8 @@ public final float getAngle(CommandContext cmdCtx, String ke @Override public final EnumSet getAxis(CommandContext cmdCtx, String key) { EnumSet set = EnumSet.noneOf(Axis.class); - EnumSet parsedEnumSet = SwizzleArgument.getSwizzle(cmdCtx, key); - for (net.minecraft.core.Direction.Axis element : parsedEnumSet) { + EnumSet parsedEnumSet = SwizzleArgument.getSwizzle(cmdCtx, key); + for (Direction.Axis element : parsedEnumSet) { set.add(switch (element) { case X -> Axis.X; case Y -> Axis.Y; @@ -347,18 +308,10 @@ public final EnumSet getAxis(CommandContext cmdCtx, St } @Override - @Unimplemented(because = NAME_CHANGED, from = "getWorld()", to = "f()", in = "1.19") - public abstract Predicate getBlockPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException; - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftBlockData") - public abstract BlockData getBlockState(CommandContext cmdCtx, String key); - - @Override - @Overridden(in = "1.20.5", because = "Serializer.toJson now needs a Provider") - public BaseComponent[] getChat(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return ComponentSerializer.parse(Serializer.toJson(MessageArgument.getMessage(cmdCtx, key))); + public final Predicate getBlockPredicate(CommandContext cmdCtx, String key) throws CommandSyntaxException { + Predicate predicate = BlockPredicateArgument.getBlockPredicate(cmdCtx, key); + return (Block block) -> predicate.test(new BlockInWorld(cmdCtx.getSource().getLevel(), + new BlockPos(block.getX(), block.getY(), block.getZ()), true)); } @Override @@ -366,14 +319,6 @@ public final ChatColor getChatColor(CommandContext cmdCtx, S return ChatColor.getByChar(ColorArgument.getColor(cmdCtx, key).getChar()); } - @Override - public BaseComponent[] getChatComponent(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return ComponentSerializer.parse(Serializer.toJson(ComponentArgument.getComponent(cmdCtx, key))); - } - - @Override - public abstract CommandSourceStack getBrigadierSourceFromCommandSender(AbstractCommandSender sender); - @Override public final BukkitCommandSender getCommandSenderFromCommandSource(CommandSourceStack css) { try { @@ -383,68 +328,29 @@ public final BukkitCommandSender getCommandSenderFromCo } } + @SuppressWarnings("deprecation") @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftWorld", info = "CraftWorld is implicitly referenced by ServerLevel#getWorld, due to package renaming, it can't resolve at runtime") - public abstract World getDimension(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.19.3") - public abstract Enchantment getEnchantment(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - @Unimplemented(because = NAME_CHANGED, from = "getKey()", to = "a()", in = "1.19") - public abstract EntityType getEntityType(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.20.2") - public abstract FloatRange getFloatRange(CommandContext cmdCtx, String key); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftEntity") - public abstract FunctionWrapper[] getFunction(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, in = "1.17, 1.18 and 1.18.2") - public abstract SimpleFunctionWrapper getFunction(NamespacedKey key); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, in = "1.17, 1.18 and 1.18.2") - public abstract Set getFunctions(); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.20.2") - public abstract IntegerRange getIntRange(CommandContext cmdCtx, String key); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftItemStack") - public abstract ItemStack getItemStack(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftItemStack") - public abstract Predicate getItemStackPredicate(CommandContext cmdCtx, String key) - throws CommandSyntaxException; - - @Override - @Unimplemented(because = { NAME_CHANGED, REQUIRES_CSS }, from = "a, b", to = "c(), d()") - public abstract Location2D getLocation2DBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - public Location2D getLocation2DPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { - Vec2 vecPos = Vec2Argument.getVec2(cmdCtx, key); - return new Location2D(getWorldForCSS(cmdCtx.getSource()), vecPos.x, vecPos.y); + public final EntityType getEntityType(CommandContext cmdCtx, String key) throws CommandSyntaxException { + return EntityType.fromName(net.minecraft.world.entity.EntityType.getKey(ResourceArgument.getSummonableEntityType(cmdCtx, key).value()).getPath()); } @Override - @Unimplemented(because = { NAME_CHANGED, REQUIRES_CSS }, from = "getX(), getY(), getZ()", to = "u(), v(), w()") - public abstract Location getLocationBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException; + public final Location2D getLocation2DBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { + ColumnPos blockPos = ColumnPosArgument.getColumnPos(cmdCtx, key); + return new Location2D(getWorldForCSS(cmdCtx.getSource()), blockPos.x(), blockPos.z()); + } @Override - @Unimplemented(because = { NAME_CHANGED, REQUIRES_CSS }, from = "getX(), getY(), getZ()", to = "a(), b(), c()") - public abstract Location getLocationPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException; + public final Location getLocationBlock(CommandContext cmdCtx, String key) throws CommandSyntaxException { + BlockPos blockPos = BlockPosArgument.getSpawnablePos(cmdCtx, key); + return new Location(getWorldForCSS(cmdCtx.getSource()), blockPos.getX(), blockPos.getY(), blockPos.getZ()); + } @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftLootTable") - public abstract LootTable getLootTable(CommandContext cmdCtx, String key); + public final Location getLocationPrecise(CommandContext cmdCtx, String key) throws CommandSyntaxException { + Vec3 vecPos = Vec3Argument.getCoordinates(cmdCtx, key).getPosition(cmdCtx.getSource()); + return new Location(getWorldForCSS(cmdCtx.getSource()), vecPos.x(), vecPos.y(), vecPos.z()); + } @Override public final MathOperation getMathOperation(CommandContext cmdCtx, String key) throws CommandSyntaxException { @@ -453,27 +359,12 @@ public final MathOperation getMathOperation(CommandContext c return MathOperation.fromString(CommandAPIHandler.getRawArgumentInput(cmdCtx, key)); } - @Override - @Overridden(in = "1.17 common; 1.18", because = "1.17 uses ArgumentMinecraftKeyRegistered.f instead of ArgumentMinecraftKeyRegistered.e") - @Overridden(in = "1.20.5, 1.20.6", because = "ArgumentMinecraftKeyRegistered.e -> ArgumentMinecraftKeyRegistered.c") - public NamespacedKey getMinecraftKey(CommandContext cmdCtx, String key) { - return fromResourceLocation(ResourceLocationArgument.getId(cmdCtx, key)); - } - @Override public final Object getNBTCompound(CommandContext cmdCtx, String key, Function nbtContainerConstructor) { return nbtContainerConstructor.apply(CompoundTagArgument.getCompoundTag(cmdCtx, key)); } - @Override - @Overridden(in = "1.17 common", because = "The Objective.getName() method mangles itself sometimes and I don't know why. Seems to be looking for Objective.b() or something") - public Objective getObjective(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - String objectiveName = ObjectiveArgument.getObjective(cmdCtx, key).getName(); - return Bukkit.getScoreboardManager().getMainScoreboard().getObjective(objectiveName); - } - @Override public final String getObjectiveCriteria(CommandContext cmdCtx, String key) { return ObjectiveCriteriaArgument.getCriteria(cmdCtx, key).getName(); @@ -484,10 +375,6 @@ public final OfflinePlayer getOfflinePlayer(CommandContext c return Bukkit.getOfflinePlayer(GameProfileArgument.getGameProfiles(cmdCtx, key).iterator().next().getId()); } - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.18, 1.19") - public abstract ParticleData getParticle(CommandContext cmdCtx, String key); - @Override public final Player getPlayer(CommandContext cmdCtx, String key) throws CommandSyntaxException { Player target = Bukkit.getPlayer(GameProfileArgument.getGameProfiles(cmdCtx, key).iterator().next().getId()); @@ -498,88 +385,6 @@ public final Player getPlayer(CommandContext cmdCtx, String } } - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.18") - public abstract Object getPotionEffect(CommandContext cmdCtx, String key, ArgumentSubType subType) - throws CommandSyntaxException; - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, info = """ - 1.17 has what appears to be a different obfuscation for recipe.getId(). - I can't be bothered to figure out what it is, but all I know is it doesn't work, - and we need to move it outta here! - """) - public abstract Recipe getRecipe(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - public final Rotation getRotation(CommandContext cmdCtx, String key) { - Vec2 rotation = RotationArgument.getRotation(cmdCtx, key).getRotation(cmdCtx.getSource()); - return new Rotation(rotation.y, rotation.x); - } - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, introducedIn = "1.20.2") - public abstract ScoreboardSlot getScoreboardSlot(CommandContext cmdCtx, String key); - - @Override - // TODO: Overridden in 1.20.3 because this now returns a Collection - public Collection getScoreHolderMultiple(CommandContext cmdCtx, String key) - throws CommandSyntaxException { - return ScoreHolderArgument.getNames(cmdCtx, key); - } - - @Override - // TODO: Overridden in 1.20.3 because this now returns a ScoreHolder - public String getScoreHolderSingle(CommandContext cmdCtx, String key) throws CommandSyntaxException { - return ScoreHolderArgument.getName(cmdCtx, key); - } - - @Override - // Whyyyyyyyyyyyyyyyyyyy - @Unimplemented(because = NAME_CHANGED, info = "CommandListenerWrapper changes:") - @Unimplemented(because = NAME_CHANGED, info = "getPosition (1.17) -> getPosition (1.18) -> e (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "i (1.17) -> getRotation (1.18) -> l (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "getEntity (1.17) -> getEntity (1.18) -> g (1.19)") - @Unimplemented(because = NAME_CHANGED, info = "getWorld (1.17) -> getLevel (1.18) -> f (1.19)") - public abstract BukkitCommandSender getSenderForCommand(CommandContext cmdCtx, boolean isNative); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "ProxiedNativeCommandSender") - public abstract NativeProxyCommandSender createNativeProxyCommandSender(CommandSender caller, CommandSender callee, Location location, World world); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftServer") - public abstract SimpleCommandMap getSimpleCommandMap(); - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftSound") - public abstract Object getSound(CommandContext cmdCtx, String key, ArgumentSubType subType); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, info = """ - The various methods that this uses is obfuscated to different method - names for different versions. For example, getMinecraftServer().getLootTables.getIds() - is mapped to a different method in 1.18 compared to 1.19.2. This also has various other - implications across all sorts of versions, so it's much more reliable to just implement - them in every version. - """) - public abstract SuggestionProvider getSuggestionProvider(SuggestionProviders provider); - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, from = "1.18.2", to = "1.19") - @Differs(from = "1.18.2", by = "getTag() now returns a Collection<> instead of a Tag<>, so don't have to call .getValues()") - public abstract SimpleFunctionWrapper[] getTag(NamespacedKey key); - - @Override - @Unimplemented(because = NAME_CHANGED, info = "See https://github.com/JorelAli/CommandAPI/issues/524") - public abstract Set getTags(); - - @Override - public Team getTeam(CommandContext cmdCtx, String key) throws CommandSyntaxException { - String teamName = TeamArgument.getTeam(cmdCtx, key).getName(); - return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName); - } - @Override public final int getTime(CommandContext cmdCtx, String key) { return cmdCtx.getArgument(key, Integer.class); @@ -589,14 +394,4 @@ public final int getTime(CommandContext cmdCtx, String key) public final UUID getUUID(CommandContext cmdCtx, String key) { return UuidArgument.getUuid(cmdCtx, key); } - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION) - public abstract void reloadDataPacks(); - - @Override - @Unimplemented(because = NAME_CHANGED, info = "MinecraftServer#getCommands() obfuscated differently across multiple versions") - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "VanillaCommandWrapper") - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "BukkitCommandWrapper") - public abstract CommandRegistrationStrategy createCommandRegistrationStrategy(); } diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_CommonWithFunctions.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_CommonWithFunctions.java deleted file mode 100644 index ee9f375b07..0000000000 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_CommonWithFunctions.java +++ /dev/null @@ -1,111 +0,0 @@ -/******************************************************************************* - * Copyright 2018, 2021 Jorel Ali (Skepter) - MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - *******************************************************************************/ -package dev.jorel.commandapi.nms; - -import static dev.jorel.commandapi.preprocessor.Unimplemented.REASON.*; - -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; -import java.util.function.ToIntFunction; - -import org.bukkit.NamespacedKey; - -import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; - -import dev.jorel.commandapi.preprocessor.Differs; -import dev.jorel.commandapi.preprocessor.Unimplemented; -import dev.jorel.commandapi.wrappers.FunctionWrapper; -import dev.jorel.commandapi.wrappers.SimpleFunctionWrapper; -import net.minecraft.commands.CommandFunction; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.MinecraftServer; - -/** - * Common NMS code To ensure that this code actually works across all versions - * of Minecraft that this is supposed to support (1.17+), you should be - * compiling this code against all of the declared Maven profiles specified in - * this submodule's pom.xml file, by running the following commands: - *

    - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_3_R2
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_19_R1
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_2_R2
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_18_R1
  • - *
  • mvn clean package -pl :commandapi-bukkit-nms-common -P Platform.Bukkit,Spigot_1_17_R1
  • - *
- * Any of these that do not work should be removed or implemented otherwise - * (introducing another NMS_Common module perhaps? - */ -public abstract class NMS_CommonWithFunctions extends NMS_Common { - - private static NamespacedKey fromResourceLocation(ResourceLocation key) { - return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath()); - } - - // Converts NMS function to SimpleFunctionWrapper - private SimpleFunctionWrapper convertFunction(CommandFunction commandFunction) { - ToIntFunction appliedObj = (CommandSourceStack css) -> this.getMinecraftServer().getFunctions() - .execute(commandFunction, css); - - CommandFunction.Entry[] cArr = commandFunction.getEntries(); - String[] result = new String[cArr.length]; - for (int i = 0, size = cArr.length; i < size; i++) { - result[i] = cArr[i].toString(); - } - return new SimpleFunctionWrapper(fromResourceLocation(commandFunction.getId()), appliedObj, result); - } - - @Override - @Unimplemented(because = REQUIRES_CRAFTBUKKIT, classNamed = "CraftEntity") - public abstract FunctionWrapper[] getFunction(CommandContext cmdCtx, String key) throws CommandSyntaxException; - - @Override - // TODO: This has its own implementation for 1.17, 1.18 and 1.18.2 - public SimpleFunctionWrapper getFunction(NamespacedKey key) { - final ResourceLocation resourceLocation = new ResourceLocation(key.getNamespace(), key.getKey()); - Optional commandFunctionOptional = this.getMinecraftServer().getFunctions().get(resourceLocation); - if(commandFunctionOptional.isPresent()) { - return convertFunction(commandFunctionOptional.get()); - } else { - throw new IllegalStateException("Failed to get defined function " + key - + "! This should never happen - please report this to the CommandAPI" - + "developers, we'd love to know how you got this error message!"); - } - } - - @Override - // TODO: This has its own implementation for 1.17, 1.18 and 1.18.2 - public Set getFunctions() { - Set result = new HashSet<>(); - for (ResourceLocation resourceLocation : this.getMinecraftServer().getFunctions().getFunctionNames()) { - result.add(fromResourceLocation(resourceLocation)); - } - return result; - } - - @Override - @Unimplemented(because = VERSION_SPECIFIC_IMPLEMENTATION, from = "1.18.2", to = "1.19") - @Differs(from = "1.18.2", by = "getTag() now returns a Collection<> instead of a Tag<>, so don't have to call .getValues()") - public abstract SimpleFunctionWrapper[] getTag(NamespacedKey key); - -} diff --git a/compileNMSCommon.sh b/compileNMSCommon.sh new file mode 100755 index 0000000000..d24540960e --- /dev/null +++ b/compileNMSCommon.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +# Make sure we are running at the script's location (CommandAPI root) +cd "${0%/*}" || (echo "cd failed" && exit) + +BUILD_DIR="./commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/target" +DIFF_DIR="./nmsCommonDiff" +NEW_JAR="./newJar.jar" +NEW_CLASSES="./newClasses" +PREV_CLASSES="./prevClasses" + +# Refresh diff directory +rm -rf "$DIFF_DIR" +mkdir -p "$DIFF_DIR" + +compileVersion() { + echo "Compiling NMS_Common for Spigot $1..." + + mvn clean package -Dmaven.source.skip=true -Dmaven.javadoc.skip=true \ + -pl :commandapi-bukkit-nms-common -am -P Platform.Bukkit,"$2" --quiet + + # Find the spigot-mapped jar + for jar in "$BUILD_DIR"/CommandAPI-*.jar; do + if [[ "$jar" != *"original-"* && "$jar" == *").jar" ]]; then + echo "Found JAR: $jar" + cp "$jar" "$NEW_JAR" + break + fi + done + + if [ ! -e "$NEW_JAR" ]; then + # Couldn't find the jar with NMS_Common, so compilation failed + echo "Failed to compile $1" + + # Clean up + rm -rf "$PREV_CLASSES" + # Fail + exit 1 + fi + + # Unzip jar so we can view the classes + unzip -q "$NEW_JAR" -d "$NEW_CLASSES" + rm "$NEW_JAR" + + # Replace all class files with their bytecode so we can compare by text + find "$NEW_CLASSES" -name "*.class" | while read -r class; do + class_name="${class%.class}" + echo "Getting bytecode for $class_name" + + javap -c -p "$class" > "$class_name.txt" + rm "$class" + done + + if [ -d "$PREV_CLASSES" ]; then + # Generate diff against the previous version to detect changes + diff_file="$DIFF_DIR/$1.diff" + git diff --no-index "$PREV_CLASSES" "$NEW_CLASSES" > "$diff_file" + + if [ ! -s "$diff_file" ]; then + # diff file is empty, so no problems here + echo "No changes in $1" + rm "$diff_file" + fi + + rm -r "$PREV_CLASSES" + fi + + # Make new classes the previous classes for the next try + mv "$NEW_CLASSES/" "$PREV_CLASSES/" +} + +compileVersion "1.20 and 1.20.1" "Spigot_1_20_R1" +compileVersion "1.20.2" "Spigot_1_20_R2" +compileVersion "1.20.3 and 1.20.4" "Spigot_1_20_R3" +compileVersion "1.20.5 and 1.20.6" "Spigot_1_20_R4" +compileVersion "1.21 and 1.21.1" "Spigot_1_21_R1" +compileVersion "1.21.2 and 1.21.3" "Spigot_1_21_R2" +compileVersion "1.21.4" "Spigot_1_21_R3" +compileVersion "1.21.5" "Spigot_1_21_R4" + +# Clean up +rm -rf "$PREV_CLASSES" + +# Report if any diff was found +if [ -z "$(ls -A "$DIFF_DIR")" ]; then + # Diff directory is empty, no problems :) + echo "No differences detected" + + rm -r "$DIFF_DIR" +else + echo "Differences found!" + + for diff in "$DIFF_DIR"/*; do + echo "=== $diff ===" + cat "$diff" + done + + echo "Inspect the $DIFF_DIR directory for details" + + # fail + exit 1 +fi \ No newline at end of file