Skip to content

Commit

Permalink
Some more component changes, re-added fluid stack and ingredient comp…
Browse files Browse the repository at this point in the history
…onents, fixed item builder bug with DataComponents
  • Loading branch information
LatvianModder committed Jun 13, 2024

Unverified

The key that signed this doesn't have usage flags that allow signing.
1 parent 977fc41 commit 849393f
Showing 25 changed files with 328 additions and 250 deletions.
16 changes: 8 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
@@ -92,6 +92,8 @@
import dev.latvian.mods.kubejs.recipe.component.BooleanComponent;
import dev.latvian.mods.kubejs.recipe.component.CharacterComponent;
import dev.latvian.mods.kubejs.recipe.component.EnumComponent;
import dev.latvian.mods.kubejs.recipe.component.FluidIngredientComponent;
import dev.latvian.mods.kubejs.recipe.component.FluidStackComponent;
import dev.latvian.mods.kubejs.recipe.component.IngredientComponent;
import dev.latvian.mods.kubejs.recipe.component.ItemStackComponent;
import dev.latvian.mods.kubejs.recipe.component.MapRecipeComponent;
@@ -149,6 +151,7 @@
import net.minecraft.core.Vec3i;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CollectionTag;
@@ -189,6 +192,7 @@
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
@@ -477,6 +481,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(CollectionTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagCollection);
registry.register(ListTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagList);
registry.register(Tag.class, NBTUtils::toTag);
registry.register(DataComponentType.class, DataComponentWrapper::wrapType);
registry.register(DataComponentMap.class, DataComponentWrapper::mapOf);
registry.register(DataComponentPatch.class, DataComponentWrapper::patchOf);

@@ -511,6 +516,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(BlockStatePredicate.class, BlockStatePredicate::of);
registry.register(RuleTest.class, BlockStatePredicate::ruleTestOf);
registry.register(FluidStack.class, FluidWrapper::wrap);
registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient);
registry.register(RecipeFilter.class, RecipeFilter::of);
registry.register(SlotFilter.class, SlotFilter::wrap);
registry.register(Tier.class, ItemBuilder::toolTierOf);
@@ -574,14 +580,8 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register("item_stack", ItemStackComponent.ITEM_STACK);
registry.register("strict_item_stack", ItemStackComponent.STRICT_ITEM_STACK);

// event.register("inputFluid", FluidComponents.INPUT);
// event.register("inputFluidArray", FluidComponents.INPUT_ARRAY);
// event.register("inputFluidOrItem", FluidComponents.INPUT_OR_ITEM);
// event.register("inputFluidOrItemArray", FluidComponents.INPUT_OR_ITEM_ARRAY);
// event.register("outputFluid", FluidComponents.OUTPUT);
// event.register("outputFluidArray", FluidComponents.OUTPUT_ARRAY);
// event.register("outputFluidOrItem", FluidComponents.OUTPUT_OR_ITEM);
// event.register("outputFluidOrItemArray", FluidComponents.OUTPUT_OR_ITEM_ARRAY);
registry.register("fluid_stack", FluidStackComponent.FLUID_STACK);
registry.register("fluid_ingredient", FluidIngredientComponent.FLUID_INGREDIENT);

