Skip to content

Commit

Permalink
Fixed server script load order. Still need to fix tags in recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 18, 2024
1 parent fd04041 commit 62a378e
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistry;
import dev.latvian.mods.kubejs.recipe.special.SpecialRecipeSerializerManager;
import dev.latvian.mods.kubejs.script.data.DataPackKubeEvent;
import dev.latvian.mods.kubejs.server.BasicCommandKubeEvent;
import dev.latvian.mods.kubejs.server.CommandKubeEvent;
import dev.latvian.mods.kubejs.server.CustomCommandKubeEvent;
import dev.latvian.mods.kubejs.server.ServerKubeEvent;
import dev.latvian.mods.kubejs.server.tag.TagKubeEvent;
import net.minecraft.core.Registry;
Expand All @@ -32,7 +32,7 @@ public interface ServerEvents {
SpecializedEventHandler<ResourceKey<Registry<?>>> TAGS = GROUP.server("tags", Extra.REGISTRY, () -> TagKubeEvent.class).required().exceptionHandler(TagKubeEvent.TAG_EVENT_HANDLER);
EventHandler COMMAND_REGISTRY = GROUP.server("commandRegistry", () -> CommandRegistryKubeEvent.class);
SpecializedEventHandler<String> COMMAND = GROUP.server("command", Extra.STRING, () -> CommandKubeEvent.class).hasResult();
SpecializedEventHandler<String> CUSTOM_COMMAND = GROUP.server("customCommand", Extra.STRING, () -> CustomCommandKubeEvent.class).hasResult();
SpecializedEventHandler<String> BASIC_COMMAND = GROUP.server("basicCommand", Extra.STRING, () -> BasicCommandKubeEvent.class).hasResult().required();
EventHandler RECIPE_MAPPING_REGISTRY = GROUP.server("recipeMappingRegistry", () -> RecipeMappingRegistry.class);
EventHandler RECIPE_SCHEMA_REGISTRY = GROUP.server("recipeSchemaRegistry", () -> RecipeSchemaRegistry.class);
EventHandler RECIPES = GROUP.server("recipes", () -> RecipesKubeEvent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void loadPostChains(Minecraft mc) {

public void tickPre(Minecraft mc) {
boolean prevKeyDown = actualKey;
actualKey = mc.level != null && mc.player != null && keyMapping != null && !mc.isPaused() && mc.kjs$isKeyMappingDown(keyMapping);
actualKey = mc.level != null && mc.player != null && keyMapping != null && !mc.isPaused() && mc.player.hasPermissions(2) && mc.kjs$isKeyMappingDown(keyMapping);

while (actualKey && mode != Mode.NONE && mc.options.keyInventory.consumeClick()) {
keyToggled(mc, Mode.NONE, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.ExportablePackResources;
import dev.latvian.mods.kubejs.server.CustomCommandKubeEvent;
import dev.latvian.mods.kubejs.server.BasicCommandKubeEvent;
import dev.latvian.mods.kubejs.server.DataExport;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -232,7 +232,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
var cmd1 = dispatcher.register(cmd);
dispatcher.register(Commands.literal("kjs").redirect(cmd1));

for (var id : ServerEvents.CUSTOM_COMMAND.findUniqueExtraIds(ScriptType.SERVER)) {
for (var id : ServerEvents.BASIC_COMMAND.findUniqueExtraIds(ScriptType.SERVER)) {
dispatcher.register(Commands.literal(id)
.requires(spOrOP)
.executes(ctx -> customCommand(ctx.getSource(), id, ""))
Expand Down Expand Up @@ -265,8 +265,8 @@ private static int help(CommandSourceStack source) {
}

private static int customCommand(CommandSourceStack source, String id, String input) {
if (ServerEvents.CUSTOM_COMMAND.hasListeners(id)) {
var result = ServerEvents.CUSTOM_COMMAND.post(new CustomCommandKubeEvent(source.getLevel(), source.getEntity(), BlockPos.containing(source.getPosition()), id, input.trim()), id);
if (ServerEvents.BASIC_COMMAND.hasListeners(id)) {
var result = ServerEvents.BASIC_COMMAND.post(new BasicCommandKubeEvent(source.getLevel(), source.getEntity(), BlockPos.containing(source.getPosition()), id, input.trim()), id);

if (result.type() == EventResult.Type.ERROR) {
source.sendFailure(Component.literal(result.value().toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe
});
}
}

default void kjs$heal() {
kjs$self().heal(kjs$self().getMaxHealth());
kjs$self().getFoodData().eat(20, 1F);
}
}
7 changes: 6 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.latvian.mods.kubejs.server.tag.TagKubeEvent;
import dev.latvian.mods.kubejs.server.tag.TagWrapper;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.Lazy;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -22,7 +23,7 @@
import java.util.Map;

public interface TagLoaderKJS<T> {
default void kjs$customTags(Map<ResourceLocation, List<TagLoader.EntryWithSource>> map) {
default void kjs$customTags(ReloadableServerResourcesKJS kjs$resources, Map<ResourceLocation, List<TagLoader.EntryWithSource>> map) {
TagContext.INSTANCE.setValue(TagContext.EMPTY);
var reg = kjs$getRegistry();

Expand Down Expand Up @@ -69,6 +70,8 @@ public interface TagLoaderKJS<T> {
if (event.totalAdded > 0 || event.totalRemoved > 0 || ConsoleJS.SERVER.shouldPrintDebug()) {
ConsoleJS.SERVER.info("[%s] Found %d tags, added %d objects, removed %d objects".formatted(regInfo, event.tags.size(), event.totalAdded, event.totalRemoved));
}

kjs$resources.kjs$getServerScriptManager().loadedTags.put(reg.key(), Lazy.of(() -> (Map) kjs$callBuild(map)));
}

if (DataExport.export != null) {
Expand Down Expand Up @@ -99,4 +102,6 @@ public interface TagLoaderKJS<T> {

@Nullable
Registry<T> kjs$getRegistry();

Map<ResourceLocation, Collection<T>> kjs$callBuild(Map<ResourceLocation, List<TagLoader.EntryWithSource>> map);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.server.ScheduledServerEvent;
import dev.latvian.mods.kubejs.server.ServerKubeEvent;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.kubejs.util.AttachedData;
import dev.latvian.mods.kubejs.util.KubeJSPlugins;
import dev.latvian.mods.kubejs.util.ScheduledEvents;
import dev.latvian.mods.rhino.util.RemapForJS;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.LayeredRegistryAccess;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -64,9 +63,6 @@ public abstract class MinecraftServerMixin implements MinecraftServerKJS {
@Shadow
public abstract RegistryAccess.Frozen registryAccess();

@Shadow
public abstract LayeredRegistryAccess<RegistryLayer> registries();

@Override
public CompoundTag kjs$getPersistentData() {
return kjs$persistentData;
Expand Down Expand Up @@ -134,8 +130,10 @@ public abstract class MinecraftServerMixin implements MinecraftServerKJS {
@RemapForJS("stop")
public abstract void stopServer();

@Shadow
private MinecraftServer.ReloadableResources resources;
@Inject(method = "reloadResources", at = @At("HEAD"))
private void startResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
ServerScriptManager.capture(registryAccess());
}

@Inject(method = "reloadResources", at = @At("TAIL"))
private void kjs$endResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import dev.latvian.mods.kubejs.core.ReloadableServerResourcesKJS;
import dev.latvian.mods.kubejs.item.ingredient.TagContext;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.commands.Commands;
import net.minecraft.core.LayeredRegistryAccess;
import net.minecraft.core.RegistryAccess;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.tags.TagManager;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.item.crafting.RecipeManager;
Expand All @@ -17,6 +19,10 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@Mixin(ReloadableServerResources.class)
public abstract class ReloadableServerResourcesMixin implements ReloadableServerResourcesKJS {
Expand All @@ -33,34 +39,20 @@ public abstract class ReloadableServerResourcesMixin implements ReloadableServer

@Inject(method = "<init>", at = @At("RETURN"))
private void init(RegistryAccess.Frozen registryAccess, FeatureFlagSet featureFlagSet, Commands.CommandSelection commandSelection, int functionCompilationLevel, CallbackInfo ci) {
var registries = new RegistryAccessContainer(registryAccess);
RegistryAccessContainer.current = registries;
kjs$serverScriptManager = new ServerScriptManager((ReloadableServerResources) (Object) this, registries);
kjs$serverScriptManager = ServerScriptManager.release();
tagManager.kjs$setResources(this);
recipes.kjs$setResources(this);
kjs$serverScriptManager.reload();
}

@Inject(method = "updateRegistryTags(Lnet/minecraft/core/RegistryAccess;Lnet/minecraft/tags/TagManager$LoadResult;)V", at = @At("RETURN"))
private static void updateRegistryTags(RegistryAccess registryAccess, TagManager.LoadResult<?> result, CallbackInfo ci) {
TagContext.INSTANCE.setValue(TagContext.usingRegistry(registryAccess));
}

/* FIXME
@Inject(method = "loadResources", at = @At("HEAD"))
private static void injectKubeJSPacks(
ResourceManager manager,
LayeredRegistryAccess<RegistryLayer> registryAccess,
FeatureFlagSet featureFlagSet,
Commands.CommandSelection commandSelection,
int functionCompilationLevel,
Executor loadExecutor,
Executor applyExecutor,
CallbackInfoReturnable<CompletableFuture<ReloadableServerResources>> cir
) {
ServerScriptManager.instance.reload(manager);
private static void injectKubeJSPacks(ResourceManager resourceManager, LayeredRegistryAccess<RegistryLayer> registries, FeatureFlagSet enabledFeatures, Commands.CommandSelection commandSelection, int functionCompilationLevel, Executor backgroundExecutor, Executor gameExecutor, CallbackInfoReturnable<CompletableFuture<ReloadableServerResources>> cir) {
ServerScriptManager.capture(registries.compositeAccess());
}
*/

@Override
public ServerScriptManager kjs$getServerScriptManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -27,7 +29,7 @@ public abstract class TagLoaderMixin<T> implements TagLoaderKJS<T> {

@Inject(method = "load", at = @At("RETURN"))
private void customTags(ResourceManager resourceManager, CallbackInfoReturnable<Map<ResourceLocation, List<TagLoader.EntryWithSource>>> cir) {
kjs$customTags(cir.getReturnValue());
kjs$customTags(kjs$resources, cir.getReturnValue());
}

@Override
Expand All @@ -46,4 +48,8 @@ private void customTags(ResourceManager resourceManager, CallbackInfoReturnable<
public Registry<T> kjs$getRegistry() {
return kjs$storedRegistry;
}

@Override
@Invoker("build")
public abstract Map<ResourceLocation, Collection<T>> kjs$callBuild(Map<ResourceLocation, List<TagLoader.EntryWithSource>> map);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.latvian.mods.kubejs.integration.jei;

import dev.latvian.mods.kubejs.recipe.viewer.AddInformationKubeEvent;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.rhino.Context;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.registration.IRecipeRegistration;
import net.minecraft.network.chat.Component;

import java.util.Collection;
import java.util.function.Predicate;

public class JEIAddInformationKubeEvent implements AddInformationKubeEvent {
private final RecipeViewerEntryType type;
private final IIngredientType ingredientType;
private final IRecipeRegistration registration;
private Collection allIngredients;

public JEIAddInformationKubeEvent(RecipeViewerEntryType type, IIngredientType<?> ingredientType, IRecipeRegistration registration) {
this.type = type;
this.ingredientType = ingredientType;
this.registration = registration;
}

@Override
public void add(Context cx, Object filter, Component[] info) {
var in = (Predicate) type.wrapPredicate(cx, filter);

if (allIngredients == null) {
var manager = registration.getIngredientManager();
allIngredients = manager.getAllIngredients(ingredientType);
}

for (var v : allIngredients) {
if (in.test(v)) {
registration.addIngredientInfo(v, ingredientType, info);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public interface JEIEvents {
EventHandler REMOVE_RECIPES = GROUP.client("removeRecipes", () -> RemoveJEIRecipesKubeEvent.class);
EventHandler ADD_ITEMS = GROUP.client("addItems", () -> AddJEIKubeEvent.class);
EventHandler ADD_FLUIDS = GROUP.client("addFluids", () -> AddJEIKubeEvent.class);
EventHandler INFORMATION = GROUP.client("information", () -> InformationJEIKubeEvent.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@

import dev.latvian.mods.kubejs.KubeJSPlugin;
import dev.latvian.mods.kubejs.event.EventGroupRegistry;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.neoforge.NeoForgeTypes;
import org.jetbrains.annotations.Nullable;

public class JEIIntegration implements KubeJSPlugin {
@Override
public void registerEvents(EventGroupRegistry registry) {
registry.register(JEIEvents.GROUP);
}

@Nullable
public static IIngredientType<?> typeOf(RecipeViewerEntryType type) {
if (type == RecipeViewerEntryType.ITEM) {
return VanillaTypes.ITEM_STACK;
} else if (type == RecipeViewerEntryType.FLUID) {
return NeoForgeTypes.FLUID_STACK;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dev.latvian.mods.kubejs.integration.jei;

import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEvents;
import dev.latvian.mods.kubejs.script.ScriptType;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.registration.IRecipeRegistration;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.resources.ResourceLocation;

Expand All @@ -16,6 +20,17 @@ public ResourceLocation getPluginUid() {
return ID;
}

@Override
public void registerRecipes(IRecipeRegistration registration) {
for (var type : RecipeViewerEntryType.ALL_TYPES.get()) {
var ingredientType = JEIIntegration.typeOf(type);

if (ingredientType != null && RecipeViewerEvents.ADD_INFORMATION.hasListeners(type)) {
RecipeViewerEvents.ADD_INFORMATION.post(ScriptType.CLIENT, type, new JEIAddInformationKubeEvent(type, ingredientType, registration));
}
}
}

/*@Override
public void onRuntimeAvailable(IJeiRuntime r) {
runtime = r;
Expand Down
Loading

0 comments on commit 62a378e

Please sign in to comment.