Skip to content

Commit

Permalink
Added partial EMI support - adding and removing item and fluid entries
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 21, 2024
1 parent 86d6e0b commit 9581b84
Show file tree
Hide file tree
Showing 24 changed files with 186 additions and 96 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ repositories {

dependencies {
minecraft "com.mojang:minecraft:$minecraft_version"
//mappings loom.officialMojangMappings()
mappings loom.layered() {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.20.6:$parchment_version@zip")
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ modrinth_id=umyGl7zF
minecraft_version=1.21
mod_version=2100.7.0

neoforge_version=21.0.20-beta
parchment_version=2024.06.02
neoforge_version=21.0.21-beta
parchment_version=2024.06.16
rhino_version=2100.2.5-build.29
architectury_version=13.0.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void slot(int x0, int y0, int x1, int y1, Consumer<ChestMenuSlot> slot) {

public void button(int x, int y, ItemStack stack, Component displayName, ChestMenuClickEvent.Callback leftClicked) {
var slot = getSlot(x, y);
slot.setItem(stack.copy().kjs$setCustomName(displayName));
slot.setItem(stack.kjs$withCustomName(displayName));
slot.setLeftClicked(leftClicked);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.stack.EmiStack;
import dev.latvian.mods.kubejs.recipe.viewer.AddEntriesKubeEvent;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.rhino.Context;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.fluids.FluidStack;

public class EMIAddEntriesKubeEvent implements AddEntriesKubeEvent {
private final RecipeViewerEntryType type;
private final EmiRegistry registry;

public EMIAddEntriesKubeEvent(RecipeViewerEntryType type, EmiRegistry registry) {
this.type = type;
this.registry = registry;
}

@Override
public void add(Context cx, Object[] items) {
for (var item : items) {
var entry = type.wrapEntry(cx, item);

if (type == RecipeViewerEntryType.ITEM) {
registry.addEmiStack(EmiStack.of((ItemStack) entry));
} else if (type == RecipeViewerEntryType.FLUID) {
registry.addEmiStack(EMIIntegration.fluid((FluidStack) entry));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.stack.EmiStack;
import dev.latvian.mods.kubejs.item.ItemPredicate;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;

import java.util.Arrays;
import java.util.HashSet;
import java.util.function.Predicate;

public class EMIIntegration {
public static EmiStack fluid(FluidStack stack) {
return EmiStack.of(stack.getFluid(), stack.getComponentsPatch(), stack.getAmount());
}

public static Predicate<EmiStack> predicate(ItemPredicate ingredient) {
return emiStack -> {
var is = emiStack.getItemStack();
return !is.isEmpty() && ingredient.test(is);
};
}

public static Predicate<EmiStack> predicate(FluidIngredient ingredient) {
var set = new HashSet<>(Arrays.stream(ingredient.getStacks()).map(EMIIntegration::fluid).toList());
return set::contains;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.EmiRegistry;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.recipe.viewer.RemoveEntriesKubeEvent;
import dev.latvian.mods.rhino.Context;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;

public class EMIRemoveEntriesKubeEvent implements RemoveEntriesKubeEvent {
private final RecipeViewerEntryType type;
private final EmiRegistry registry;

public EMIRemoveEntriesKubeEvent(RecipeViewerEntryType type, EmiRegistry registry) {
this.type = type;
this.registry = registry;
}

@Override
public void remove(Context cx, Object filter) {
var predicate = type.wrapPredicate(cx, filter);

if (type == RecipeViewerEntryType.ITEM) {
registry.removeEmiStacks(EMIIntegration.predicate((Ingredient) predicate));
} else if (type == RecipeViewerEntryType.FLUID) {
registry.removeEmiStacks(EMIIntegration.predicate((FluidIngredient) predicate));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.EmiEntrypoint;
import dev.emi.emi.api.EmiPlugin;
import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.stack.EmiStack;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEvents;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData;
import dev.latvian.mods.kubejs.script.ScriptType;

@EmiEntrypoint
public class KubeJSEMIPlugin implements EmiPlugin {
@Override
public void register(EmiRegistry registry) {
var remote = RecipeViewerData.remote;

for (var type : RecipeViewerEntryType.ALL_TYPES.get()) {
if (RecipeViewerEvents.REMOVE_ENTRIES.hasListeners(type)) {
RecipeViewerEvents.REMOVE_ENTRIES.post(ScriptType.CLIENT, type, new EMIRemoveEntriesKubeEvent(type, registry));
}

if (RecipeViewerEvents.REMOVE_ENTRIES_COMPLETELY.hasListeners(type)) {
RecipeViewerEvents.REMOVE_ENTRIES_COMPLETELY.post(ScriptType.CLIENT, type, new EMIRemoveEntriesKubeEvent(type, registry));
}
}

if (remote != null) {
for (var ingredient : remote.itemData().removedEntries()) {
registry.removeEmiStacks(EMIIntegration.predicate(ingredient));
}

for (var ingredient : remote.itemData().completelyRemovedEntries()) {
registry.removeEmiStacks(EMIIntegration.predicate(ingredient));
}

for (var ingredient : remote.fluidData().removedEntries()) {
registry.removeEmiStacks(EMIIntegration.predicate(ingredient));
}

for (var ingredient : remote.fluidData().completelyRemovedEntries()) {
registry.removeEmiStacks(EMIIntegration.predicate(ingredient));
}
}

for (var type : RecipeViewerEntryType.ALL_TYPES.get()) {
if (RecipeViewerEvents.ADD_ENTRIES.hasListeners(type)) {
RecipeViewerEvents.ADD_ENTRIES.post(ScriptType.CLIENT, type, new EMIAddEntriesKubeEvent(type, registry));
}
}

if (remote != null) {
for (var stack : remote.itemData().addedEntries()) {
registry.addEmiStack(EmiStack.of(stack));
}

for (var stack : remote.fluidData().addedEntries()) {
registry.addEmiStack(EmiStack.of(stack.getFluid(), stack.getComponentsPatch(), stack.getAmount()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.resources.ResourceLocation;

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

public class JEIRemoveCategoriesKubeEvent implements RemoveCategoriesKubeEvent {
Expand All @@ -29,9 +28,4 @@ public void remove(Context cx, ResourceLocation[] ids) {
}
}
}

@Override
public Collection<ResourceLocation> getCategories() {
return categories.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public JEIRemoveEntriesKubeEvent(IJeiRuntime r, RecipeViewerEntryType type, IIng
this.allIngredients = List.copyOf(runtime.getIngredientManager().getAllIngredients(ingredientType));
}

@Override
public List<Object> getAllEntryValues() {
return allIngredients;
}

@Override
public void remove(Context cx, Object filter) {
var predicate = (Predicate) type.wrapPredicate(cx, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mezz.jei.api.recipe.IRecipeManager;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -30,11 +31,6 @@ public JEIRemoveRecipesKubeEvent(IRecipeManager recipeManager, Map<ResourceLocat
this.removed = new IdentityHashMap<>();
}

@Override
public Collection<ResourceLocation> getCategories() {
return categories.keySet();
}

@Override
public void remove(Context cx, ResourceLocation[] recipesToRemove) {
for (var cat : categories.values()) {
Expand All @@ -43,7 +39,12 @@ public void remove(Context cx, ResourceLocation[] recipesToRemove) {
}

@Override
public void removeFromCategory(Context cx, ResourceLocation category, ResourceLocation[] recipesToRemove) {
public void removeFromCategory(Context cx, @Nullable ResourceLocation category, ResourceLocation[] recipesToRemove) {
if (category == null) {
remove(cx, recipesToRemove);
return;
}

var cat = categories.get(category);

if (cat == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import dev.latvian.mods.kubejs.recipe.viewer.RemoveCategoriesKubeEvent;
import dev.latvian.mods.rhino.Context;
import me.shedaniel.rei.api.client.registry.category.CategoryRegistry;
import me.shedaniel.rei.api.client.registry.category.CategoryRegistry.CategoryConfiguration;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.util.CollectionUtils;
import net.minecraft.resources.ResourceLocation;

import java.util.Collection;
import java.util.Set;

public class REIRemoveCategoriesKubeEvent implements RemoveCategoriesKubeEvent {
Expand All @@ -26,9 +23,4 @@ public void remove(Context cx, ResourceLocation[] categories) {
categoriesRemoved.add(CategoryIdentifier.of(id));
}
}

@Override
public Collection<ResourceLocation> getCategories() {
return CollectionUtils.map(registry, CategoryConfiguration::getIdentifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class REIRemoveEntriesCompletelyKubeEvent implements RemoveEntriesKubeEve
private final RecipeViewerEntryType type;
private final List<EntryStack<?>> allEntries;
private final BasicFilteringRule<?> rule;
private List<Object> allValues;

public REIRemoveEntriesCompletelyKubeEvent(RecipeViewerEntryType type, List<EntryStack<?>> allEntries, BasicFilteringRule<?> rule) {
this.type = type;
Expand All @@ -26,13 +25,4 @@ public void remove(Context cx, Object filter) {
var predicate = (Predicate) type.wrapPredicate(cx, filter);
rule.hide(allEntries.stream().filter(e -> predicate.test(e.getValue())).toList());
}

@Override
public List<Object> getAllEntryValues() {
if (allValues == null) {
allValues = List.copyOf(allEntries.stream().map(EntryStack::getValue).toList());
}

return allValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class REIRemoveEntriesKubeEvent implements RemoveEntriesKubeEvent {
private final RecipeViewerEntryType type;
private final EntryRegistry registry;
private final List<EntryStack<?>> allEntries;
private List<Object> allValues;

public REIRemoveEntriesKubeEvent(RecipeViewerEntryType type, EntryRegistry registry, List<EntryStack<?>> allEntries) {
this.type = type;
Expand All @@ -31,13 +30,4 @@ public void remove(Context cx, Object filter) {
}
}
}

@Override
public List<Object> getAllEntryValues() {
if (allValues == null) {
allValues = List.copyOf(allEntries.stream().map(EntryStack::getValue).toList());
}

return allValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import dev.latvian.mods.rhino.Context;
import me.shedaniel.rei.api.client.registry.category.CategoryRegistry;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.util.CollectionUtils;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashSet;
Expand All @@ -32,7 +32,12 @@ public void remove(Context cx, ResourceLocation[] recipesToRemove) {
}

@Override
public void removeFromCategory(Context cx, ResourceLocation category, ResourceLocation[] recipesToRemove) {
public void removeFromCategory(Context cx, @Nullable ResourceLocation category, ResourceLocation[] recipesToRemove) {
if (category == null) {
remove(cx, recipesToRemove);
return;
}

var catId = CategoryIdentifier.of(category);

if (categories.tryGet(catId).isEmpty()) {
Expand All @@ -42,9 +47,4 @@ public void removeFromCategory(Context cx, ResourceLocation category, ResourceLo

recipesRemoved.computeIfAbsent(catId, _0 -> new HashSet<>()).addAll(List.of(recipesToRemove));
}

@Override
public Collection<ResourceLocation> getCategories() {
return CollectionUtils.map(categories, CategoryRegistry.CategoryConfiguration::getIdentifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void accept(CreativeModeTab.ItemDisplayParameters itemDisplayParameters,
}

if (items.isEmpty()) {
output.accept(Items.PAPER.getDefaultInstance().kjs$setCustomName(Component.literal("Use .content(showRestrictedItems => ['kubejs:example']) to add more items!")));
output.accept((ItemStack) Items.PAPER.getDefaultInstance().kjs$setCustomName(Component.literal("Use .content(showRestrictedItems => ['kubejs:example']) to add more items!")));
} else {
for (var item : items) {
output.accept(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public Type<?> type() {
}

public void handle(IPayloadContext ctx) {
ctx.enqueueWork(() -> NeoForge.EVENT_BUS.post(new RemoteRecipeViewerDataUpdatedEvent(data.orElse(null))));
ctx.enqueueWork(() -> {
RecipeViewerData.remote = data.orElse(null);
NeoForge.EVENT_BUS.post(new RemoteRecipeViewerDataUpdatedEvent(RecipeViewerData.remote));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import dev.latvian.mods.rhino.Context;
import net.minecraft.resources.ResourceLocation;

import java.util.Collection;

public interface RemoveCategoriesKubeEvent extends KubeEvent {
void remove(Context cx, ResourceLocation[] categories);

Collection<ResourceLocation> getCategories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import dev.latvian.mods.kubejs.event.KubeEvent;
import dev.latvian.mods.rhino.Context;

import java.util.List;

public interface RemoveEntriesKubeEvent extends KubeEvent {
void remove(Context cx, Object filter);

List<Object> getAllEntryValues();
}
Loading

0 comments on commit 9581b84

Please sign in to comment.