Skip to content

Commit

Permalink
Fixed a few small bugs, added EMI info integration
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 27, 2024
1 parent f3e014f commit cb75f61
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 76 deletions.
5 changes: 3 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static IntProvider intProviderOf(Context cx, Object o) {
}
}

var decoded = IntProvider.CODEC.parse(((KubeJSContext) cx).getNbtOps(), NBTUtils.toTagCompound(cx, m)).result();
var decoded = IntProvider.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), NBTUtils.toTagCompound(cx, m)).result();
if (decoded.isPresent()) {
return decoded.get();
}
Expand Down Expand Up @@ -90,7 +90,8 @@ static FloatProvider floatProviderOf(Context cx, Object o) {
}
}

var decoded = FloatProvider.CODEC.parse(((KubeJSContext) cx).getNbtOps(), NBTUtils.toTagCompound(cx, m)).result();
var decoded = FloatProvider.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), NBTUtils.toTagCompound(cx, m)).result();

if (decoded.isPresent()) {
return decoded.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static RuleTest ruleTestOf(Context cx, Object o) {
}

return Optional.ofNullable(NBTUtils.toTagCompound(cx, o))
.map(tag -> RuleTest.CODEC.parse(((KubeJSContext) cx).getNbtOps(), tag))
.map(tag -> RuleTest.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), tag))
.flatMap(DataResult::result)
.or(() -> Optional.ofNullable(of(o).asRuleTest()))
.orElseThrow(() -> new IllegalArgumentException("Could not parse valid rule test from " + o + "!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public Model model(String s) {
return this;
}

public Model x(int _x) {
x = _x;
public Model x(int x) {
this.x = x;
return this;
}

public Model y(int _y) {
y = _y;
public Model y(int y) {
this.y = y;
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) {
}

default String kjs$getComponentString(KubeJSContext cx) {
return DataComponentWrapper.patchToString(new StringBuilder(), cx.getNbtOps(), kjs$self().getComponentsPatch()).toString();
return DataComponentWrapper.patchToString(new StringBuilder(), cx.getRegistries().nbt(), kjs$self().getComponentsPatch()).toString();
}

@ReturnsSelf(copy = true)
Expand Down Expand Up @@ -195,11 +195,11 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) {

@Override
default String toStringJS(Context cx) {
return kjs$toItemString0(((KubeJSContext) cx).getNbtOps());
return kjs$toItemString0(((KubeJSContext) cx).getRegistries().nbt());
}

default String kjs$toItemString(KubeJSContext cx) {
return kjs$toItemString0(cx.getNbtOps());
return kjs$toItemString0(cx.getRegistries().nbt());
}

default String kjs$toItemString0(DynamicOps<Tag> dynamicOps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.recipe.EmiInfoRecipe;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.latvian.mods.kubejs.recipe.viewer.AddInformationKubeEvent;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.rhino.Context;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;

import java.util.List;

public class EMIAddInformationKubeEvent implements AddInformationKubeEvent {
private final RecipeViewerEntryType type;
private final EmiRegistry registry;

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

@Override
public void add(Context cx, Object filter, List<Component> info) {
var in = type.wrapPredicate(cx, filter);

if (type == RecipeViewerEntryType.ITEM) {
registry.addRecipe(new EmiInfoRecipe(List.of(EmiIngredient.of((Ingredient) in)), info, null));
} else if (type == RecipeViewerEntryType.FLUID) {
registry.addRecipe(new EmiInfoRecipe(List.of(EMIIntegration.fluidIngredient((FluidIngredient) in)), info, null));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.integration.emi;

import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.latvian.mods.kubejs.item.ItemPredicate;
import net.neoforged.neoforge.fluids.FluidStack;
Expand All @@ -14,6 +15,10 @@ public static EmiStack fluid(FluidStack stack) {
return EmiStack.of(stack.getFluid(), stack.getComponentsPatch(), stack.getAmount());
}

public static EmiIngredient fluidIngredient(FluidIngredient ingredient) {
return EmiIngredient.of(Arrays.stream(ingredient.getStacks()).map(EMIIntegration::fluid).toList());
}

public static Predicate<EmiStack> predicate(ItemPredicate ingredient) {
return emiStack -> {
var is = emiStack.getItemStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import dev.emi.emi.api.EmiEntrypoint;
import dev.emi.emi.api.EmiPlugin;
import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.recipe.EmiInfoRecipe;
import dev.emi.emi.api.stack.EmiIngredient;
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;

import java.util.List;

@EmiEntrypoint
public class KubeJSEMIPlugin implements EmiPlugin {
@Override
Expand Down Expand Up @@ -60,5 +64,21 @@ public void register(EmiRegistry registry) {
registry.addEmiStack(EmiStack.of(stack.getFluid(), stack.getComponentsPatch(), stack.getAmount()));
}
}

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

if (remote != null) {
for (var info : remote.itemData().info()) {
registry.addRecipe(new EmiInfoRecipe(List.of(EmiIngredient.of(info.filter())), info.info(), null));
}

for (var info : remote.fluidData().info()) {
registry.addRecipe(new EmiInfoRecipe(List.of(EMIIntegration.fluidIngredient(info.filter())), info.info(), null));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.network.chat.Component;

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

public class JEIAddInformationKubeEvent implements AddInformationKubeEvent {
Expand All @@ -23,17 +24,19 @@ public JEIAddInformationKubeEvent(RecipeViewerEntryType type, IIngredientType<?>
}

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

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

var infoArr = info.toArray(new Component[0]);

for (var v : allIngredients) {
if (in.test(v)) {
registration.addIngredientInfo(v, ingredientType, info);
registration.addIngredientInfo(v, ingredientType, infoArr);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import me.shedaniel.rei.plugin.client.BuiltinClientPlugin;
import net.minecraft.network.chat.Component;

import java.util.List;

public class REIAddInformationKubeEvent implements AddInformationKubeEvent {
private final RecipeViewerEntryType type;

Expand All @@ -14,8 +16,8 @@ public REIAddInformationKubeEvent(RecipeViewerEntryType type) {
}

@Override
public void add(Context cx, Object filter, Component[] info) {
if (info.length == 0) {
public void add(Context cx, Object filter, List<Component> info) {
if (info.isEmpty()) {
return;
}

Expand All @@ -25,10 +27,9 @@ public void add(Context cx, Object filter, Component[] info) {
return;
}

BuiltinClientPlugin.getInstance().registerInformation(in, info[0], components -> {
//noinspection ManualArrayToCollectionCopy
for (int i = 1; i < info.length; i++) {
components.add(info[i]);
BuiltinClientPlugin.getInstance().registerInformation(in, info.getFirst(), components -> {
for (int i = 1; i < info.size(); i++) {
components.add(info.get(i));
}

return components;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/level/ExplosionJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class ExplosionJS {
public boolean causesFire;
public Level.ExplosionInteraction explosionMode;

public ExplosionJS(LevelAccessor l, double _x, double _y, double _z) {
level = l;
x = _x;
y = _y;
z = _z;
exploder = null;
strength = 3F;
causesFire = false;
explosionMode = Level.ExplosionInteraction.NONE;
public ExplosionJS(LevelAccessor level, double x, double y, double z) {
this.level = level;
this.x = x;
this.y = y;
this.z = z;
this.exploder = null;
this.strength = 3F;
this.causesFire = false;
this.explosionMode = Level.ExplosionInteraction.NONE;
}

public ExplosionJS exploder(Entity entity) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public final void save() {
changed = true;
}

public KubeRecipe id(ResourceLocation _id) {
id = _id;
public KubeRecipe id(ResourceLocation id) {
this.id = id;
save();
return this;
}
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -126,7 +125,6 @@ private String recipeToString(Recipe<?> recipe) {
public final Collection<KubeRecipe> addedRecipes;
private final BinaryOperator<RecipeHolder<?>> mergeOriginal, mergeAdded;

private ForkJoinPool parallelThreadPool;
public final AtomicInteger failedCount;
public final Map<ResourceLocation, KubeRecipe> takenIds;

Expand Down Expand Up @@ -167,7 +165,6 @@ public RecipesKubeEvent(ServerScriptManager manager) {
return b;
};

this.parallelThreadPool = null;
this.failedCount = new AtomicInteger(0);

for (var namespace : recipeSchemaStorage.namespaces.values()) {
Expand Down Expand Up @@ -372,15 +369,6 @@ public void post(RecipeManagerKJS recipeManager, Map<ResourceLocation, JsonEleme
ConsoleJS.SERVER.info(r.getOrCreateId() + ": " + r.json);
}
}

if (parallelThreadPool != null) {
try {
parallelThreadPool.shutdown();
parallelThreadPool = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

@Nullable
Expand Down Expand Up @@ -525,8 +513,6 @@ public void replaceOutput(Context cx, RecipeFilter filter, ReplacementMatchInfo
var dstring = (DevProperties.get().logModifiedRecipes || ConsoleJS.SERVER.shouldPrintDebug()) ? (": OUT " + match + " -> " + with) : "";

forEachRecipeAsync(cx, filter, r -> {
ConsoleJS.SERVER.info("Thread of " + r.id + ": " + Thread.currentThread().getName());

if (r.replaceOutput(cx, match, with)) {
if (DevProperties.get().logModifiedRecipes) {
ConsoleJS.SERVER.info("~ " + r + dstring);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import dev.latvian.mods.rhino.Context;
import net.minecraft.network.chat.Component;

import java.util.List;

public interface AddInformationKubeEvent extends KubeEvent {
void add(Context cx, Object filter, Component[] info);
void add(Context cx, Object filter, List<Component> info);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.latvian.mods.rhino.Context;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
import java.util.List;

public class ServerAddFluidInformationKubeEvent implements AddInformationKubeEvent {
Expand All @@ -17,7 +16,7 @@ public ServerAddFluidInformationKubeEvent(List<FluidData.Info> list) {
}

@Override
public void add(Context cx, Object filter, Component[] info) {
list.add(new FluidData.Info(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter), Arrays.asList(info)));
public void add(Context cx, Object filter, List<Component> info) {
list.add(new FluidData.Info(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter), info));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.latvian.mods.rhino.Context;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
import java.util.List;

public class ServerAddItemInformationKubeEvent implements AddInformationKubeEvent {
Expand All @@ -17,7 +16,7 @@ public ServerAddItemInformationKubeEvent(List<ItemData.Info> list) {
}

@Override
public void add(Context cx, Object filter, Component[] info) {
list.add(new ItemData.Info(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter), Arrays.asList(info)));
public void add(Context cx, Object filter, List<Component> info) {
list.add(new ItemData.Info(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter), info));
}
}
Loading

0 comments on commit cb75f61

Please sign in to comment.