Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Aug 30, 2024
1 parent 6323a27 commit d67aeea
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ mod_version=2101.1.6
neoforge_version=21.1.31
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
kubejs_version=2101.7.0-build.155
kubejs_version=2101.7.0-build.160
mekanism_version=1.21.1-10.7.5.62
129 changes: 129 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/mekanism/ChemicalComponents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package dev.latvian.mods.kubejs.mekanism;

import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.SimpleRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.UniqueIdBuilder;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.rhino.Context;
import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.recipes.ingredients.ChemicalStackIngredient;
import mekanism.api.recipes.ingredients.chemical.ChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.CompoundChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.TagChemicalIngredient;
import mekanism.api.recipes.ingredients.creator.IngredientCreatorAccess;

public interface ChemicalComponents {
private static boolean isChemicalLike(Object obj) {
return obj instanceof Chemical || obj instanceof ChemicalStack || obj instanceof ChemicalIngredient || obj instanceof ChemicalStackIngredient;
}

RecipeComponent<Chemical> CHEMICAL = new SimpleRecipeComponent<>("mekanism:chemical", Chemical.CODEC, MekanismChemicalWrapper.CHEMICAL_TYPE_INFO) {
@Override
public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) {
return isChemicalLike(from);
}

@Override
public boolean matches(Context cx, KubeRecipe recipe, Chemical value, ReplacementMatchInfo match) {
return match.match() instanceof ChemicalIngredient m && !value.isEmptyType() && m.test(value);
}

@Override
public boolean isEmpty(Chemical value) {
return value.isEmptyType();
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, Chemical value) {
builder.append(value.getRegistryName());
}
};

RecipeComponent<ChemicalStack> CHEMICAL_STACK = new SimpleRecipeComponent<>("mekanism:chemical_stack", ChemicalStack.OPTIONAL_CODEC, MekanismChemicalWrapper.CHEMICAL_STACK_TYPE_INFO) {
@Override
public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) {
return isChemicalLike(from);
}

@Override
public boolean matches(Context cx, KubeRecipe recipe, ChemicalStack value, ReplacementMatchInfo match) {
return match.match() instanceof ChemicalIngredient m && !value.isEmpty() && m.test(value.getChemical());
}

@Override
public boolean isEmpty(ChemicalStack value) {
return value.isEmpty();
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, ChemicalStack value) {
builder.append(value.getTypeRegistryName());
}
};

RecipeComponent<ChemicalIngredient> CHEMICAL_INGREDIENT = new SimpleRecipeComponent<>("mekanism:chemical_ingredient", IngredientCreatorAccess.chemical().codec(), MekanismChemicalWrapper.CHEMICAL_INGREDIENT_TYPE_INFO) {
@Override
public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) {
return isChemicalLike(from);
}

@Override
public boolean matches(Context cx, KubeRecipe recipe, ChemicalIngredient value, ReplacementMatchInfo match) {
return match.match() instanceof ChemicalIngredient m && !value.isEmpty() && value.getChemicals().stream().anyMatch(m);
}

@Override
public boolean isEmpty(ChemicalIngredient value) {
return value.isEmpty();
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, ChemicalIngredient value) {
if (value instanceof CompoundChemicalIngredient c) {
boolean first = true;

for (var in : c.children()) {
if (first) {
first = false;
} else {
builder.appendSeparator();
}

buildUniqueId(builder, in);
}
} else if (value instanceof TagChemicalIngredient tag) {
builder.append(tag.tag().location());
} else {
var list = value.getChemicals();

if (!list.isEmpty()) {
builder.append(list.getFirst().getRegistryName());
}
}
}
};

RecipeComponent<ChemicalStackIngredient> CHEMICAL_STACK_INGREDIENT = new SimpleRecipeComponent<>("mekanism:chemical_stack_ingredient", ChemicalStackIngredient.CODEC, MekanismChemicalWrapper.CHEMICAL_STACK_INGREDIENT_TYPE_INFO) {
@Override
public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) {
return isChemicalLike(from);
}

