Skip to content

Commit

Permalink
Fix OutputItems in kubeJS stripping NBT (#1852)
Browse files Browse the repository at this point in the history
Co-authored-by: YoungOnionMC <[email protected]>
Co-authored-by: screret <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2024
1 parent c7a3bce commit 3361079
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.gregtechceu.gtceu.integration.kjs.builders.prefix.OreTagPrefixBuilder;
import com.gregtechceu.gtceu.integration.kjs.helpers.MaterialStackWrapper;
import com.gregtechceu.gtceu.integration.kjs.recipe.GTRecipeSchema;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.ExtendedOutputItem;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.GTRecipeComponents;

import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -182,6 +183,7 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event)
event.register("gtSuOut", GTRecipeComponents.SU_OUT);

event.register("gtChance", GTRecipeComponents.CHANCE_LOGIC_MAP);
event.register("extendedOutputItem", GTRecipeComponents.EXTENDED_OUTPUT);

event.register("fluidIngredient", GTRecipeComponents.FLUID_INGREDIENT);
event.register("fluidIngredientOut", GTRecipeComponents.FLUID_INGREDIENT_OUT);
Expand Down Expand Up @@ -249,7 +251,7 @@ public void registerBindings(BindingsEvent event) {
@Override
public void registerTypeWrappers(ScriptType type, TypeWrappers typeWrappers) {
super.registerTypeWrappers(type, typeWrappers);
typeWrappers.register(GTRecipeType.class, (ctx, o) -> {
typeWrappers.registerSimple(GTRecipeType.class, o -> {
if (o instanceof Wrapper w) {
o = w.unwrap();
}
Expand All @@ -258,28 +260,28 @@ public void registerTypeWrappers(ScriptType type, TypeWrappers typeWrappers) {
return null;
});

typeWrappers.register(Element.class, (ctx, o) -> {
typeWrappers.registerSimple(Element.class, o -> {
if (o instanceof Element element) return element;
if (o instanceof CharSequence chars) return GTElements.get(chars.toString());
return null;
});
typeWrappers.register(Material.class, (ctx, o) -> {
typeWrappers.registerSimple(Material.class, o -> {
if (o instanceof Material material) return material;
if (o instanceof CharSequence chars) return GTMaterials.get(chars.toString());
return null;
});
typeWrappers.register(MachineDefinition.class, (ctx, o) -> {
typeWrappers.registerSimple(MachineDefinition.class, o -> {
if (o instanceof MachineDefinition definition) return definition;
if (o instanceof CharSequence chars) return GTMachines.get(chars.toString());
return null;
});

typeWrappers.register(TagPrefix.class, (ctx, o) -> {
typeWrappers.registerSimple(TagPrefix.class, o -> {
if (o instanceof TagPrefix tagPrefix) return tagPrefix;
if (o instanceof CharSequence chars) return TagPrefix.get(chars.toString());
return null;
});
typeWrappers.register(UnificationEntry.class, (ctx, o) -> {
typeWrappers.registerSimple(UnificationEntry.class, o -> {
if (o instanceof UnificationEntry entry) return entry;
if (o instanceof CharSequence chars) {
var values = chars.toString().split(":");
Expand All @@ -292,81 +294,82 @@ public void registerTypeWrappers(ScriptType type, TypeWrappers typeWrappers) {
}
return null;
});
typeWrappers.register(RecipeCapability.class, (ctx, o) -> {
typeWrappers.registerSimple(RecipeCapability.class, o -> {
if (o instanceof RecipeCapability<?> capability) return capability;
if (o instanceof CharSequence chars) return GTRegistries.RECIPE_CAPABILITIES.get(chars.toString());
return null;
});
typeWrappers.register(ChanceLogic.class, (ctx, o) -> {
typeWrappers.registerSimple(ChanceLogic.class, o -> {
if (o instanceof ChanceLogic capability) return capability;
if (o instanceof CharSequence chars) return GTRegistries.CHANCE_LOGICS.get(chars.toString());
return null;
});
typeWrappers.registerSimple(ExtendedOutputItem.class, ExtendedOutputItem::of);

typeWrappers.register(MaterialIconSet.class, (ctx, o) -> {
typeWrappers.registerSimple(MaterialIconSet.class, o -> {
if (o instanceof MaterialIconSet iconSet) return iconSet;
if (o instanceof CharSequence chars) return MaterialIconSet.getByName(chars.toString());
return null;
});
typeWrappers.register(MaterialStack.class, (ctx, o) -> {
typeWrappers.registerSimple(MaterialStack.class, o -> {
if (o instanceof MaterialStack stack) return stack;
if (o instanceof Material material) return new MaterialStack(material, 1);
if (o instanceof CharSequence chars) return MaterialStack.fromString(chars);
return null;
});
typeWrappers.register(MaterialStackWrapper.class, (ctx, o) -> {
typeWrappers.registerSimple(MaterialStackWrapper.class, o -> {
if (o instanceof MaterialStackWrapper wrapper) return wrapper;
if (o instanceof MaterialStack stack) return new MaterialStackWrapper(stack::material, stack.amount());
if (o instanceof Material material) return new MaterialStackWrapper(() -> material, 1);
if (o instanceof CharSequence chars) return MaterialStackWrapper.fromString(chars);
return null;
});

typeWrappers.register(IWorldGenLayer.class, (ctx, o) -> {
typeWrappers.registerSimple(IWorldGenLayer.class, o -> {
if (o instanceof IWorldGenLayer layer) return layer;
if (o instanceof CharSequence chars) return WorldGenLayers.getByName(chars.toString());
return null;
});
typeWrappers.register(HeightRangePlacement.class, (ctx, o) -> {
typeWrappers.registerSimple(HeightRangePlacement.class, o -> {
if (o instanceof HeightRangePlacement placement) return placement;
return Optional.ofNullable(NBTUtils.toTagCompound(o))
.map(tag -> HeightRangePlacement.CODEC.parse(NbtOps.INSTANCE, tag))
.flatMap(DataResult::result)
.orElse(null);
});
typeWrappers.register(BiomeWeightModifier.class, (ctx, o) -> {
typeWrappers.registerSimple(BiomeWeightModifier.class, o -> {
if (o instanceof BiomeWeightModifier modifier) return modifier;
return Optional.ofNullable(NBTUtils.toTagCompound(o))
.map(tag -> BiomeWeightModifier.CODEC.parse(NbtOps.INSTANCE, tag))
.flatMap(DataResult::result)
.orElse(null);
});
typeWrappers.register(VeinGenerator.class, (ctx, o) -> {
typeWrappers.registerSimple(VeinGenerator.class, o -> {
if (o instanceof VeinGenerator generator) return generator;
return Optional.ofNullable(NBTUtils.toTagCompound(o))
.map(tag -> VeinGenerator.DIRECT_CODEC.parse(NbtOps.INSTANCE, tag))
.flatMap(DataResult::result)
.orElse(null);
});
typeWrappers.register(IndicatorGenerator.class, (ctx, o) -> {
typeWrappers.registerSimple(IndicatorGenerator.class, o -> {
if (o instanceof IndicatorGenerator generator) return generator;
return Optional.ofNullable(NBTUtils.toTagCompound(o))
.map(tag -> IndicatorGenerator.DIRECT_CODEC.parse(NbtOps.INSTANCE, tag))
.flatMap(DataResult::result)
.orElse(null);
});
typeWrappers.register(IndicatorPlacement.class, (ctx, o) -> {
typeWrappers.registerSimple(IndicatorPlacement.class, o -> {
if (o instanceof IndicatorPlacement placement) return placement;
if (o instanceof CharSequence str) return IndicatorPlacement.getByName(str.toString());
return null;
});
typeWrappers.register(MedicalCondition.class, (ctx, o) -> {
typeWrappers.registerSimple(MedicalCondition.class, o -> {
if (o instanceof MedicalCondition condition) return condition;
if (o instanceof CharSequence str) return MedicalCondition.CONDITIONS.get(str.toString());
return null;
});
// jank because Rhino doesn't agree that it's an interface
typeWrappers.register(IWorldGenLayer.RuleTestSupplier.class, (ctx, o) -> {
typeWrappers.registerSimple(IWorldGenLayer.RuleTestSupplier.class, o -> {
if (o instanceof IWorldGenLayer.RuleTestSupplier supplier) return supplier;
return () -> BlockStatePredicate.ruleTestOf(o);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.gregtechceu.gtceu.data.recipe.builder.GTRecipeBuilder;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.CapabilityMap;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.ExtendedOutputItem;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.GTRecipeComponents;
import com.gregtechceu.gtceu.utils.ResearchManager;

Expand Down Expand Up @@ -256,7 +257,7 @@ public GTRecipeJS inputItems(MachineDefinition machine, int count) {
return inputItems(machine.asStack(count));
}

public GTRecipeJS itemOutputs(InputItem... outputs) {
public GTRecipeJS itemOutputs(ExtendedOutputItem... outputs) {
return outputItems(outputs);
}

Expand All @@ -268,8 +269,8 @@ public GTRecipeJS itemOutput(UnificationEntry unificationEntry, int count) {
return outputItems(unificationEntry.tagPrefix, unificationEntry.material, count);
}

public GTRecipeJS outputItems(InputItem... outputs) {
for (InputItem itemStack : outputs) {
public GTRecipeJS outputItems(ExtendedOutputItem... outputs) {
for (ExtendedOutputItem itemStack : outputs) {
if (itemStack.isEmpty()) {
GTCEu.LOGGER.error("gt recipe {} output items is empty", id);
}
Expand All @@ -278,45 +279,43 @@ public GTRecipeJS outputItems(InputItem... outputs) {
}

public GTRecipeJS outputItems(Item input, int amount) {
return outputItems(InputItem.of(new ItemStack(input, amount)));
return outputItems(ExtendedOutputItem.of(new ItemStack(input, amount)));
}

public GTRecipeJS outputItems(Item input) {
return outputItems(InputItem.of(new ItemStack(input)));
return outputItems(ExtendedOutputItem.of(new ItemStack(input)));
}

public GTRecipeJS outputItems(TagPrefix orePrefix, Material material) {
return outputItems(orePrefix, material, 1);
}

public GTRecipeJS outputItems(TagPrefix orePrefix, Material material, int count) {
return outputItems(InputItem.of(ChemicalHelper.get(orePrefix, material, count)));
return outputItems(ExtendedOutputItem.of(ChemicalHelper.get(orePrefix, material, count)));
}

public GTRecipeJS outputItems(MachineDefinition machine) {
return outputItems(machine, 1);
}

public GTRecipeJS outputItems(MachineDefinition machine, int count) {
return outputItems(InputItem.of(machine.asStack(count)));
return outputItems(new ExtendedOutputItem(machine.asStack(count)));
}

public GTRecipeJS itemOutputsRanged(InputItem ingredient, int min, int max) {
return output(ItemRecipeCapability.CAP,
new IntProviderIngredient(ingredient.ingredient, UniformInt.of(min, max)));
public GTRecipeJS itemOutputsRanged(ExtendedOutputItem ingredient, int min, int max) {
return outputItemsRanged(ingredient.ingredient.getInner(), min, max);
}

public GTRecipeJS outputItemsRanged(Ingredient ingredient, int min, int max) {
return output(ItemRecipeCapability.CAP, new IntProviderIngredient(ingredient, UniformInt.of(min, max)));
}

public GTRecipeJS outputItemsRanged(ItemStack stack, int min, int max) {
return output(ItemRecipeCapability.CAP,
new IntProviderIngredient(SizedIngredient.create(stack), UniformInt.of(min, max)));
return outputItemsRanged(Ingredient.of(stack), min, max);
}

public GTRecipeJS outputItemsRanged(TagPrefix orePrefix, Material material, int min, int max) {
return outputItemsRanged(ChemicalHelper.get(orePrefix, material, 1), min, max);
return outputItemsRanged(ChemicalHelper.get(orePrefix, material), min, max);
}

public GTRecipeJS notConsumable(InputItem itemStack) {
Expand Down Expand Up @@ -380,7 +379,7 @@ public GTRecipeJS chancedFluidInput(GTRecipeComponents.FluidIngredientJS stack,
return this;
}

public GTRecipeJS chancedOutput(InputItem stack, int chance, int tierChanceBoost) {
public GTRecipeJS chancedOutput(ExtendedOutputItem stack, int chance, int tierChanceBoost) {
if (0 >= chance || chance > ChanceLogic.getMaxChancedValue()) {
GTCEu.LOGGER.error("Chance cannot be less or equal to 0 or more than {}. Actual: {}.",
ChanceLogic.getMaxChancedValue(), chance, new Throwable());
Expand All @@ -397,14 +396,14 @@ public GTRecipeJS chancedOutput(InputItem stack, int chance, int tierChanceBoost
}

public GTRecipeJS chancedOutput(TagPrefix tag, Material mat, int chance, int tierChanceBoost) {
return chancedOutput(InputItem.of(ChemicalHelper.get(tag, mat)), chance, tierChanceBoost);
return chancedOutput(ExtendedOutputItem.of(ChemicalHelper.get(tag, mat)), chance, tierChanceBoost);
}

public GTRecipeJS chancedOutput(TagPrefix tag, Material mat, int count, int chance, int tierChanceBoost) {
return chancedOutput(InputItem.of(ChemicalHelper.get(tag, mat, count)), chance, tierChanceBoost);
return chancedOutput(ExtendedOutputItem.of(ChemicalHelper.get(tag, mat, count)), chance, tierChanceBoost);
}

public GTRecipeJS chancedOutput(InputItem stack, String fraction, int tierChanceBoost) {
public GTRecipeJS chancedOutput(ExtendedOutputItem stack, String fraction, int tierChanceBoost) {
if (stack.isEmpty()) {
return this;
}
Expand Down Expand Up @@ -472,7 +471,7 @@ public GTRecipeJS chancedOutput(InputItem stack, String fraction, int tierChance

public GTRecipeJS chancedOutput(TagPrefix prefix, Material material, int count, String fraction,
int tierChanceBoost) {
return chancedOutput(InputItem.of(ChemicalHelper.get(prefix, material, count)), fraction,
return chancedOutput(ExtendedOutputItem.of(ChemicalHelper.get(prefix, material, count)), fraction,
tierChanceBoost);
}

Expand Down Expand Up @@ -894,8 +893,17 @@ public JsonElement writeInputItem(InputItem value) {

@Override
public OutputItem readOutputItem(Object from) {
if (from instanceof SizedIngredient ingredient) {
if (from instanceof ExtendedOutputItem outputItem) {
return outputItem;
} else if (from instanceof OutputItem outputItem) {
return outputItem;
} else if (from instanceof SizedIngredient ingredient) {
if (ingredient.getInner() instanceof IntProviderIngredient intProvider) {
return new ExtendedOutputItem(intProvider, 1);
}
return OutputItem.of(ingredient.getInner().getItems()[0], Double.NaN);
} else if (from instanceof IntProviderIngredient ingredient) {
return new ExtendedOutputItem(ingredient, 1);
} else if (from instanceof JsonObject jsonObject) {
float chance = 1.0f;
if (jsonObject.has("chance")) {
Expand All @@ -912,6 +920,12 @@ public OutputItem readOutputItem(Object from) {

@Override
public JsonElement writeOutputItem(OutputItem value) {
if (value instanceof ExtendedOutputItem extended) {
if (extended.ingredient.getInner() instanceof IntProviderIngredient intProvider) {
return intProvider.toJson();
}
return extended.ingredient.toJson();
}
return SizedIngredient.create(value.item).toJson();
}

Expand Down
Loading

0 comments on commit 3361079

Please sign in to comment.