Skip to content

Commit

Permalink
Re-added JEI integration, removed JEIEvents, improved highlight shade…
Browse files Browse the repository at this point in the history
…r, started working on server-sided RecipeViewerEvents
  • Loading branch information
LatvianModder committed Jun 20, 2024
1 parent c70d756 commit 49cf941
Show file tree
Hide file tree
Showing 22 changed files with 348 additions and 97 deletions.
4 changes: 4 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/bindings/TextIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ static MutableComponent patchedComponent() {
return icon(Component.literal("Q"));
}

static MutableComponent error() {
return icon(Component.literal("R"));
}

static MutableComponent tag() {
return icon(Component.literal("T"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.net.SendDataFromServerPayload;
import dev.latvian.mods.kubejs.net.SyncRecipeViewerDataPayload;
import dev.latvian.mods.kubejs.player.EntityArrayList;
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.kubejs.util.ConsoleJS;
Expand All @@ -22,6 +23,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

@RemapPrefixForJS("kjs$")
Expand Down Expand Up @@ -110,6 +112,11 @@ public interface MinecraftServerKJS extends WithAttachedData<MinecraftServer>, W
}
}

if (reload) {
var manager = kjs$self().getServerResources().managers().kjs$getServerScriptManager();
PacketDistributor.sendToAllPlayers(new SyncRecipeViewerDataPayload(Optional.ofNullable(manager.recipeViewerData)));
}

ConsoleJS.SERVER.setCapturingErrors(false);
ConsoleJS.SERVER.info("Server resource reload complete!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.latvian.mods.kubejs.integration.jei;

import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerDataUpdatedEvent;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;

@EventBusSubscriber(modid = "jei")
public class JEIIntegrationEventHandler {
public static RecipeViewerData remote = null;

@SubscribeEvent
public static void loadRemote(RecipeViewerDataUpdatedEvent event) {
remote = event.data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public void onRuntimeAvailable(IJeiRuntime runtime) {
RecipeViewerEvents.ADD_ENTRIES.post(ScriptType.CLIENT, type, new JEIAddEntriesKubeEvent(runtime, type, ingredientType));
}
}

/*
var remote = JEIIntegrationEventHandler.remote;
if (remote != null) {
var idSet = Set.of(remote.removedCategories());
runtime.getRecipeManager().createRecipeCategoryLookup().get()
.map(IRecipeCategory::getRecipeType)
.filter(type -> idSet.contains(type.getUid()))
.forEach(runtime.getRecipeManager()::hideRecipeCategory);
}
*/
}

@Override
Expand All @@ -71,59 +84,4 @@ public <T> void registerFluidSubtypes(ISubtypeRegistration registration, IPlatfo
RecipeViewerEvents.REGISTER_SUBTYPES.post(ScriptType.CLIENT, RecipeViewerEntryType.FLUID, new JEIRegisterSubtypesKubeEvent(RecipeViewerEntryType.FLUID, NeoForgeTypes.FLUID_STACK, registration));
}
}

/*@Override
public void onRuntimeAvailable(IJeiRuntime r) {
runtime = r;
BuiltinKubeJSPlugin.GLOBAL.put("jeiRuntime", runtime);
if (JEIEvents.HIDE_ITEMS.hasListeners()) {
JEIEvents.HIDE_ITEMS.post(ScriptType.CLIENT, new HideJEIEventJS<>(runtime, VanillaTypes.ITEM_STACK, IngredientJS::of, stack -> !stack.isEmpty()));
}
if (JEIEvents.HIDE_FLUIDS.hasListeners()) {
JEIEvents.HIDE_FLUIDS.post(ScriptType.CLIENT, new HideJEIEventJS<>(runtime, ForgeTypes.FLUID_STACK, object -> {
var fs = FluidStackJS.of(object);
return fluidStack -> fluidStack.getFluid().isSame(fs.getFluid()) && Objects.equals(fluidStack.getTag(), fs.getNbt());
}, stack -> !stack.isEmpty()));
}
if (JEIEvents.HIDE_CUSTOM.hasListeners()) {
JEIEvents.HIDE_CUSTOM.post(ScriptType.CLIENT, new HideCustomJEIEventJS(runtime));
}
if (JEIEvents.REMOVE_CATEGORIES.hasListeners()) {
JEIEvents.REMOVE_CATEGORIES.post(ScriptType.CLIENT, new RemoveJEICategoriesEvent(runtime));
}
if (JEIEvents.REMOVE_RECIPES.hasListeners()) {
JEIEvents.REMOVE_RECIPES.post(ScriptType.CLIENT, new RemoveJEIRecipesEvent(runtime));
}
if (JEIEvents.ADD_ITEMS.hasListeners()) {
JEIEvents.ADD_ITEMS.post(ScriptType.CLIENT, new AddJEIEventJS<>(runtime, VanillaTypes.ITEM_STACK, ItemStackJS::of, stack -> !stack.isEmpty()));
}
if (JEIEvents.ADD_FLUIDS.hasListeners()) {
JEIEvents.ADD_FLUIDS.post(ScriptType.CLIENT, new AddJEIEventJS<>(runtime, ForgeTypes.FLUID_STACK, object -> fromArchitectury(FluidStackJS.of(object).getFluidStack()), stack -> !stack.isEmpty()));
}
}
public static FluidStack fromArchitectury(dev.architectury.fluid.FluidStack stack) {
return new FluidStack(stack.getFluid(), (int) stack.getAmount(), stack.getTag());
}
@Override
public void registerItemSubtypes(ISubtypeRegistration registration) {
if (JEIEvents.SUBTYPES.hasListeners()) {
JEIEvents.SUBTYPES.post(ScriptType.CLIENT, new JEISubtypesEventJS(registration));
}
}
@Override
public void registerRecipes(IRecipeRegistration registration) {
if (JEIEvents.INFORMATION.hasListeners()) {
JEIEvents.INFORMATION.post(ScriptType.CLIENT, new InformationJEIEventJS(registration));
}
}*/
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package dev.latvian.mods.kubejs.net;

import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerDataSyncedEvent;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerDataUpdatedEvent;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.handling.IPayloadContext;

public record SyncRecipeViewerDataPayload(RecipeViewerData data) implements CustomPacketPayload {
public static final StreamCodec<RegistryFriendlyByteBuf, SyncRecipeViewerDataPayload> STREAM_CODEC = RecipeViewerData.STREAM_CODEC.map(SyncRecipeViewerDataPayload::new, SyncRecipeViewerDataPayload::data);
import java.util.Optional;

public record SyncRecipeViewerDataPayload(Optional<RecipeViewerData> data) implements CustomPacketPayload {
public static final StreamCodec<RegistryFriendlyByteBuf, SyncRecipeViewerDataPayload> STREAM_CODEC = ByteBufCodecs.optional(RecipeViewerData.STREAM_CODEC).map(SyncRecipeViewerDataPayload::new, SyncRecipeViewerDataPayload::data);

@Override
public Type<?> type() {
return KubeJSNet.SYNC_RECIPE_VIEWER;
}

public void handle(IPayloadContext ctx) {
ctx.enqueueWork(() -> NeoForge.EVENT_BUS.post(new RecipeViewerDataSyncedEvent(data)));
ctx.enqueueWork(() -> NeoForge.EVENT_BUS.post(new RecipeViewerDataUpdatedEvent(data.orElse(null))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.bindings.event.PlayerEvents;
import dev.latvian.mods.kubejs.net.SyncRecipeViewerDataPayload;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import net.minecraft.resources.ResourceKey;
Expand All @@ -18,6 +19,9 @@
import net.neoforged.neoforge.event.entity.player.PlayerContainerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.tick.PlayerTickEvent;
import net.neoforged.neoforge.network.PacketDistributor;

import java.util.Optional;

@EventBusSubscriber(modid = KubeJS.MOD_ID)
public class KubeJSPlayerEventHandler {
Expand All @@ -33,6 +37,10 @@ public static void loggedIn(PlayerEvent.PlayerLoggedInEvent event) {

player.kjs$getStages().sync();
}

if (event.getEntity() instanceof ServerPlayer player) {
PacketDistributor.sendToPlayer(player, new SyncRecipeViewerDataPayload(Optional.ofNullable(player.server.getServerResources().managers().kjs$getServerScriptManager().recipeViewerData)));
}
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.recipe.viewer;

import dev.latvian.mods.kubejs.core.FluidKJS;
import dev.latvian.mods.kubejs.event.Extra;
import dev.latvian.mods.kubejs.fluid.FluidWrapper;
import dev.latvian.mods.kubejs.item.ItemPredicate;
Expand All @@ -10,20 +11,34 @@
import dev.latvian.mods.kubejs.util.Lazy;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
* Note: predicateType has to be able to be cast to {@link java.util.function.Predicate} of entryType
*/
public class RecipeViewerEntryType {
public static final RecipeViewerEntryType ITEM = new RecipeViewerEntryType("item", ItemStackJS.TYPE_INFO, ItemPredicate.TYPE_INFO, ItemStackJS.ITEM_TYPE_INFO) {
public record Component<T>(TypeInfo type, StreamCodec<?, T> streamCodec, Predicate<T> empty) {
}

public static final RecipeViewerEntryType ITEM = new RecipeViewerEntryType("item",
new Component<>(ItemStackJS.TYPE_INFO, ItemStack.STREAM_CODEC, ItemStack::isEmpty),
new Component<>(ItemPredicate.TYPE_INFO, Ingredient.CONTENTS_STREAM_CODEC, Ingredient::isEmpty),
new Component<>(ItemStackJS.ITEM_TYPE_INFO, ByteBufCodecs.registry(Registries.ITEM), i -> i == Items.AIR)
) {
@Override
public Object wrapEntry(Context cx, Object from) {
return ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from);
Expand All @@ -40,7 +55,11 @@ public Object getBase(Object from) {
}
};

public static final RecipeViewerEntryType FLUID = new RecipeViewerEntryType("fluid", FluidWrapper.TYPE_INFO, FluidWrapper.INGREDIENT_TYPE_INFO, FluidWrapper.FLUID_TYPE_INFO) {
public static final RecipeViewerEntryType FLUID = new RecipeViewerEntryType("fluid",
new Component<>(FluidWrapper.TYPE_INFO, FluidStack.STREAM_CODEC, FluidStack::isEmpty),
new Component<>(FluidWrapper.INGREDIENT_TYPE_INFO, FluidIngredient.STREAM_CODEC, FluidIngredient::isEmpty),
new Component<>(FluidWrapper.FLUID_TYPE_INFO, ByteBufCodecs.registry(Registries.FLUID), FluidKJS::kjs$isEmpty)
) {
@Override
public Object wrapEntry(Context cx, Object from) {
return FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), from);
Expand Down Expand Up @@ -84,27 +103,23 @@ public static RecipeViewerEntryType fromString(@Nullable Object id) {
public static final Extra<RecipeViewerEntryType> EXTRA = Extra.create(RecipeViewerEntryType.class).transformer(RecipeViewerEntryType::fromString).identity();

public final String id;
public final TypeInfo entryType;
public final TypeInfo predicateType;
public final TypeInfo baseClass;
public final Component<?> entryType;
public final Component<?> predicateType;
public final Component<?> baseClass;

public RecipeViewerEntryType(String id, TypeInfo entryType, TypeInfo predicateType, TypeInfo baseClass) {
public RecipeViewerEntryType(String id, Component<?> entryType, Component<?> predicateType, @Nullable Component<?> baseClass) {
this.id = id;
this.entryType = entryType;
this.predicateType = predicateType;
this.baseClass = baseClass;
}

public RecipeViewerEntryType(String id, TypeInfo entryType, TypeInfo predicateType) {
this(id, entryType, predicateType, TypeInfo.NONE);
}

public Object wrapEntry(Context cx, Object from) {
return cx.jsToJava(from, entryType);
return cx.jsToJava(from, entryType.type);
}

public Object wrapPredicate(Context cx, Object from) {
return cx.jsToJava(from, predicateType);
return cx.jsToJava(from, predicateType.type);
}

public Object getBase(Object from) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public record CategoryData(
Expand All @@ -17,7 +19,11 @@ public record CategoryData(
CategoryData::new
);

public CategoryData(ResourceLocation category) {
this(category, new ArrayList<>());
}

public CategoryData lock() {
return new CategoryData(category, List.copyOf(removedRecipes));
return new CategoryData(category, List.copyOf(new HashSet<>(removedRecipes)));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.recipe.viewer.server;

import dev.latvian.mods.kubejs.util.MutableBoolean;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
Expand Down Expand Up @@ -49,19 +50,23 @@ public record Info(FluidIngredient ingredient, List<Component> info) {

public static FluidData collect() {
var addedEntries = new ArrayList<FluidStack>();
var removeAll = false;
var removeAll = new MutableBoolean(false);
var removedEntries = new ArrayList<FluidIngredient>();
var directlyRemovedEntries = new ArrayList<FluidIngredient>();
var groupedEntries = new ArrayList<Group>();
var info = new ArrayList<Info>();

return new FluidData(
List.copyOf(addedEntries),
removeAll,
removeAll.value,
List.copyOf(removedEntries),
List.copyOf(directlyRemovedEntries),
List.copyOf(groupedEntries),
List.copyOf(info)
);
}

public boolean isEmpty() {
return addedEntries.isEmpty() && !removeAll && removedEntries.isEmpty() && directlyRemovedEntries.isEmpty() && groupedEntries.isEmpty() && info.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dev.latvian.mods.kubejs.recipe.viewer.server;

import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEvents;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.MutableBoolean;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
Expand Down Expand Up @@ -49,19 +53,39 @@ public record Info(Ingredient filter, List<Component> info) {

public static ItemData collect() {
var addedEntries = new ArrayList<ItemStack>();
var removeAll = false;
var removeAll = new MutableBoolean(false);
var removedEntries = new ArrayList<Ingredient>();
var directlyRemovedEntries = new ArrayList<Ingredient>();
var groupedEntries = new ArrayList<Group>();
var info = new ArrayList<Info>();

if (RecipeViewerEvents.ADD_ENTRIES.hasListeners(RecipeViewerEntryType.ITEM)) {
RecipeViewerEvents.ADD_ENTRIES.post(ScriptType.SERVER, RecipeViewerEntryType.ITEM, new ServerAddItemEntriesKubeEvent(addedEntries));
}

if (RecipeViewerEvents.REMOVE_ENTRIES.hasListeners(RecipeViewerEntryType.ITEM)) {
RecipeViewerEvents.REMOVE_ENTRIES.post(ScriptType.SERVER, RecipeViewerEntryType.ITEM, new ServerRemoveItemEntriesKubeEvent(removedEntries, directlyRemovedEntries, removeAll));
}

if (RecipeViewerEvents.GROUP_ENTRIES.hasListeners(RecipeViewerEntryType.ITEM)) {
RecipeViewerEvents.GROUP_ENTRIES.post(ScriptType.SERVER, RecipeViewerEntryType.ITEM, new ServerGroupItemEntriesKubeEvent(groupedEntries));
}

if (RecipeViewerEvents.ADD_INFORMATION.hasListeners(RecipeViewerEntryType.ITEM)) {
RecipeViewerEvents.ADD_INFORMATION.post(ScriptType.SERVER, RecipeViewerEntryType.ITEM, new ServerAddItemInformationKubeEvent(info));
}

return new ItemData(
List.copyOf(addedEntries),
removeAll,
removeAll.value,
List.copyOf(removedEntries),
List.copyOf(directlyRemovedEntries),
List.copyOf(groupedEntries),
List.copyOf(info)
);
}

public boolean isEmpty() {
return addedEntries.isEmpty() && !removeAll && removedEntries.isEmpty() && directlyRemovedEntries.isEmpty() && groupedEntries.isEmpty() && info.isEmpty();
}
}
Loading

0 comments on commit 49cf941

Please sign in to comment.