Skip to content

Commit 5d4111b

Browse files
committed
Tweak NMS 1.21.2 reloading to handle fuel values and recipe loading finalization
1 parent 52ab132 commit 5d4111b

File tree

2 files changed

+29
-8
lines changed
  • commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src

2 files changed

+29
-8
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_21_R2.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
import java.util.function.Predicate;
4040
import java.util.function.ToIntFunction;
4141

42-
import net.minecraft.commands.arguments.ResourceKeyArgument;
43-
import net.minecraft.core.Holder;
44-
import net.minecraft.resources.ResourceKey;
45-
import net.minecraft.util.profiling.Profiler;
4642
import org.bukkit.Bukkit;
4743
import org.bukkit.Color;
4844
import org.bukkit.Location;
@@ -136,6 +132,7 @@
136132
import net.minecraft.commands.arguments.ParticleArgument;
137133
import net.minecraft.commands.arguments.RangeArgument;
138134
import net.minecraft.commands.arguments.ResourceArgument;
135+
import net.minecraft.commands.arguments.ResourceKeyArgument;
139136
import net.minecraft.commands.arguments.ResourceLocationArgument;
140137
import net.minecraft.commands.arguments.ScoreHolderArgument;
141138
import net.minecraft.commands.arguments.ScoreboardSlotArgument;
@@ -155,6 +152,7 @@
155152
import net.minecraft.commands.functions.InstantiatedFunction;
156153
import net.minecraft.commands.synchronization.ArgumentUtils;
157154
import net.minecraft.core.BlockPos;
155+
import net.minecraft.core.Holder;
158156
import net.minecraft.core.particles.BlockParticleOption;
159157
import net.minecraft.core.particles.ColorParticleOption;
160158
import net.minecraft.core.particles.DustColorTransitionOptions;
@@ -185,13 +183,16 @@
185183
import net.minecraft.server.packs.resources.SimpleReloadInstance;
186184
import net.minecraft.sounds.SoundEvent;
187185
import net.minecraft.util.Unit;
186+
import net.minecraft.util.profiling.Profiler;
188187
import net.minecraft.util.profiling.ProfilerFiller;
189188
import net.minecraft.world.entity.Entity;
189+
import net.minecraft.world.flag.FeatureFlagSet;
190190
import net.minecraft.world.item.ItemStack;
191191
import net.minecraft.world.item.crafting.RecipeHolder;
192192
import net.minecraft.world.level.DataPackConfig;
193193
import net.minecraft.world.level.Level;
194194
import net.minecraft.world.level.WorldDataConfiguration;
195+
import net.minecraft.world.level.block.entity.FuelValues;
195196
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
196197
import net.minecraft.world.level.gameevent.BlockPositionSource;
197198
import net.minecraft.world.phys.Vec2;
@@ -207,13 +208,15 @@
207208
@RequireField(in = EntitySelector.class, name = "usesSelector", ofType = boolean.class)
208209
// @RequireField(in = ItemInput.class, name = "tag", ofType = CompoundTag.class)
209210
@RequireField(in = ServerFunctionLibrary.class, name = "dispatcher", ofType = CommandDispatcher.class)
211+
@RequireField(in = MinecraftServer.class, name = "fuelValues", ofType = FuelValues.class)
210212
public class NMS_1_21_R2 extends NMS_Common {
211213

212214
private static final SafeVarHandle<SimpleHelpMap, Map<String, HelpTopic>> helpMapTopics;
213215
private static final Field entitySelectorUsesSelector;
214216
// private static final SafeVarHandle<ItemInput, CompoundTag> itemInput;
215217
private static final Field serverFunctionLibraryDispatcher;
216218
private static final boolean vanillaCommandDispatcherFieldExists;
219+
private static final SafeVarHandle<MinecraftServer, FuelValues> minecraftServerFuelValues;
217220

218221
// Derived from net.minecraft.commands.Commands;
219222
private static final CommandBuildContext COMMAND_BUILD_CONTEXT;
@@ -244,6 +247,8 @@ public class NMS_1_21_R2 extends NMS_Common {
244247
fieldExists = false;
245248
}
246249
vanillaCommandDispatcherFieldExists = fieldExists;
250+
251+
minecraftServerFuelValues = SafeVarHandle.ofOrNull(MinecraftServer.class, "fuelValues", "fuelValues", FuelValues.class);
247252
}
248253

