Skip to content

Commit 2db2b01

Browse files
add wrappers / utils in the style of Item.of for ProcessingOutput
1 parent 2f4ec54 commit 2db2b01

8 files changed

Lines changed: 88 additions & 59 deletions

File tree

src/main/java/dev/latvian/mods/kubejs/create/KubeJSCreatePlugin.java

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.latvian.mods.kubejs.create;
22

3-
import com.google.gson.JsonObject;
43
import com.simibubi.create.Create;
54
import com.simibubi.create.content.processing.recipe.ProcessingOutput;
65
import com.simibubi.create.foundation.fluid.FluidIngredient;
@@ -10,30 +9,20 @@
109
import dev.latvian.mods.kubejs.create.events.SpecialSpoutHandlerEvent;
1110
import dev.latvian.mods.kubejs.create.item.SandpaperItemBuilder;
1211
import dev.latvian.mods.kubejs.create.item.SequencedAssemblyItemBuilder;
13-
import dev.latvian.mods.kubejs.create.platform.FluidIngredientHelper;
1412
import dev.latvian.mods.kubejs.create.recipe.CreateFluidIngredientRecipeComponent;
1513
import dev.latvian.mods.kubejs.create.recipe.CreateRecipeComponents;
1614
import dev.latvian.mods.kubejs.create.recipe.ProcessingOutputRecipeComponent;
15+
import dev.latvian.mods.kubejs.create.wrapper.FluidIngredientWrapper;
16+
import dev.latvian.mods.kubejs.create.wrapper.KubeCreateOutput;
1717
import dev.latvian.mods.kubejs.event.EventGroupRegistry;
1818
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
19-
import dev.latvian.mods.kubejs.plugin.builtin.wrapper.ItemWrapper;
20-
import dev.latvian.mods.kubejs.plugin.builtin.wrapper.StringUtilsWrapper;
2119
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentTypeRegistry;
2220
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistry;
2321
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
2422
import dev.latvian.mods.kubejs.script.BindingRegistry;
2523
import dev.latvian.mods.kubejs.script.ScriptType;
2624
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
27-
import dev.latvian.mods.rhino.Context;
2825
import net.minecraft.core.registries.Registries;
29-
import net.minecraft.util.Mth;
30-
import net.minecraft.world.item.ItemStack;
31-
import net.minecraft.world.item.Items;
32-
import net.minecraft.world.level.ItemLike;
33-
import org.jetbrains.annotations.Nullable;
34-
35-
import java.util.Map;
36-
import java.util.function.Function;
3726

