-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IterativeSlotWidget for EMI previews
- Loading branch information
1 parent
7d7d63e
commit 2f767a7
Showing
8 changed files
with
117 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/folk/sisby/tinkerers_smithing/client/emi/IterativeSlotWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters