Skip to content

Commit

Permalink
IterativeSlotWidget for EMI previews
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Jul 10, 2023
1 parent 7d7d63e commit 2f767a7
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 53 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ org.gradle.jvmargs = -Xmx1G
org.gradle.parallel = true

# Mod Properties
version = 2.2.9
version = 2.2.10
maven_group = folk.sisby
archives_base_name = tinkerers_smithing
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package folk.sisby.tinkerers_smithing.client.emi;

import dev.emi.emi.api.render.EmiRender;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.SlotWidget;
import dev.emi.emi.runtime.EmiDrawContext;
import net.minecraft.client.util.math.MatrixStack;

import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.Function;


public class IterativeSlotWidget extends SlotWidget {
private static final int INCREMENT = 1000;
private final BiFunction<Random, Long, EmiIngredient> stackSupplier;
private final int unique;
private long lastGenerate = 0;
private EmiIngredient stack = null;

public IterativeSlotWidget(BiFunction<Random, Long, EmiIngredient> stackSupplier, int unique, int x, int y) {
super(EmiStack.EMPTY, x, y);
this.stackSupplier = stackSupplier;
this.unique = unique;
}

public IterativeSlotWidget(Function<Long, EmiIngredient> stackSupplier, int x, int y) {
super(EmiStack.EMPTY, x, y);
this.stackSupplier = (r, i) -> stackSupplier.apply(i);
this.unique = 0;
}

public IterativeSlotWidget(Function<Random, EmiIngredient> stackSupplier, int unique, int x, int y) {
super(EmiStack.EMPTY, x, y);
this.stackSupplier = (r, i) -> stackSupplier.apply(r);
this.unique = unique;
}

@Override
public void drawOverlay(MatrixStack matrices, int mouseX, int mouseY, float delta) {
EmiDrawContext context = EmiDrawContext.wrap(matrices);
if (!getStack().isEmpty()) {
int off = 1;
if (output) {
off = 5;
}
EmiRender.renderIngredientIcon(getStack(), context.raw(), x + off, y + off);
}
super.drawOverlay(context.raw(), mouseX, mouseY, delta);
}

@Override
public EmiIngredient getStack() {
long time = System.currentTimeMillis() / INCREMENT;
if (stack == null || time > lastGenerate) {
lastGenerate = time;
stack = stackSupplier.apply(getRandom(time), time);
}
return stack;
}

private Random getRandom(long time) {
return new Random(new Random(time ^ unique).nextInt());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package folk.sisby.tinkerers_smithing.client.emi.recipe;

import dev.emi.emi.EmiUtil;
import dev.emi.emi.api.recipe.EmiRecipe;
import dev.emi.emi.api.recipe.EmiRecipeCategory;
import dev.emi.emi.api.recipe.VanillaEmiRecipeCategories;
Expand All @@ -9,6 +8,7 @@
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import folk.sisby.tinkerers_smithing.TinkerersSmithing;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.client.gui.tooltip.OrderedTextTooltipComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -19,11 +19,9 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Random;

public class EmiAnvilDeworkRecipe implements EmiRecipe {
private final Item tool;
private final int uniq = EmiUtil.RANDOM.nextInt();

public EmiAnvilDeworkRecipe(Item tool) {
this.tool = tool;
Expand Down Expand Up @@ -68,18 +66,18 @@ public int getDisplayHeight() {
public void addWidgets(WidgetHolder widgets) {
widgets.addTexture(EmiTexture.PLUS, 27, 3);
widgets.addTexture(EmiTexture.EMPTY_ARROW, 75, 1);
widgets.addGeneratedSlot(r -> getTool(r, false), uniq, 0, 0).appendTooltip(ingredient -> new OrderedTextTooltipComponent(Text.literal("Repair Cost: " + ingredient.getEmiStacks().get(0).getItemStack().getRepairCost()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)).asOrderedText()));
widgets.addGeneratedSlot(this::getRepairStack, uniq, 49, 0);
widgets.addGeneratedSlot(r -> getTool(r, true), uniq, 107, 0).appendTooltip(ingredient -> new OrderedTextTooltipComponent(Text.literal("Repair Cost: " + ingredient.getEmiStacks().get(0).getItemStack().getRepairCost()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)).asOrderedText())).recipeContext(this);
widgets.add(new IterativeSlotWidget(i -> getTool(i, false),0, 0).appendTooltip(ingredient -> new OrderedTextTooltipComponent(Text.literal("Repair Cost: " + ingredient.getEmiStacks().get(0).getItemStack().getRepairCost()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)).asOrderedText())));
widgets.add(new IterativeSlotWidget(this::getRepairStack, 49, 0));
widgets.add(new IterativeSlotWidget(i -> getTool(i, true), 107, 0).appendTooltip(ingredient -> new OrderedTextTooltipComponent(Text.literal("Repair Cost: " + ingredient.getEmiStacks().get(0).getItemStack().getRepairCost()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)).asOrderedText())).recipeContext(this));
}

private int getStackCount(Random r) {
return r.nextInt(1, 6);
private int getStackCount(long i) {
return Math.floorMod(i, 5) + 1;
}

private EmiIngredient getTool(Random r, boolean applied) {
private EmiIngredient getTool(long i, boolean applied) {
ItemStack stack = tool.getDefaultStack();
int stackCount = getStackCount(r);
int stackCount = getStackCount(i);
int work = (int) Math.pow(2, 5 - (applied ? stackCount : 0)) - 1;
if (work <= 0) {
return EmiStack.of(tool);
Expand All @@ -88,7 +86,7 @@ private EmiIngredient getTool(Random r, boolean applied) {
return EmiStack.of(stack);
}

private EmiIngredient getRepairStack(Random r) {
return EmiIngredient.of(TinkerersSmithing.DEWORK_INGREDIENTS).copy().setAmount(getStackCount(r));
private EmiIngredient getRepairStack(long i) {
return EmiIngredient.of(TinkerersSmithing.DEWORK_INGREDIENTS).copy().setAmount(getStackCount(i));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
Expand Down Expand Up @@ -66,9 +67,9 @@ public int getDisplayHeight() {
public void addWidgets(WidgetHolder widgets) {
widgets.addTexture(EmiTexture.PLUS, 27, 3);
widgets.addTexture(EmiTexture.EMPTY_ARROW, 75, 1);
widgets.addGeneratedSlot(r -> getTool(r, false), uniq, 0, 0);
widgets.add(new IterativeSlotWidget((r) -> getTool(r, false), uniq, 0, 0));
widgets.addSlot(repairMaterial, 49, 0);
widgets.addGeneratedSlot(r -> getTool(r, true), uniq, 107, 0).recipeContext(this);
widgets.add(new IterativeSlotWidget((r) -> getTool(r, true), uniq, 107, 0).recipeContext(this));
}

private EmiStack getTool(Random r, boolean repaired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import dev.emi.emi.api.recipe.EmiPatternCraftingRecipe;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.GeneratedSlotWidget;
import dev.emi.emi.api.widget.SlotWidget;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
Expand All @@ -27,37 +27,37 @@ public EmiCraftingRepairRecipe(Item tool, List<Ingredient> ingredients, int cost
@Override
public SlotWidget getInputWidget(int slot, int x, int y) {
if (slot == 0) {
return new GeneratedSlotWidget(r -> getTool(r, false), unique, x, y);
return new IterativeSlotWidget((r, i) -> getTool(r, i,false), unique, x, y);
} else if (slot <= cost) {
return new GeneratedSlotWidget(r -> {
int d = r.nextInt(tool.getMaxDamage());
int stackCount = getStackCount(r, d);
return slot <= stackCount ? repairMaterial : EmiStack.EMPTY;
}, unique, x, y);
return new IterativeSlotWidget((r, i) -> slot <= getStackCount(i) ? repairMaterial : EmiStack.EMPTY, unique, x, y);
}
return new SlotWidget(EmiStack.EMPTY, x, y);
}

@Override
public SlotWidget getOutputWidget(int x, int y) {
return new GeneratedSlotWidget(r -> getTool(r, true), unique, x, y);
return new IterativeSlotWidget((r, i) -> getTool(r, i,true), unique, x, y);
}

private int getStackCount(Random r, int damage) {
return r.nextInt(1, (damage * cost) / tool.getMaxDamage() + 2);
private int getStackCount(long i) {
return Math.floorMod(i, cost) + 1;
}

private EmiStack getTool(Random r, boolean result) {
private int getStackDamage(Random r, int units) {
return r.nextInt(((int) Math.ceil((tool.getMaxDamage() * (units - 1)) / (double) cost)), tool.getMaxDamage());
}

private EmiStack getTool(Random r, long i, boolean result) {
ItemStack stack = tool.getDefaultStack();
int d = r.nextInt(tool.getMaxDamage());
int stackCount = getStackCount(r, d);
int units = getStackCount(i);
int damage = getStackDamage(r, units);
if (result) {
d = Math.max(0, d - ((int) Math.ceil((tool.getMaxDamage() * (stackCount)) / (double) cost)));
damage = Math.max(0, damage - ((int) Math.ceil((tool.getMaxDamage() * (units)) / (double) cost)));
}
if (d == 0) {
if (damage == 0) {
return EmiStack.of(tool);
}
stack.setDamage(d);
stack.setDamage(damage);
return EmiStack.of(stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import dev.emi.emi.api.recipe.EmiPatternCraftingRecipe;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.GeneratedSlotWidget;
import dev.emi.emi.api.widget.SlotWidget;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
Expand All @@ -29,7 +29,7 @@ public EmiCraftingUpgradeRecipe(Item tool, List<Ingredient> ingredients, Item re
@Override
public SlotWidget getInputWidget(int slot, int x, int y) {
if (slot == 0) {
return new GeneratedSlotWidget(r -> EmiStack.of(getTool(r, false)), unique, x, y);
return new IterativeSlotWidget((r) -> EmiStack.of(getTool(r, false)), unique, x, y);
} else if (slot <= cost) {
return new SlotWidget(upgradeMaterial, x, y);
}
Expand All @@ -38,7 +38,7 @@ public SlotWidget getInputWidget(int slot, int x, int y) {

@Override
public SlotWidget getOutputWidget(int x, int y) {
return new GeneratedSlotWidget(r -> EmiStack.of(getTool(r, true)), unique, x, y);
return new IterativeSlotWidget((r) -> EmiStack.of(getTool(r, true)), unique, x, y);
}

private ItemStack getTool(Random r, boolean result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -71,24 +72,24 @@ public void addWidgets(WidgetHolder widgets) {
widgets.addTexture(EmiTexture.PLUS, 27, 3);
widgets.addTexture(EmiTexture.EMPTY_ARROW, 75, 1);
widgets.addSlot(EmiStack.of(tool), 0, 0);
widgets.addGeneratedSlot(this::getSacrificeStack, uniq, 49, 0);
widgets.addGeneratedSlot(this::getTool, uniq, 107, 0).recipeContext(this);
widgets.add(new IterativeSlotWidget(this::getSacrificeStack, uniq, 49, 0));
widgets.add(new IterativeSlotWidget(this::getTool, uniq, 107, 0).recipeContext(this));
}

private Map.Entry<Item, Integer> getSacrificeTool(Random r) {
return this.sacrificeTools.entrySet().stream().toList().get(r.nextInt(this.sacrificeTools.size()));
private Map.Entry<Item, Integer> getSacrificeTool(long i) {
return this.sacrificeTools.entrySet().stream().toList().get(Math.floorMod(i, this.sacrificeTools.size()));
}

private EmiStack getSacrificeStack(Random r) {
Map.Entry<Item, Integer> sacrificeTool = getSacrificeTool(r);
private EmiStack getSacrificeStack(Random r, long i) {
Map.Entry<Item, Integer> sacrificeTool = getSacrificeTool(i);
ItemStack sacrificeStack = sacrificeTool.getKey().getDefaultStack();
int sacrificeDamage = r.nextInt(sacrificeStack.getMaxDamage());
sacrificeStack.setDamage(sacrificeDamage);
return sacrificeDamage == 0 ? EmiStack.of(sacrificeTool.getKey()) : EmiStack.of(sacrificeStack);
}

private EmiStack getTool(Random r) {
Map.Entry<Item, Integer> sacrificeTool = getSacrificeTool(r);
private EmiStack getTool(Random r, long i) {
Map.Entry<Item, Integer> sacrificeTool = getSacrificeTool(i);
int sacrificeDamage = r.nextInt(sacrificeTool.getKey().getMaxDamage());
int resultDamage = (int) Math.ceil(resultTool.getMaxDamage() - ((sacrificeTool.getKey().getMaxDamage() - sacrificeDamage) * ((double) sacrificeTool.getValue() * resultTool.getMaxDamage()) / ((double)sacrificeTool.getKey().getMaxDamage() * this.resultCost)));
ItemStack stack = resultTool.getDefaultStack();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package folk.sisby.tinkerers_smithing.client.emi.recipe;

import dev.emi.emi.EmiUtil;
import dev.emi.emi.api.recipe.EmiRecipe;
import dev.emi.emi.api.recipe.EmiRecipeCategory;
import dev.emi.emi.api.recipe.VanillaEmiRecipeCategories;
import dev.emi.emi.api.render.EmiTexture;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import folk.sisby.tinkerers_smithing.client.emi.IterativeSlotWidget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Random;

public class EmiSmithingUpgradeRecipe implements EmiRecipe {
private final Item tool;
private final EmiIngredient upgradeMaterial;
private final Item resultTool;
private final int cost;
private final int uniq = EmiUtil.RANDOM.nextInt();

public EmiSmithingUpgradeRecipe(Item tool, List<Ingredient> ingredients, Item resultTool, int cost) {
this.tool = tool;
Expand Down Expand Up @@ -71,26 +69,26 @@ public void addWidgets(WidgetHolder widgets) {
widgets.addTexture(EmiTexture.PLUS, 27, 3);
widgets.addTexture(EmiTexture.EMPTY_ARROW, 75, 1);
widgets.addSlot(EmiStack.of(tool), 0, 0);
widgets.addGeneratedSlot(this::getRepairStack, uniq, 49, 0);
widgets.addGeneratedSlot(this::getTool, uniq, 107, 0).recipeContext(this);
widgets.add(new IterativeSlotWidget(this::getRepairStack, 49, 0));
widgets.add(new IterativeSlotWidget(this::getTool, 107, 0).recipeContext(this));
}

private int getStackCount(Random r) {
private int getStackCount(long i) {
int minStack = Math.max(cost - 4, 1);
return r.nextInt(minStack, cost + 1);
return Math.floorMod(i, cost + 1 - minStack) + minStack;
}

private EmiStack getTool(Random r) {
private EmiStack getTool(long i) {
ItemStack stack = resultTool.getDefaultStack();
int stackCount = getStackCount(r);
int stackCount = getStackCount(i);
if (stackCount == cost) {
return EmiStack.of(resultTool);
}
stack.setDamage((int) Math.floor(resultTool.getMaxDamage() * ((cost - stackCount) / 4.0)));
return EmiStack.of(stack);
}

private EmiIngredient getRepairStack(Random r) {
return upgradeMaterial.copy().setAmount(getStackCount(r));
private EmiIngredient getRepairStack(long i) {
return upgradeMaterial.copy().setAmount(getStackCount(i));
}
}

0 comments on commit 2f767a7

Please sign in to comment.