Skip to content

Commit

Permalink
Create CommandBuildContext only once in later versions
Browse files Browse the repository at this point in the history
  • Loading branch information
DerEchtePilz committed Nov 4, 2024
1 parent ceef989 commit 3e6e617
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.function.ToIntFunction;

import dev.jorel.commandapi.*;
import net.minecraft.commands.CommandSource;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
Expand All @@ -64,7 +63,6 @@
import org.bukkit.craftbukkit.v1_20_R4.CraftServer;
import org.bukkit.craftbukkit.v1_20_R4.CraftSound;
import org.bukkit.craftbukkit.v1_20_R4.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_20_R4.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_20_R4.command.VanillaCommandWrapper;
import org.bukkit.craftbukkit.v1_20_R4.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_20_R4.help.CustomHelpTopic;
Expand Down Expand Up @@ -108,23 +106,16 @@
import dev.jorel.commandapi.wrappers.ParticleData;
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.CommandResultCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.FunctionInstantiationException;
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.ParticleArgument;
import net.minecraft.commands.arguments.RangeArgument;
import net.minecraft.commands.arguments.ResourceArgument;
Expand Down Expand Up @@ -209,18 +200,15 @@ public class NMS_1_20_R4 extends NMS_Common {
private static final Field serverFunctionLibraryDispatcher;

// Derived from net.minecraft.commands.Commands;
private static final CommandBuildContext COMMAND_BUILD_CONTEXT;
private final CommandBuildContext commandBuildContext;

public NMS_1_20_R4(CommandBuildContext commandBuildContext) {
this.commandBuildContext = commandBuildContext;
}

// Compute all var handles all in one go so we don't do this during main server
// runtime
static {
if (Bukkit.getServer() instanceof CraftServer server) {
COMMAND_BUILD_CONTEXT = CommandBuildContext.simple(server.getServer().registryAccess(),
server.getServer().getWorldData().getDataConfiguration().enabledFeatures());
} else {
COMMAND_BUILD_CONTEXT = null;
}

helpMapTopics = SafeVarHandle.ofOrNull(SimpleHelpMap.class, "helpTopics", "helpTopics", Map.class);
// For some reason, MethodHandles fails for this field, but Field works okay
entitySelectorUsesSelector = CommandAPIHandler.getField(EntitySelector.class, "p", "usesSelector");
Expand All @@ -235,23 +223,23 @@ private static NamespacedKey fromResourceLocation(ResourceLocation key) {

@Override
public final ArgumentType<?> _ArgumentBlockPredicate() {
return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT);
return BlockPredicateArgument.blockPredicate(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentBlockState() {
return BlockStateArgument.block(COMMAND_BUILD_CONTEXT);
return BlockStateArgument.block(commandBuildContext);
}

@Differs(from = "1.20.4", by = "Now needs a command build context")
@Override
public ArgumentType<?> _ArgumentChatComponent() {
return ComponentArgument.textComponent(COMMAND_BUILD_CONTEXT);
return ComponentArgument.textComponent(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentEnchantment() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENCHANTMENT);
return ResourceArgument.resource(commandBuildContext, Registries.ENCHANTMENT);
}

@Override
Expand All @@ -267,22 +255,22 @@ public final ArgumentType<?> _ArgumentEntity(ArgumentSubType subType) {

@Override
public final ArgumentType<?> _ArgumentItemPredicate() {
return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT);
return ItemPredicateArgument.itemPredicate(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentItemStack() {
return ItemArgument.item(COMMAND_BUILD_CONTEXT);
return ItemArgument.item(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentParticle() {
return ParticleArgument.particle(COMMAND_BUILD_CONTEXT);
return ParticleArgument.particle(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentSyntheticBiome() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.BIOME);
return ResourceArgument.resource(commandBuildContext, Registries.BIOME);
}

@Override
Expand All @@ -295,9 +283,9 @@ public String[] compatibleVersions() {
return new String[] { "1.20.5", "1.20.6" };
};

private static String serializeNMSItemStack(ItemStack is) {
private String serializeNMSItemStack(ItemStack is) {
final DataComponentMap patchedMap = PatchedDataComponentMap.fromPatch(PatchedDataComponentMap.EMPTY, is.getComponentsPatch());
return new ItemInput(is.getItemHolder(), patchedMap).serialize(COMMAND_BUILD_CONTEXT);
return new ItemInput(is.getItemHolder(), patchedMap).serialize(commandBuildContext);
}

@Differs(from = "1.20.4", by = "Everything")
Expand Down Expand Up @@ -991,7 +979,7 @@ public final void reloadDataPacks() {
@Override
public Message generateMessageFromJson(String json) {
// TODO: Same as #getAdventureChatComponent, figure out if an empty provider is suitable here
return Serializer.fromJson(json, COMMAND_BUILD_CONTEXT);
return Serializer.fromJson(json, commandBuildContext);
}

@SuppressWarnings("unchecked")
Expand All @@ -1006,11 +994,11 @@ public <T> T getMinecraftServer() {

@Override
public ArgumentType<?> _ArgumentMobEffect() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.MOB_EFFECT);
return ResourceArgument.resource(commandBuildContext, Registries.MOB_EFFECT);
}

@Override
public ArgumentType<?> _ArgumentEntitySummon() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENTITY_TYPE);
return ResourceArgument.resource(commandBuildContext, Registries.ENTITY_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.bukkit.craftbukkit.v1_21_R1.CraftServer;
import org.bukkit.craftbukkit.v1_21_R1.CraftSound;
import org.bukkit.craftbukkit.v1_21_R1.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_21_R1.command.BukkitCommandWrapper;
import org.bukkit.craftbukkit.v1_21_R1.command.VanillaCommandWrapper;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_21_R1.help.CustomHelpTopic;
Expand Down Expand Up @@ -110,23 +109,16 @@
import dev.jorel.commandapi.wrappers.ParticleData;
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.CommandResultCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.FunctionInstantiationException;
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.ParticleArgument;
import net.minecraft.commands.arguments.RangeArgument;
import net.minecraft.commands.arguments.ResourceArgument;
Expand Down Expand Up @@ -210,18 +202,15 @@ public class NMS_1_21_R1 extends NMS_Common {
private static final boolean vanillaCommandDispatcherFieldExists;

// Derived from net.minecraft.commands.Commands;
private static final CommandBuildContext COMMAND_BUILD_CONTEXT;
private final CommandBuildContext commandBuildContext;

public NMS_1_21_R1(CommandBuildContext commandBuildContext) {
this.commandBuildContext = commandBuildContext;
}

// Compute all var handles all in one go so we don't do this during main server
// runtime
static {
if (Bukkit.getServer() instanceof CraftServer server) {
COMMAND_BUILD_CONTEXT = CommandBuildContext.simple(server.getServer().registryAccess(),
server.getServer().getWorldData().getDataConfiguration().enabledFeatures());
} else {
COMMAND_BUILD_CONTEXT = null;
}

helpMapTopics = SafeVarHandle.ofOrNull(SimpleHelpMap.class, "helpTopics", "helpTopics", Map.class);
// For some reason, MethodHandles fails for this field, but Field works okay
entitySelectorUsesSelector = CommandAPIHandler.getField(EntitySelector.class, "p", "usesSelector");
Expand All @@ -246,22 +235,22 @@ private static NamespacedKey fromResourceLocation(ResourceLocation key) {

@Override
public final ArgumentType<?> _ArgumentBlockPredicate() {
return BlockPredicateArgument.blockPredicate(COMMAND_BUILD_CONTEXT);
return BlockPredicateArgument.blockPredicate(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentBlockState() {
return BlockStateArgument.block(COMMAND_BUILD_CONTEXT);
return BlockStateArgument.block(commandBuildContext);
}

@Override
public ArgumentType<?> _ArgumentChatComponent() {
return ComponentArgument.textComponent(COMMAND_BUILD_CONTEXT);
return ComponentArgument.textComponent(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentEnchantment() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENCHANTMENT);
return ResourceArgument.resource(commandBuildContext, Registries.ENCHANTMENT);
}

@Override
Expand All @@ -277,22 +266,22 @@ public final ArgumentType<?> _ArgumentEntity(ArgumentSubType subType) {

@Override
public final ArgumentType<?> _ArgumentItemPredicate() {
return ItemPredicateArgument.itemPredicate(COMMAND_BUILD_CONTEXT);
return ItemPredicateArgument.itemPredicate(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentItemStack() {
return ItemArgument.item(COMMAND_BUILD_CONTEXT);
return ItemArgument.item(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentParticle() {
return ParticleArgument.particle(COMMAND_BUILD_CONTEXT);
return ParticleArgument.particle(commandBuildContext);
}

@Override
public final ArgumentType<?> _ArgumentSyntheticBiome() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.BIOME);
return ResourceArgument.resource(commandBuildContext, Registries.BIOME);
}

@Override
Expand All @@ -306,8 +295,8 @@ public String[] compatibleVersions() {
};

@Differs(from = "1.20.6", by = "ItemInput constructor uses a data components patch, instead of a data components map")
private static String serializeNMSItemStack(ItemStack is) {
return new ItemInput(is.getItemHolder(), is.getComponentsPatch()).serialize(COMMAND_BUILD_CONTEXT);
private String serializeNMSItemStack(ItemStack is) {
return new ItemInput(is.getItemHolder(), is.getComponentsPatch()).serialize(commandBuildContext);
}

@Override
Expand Down Expand Up @@ -998,7 +987,7 @@ public final void reloadDataPacks() {
@Override
public Message generateMessageFromJson(String json) {
// TODO: Same as #getAdventureChatComponent, figure out if an empty provider is suitable here
return Serializer.fromJson(json, COMMAND_BUILD_CONTEXT);
return Serializer.fromJson(json, commandBuildContext);
}

@SuppressWarnings("unchecked")
Expand All @@ -1013,12 +1002,12 @@ public <T> T getMinecraftServer() {

@Override
public ArgumentType<?> _ArgumentMobEffect() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.MOB_EFFECT);
return ResourceArgument.resource(commandBuildContext, Registries.MOB_EFFECT);
}

@Override
public ArgumentType<?> _ArgumentEntitySummon() {
return ResourceArgument.resource(COMMAND_BUILD_CONTEXT, Registries.ENTITY_TYPE);
return ResourceArgument.resource(commandBuildContext, Registries.ENTITY_TYPE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Component getChatComponent(CommandContext<CommandSourceStack> cmdCtx, Str
@Override
public NMS<?> bukkitNMS() {
if (bukkitNMS == null) {
this.bukkitNMS = new NMS_1_20_R4();
this.bukkitNMS = new NMS_1_20_R4(COMMAND_BUILD_CONTEXT);
}
return bukkitNMS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Component getChatComponent(CommandContext<CommandSourceStack> cmdCtx, Str
@Override
public NMS<?> bukkitNMS() {
if (bukkitNMS == null) {
this.bukkitNMS = new NMS_1_21_R1();
this.bukkitNMS = new NMS_1_21_R1(COMMAND_BUILD_CONTEXT);
}
return bukkitNMS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public BaseComponent[] getChatComponent(CommandContext<CommandSourceStack> cmdCt
@Override
public NMS<?> bukkitNMS() {
if (bukkitNMS == null) {
this.bukkitNMS = new NMS_1_20_R4();
this.bukkitNMS = new NMS_1_20_R4(COMMAND_BUILD_CONTEXT);
}
return bukkitNMS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public BaseComponent[] getChatComponent(CommandContext<CommandSourceStack> cmdCt
@Override
public NMS<?> bukkitNMS() {
if (bukkitNMS == null) {
this.bukkitNMS = new NMS_1_21_R1();
this.bukkitNMS = new NMS_1_21_R1(COMMAND_BUILD_CONTEXT);
}
return bukkitNMS;
}
Expand Down

0 comments on commit 3e6e617

Please sign in to comment.