@Override
public boolean matches(Context cx, KubeRecipe recipe, ChemicalStackIngredient value, ReplacementMatchInfo match) {
return match.match() instanceof ChemicalIngredient m && !value.ingredient().isEmpty() && value.ingredient().getChemicals().stream().anyMatch(m);
}

@Override
public boolean isEmpty(ChemicalStackIngredient value) {
return value.ingredient().isEmpty();
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, ChemicalStackIngredient value) {
CHEMICAL_INGREDIENT.buildUniqueId(builder, value.ingredient());
}
};
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package dev.latvian.mods.kubejs.mekanism;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.latvian.mods.kubejs.fluid.FluidWrapper;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.rhino.util.HideFromJS;
import mekanism.api.MekanismAPI;
import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.recipes.ingredients.ChemicalStackIngredient;
import mekanism.api.recipes.ingredients.chemical.ChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.CompoundChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.DifferenceChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.EmptyChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.SingleChemicalIngredient;
import mekanism.api.recipes.ingredients.chemical.TagChemicalIngredient;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.neoforged.neoforge.fluids.FluidType;
import org.codehaus.plexus.util.cli.CommandLineException;

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

public interface MekanismChemicalWrapper {
TypeInfo CHEMICAL_TYPE_INFO = TypeInfo.of(Chemical.class);
Expand Down Expand Up @@ -40,13 +49,19 @@ static ChemicalStack stack(Chemical chemical, long amount) {
return new ChemicalStack(chemical, amount);
}

private static long readAmount(StringReader reader) throws CommandLineException {
// long amount = FluidWrapper.readFluidAmount(reader);
return 0L;
static ChemicalIngredient ingredientExcept(ChemicalIngredient ingredient, ChemicalIngredient except) {
return new DifferenceChemicalIngredient(ingredient, except);
}

@HideFromJS
static ChemicalStack wrapStack(Object from) {
static ChemicalIngredient ingredientIntersection(List<ChemicalIngredient> children) {
return new CompoundChemicalIngredient(children);
}

static ChemicalStackIngredient ingredientStack(ChemicalIngredient ingredient, long amount) {
return new ChemicalStackIngredient(ingredient, amount);
}

static ChemicalStack stackOf(Object from) {
return switch (from) {
case null -> null;
case ChemicalStack c -> c;
Expand All @@ -57,23 +72,103 @@ static ChemicalStack wrapStack(Object from) {
if (str.isEmpty() || str.equals("mekanism:empty")) {
yield ChemicalStack.EMPTY;
} else {
yield new ChemicalStack(of(from), FluidType.BUCKET_VOLUME);
try {
yield readStack(new StringReader(str));
} catch (CommandSyntaxException ex) {
ex.printStackTrace();
yield ChemicalStack.EMPTY;
}
}
}
};
}

@HideFromJS
static ChemicalIngredient wrapIngredient(Object from) {
return EmptyChemicalIngredient.INSTANCE;
static Chemical read(StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();
return MekanismAPI.CHEMICAL_REGISTRY.get(ResourceLocation.read(reader));
}

static ChemicalStackIngredient ingredientStack(ChemicalIngredient ingredient, long amount) {
return new ChemicalStackIngredient(ingredient, amount);
static ChemicalStack readStack(StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();
var amount = FluidWrapper.readFluidAmount(reader);
var chemical = read(reader);
return new ChemicalStack(chemical, amount);
}

static ChemicalIngredient ingredientOf(Object from) {
return switch (from) {
case null -> EmptyChemicalIngredient.INSTANCE;
case ChemicalIngredient c -> c;
case ChemicalStackIngredient c -> c.ingredient();
case ChemicalStack c -> new SingleChemicalIngredient(c.getChemicalHolder());
case Chemical c -> new SingleChemicalIngredient(c.getAsHolder());
case Iterable<?> itr -> {
var a = new ArrayList<ChemicalIngredient>();

for (var o : itr) {
var i = ingredientOf(o);

if (!i.isEmpty()) {
a.add(i);
}
}

yield a.isEmpty() ? EmptyChemicalIngredient.INSTANCE : a.size() == 1 ? a.getFirst() : new CompoundChemicalIngredient(a);
}
default -> {
var str = from.toString();

if (str.isEmpty() || str.equals("mekanism:empty")) {
yield EmptyChemicalIngredient.INSTANCE;
} else {
try {
yield readIngredient(new StringReader(str));
} catch (CommandSyntaxException ex) {
ex.printStackTrace();
yield EmptyChemicalIngredient.INSTANCE;
}
}
}
};
}

static ChemicalIngredient readIngredient(StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();

if (reader.peek() == '#') {
reader.skip();
return new TagChemicalIngredient(TagKey.create(MekanismAPI.CHEMICAL_REGISTRY_NAME, ResourceLocation.read(reader)));
}

return new SingleChemicalIngredient(read(reader).getAsHolder());
}

@HideFromJS
static ChemicalStackIngredient wrapStackIngredient(Object from) {
return new ChemicalStackIngredient(EmptyChemicalIngredient.INSTANCE, 1);
static ChemicalStackIngredient stackIngredientOf(Object from) {
return switch (from) {
case null -> new ChemicalStackIngredient(EmptyChemicalIngredient.INSTANCE, 1);
case ChemicalStackIngredient c -> c;
case ChemicalIngredient c -> new ChemicalStackIngredient(c, FluidType.BUCKET_VOLUME);
case ChemicalStack c -> new ChemicalStackIngredient(new SingleChemicalIngredient(c.getChemicalHolder()), c.getAmount());
case Chemical c -> new ChemicalStackIngredient(new SingleChemicalIngredient(c.getAsHolder()), FluidType.BUCKET_VOLUME);
default -> {
var str = from.toString();

try {
yield readStackIngredient(new StringReader(str));
} catch (CommandSyntaxException ex) {
ex.printStackTrace();
yield new ChemicalStackIngredient(EmptyChemicalIngredient.INSTANCE, 1);
}
}
};
}

static ChemicalStackIngredient readStackIngredient(StringReader reader) throws CommandSyntaxException {
reader.skipWhitespace();
var amount = FluidWrapper.readFluidAmount(reader);
reader.skipWhitespace();
var ingredient = readIngredient(reader);
return new ChemicalStackIngredient(ingredient, amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public void registerBindings(BindingRegistry bindings) {
@Override
public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(Chemical.class, MekanismChemicalWrapper::of);
registry.register(ChemicalStack.class, MekanismChemicalWrapper::wrapStack);
registry.register(ChemicalIngredient.class, MekanismChemicalWrapper::wrapIngredient);
registry.register(ChemicalStackIngredient.class, MekanismChemicalWrapper::wrapStackIngredient);
registry.register(ChemicalStack.class, MekanismChemicalWrapper::stackOf);
registry.register(ChemicalIngredient.class, MekanismChemicalWrapper::ingredientOf);
registry.register(ChemicalStackIngredient.class, MekanismChemicalWrapper::stackIngredientOf);
}

@Override
public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register(ChemicalRecipeComponents.CHEMICAL);
registry.register(ChemicalRecipeComponents.CHEMICAL_STACK);
registry.register(ChemicalRecipeComponents.CHEMICAL_INGREDIENT);
registry.register(ChemicalRecipeComponents.CHEMICAL_STACK_INGREDIENT);
registry.register(ChemicalComponents.CHEMICAL);
registry.register(ChemicalComponents.CHEMICAL_STACK);
registry.register(ChemicalComponents.CHEMICAL_INGREDIENT);
registry.register(ChemicalComponents.CHEMICAL_STACK_INGREDIENT);
}

@Override
Expand Down

0 comments on commit d67aeea

Please sign in to comment.