3827
public class KubeJSCreatePlugin implements KubeJSPlugin {
3928
@Override
@@ -58,37 +47,15 @@ public void registerEvents(EventGroupRegistry registry) {
5847

5948
@Override
6049
public void registerBindings(BindingRegistry bindings) {
50+
bindings.add("CreateItem", KubeCreateOutput.class);
51+
// this implements KubeCreateOutput through a (mildly cursed) mixin
6152
bindings.add("CreateProcessingOutput", ProcessingOutput.class);
6253
}
6354

6455
@Override
6556
public void registerTypeWrappers(TypeWrapperRegistry registry) {
66-
registry.register(FluidIngredient.class, FluidIngredientHelper::wrap);
67-
registry.register(ProcessingOutput.class, KubeJSCreatePlugin::wrapProcessingOutput);
68-
}
69-
70-
private static ProcessingOutput fromMapLike(Context cx, Object from, Function<String, Object> getter, boolean nested) {
71-
var chance = (float) Mth.clamp(StringUtilsWrapper.parseDouble(getter.apply("chance"), 1.0), 0.0, 1.0);
72-
if (nested) {
73-
var output = ItemWrapper.wrap(cx, getter.apply("output"));
74-
return new ProcessingOutput(output, chance);
75-
} else {
76-
return new ProcessingOutput(ItemWrapper.wrap(cx, from), chance);
77-
}
78-
}
79-
80-
private static ProcessingOutput wrapProcessingOutput(Context cx, @Nullable Object from) {
81-
return switch (from) {
82-
case null -> ProcessingOutput.EMPTY;
83-
case ProcessingOutput id -> id;
84-
case ItemStack s -> s.isEmpty() ? ProcessingOutput.EMPTY : new ProcessingOutput(s, 1F);
85-
case ItemLike i when i.asItem() == Items.AIR -> ProcessingOutput.EMPTY;
86-
case ItemLike i -> new ProcessingOutput(i.asItem(), 1, 1F);
87-
case JsonObject json when json.has("chance") -> fromMapLike(cx, json, json::get, json.has("output"));
88-
case Map<?, ?> map when map.containsKey("chance") -> fromMapLike(cx, map, map::get, map.containsKey("output"));
89-
// TODO: maybe a custom string-like type wrapper ("2x apple @ 20%" or something like that???)
90-
default -> new ProcessingOutput(ItemWrapper.wrap(cx, from), 1F);
91-
};
57+
registry.register(FluidIngredient.class, FluidIngredientWrapper::wrap);
58+
registry.register(ProcessingOutput.class, KubeCreateOutput::wrapProcessingOutput);
9259
}
9360

9461
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.latvian.mods.kubejs.create.core.mixin;
2+
3+
import com.simibubi.create.content.processing.recipe.ProcessingOutput;
4+
import dev.latvian.mods.kubejs.create.wrapper.KubeCreateOutput;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
7+
@Mixin(ProcessingOutput.class)
8+
public class ProcessingOutputMixin implements KubeCreateOutput {
9+
}

src/main/java/dev/latvian/mods/kubejs/create/events/SpecialFluidHandlerEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.simibubi.create.api.effect.OpenPipeEffectHandler;
44
import com.simibubi.create.foundation.fluid.FluidIngredient;
5-
import dev.latvian.mods.kubejs.create.platform.FluidIngredientHelper;
5+
import dev.latvian.mods.kubejs.create.wrapper.FluidIngredientWrapper;
66
import dev.latvian.mods.kubejs.event.KubeEvent;
77
import net.minecraft.world.level.Level;
88
import net.minecraft.world.phys.AABB;
@@ -14,6 +14,6 @@ public interface PipeHandler {
1414
}
1515

1616
public void add(FluidIngredient fluidIngredient, PipeHandler handler) {
17-
OpenPipeEffectHandler.REGISTRY.registerProvider(FluidIngredientHelper.createEffectHandler(fluidIngredient, handler));
17+
OpenPipeEffectHandler.REGISTRY.registerProvider(FluidIngredientWrapper.createEffectHandler(fluidIngredient, handler));
1818
}
1919
}

src/main/java/dev/latvian/mods/kubejs/create/events/SpecialSpoutHandlerEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.simibubi.create.api.behaviour.spouting.BlockSpoutingBehaviour;
44
import dev.latvian.mods.kubejs.block.state.BlockStatePredicate;
5-
import dev.latvian.mods.kubejs.create.platform.FluidIngredientHelper;
5+
import dev.latvian.mods.kubejs.create.wrapper.FluidIngredientWrapper;
66
import dev.latvian.mods.kubejs.event.KubeEvent;
77
import dev.latvian.mods.kubejs.level.LevelBlock;
88
import net.minecraft.resources.ResourceLocation;
@@ -15,6 +15,6 @@ public interface SpoutHandler {
1515
}
1616

1717
public void add(ResourceLocation path, BlockStatePredicate block, SpoutHandler handler) {
18-
BlockSpoutingBehaviour.BY_BLOCK.registerProvider(FluidIngredientHelper.createSpoutingHandler(block, handler));
18+
BlockSpoutingBehaviour.BY_BLOCK.registerProvider(FluidIngredientWrapper.createSpoutingHandler(block, handler));
1919
}
2020
}