249254
private static NamespacedKey fromResourceLocation(ResourceLocation key) {
@@ -974,6 +979,10 @@ public final void reloadDataPacks() {
974979
MultiPackResourceManager resourceManager = new MultiPackResourceManager(PackType.SERVER_DATA,
975980
packResources);
976981

982+
// TODO: I'm not sure if this is sufficient anymore - Do we not want to load tags for existing
983+
// registries here as well?
984+
// List<PendingTags<?>> TagList = TagLoader.loadTagsForExistingRegistries(resourceManager, this.<MinecraftServer>getMinecraftServer().registries().compositeAccess());
985+
977986
// Not using packResources, because we really really want this to work
978987
CompletableFuture<?> simpleReloadInstance = SimpleReloadInstance.create(resourceManager,
979988
serverResources.managers().listeners(), this.<MinecraftServer>getMinecraftServer().executor,
@@ -990,6 +999,8 @@ public final void reloadDataPacks() {
990999
this.<MinecraftServer>getMinecraftServer().resources = serverResources;
9911000
this.<MinecraftServer>getMinecraftServer().server.syncCommands();
9921001
this.<MinecraftServer>getMinecraftServer().getPackRepository().setSelected(collection);
1002+
1003+
final FeatureFlagSet enabledFeatures = this.<MinecraftServer>getMinecraftServer().getWorldData().getDataConfiguration().enabledFeatures();
9931004

9941005
// this.<MinecraftServer>getMinecraftServer().getSelectedPacks
9951006
Collection<String> selectedIDs = this.<MinecraftServer>getMinecraftServer().getPackRepository()
@@ -1001,11 +1012,11 @@ public final void reloadDataPacks() {
10011012
disabledIDs.removeIf(enabledIDs::contains);
10021013

10031014
this.<MinecraftServer>getMinecraftServer().getWorldData()
1004-
.setDataConfiguration(new WorldDataConfiguration(new DataPackConfig(enabledIDs, disabledIDs),
1005-
this.<MinecraftServer>getMinecraftServer().getWorldData().getDataConfiguration()
1006-
.enabledFeatures()));
1015+
.setDataConfiguration(new WorldDataConfiguration(new DataPackConfig(enabledIDs, disabledIDs), enabledFeatures));
10071016
// this.<MinecraftServer>getMinecraftServer().resources.managers().updateRegistryTags(registryAccess);
1008-
this.<MinecraftServer>getMinecraftServer().resources.managers().updateStaticRegistryTags(); // TODO
1017+
this.<MinecraftServer>getMinecraftServer().resources.managers().updateStaticRegistryTags(); // TODO: Review this
1018+
this.<MinecraftServer>getMinecraftServer().resources.managers().getRecipeManager().finalizeRecipeLoading(enabledFeatures);
1019+
10091020
// May need to be commented out, may not. Comment it out just in case.
10101021
// For some reason, calling getPlayerList().saveAll() may just hang
10111022
// the server indefinitely. Not sure why!
@@ -1014,6 +1025,13 @@ public final void reloadDataPacks() {
10141025
// this.<MinecraftServer>getMinecraftServer().getFunctions().replaceLibrary(this.<MinecraftServer>getMinecraftServer().resources.managers().getFunctionLibrary());
10151026
this.<MinecraftServer>getMinecraftServer().getStructureManager()
10161027
.onResourceManagerReload(this.<MinecraftServer>getMinecraftServer().resources.resourceManager());
1028+
1029+
// Set fuel values with the new loaded fuel values from the list of enabled features
1030+
minecraftServerFuelValues.set(this.<MinecraftServer>getMinecraftServer(),
1031+
FuelValues.vanillaBurnTimes(this.<MinecraftServer>getMinecraftServer().registries().compositeAccess(),
1032+
enabledFeatures
1033+
)
1034+
);
10171035
});
10181036

10191037
// Step 4: Block the thread until everything's done

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.21.2/src/test/java/SafeReflect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import dev.jorel.commandapi.preprocessor.RequireField;
2929
import net.minecraft.commands.arguments.selector.EntitySelector;
3030
import net.minecraft.server.CustomFunctionManager;
31+
import net.minecraft.server.MinecraftServer;
32+
import net.minecraft.world.level.block.entity.FuelValues;
3133
import net.minecraft.world.level.gameevent.EntityPositionSource;
3234

3335
// Spigot-Mapped reflection
3436
@RequireField(in = CustomFunctionManager.class, name = "h", ofType = CommandDispatcher.class)
3537
@RequireField(in = EntitySelector.class, name = "p", ofType = boolean.class)
3638
@RequireField(in = SimpleHelpMap.class, name = "helpTopics", ofType = Map.class)
3739
@RequireField(in = EntityPositionSource.class, name = "e", ofType = Either.class)
40+
@RequireField(in = MinecraftServer.class, name = "fuelValues", ofType = FuelValues.class)
3841
public class SafeReflect {}

0 commit comments

Comments
 (0)