Skip to content

Commit

Permalink
Added SizedFluidIngredient wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 29, 2024
1 parent 9f56ffb commit 8aadb34
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
Expand Down Expand Up @@ -510,6 +511,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(RuleTest.class, BlockStatePredicate::ruleTestOf);
registry.register(FluidStack.class, FluidWrapper::wrap);
registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient);
registry.register(SizedFluidIngredient.class, FluidWrapper::wrapSizedIngredient);
registry.register(RecipeFilter.class, RecipeFilter::of);
registry.register(SlotFilter.class, SlotFilter::wrap);
registry.register(Tier.class, ItemToolTiers::wrap);
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface FluidWrapper {
TypeInfo INGREDIENT_TYPE_INFO = TypeInfo.of(FluidIngredient.class);
TypeInfo SIZED_INGREDIENT_TYPE_INFO = TypeInfo.of(SizedFluidIngredient.class);

SizedFluidIngredient EMPTY_SIZED = new SizedFluidIngredient(FluidIngredient.empty(), FluidType.BUCKET_VOLUME);

static FluidStack wrap(RegistryAccessContainer registries, Object o) {
if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) {
return FluidStack.EMPTY;
Expand All @@ -42,6 +44,8 @@ static FluidStack wrap(RegistryAccessContainer registries, Object o) {
return new FluidStack(fluid, FluidType.BUCKET_VOLUME);
} else if (o instanceof FluidIngredient in) {
return in.hasNoFluids() ? FluidStack.EMPTY : in.getStacks()[0];
} else if (o instanceof SizedFluidIngredient s) {
return s.getFluids()[0];
} else {
return ofString(registries.nbt(), o.toString());
}
Expand All @@ -56,11 +60,29 @@ static FluidIngredient wrapIngredient(RegistryAccessContainer registries, Object
return FluidIngredient.of(fluid);
} else if (o instanceof FluidIngredient in) {
return in;
} else if (o instanceof SizedFluidIngredient s) {
return s.ingredient();
} else {
return ingredientOfString(registries.nbt(), o.toString());
}
}

static SizedFluidIngredient wrapSizedIngredient(RegistryAccessContainer registries, Object o) {
if (o == null || o == FluidStack.EMPTY || o == Fluids.EMPTY || o == EmptyFluidIngredient.INSTANCE) {
return EMPTY_SIZED;
} else if (o instanceof SizedFluidIngredient s) {
return s;
} else if (o instanceof FluidStack stack) {
return SizedFluidIngredient.of(stack);
} else if (o instanceof Fluid fluid) {
return SizedFluidIngredient.of(fluid, FluidType.BUCKET_VOLUME);
} else if (o instanceof FluidIngredient in) {
return new SizedFluidIngredient(in, FluidType.BUCKET_VOLUME);
} else {
return sizedIngredientOfString(registries.nbt(), o.toString());
}
}

static FluidStack of(FluidStack o) {
return o;
}
Expand Down Expand Up @@ -231,4 +253,58 @@ static FluidIngredient readIngredient(DynamicOps<Tag> registryOps, StringReader

return FluidIngredient.of(fluid);
}

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

if (!reader.canRead()) {
return EMPTY_SIZED;
}

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

static SizedFluidIngredient readSizedIngredient(DynamicOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
if (!reader.canRead()) {
return EMPTY_SIZED;
}

int amount = FluidType.BUCKET_VOLUME;

if (StringReader.isAllowedNumber(reader.peek())) {
var amountd = reader.readDouble();
reader.skipWhitespace();

if (reader.peek() == 'b' || reader.peek() == 'B') {
reader.skip();
reader.skipWhitespace();
amountd *= FluidType.BUCKET_VOLUME;
}

if (reader.peek() == '/') {
reader.skip();
reader.skipWhitespace();
amountd = amountd / reader.readDouble();
}

amount = Mth.ceil(amountd);
reader.expect('x');
reader.skipWhitespace();

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

return new SizedFluidIngredient(readIngredient(registryOps, reader), amount);
}
}

0 comments on commit 8aadb34

Please sign in to comment.