registry.register("block", BlockComponent.BLOCK);

Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
@@ -25,6 +26,14 @@ public interface DataComponentWrapper {
Dynamic2CommandExceptionType ERROR_MALFORMED_COMPONENT = new Dynamic2CommandExceptionType((object, object2) -> Component.translatableEscape("arguments.item.component.malformed", object, object2));
SimpleCommandExceptionType ERROR_EXPECTED_COMPONENT = new SimpleCommandExceptionType(Component.translatable("arguments.item.component.expected"));

static DataComponentType<?> wrapType(Object object) {
if (object instanceof DataComponentType) {
return (DataComponentType<?>) object;
}

return BuiltInRegistries.DATA_COMPONENT_TYPE.get(ID.mc(object));
}

static DataComponentMap readMap(DynamicOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();
DataComponentMap.Builder builder = null;
18 changes: 16 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package dev.latvian.mods.kubejs.core;

import dev.latvian.mods.kubejs.fluid.FluidLike;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidType;

@RemapPrefixForJS("kjs$")
public interface FluidKJS extends RegistryObjectKJS<Fluid> {
default Fluid kjs$self() {
public interface FluidKJS extends RegistryObjectKJS<Fluid>, FluidLike {
@Override
default Fluid kjs$getFluid() {
return (Fluid) this;
}

@Override
default RegistryInfo<Fluid> kjs$getKubeRegistry() {
return RegistryInfo.FLUID;
}

@Override
default int kjs$getAmount() {
return FluidType.BUCKET_VOLUME;
}

@Override
default boolean kjs$isEmpty() {
return kjs$getFluid().isSame(Fluids.EMPTY);
}
}
12 changes: 9 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import dev.latvian.mods.kubejs.recipe.OutputReplacement;
import dev.latvian.mods.kubejs.recipe.ReplacementMatch;
import dev.latvian.mods.rhino.Context;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;

public interface FluidStackKJS extends OutputReplacement, FluidLike {
@@ -13,7 +14,7 @@ public interface FluidStackKJS extends OutputReplacement, FluidLike {
}

@Override
default long kjs$getAmount() {
default int kjs$getAmount() {
return kjs$self().getAmount();
}

@@ -23,8 +24,13 @@ public interface FluidStackKJS extends OutputReplacement, FluidLike {
}

@Override
default FluidLike kjs$copy(long amount) {
return (FluidLike) (Object) kjs$self().copyWithAmount((int) amount);
default Fluid kjs$getFluid() {
return kjs$self().getFluid();
}

@Override
default FluidLike kjs$copy(int amount) {
return (FluidLike) (Object) kjs$self().copyWithAmount(amount);
}

@Override
13 changes: 8 additions & 5 deletions src/main/java/dev/latvian/mods/kubejs/fluid/FluidLike.java
Original file line number Diff line number Diff line change
@@ -2,20 +2,23 @@

import dev.latvian.mods.kubejs.recipe.ReplacementMatch;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.world.level.material.Fluid;

@RemapPrefixForJS("kjs$")
public interface FluidLike extends ReplacementMatch {
long kjs$getAmount();
int kjs$getAmount();

Fluid kjs$getFluid();

default boolean kjs$isEmpty() {
return kjs$getAmount() <= 0L;
return kjs$getAmount() <= 0;
}

default FluidLike kjs$copy(long amount) {
default FluidLike kjs$copy(int amount) {
return this;
}

default boolean matches(FluidLike other) {
return equals(other);
default boolean contains(FluidLike other) {
return kjs$getFluid().isSame(other.kjs$getFluid());
}
}
90 changes: 89 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
package dev.latvian.mods.kubejs.fluid;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.bindings.DataComponentWrapper;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.crafting.EmptyFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;

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

public interface FluidWrapper {
TypeInfo TYPE_INFO = TypeInfo.of(FluidStack.class);
TypeInfo INGREDIENT_TYPE_INFO = TypeInfo.of(FluidIngredient.class);

static FluidStack wrap(Context cx, Object o) {
return FluidStack.EMPTY; // FIXME
if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) {
return FluidStack.EMPTY;
} else if (o instanceof FluidStack stack) {
return stack;
} else if (o instanceof Fluid fluid) {
return new FluidStack(fluid, FluidType.BUCKET_VOLUME);
} else if (o instanceof FluidIngredient in) {
return in.hasNoFluids() ? FluidStack.EMPTY : in.getStacks()[0];
} else {
return ofString(((KubeJSContext) cx).getNbtOps(), o.toString());
}
}

static FluidIngredient wrapIngredient(Context cx, Object o) {
if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) {
return EmptyFluidIngredient.INSTANCE;
} else if (o instanceof FluidStack stack) {
return FluidIngredient.of(stack);
} else if (o instanceof Fluid fluid) {
return FluidIngredient.of(fluid);
} else if (o instanceof FluidIngredient in) {
return in;
} else {
var stack = wrap(cx, o);
return stack.isEmpty() ? EmptyFluidIngredient.INSTANCE : FluidIngredient.of(stack);
}
}

static FluidStack of(FluidStack o) {
@@ -81,4 +116,57 @@ static boolean exists(ResourceLocation id) {
static ResourceLocation getId(Fluid fluid) {
return RegistryInfo.FLUID.getId(fluid);
}

static FluidStack ofString(DynamicOps<Tag> registryOps, String s) {
if (s.isEmpty() || s.equals("-") || s.equals("air") || s.equals("minecraft:air")) {
return FluidStack.EMPTY;
} else {
try {
var reader = new StringReader(s);
reader.skipWhitespace();

if (!reader.canRead()) {
return FluidStack.EMPTY;
}

return read(registryOps, new StringReader(s));
} catch (CommandSyntaxException ex) {
throw new RuntimeException(ex);
}
}
}

static FluidStack read(DynamicOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
if (!reader.canRead()) {
return FluidStack.EMPTY;
}

if (reader.peek() == '-') {
return FluidStack.EMPTY;
}

int amount = 1;

if (reader.canRead() && StringReader.isAllowedNumber(reader.peek())) {
amount = Mth.ceil(reader.readDouble());
reader.skipWhitespace();
reader.expect('x');
reader.skipWhitespace();

if (amount < 1) {
throw new IllegalArgumentException("Item count smaller than 1 is not allowed!");
}
}

var fluidId = ResourceLocation.read(reader);
var fluidStack = new FluidStack(RegistryInfo.FLUID.getValue(fluidId), amount);

var next = reader.canRead() ? reader.peek() : 0;

if (next == '[' || next == '{') {
fluidStack.applyComponents(DataComponentWrapper.readPatch(registryOps, reader));
}

return fluidStack;
}
}
24 changes: 16 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.kubejs.item;

import com.google.gson.JsonObject;
import dev.latvian.mods.kubejs.bindings.DataComponentWrapper;
import dev.latvian.mods.kubejs.bindings.ItemWrapper;
import dev.latvian.mods.kubejs.color.Color;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
@@ -66,7 +67,7 @@ public static Tier toolTierOf(Object o) {
return ItemBuilder.TOOL_TIERS.getOrDefault(ID.kjsString(asString), Tiers.IRON);
}

public transient Map<DataComponentType<?>, Object> components;
public transient Map<Object, Object> components;
public transient int maxStackSize;
public transient int maxDamage;
public transient int burnTime;
@@ -100,12 +101,12 @@ public static Tier toolTierOf(Object o) {

public ItemBuilder(ResourceLocation i) {
super(i);
maxStackSize = 64;
maxStackSize = -1;
maxDamage = 0;
burnTime = 0;
containerItem = null;
subtypes = null;
rarity = Rarity.COMMON;
rarity = null;
glow = false;
tooltip = new ArrayList<>();
textureJson = new JsonObject();
@@ -323,7 +324,6 @@ public ItemBuilder fireResistant(boolean isFireResistant) {
return this;
}


@Info("Makes the item fire resistant like netherite tools.")
public ItemBuilder fireResistant() {
return fireResistant(true);
@@ -332,19 +332,27 @@ public ItemBuilder fireResistant() {
public Item.Properties createItemProperties() {
var properties = new KubeJSItemProperties(this);

if (components != null) {
if (components != null && !components.isEmpty()) {
for (var entry : components.entrySet()) {
properties.component((DataComponentType) entry.getKey(), entry.getValue());
var type = DataComponentWrapper.wrapType(entry.getKey());

if (type != null) {
properties.component((DataComponentType) type, entry.getValue());
} else {
ConsoleJS.STARTUP.error("Component '" + entry.getKey() + "' not found for item " + id);
}
}
}

if (maxDamage > 0) {
properties.durability(maxDamage);
} else {
} else if (maxStackSize != -1) {
properties.stacksTo(maxStackSize);
}

properties.rarity(rarity);
if (rarity != null) {
properties.rarity(rarity);
}

var item = containerItem == null ? Items.AIR : ItemWrapper.getItem(containerItem);

Loading

0 comments on commit 849393f

Please sign in to comment.