Skip to content

Commit

Permalink
addon support WIP, optimizations, etc etc
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAHuman-xD committed May 3, 2024
1 parent 56f7023 commit 68e7c80
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.justahuman.slimefun_essentials.api;

import me.justahuman.slimefun_essentials.client.ResourceLoader;
import me.justahuman.slimefun_essentials.client.SlimefunItemStack;
import me.justahuman.slimefun_essentials.client.SlimefunRecipeComponent;
import me.justahuman.slimefun_essentials.utils.Utils;
import net.minecraft.entity.EntityType;
Expand All @@ -14,14 +13,14 @@
import org.jetbrains.annotations.NotNull;

public interface IdInterpreter<T> {
default T interpretId(@NotNull SlimefunRecipeComponent component, @NotNull String id, @NotNull T defaultValue) {
default T interpretId(@NotNull SlimefunRecipeComponent component, @NotNull String id, @NotNull T def) {
if (id.isEmpty() || id.isBlank()) {
return defaultValue;
return def;
}

if (!id.contains(":")) {
Utils.warn("Invalid Ingredient Id:" + id);
return defaultValue;
return def;
}

int chance = 100;
Expand All @@ -31,65 +30,89 @@ default T interpretId(@NotNull SlimefunRecipeComponent component, @NotNull Strin
id = id.substring(0, id.indexOf("%"));
} catch (Exception ignored) {}
}

int damage = 0;
if (id.contains("^")) {
try {
damage = Integer.parseInt(id.substring(id.indexOf("^") + 1));
id = id.substring(0, id.indexOf("^"));
} catch (Exception ignored) {}
}

boolean consumed = true;
if (id.endsWith("*")) {
consumed = false;
id = id.substring(id.lastIndexOf('*'));
}

final String type = id.substring(0, id.indexOf(":"));
final String count = id.substring(id.indexOf(":") + 1);
int amount = 1;
try {
amount = Integer.parseInt(count);
} catch (Exception ignored) {}


// Slimefun Item
if (ResourceLoader.getSlimefunItems().containsKey(type)) {
return fromSlimefunItemStack(chance, ResourceLoader.getSlimefunItems().get(type).copy(), amount, defaultValue);
final ItemStack itemStack = ResourceLoader.getSlimefunItems().get(type).copy().itemStack();
if (damage > 0) {
itemStack.setDamage(damage);
}
return fromItemStack(chance, consumed, itemStack, amount, def);
}

// Complex Item
if (type.startsWith("?")) {
else if (type.startsWith("?")) {
int index = 0;
try {
index = Integer.parseInt(type.substring(1));
} catch (Exception ignored) {}
return fromItemStack(chance, component.getComplexStacks().get(index), amount, defaultValue);
return fromItemStack(chance, consumed, component.getComplexStacks().get(index), amount, def);
}
// Entity
else if (type.startsWith("@")) {
final Identifier identifier = new Identifier("minecraft:" + type.substring(1));
if (! Registries.ENTITY_TYPE.containsId(identifier)) {
Utils.warn("Invalid Ingredient Entity Id: " + id);
return defaultValue;
return def;
}
return fromEntityType(chance, Registries.ENTITY_TYPE.get(identifier), amount, defaultValue);
return fromEntityType(chance, consumed, Registries.ENTITY_TYPE.get(identifier), amount, def);
}
// Fluid
else if (type.startsWith("~")) {
final Identifier identifier = new Identifier("minecraft:" + type.substring(1));
if (!Registries.FLUID.containsId(identifier)) {
Utils.warn("Invalid Ingredient Fluid Id: " + id);
return defaultValue;
return def;
}
return fromFluid(chance, Registries.FLUID.get(identifier), amount, defaultValue);
return fromFluid(chance, consumed, Registries.FLUID.get(identifier), amount, def);
}
// Tag
else if (type.startsWith("#")) {
final Identifier identifier = new Identifier("minecraft:" + type.substring(1));
return fromTag(chance, TagKey.of(Registries.ITEM.getKey(), identifier), amount, defaultValue);
return fromTag(chance, consumed, TagKey.of(Registries.ITEM.getKey(), identifier), amount, def);
}
// Experience
else if (type.equals("$")) {
return fromEntityType(chance, consumed, EntityType.EXPERIENCE_ORB, amount, def);
}
// Item (Or Mistake)
else {
final Identifier identifier = new Identifier("minecraft:" + type.toLowerCase());
if (!Registries.ITEM.containsId(identifier)) {
Utils.warn("Invalid Ingredient ItemStack Id: " + id);
return defaultValue;
return def;
}
return fromItemStack(chance, Registries.ITEM.get(identifier).getDefaultStack().copy(), amount, defaultValue);

final ItemStack itemStack = Registries.ITEM.get(identifier).getDefaultStack().copy();
if (damage > 0) {
itemStack.setDamage(damage);
}
return fromItemStack(chance, consumed, itemStack, amount, def);
}
}

T fromTag(int chance, TagKey<Item> tagKey, int amount, T defaultValue);
T fromItemStack(int chance, ItemStack itemStack, int amount, T defaultValue);
default T fromSlimefunItemStack(int chance, SlimefunItemStack slimefunItemStack, int amount, T defaultValue) {
return fromItemStack(chance, slimefunItemStack.itemStack(), amount, defaultValue);
}
T fromFluid(int chance, Fluid fluid, int amount, T defaultValue);
T fromEntityType(int chance, EntityType<?> entityType, int amount, T defaultValue);
T fromTag(int chance, boolean consumed, TagKey<Item> tagKey, int amount, T def);
T fromItemStack(int chance, boolean consumed, ItemStack itemStack, int amount, T def);
T fromFluid(int chance, boolean consumed, Fluid fluid, int amount, T def);
T fromEntityType(int chance, boolean consumed, EntityType<?> entityType, int amount, T def);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ public record SlimefunItemGroup(Identifier identifier, ItemStack itemStack, List

public static void deserialize(String addon, String id, JsonObject groupObject) {
final Identifier identifier = new Identifier(addon, id);
final ItemStack itemStack = JsonUtils.deserializeItem(JsonUtils.getObjectOrDefault(groupObject, "item", null));
final ItemStack itemStack = JsonUtils.deserializeItem(JsonUtils.getObject(groupObject, "item", null));
final List<String> content = new ArrayList<>();
final List<String> requirements = new ArrayList<>();

for (JsonElement element : JsonUtils.getArrayOrDefault(groupObject, "items", new JsonArray())) {
for (JsonElement element : JsonUtils.getArray(groupObject, "items", new JsonArray())) {
if (element instanceof JsonPrimitive primitive && primitive.isString()) {
content.add(primitive.getAsString());
}
}

for (JsonElement element : JsonUtils.getArrayOrDefault(groupObject, "nested", new JsonArray())) {
for (JsonElement element : JsonUtils.getArray(groupObject, "nested", new JsonArray())) {
if (element instanceof JsonPrimitive primitive && primitive.isString()) {
content.add(primitive.getAsString());
}
}

for (JsonElement element : JsonUtils.getArrayOrDefault(groupObject, "locked", new JsonArray())) {
for (JsonElement element : JsonUtils.getArray(groupObject, "locked", new JsonArray())) {
if (element instanceof JsonPrimitive primitive && primitive.isString()) {
requirements.add(primitive.getAsString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public static void deserialize(String id, JsonObject labelObject) {
for (String mode : labelObject.keySet()) {
if (labelObject.get(mode) instanceof JsonObject settings) {
builder.mode(DrawMode.valueOf(mode),
JsonUtils.getIntegerOrDefault(settings, "u", 0),
JsonUtils.getIntegerOrDefault(settings, "v", 0),
JsonUtils.getIntegerOrDefault(settings, "width", 13),
JsonUtils.getIntegerOrDefault(settings, "height", 13));
JsonUtils.getInt(settings, "u", 0),
JsonUtils.getInt(settings, "v", 0),
JsonUtils.getInt(settings, "width", 13),
JsonUtils.getInt(settings, "height", 13));
}
}
slimefunLabels.put(id, builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ public SlimefunRecipe(SlimefunRecipeCategory parent, Integer time, Integer energ
}

public static SlimefunRecipe deserialize(SlimefunRecipeCategory parent, JsonObject recipeObject, Integer workstationEnergy) {
final Integer time = JsonUtils.getIntegerOrDefault(recipeObject, "time", null);
final Integer energy = JsonUtils.getIntegerOrDefault(recipeObject, "energy", workstationEnergy);
final Integer time = JsonUtils.getInt(recipeObject, "time", null);
final Integer energy = JsonUtils.getInt(recipeObject, "energy", workstationEnergy);
final List<SlimefunRecipeComponent> inputs = new ArrayList<>();
final List<SlimefunRecipeComponent> outputs = new ArrayList<>();
final List<SlimefunLabel> labels = new ArrayList<>();
final JsonArray complex = JsonUtils.getArrayOrDefault(recipeObject, "complex", new JsonArray());
final JsonArray complex = JsonUtils.getArray(recipeObject, "complex", new JsonArray());

for (JsonElement inputElement : JsonUtils.getArrayOrDefault(recipeObject, "inputs", new JsonArray())) {
for (JsonElement inputElement : JsonUtils.getArray(recipeObject, "inputs", new JsonArray())) {
final SlimefunRecipeComponent inputRecipeElement = SlimefunRecipeComponent.deserialize(complex, inputElement);
if (inputRecipeElement != null) {
inputs.add(inputRecipeElement);
}
}

for (JsonElement outputElement : JsonUtils.getArrayOrDefault(recipeObject, "outputs", new JsonArray())) {
for (JsonElement outputElement : JsonUtils.getArray(recipeObject, "outputs", new JsonArray())) {
final SlimefunRecipeComponent outputRecipeElement = SlimefunRecipeComponent.deserialize(complex, outputElement);
if (outputRecipeElement != null) {
outputs.add(outputRecipeElement);
}
}

for (JsonElement labelElement : JsonUtils.getArrayOrDefault(recipeObject, "labels", new JsonArray())) {
for (JsonElement labelElement : JsonUtils.getArray(recipeObject, "labels", new JsonArray())) {
if (! (labelElement instanceof JsonPrimitive jsonPrimitive) || ! jsonPrimitive.isString()) {
continue;
}
Expand All @@ -73,7 +73,7 @@ public void fillInputs(int size) {
}

for (int i = this.inputs.size(); i < size; i++) {
inputs.add(new SlimefunRecipeComponent(""));
inputs.add(SlimefunRecipeComponent.EMPTY);
}
}

Expand All @@ -88,7 +88,7 @@ public void fillOutputs(int size) {
}

for (int i = this.inputs.size(); i < size; i++) {
outputs.add(new SlimefunRecipeComponent(""));
outputs.add(SlimefunRecipeComponent.EMPTY);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ public List<SlimefunRecipe> childRecipes() {
public static void deserialize(String id, JsonObject categoryObject) {
final ItemStack itemStack = ResourceLoader.getSlimefunItem(id) != null
? ResourceLoader.getSlimefunItem(id).itemStack()
: JsonUtils.deserializeItem(JsonUtils.getObjectOrDefault(categoryObject, "item", new JsonObject()));
final String type = JsonUtils.getStringOrDefault(categoryObject, "type", "process");
final Integer speed = JsonUtils.getIntegerOrDefault(categoryObject, "speed", null);
final Integer energy = JsonUtils.getIntegerOrDefault(categoryObject, "energy", null);
: JsonUtils.deserializeItem(JsonUtils.getObject(categoryObject, "item", new JsonObject()));
final String type = JsonUtils.getString(categoryObject, "type", "process");
final Integer speed = JsonUtils.getInt(categoryObject, "speed", null);
final Integer energy = JsonUtils.getInt(categoryObject, "energy", null);
final List<SlimefunRecipe> recipes = new ArrayList<>();

final SlimefunRecipeCategory category = new SlimefunRecipeCategory(id, itemStack, type, speed, energy, recipes);
for (JsonElement recipeElement : JsonUtils.getArrayOrDefault(categoryObject, "recipes", new JsonArray())) {
for (JsonElement recipeElement : JsonUtils.getArray(categoryObject, "recipes", new JsonArray())) {
if (recipeElement instanceof JsonObject recipeObject) {
recipes.add(SlimefunRecipe.deserialize(category, recipeObject, energy));
}
}

toCopy.put(id, JsonUtils.getStringOrDefault(categoryObject, "copy", ""));
toCopy.put(id, JsonUtils.getString(categoryObject, "copy", ""));
recipeCategories.put(id, category);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@Getter
public class SlimefunRecipeComponent {
public static final SlimefunRecipeComponent EMPTY = new SlimefunRecipeComponent(new JsonArray(), "");
private final List<ItemStack> complexStacks = new ArrayList<>();
private final String id;
private final List<String> multiId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
import java.util.List;

public class EmiRecipeInterpreter implements IdInterpreter<EmiIngredient> {
public List<EmiIngredient> getInputIngredients(SlimefunRecipe slimefunRecipe) {
return slimefunRecipe.inputs() != null && !slimefunRecipe.inputs().isEmpty() ? slimefunRecipe.inputs().stream().map(this::emiIngredientFromComponent).toList() : new ArrayList<>();
public List<EmiIngredient> getInputIngredients(SlimefunRecipe recipe) {
return recipe.inputs() != null && !recipe.inputs().isEmpty()
? recipe.inputs().stream().map(this::emiIngredientFromComponent).toList()
: new ArrayList<>();
}

public List<EmiStack> getOutputStacks(SlimefunRecipe slimefunRecipe) {
return slimefunRecipe.outputs() != null && !slimefunRecipe.outputs().isEmpty() ? slimefunRecipe.outputs().stream().map(this::emiStackFromComponent).toList() : new ArrayList<>();
public List<EmiStack> getOutputStacks(SlimefunRecipe recipe) {
return recipe.outputs() != null && !recipe.outputs().isEmpty()
? recipe.outputs().stream().map(this::emiStackFromComponent).toList()
: new ArrayList<>();
}

public EmiStack emiStackFromComponent(SlimefunRecipeComponent component) {
final EmiIngredient emiIngredient = emiIngredientFromComponent(component);
if (emiIngredient instanceof EmiStack emiStack) {
final EmiIngredient ingredient = emiIngredientFromComponent(component);
if (ingredient instanceof EmiStack emiStack) {
return emiStack;
}

Expand All @@ -48,22 +52,22 @@ public EmiIngredient emiIngredientFromComponent(SlimefunRecipeComponent componen
}

@Override
public EmiIngredient fromTag(int chance, TagKey<Item> tagKey, int amount, EmiIngredient defaultValue) {
public EmiIngredient fromTag(int chance, boolean consumed, TagKey<Item> tagKey, int amount, EmiIngredient def) {
return EmiIngredient.of(tagKey, amount).setChance(chance / 100F);
}

@Override
public EmiIngredient fromItemStack(int chance, ItemStack itemStack, int amount, EmiIngredient defaultValue) {
public EmiIngredient fromItemStack(int chance, boolean consumed, ItemStack itemStack, int amount, EmiIngredient def) {
return EmiStack.of(itemStack, amount).setChance(chance / 100F);
}

@Override
public EmiIngredient fromFluid(int chance, Fluid fluid, int amount, EmiIngredient defaultValue) {
public EmiIngredient fromFluid(int chance, boolean consumed, Fluid fluid, int amount, EmiIngredient def) {
return EmiStack.of(fluid).setChance(chance / 100F);
}

@Override
public EmiIngredient fromEntityType(int chance, EntityType<?> entityType, int amount, EmiIngredient defaultValue) {
public EmiIngredient fromEntityType(int chance, boolean consumed, EntityType<?> entityType, int amount, EmiIngredient def) {
return new EntityEmiStack(entityType, amount).setChance(chance / 100F);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ public void addIngredientObject(IRecipeSlotBuilder slotBuilder, Object ingredien
}

@Override
public Object fromTag(int chance, TagKey<Item> tagKey, int amount, Object defaultValue) {
public Object fromTag(int chance, boolean consumed, TagKey<Item> tagKey, int amount, Object def) {
Optional<RegistryEntryList.Named<Item>> optional = Registries.ITEM.getEntryList(tagKey);
if (optional.isEmpty()) {
return defaultValue;
return def;
}

return optional.get().stream().map(item -> new ItemStack(item, amount)).toList();
}

@Override
public Object fromItemStack(int chance, ItemStack itemStack, int amount, Object defaultValue) {
public Object fromItemStack(int chance, boolean consumed, ItemStack itemStack, int amount, Object def) {
itemStack.setCount(amount);
return itemStack;
}

@Override
public Object fromFluid(int chance, Fluid fluid, int amount, Object defaultValue) {
public Object fromFluid(int chance, boolean consumed, Fluid fluid, int amount, Object def) {
return new JeiFluidIngredient(fluid, amount);
}

@Override
public Object fromEntityType(int chance, EntityType<?> entityType, int amount, Object defaultValue) {
public Object fromEntityType(int chance, boolean consumed, EntityType<?> entityType, int amount, Object def) {
// TODO: add support for entities
return defaultValue;
return def;
}
}
Loading

0 comments on commit 68e7c80

Please sign in to comment.