src/main/java/dev/latvian/mods/kubejs/create/recipe/CreateFluidIngredientRecipeComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.mojang.serialization.Codec;
44
import com.simibubi.create.Create;
55
import com.simibubi.create.foundation.fluid.FluidIngredient;
6-
import dev.latvian.mods.kubejs.create.platform.FluidIngredientHelper;
6+
import dev.latvian.mods.kubejs.create.wrapper.FluidIngredientWrapper;
77
import dev.latvian.mods.kubejs.recipe.component.RecipeComponent;
88
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentType;
99
import dev.latvian.mods.kubejs.recipe.component.UniqueIdBuilder;
@@ -42,7 +42,7 @@ public boolean hasPriority(RecipeMatchContext cx, Object from) {
4242

4343
@Override
4444
public boolean matches(RecipeMatchContext cx, FluidIngredient value, ReplacementMatchInfo match) {
45-
return match.match() instanceof FluidMatch m && m.matches(cx, FluidIngredientHelper.convert(value), match.exact());
45+
return match.match() instanceof FluidMatch m && m.matches(cx, FluidIngredientWrapper.toNeo(value), match.exact());
4646
}
4747

4848
@Override

src/main/java/dev/latvian/mods/kubejs/create/platform/FluidIngredientHelper.java renamed to src/main/java/dev/latvian/mods/kubejs/create/wrapper/FluidIngredientWrapper.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.latvian.mods.kubejs.create.platform;
1+
package dev.latvian.mods.kubejs.create.wrapper;
22

33
import com.mojang.serialization.DataResult;
44
import com.simibubi.create.api.behaviour.spouting.BlockSpoutingBehaviour;
@@ -36,20 +36,20 @@
3636
import static com.mojang.serialization.DataResult.error;
3737
import static com.mojang.serialization.DataResult.success;
3838

39-
public class FluidIngredientHelper {
40-
public static FluidIngredient toFluidIngredient(FluidStack fluidStack) {
39+
public interface FluidIngredientWrapper {
40+
static FluidIngredient toFluidIngredient(FluidStack fluidStack) {
4141
return FluidIngredient.fromFluidStack(fluidStack);
4242
}
4343

44-
public static FluidIngredient toFluidIngredient(TagKey<Fluid> tag) {
44+
static FluidIngredient toFluidIngredient(TagKey<Fluid> tag) {
4545
return FluidIngredient.fromTag(tag, FluidType.BUCKET_VOLUME);
4646
}
4747

48-
public static FluidIngredient toFluidIngredient(Fluid fluid) {
48+
static FluidIngredient toFluidIngredient(Fluid fluid) {
4949
return FluidIngredient.fromFluid(fluid, FluidType.BUCKET_VOLUME);
5050
}
5151

52-
public static DataResult<FluidIngredient> convert(SizedFluidIngredient neoIn) {
52+
static DataResult<FluidIngredient> fromNeo(SizedFluidIngredient neoIn) {
5353
return switch (neoIn.ingredient()) {
5454
case SingleFluidIngredient single -> success(FluidIngredient.fromFluid(single.fluid().value(), neoIn.amount()));
5555
case TagFluidIngredient tag -> success(FluidIngredient.fromTag(tag.tag(), neoIn.amount()));
@@ -68,11 +68,11 @@ public static DataResult<FluidIngredient> convert(SizedFluidIngredient neoIn) {
6868
};
6969
}
7070

71-
public static net.neoforged.neoforge.fluids.crafting.FluidIngredient convert(FluidIngredient fluidIngredient) {
71+
static net.neoforged.neoforge.fluids.crafting.FluidIngredient toNeo(FluidIngredient fluidIngredient) {
7272
return EmptyFluidIngredient.INSTANCE;
7373
}
7474

75-
public static SimpleRegistry.Provider<Fluid, OpenPipeEffectHandler> createEffectHandler(FluidIngredient fluidIngredient, SpecialFluidHandlerEvent.PipeHandler handler) {
75+
static SimpleRegistry.Provider<Fluid, OpenPipeEffectHandler> createEffectHandler(FluidIngredient fluidIngredient, SpecialFluidHandlerEvent.PipeHandler handler) {
7676
return new SimpleRegistry.Provider<>() {
7777
final FluidIngredient filter = fluidIngredient;
7878
Set<Fluid> validFluids = null;
@@ -121,7 +121,7 @@ public void onRegister(Runnable invalidate) {
121121
};
122122
}
123123

124-
public static SimpleRegistry.Provider<Block, BlockSpoutingBehaviour> createSpoutingHandler(BlockStatePredicate block, SpecialSpoutHandlerEvent.SpoutHandler handler) {
124+
static SimpleRegistry.Provider<Block, BlockSpoutingBehaviour> createSpoutingHandler(BlockStatePredicate block, SpecialSpoutHandlerEvent.SpoutHandler handler) {
125125
final BlockSpoutingBehaviour internalHandler = (world, pos, spout, availableFluid, simulate) -> {
126126
if (!block.test(world.getBlockState(pos))) {
127127
return 0;
@@ -133,16 +133,16 @@ public static SimpleRegistry.Provider<Block, BlockSpoutingBehaviour> createSpout
133133
return blockIn -> block.testBlock(blockIn) ? internalHandler : null;
134134
}
135135

136-
public static FluidIngredient wrap(Context cx, Object o) {
136+
static FluidIngredient wrap(Context cx, Object o) {
137137
return (switch (o) {
138138
case FluidIngredient id -> success(id);
139139
case FluidStack stack -> success(toFluidIngredient(stack));
140140
case TagKey<?> tag -> success(toFluidIngredient(FluidTags.create(tag.location())));
141141
case Fluid fluid -> success(toFluidIngredient(fluid));
142-
case SizedFluidIngredient neoIn -> convert(neoIn);
143-
case net.neoforged.neoforge.fluids.crafting.FluidIngredient neoIn -> convert(new SizedFluidIngredient(neoIn, FluidType.BUCKET_VOLUME));
142+
case SizedFluidIngredient neoIn -> fromNeo(neoIn);
143+
case net.neoforged.neoforge.fluids.crafting.FluidIngredient neoIn -> fromNeo(new SizedFluidIngredient(neoIn, FluidType.BUCKET_VOLUME));
144144
case Map<?, ?> map when map.containsKey("type") -> FluidIngredient.CODEC.parse(RegistryAccessContainer.of(cx).java(), map);
145-
default -> convert(FluidWrapper.wrapSizedIngredient(cx, o));
145+
default -> fromNeo(FluidWrapper.wrapSizedIngredient(cx, o));
146146
}).getOrThrow(msg -> new KubeRuntimeException("Failed to parse Create FluidIngredient: " + msg).source(SourceLine.of(cx)));
147147
}
148148
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.latvian.mods.kubejs.create.wrapper;
2+
3+
import com.google.gson.JsonObject;
4+
import com.simibubi.create.content.processing.recipe.ProcessingOutput;
5+
import dev.latvian.mods.kubejs.plugin.builtin.wrapper.ItemWrapper;
6+
import dev.latvian.mods.kubejs.plugin.builtin.wrapper.StringUtilsWrapper;
7+
import dev.latvian.mods.rhino.Context;
8+
import dev.latvian.mods.rhino.util.HideFromJS;
9+
import net.minecraft.util.Mth;
10+
import net.minecraft.world.item.ItemStack;
11+
import net.minecraft.world.item.Items;
12+
import net.minecraft.world.level.ItemLike;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
import java.util.Map;
16+
import java.util.function.Function;
17+
18+
public interface KubeCreateOutput {
19+
static ProcessingOutput of(ItemStack stack) {
20+
return new ProcessingOutput(stack, 1F);
21+
}
22+
23+
static ProcessingOutput of(ItemStack stack, double c) {
24+
var chance = (float) Mth.clamp(c, 0.0, 1.0);
25+
return new ProcessingOutput(stack, chance);
26+
}
27+
28+
private static ProcessingOutput fromMapLike(Context cx, Object from, Function<String, Object> getter, boolean nested) {
29+
var chance = (float) Mth.clamp(StringUtilsWrapper.parseDouble(getter.apply("chance"), 1.0), 0.0, 1.0);
30+
if (nested) {
31+
var output = ItemWrapper.wrap(cx, getter.apply("output"));
32+
return new ProcessingOutput(output, chance);
33+
} else {
34+
return new ProcessingOutput(ItemWrapper.wrap(cx, from), chance);
35+
}
36+
}
37+
38+
@HideFromJS
39+
static ProcessingOutput wrapProcessingOutput(Context cx, @Nullable Object from) {
40+
return switch (from) {
41+
case null -> ProcessingOutput.EMPTY;
42+
case ProcessingOutput id -> id;
43+
case ItemStack s -> s.isEmpty() ? ProcessingOutput.EMPTY : new ProcessingOutput(s, 1F);
44+
case ItemLike i when i.asItem() == Items.AIR -> ProcessingOutput.EMPTY;
45+
case ItemLike i -> new ProcessingOutput(i.asItem(), 1, 1F);
46+
case JsonObject json when json.has("chance") -> fromMapLike(cx, json, json::get, json.has("output"));
47+
case Map<?, ?> map when map.containsKey("chance") -> fromMapLike(cx, map, map::get, map.containsKey("output"));
48+
// TODO: maybe a custom string-like type wrapper ("2x apple @ 20%" or something like that???)
49+
default -> new ProcessingOutput(ItemWrapper.wrap(cx, from), 1F);
50+
};
51+
}
52+
}

src/main/resources/kubejs_create.mixins.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"package": "dev.latvian.mods.kubejs.create.core.mixin",
55
"compatibilityLevel": "JAVA_21",
66
"mixins": [
7-
"FluidTagIngredientMixin"
7+
"FluidTagIngredientMixin",
8+
"ProcessingOutputMixin"
89
],
910
"client": [
1011
],

0 commit comments

Comments
 (0)