mirror of
https://github.com/GTNewHorizons/JustEnoughCalculation.git
synced 2026-11-14 01:04:06 +00:00
feat: simple item transfer
This commit is contained in:
1 parent
2ec160b9f0
commit
86213bf6c8
9 files changed
+102
-69
No files matched your search
@@ -40,7 +40,7 @@ public class JecGui extends GuiContainer {
|
||||
public static final int COLOR_GUI_GREY = 0xFFA1A1A1;
|
||||
public static final int COLOR_TEXT_RED = 0xFF0000;
|
||||
public static final int COLOR_TEXT_WHITE = 0xFFFFFF;
|
||||
protected static JecGui last;
|
||||
public static JecGui current;
|
||||
public static final boolean ALWAYS_TOOLTIP = false;
|
||||
|
||||
protected JecGui parent;
|
||||
@@ -56,6 +56,7 @@ public class JecGui extends GuiContainer {
|
||||
super(acceptsTransfer ? new ContainerTransfer() : new ContainerNonTransfer());
|
||||
this.parent = parent;
|
||||
this.root = root;
|
||||
if (inventorySlots instanceof JecContainer) ((JecContainer) inventorySlots).setGui(this);
|
||||
}
|
||||
|
||||
public static boolean mouseIn(int xPos, int yPos, int xSize, int ySize, int xMouse, int yMouse) {
|
||||
@@ -73,25 +74,25 @@ public class JecGui extends GuiContainer {
|
||||
}
|
||||
|
||||
public static JecGui getCurrent() {
|
||||
return last;
|
||||
return current;
|
||||
}
|
||||
|
||||
private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IDrawable root) {
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
JecGui parent;
|
||||
if (mc.currentScreen == null) parent = null;
|
||||
else if (!(mc.currentScreen instanceof JecGui)) parent = last;
|
||||
else if (!(mc.currentScreen instanceof JecGui)) parent = current;
|
||||
else if (updateParent) parent = (JecGui) mc.currentScreen;
|
||||
else parent = ((JecGui) mc.currentScreen).parent;
|
||||
JecGui toShow = new JecGui(parent, acceptsTransfer, root);
|
||||
mc.displayGuiScreen(toShow);
|
||||
last = toShow;
|
||||
current = toShow;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuiClosed() {
|
||||
super.onGuiClosed();
|
||||
last = null;
|
||||
current = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -392,18 +393,27 @@ public class JecGui extends GuiContainer {
|
||||
return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
|
||||
}
|
||||
|
||||
public static class ContainerTransfer extends Container {
|
||||
public static class JecContainer extends Container {
|
||||
JecGui gui;
|
||||
|
||||
public JecGui getGui() {
|
||||
return gui;
|
||||
}
|
||||
|
||||
public void setGui(JecGui gui) {
|
||||
this.gui = gui;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer playerIn) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ContainerNonTransfer extends Container {
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer playerIn) {
|
||||
return true;
|
||||
}
|
||||
public static class ContainerTransfer extends JecContainer {
|
||||
}
|
||||
|
||||
public static class ContainerNonTransfer extends JecContainer {
|
||||
}
|
||||
|
||||
public int getGuiLeft() {
|
||||
|
||||
@@ -51,7 +51,7 @@ public class DLabel implements IDrawable {
|
||||
gui.drawText(xPos + xSize / 2 + 7.5f,
|
||||
yPos + ySize / 2 + 7 - (int) (font.size * gui.getFontRenderer().FONT_HEIGHT), font,
|
||||
label.getAmountString());
|
||||
if (mouseIn(xMouse, yMouse)) {
|
||||
if (label != ILabel.EMPTY && mouseIn(xMouse, yMouse)) {
|
||||
gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF);
|
||||
ArrayList<String> buf = new ArrayList<>();
|
||||
buf.add(label.getDisplayName());
|
||||
|
||||
@@ -23,7 +23,7 @@ public class GuiCalculator extends DContainer {
|
||||
JecGui.getCurrent().hand = l;
|
||||
}))));
|
||||
add(new DButtonIcon(130, 7, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "calculator.recipe")
|
||||
.setListenerLeft(() -> JecGui.displayGui(new GuiRecipe())));
|
||||
.setListenerLeft(() -> JecGui.displayGui(true, true, new GuiRecipe())));
|
||||
add(new DButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH_N, Resource.BTN_SEARCH_F, "calculator.search"));
|
||||
add(new DLabelGroup(7, 87, 9, 4, RESULT));
|
||||
add(new DLabelGroup(7, 31, 8, 1, PICKER));
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
package me.towdium.jecalculation.client.gui.guis;
|
||||
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import me.towdium.jecalculation.client.gui.Resource;
|
||||
import me.towdium.jecalculation.client.gui.drawables.*;
|
||||
import me.towdium.jecalculation.core.labels.ILabel;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static me.towdium.jecalculation.client.gui.drawables.DLabel.enumMode.EDITOR;
|
||||
|
||||
@@ -43,4 +51,27 @@ public class GuiRecipe extends DContainer {
|
||||
removeAll(buttonYes, buttonNo, textField);
|
||||
}
|
||||
}
|
||||
|
||||
public void transfer(IRecipeHandler recipe, int recipeIndex) {
|
||||
List<List<ILabel>> buf = new ArrayList<>();
|
||||
List<ILabel> input = new ArrayList<>();
|
||||
List<ILabel> output = new ArrayList<>();
|
||||
|
||||
List<ILabel> raw = new ArrayList<>();
|
||||
List<ItemStack> itemStacks = recipe.getIngredientStacks(recipeIndex).stream()
|
||||
.map((positionedStack) -> positionedStack.item).collect(Collectors.toList());
|
||||
|
||||
itemStacks.forEach((itemStack) -> {
|
||||
raw.add(ILabel.CONVERTER_ITEM.toLabel(itemStack));
|
||||
});
|
||||
List<ILabel> guessed = ILabel.CONVERTER_ITEM.toLabel(itemStacks);
|
||||
input.add(guessed.isEmpty() ? ILabel.CONVERTER_ITEM.toLabel(itemStacks.get(0)) : guessed.get(0));
|
||||
buf.add(raw);
|
||||
|
||||
ItemStack outputStack = recipe.getResultStack(recipeIndex).item;
|
||||
output.add(ILabel.CONVERTER_ITEM.toLabel(outputStack));
|
||||
|
||||
groupInput.setLabel(input, 0);
|
||||
groupOutput.setLabel(output, 0);
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,9 @@ public interface ILabel {
|
||||
RegistryConverterFluid CONVERTER_FLUID = RegistryConverterFluid.INSTANCE;
|
||||
RegistryEditor EDITOR = RegistryEditor.INSTANCE;
|
||||
ILabel EMPTY = new LabelItemStack(ItemStackHelper.EMPTY_ITEM_STACK, 0);
|
||||
String LOCALIZE_PREFIX = "label";
|
||||
String LOCALIZE_SUFFIX = "name";
|
||||
|
||||
String FORMAT_BLUE = "\u00A79";
|
||||
String FORMAT_ITALIC = "\u00A7o";
|
||||
|
||||
ILabel increaseAmount();
|
||||
|
||||
@@ -71,8 +72,8 @@ public interface ILabel {
|
||||
/**
|
||||
* Since {@link ILabel} merging is bidirectional, it is redundant to
|
||||
* implement on both side. So this class is created for merging
|
||||
* {@link ILabel ILabel(s)}.
|
||||
* It uses singleton mode. First register merge functions, then use
|
||||
* {@link ILabel label(s)}.
|
||||
* It uses singleton mode. First registerGuess merge functions, then use
|
||||
* {@link #test(ILabel, ILabel)} and {@link #merge(ILabel, ILabel, boolean)}
|
||||
* to operate the {@link ILabel}.
|
||||
* For registering, see {@link RegistryDeserializer}.
|
||||
@@ -82,7 +83,7 @@ public interface ILabel {
|
||||
|
||||
static {
|
||||
INSTANCE = new RegistryEntryMerger();
|
||||
// register functions here
|
||||
// registerGuess functions here
|
||||
}
|
||||
|
||||
private Relation<String, MergerFunction> functions = new Relation<>();
|
||||
@@ -115,14 +116,14 @@ public interface ILabel {
|
||||
* @param a an {@link ILabel} to merge
|
||||
* @param b another {@link ILabel} to merge
|
||||
* @param add add together or cancel each other
|
||||
* @return merged {@link ILabel ILabel(s)} if not changed (no matter order), they cannot merge.
|
||||
* @return merged {@link ILabel label(s)} if not changed (no matter order), they cannot merge.
|
||||
*/
|
||||
Pair<ILabel, ILabel> merge(ILabel a, ILabel b, boolean add);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is used to register an {@link ILabel} type.
|
||||
* This class is used to registerGuess an {@link ILabel} type.
|
||||
* Here you can find the identifier and deserializer of one type.
|
||||
* For {@link ILabel} operations, see {@link RegistryEntryMerger}
|
||||
*/
|
||||
@@ -148,7 +149,7 @@ public interface ILabel {
|
||||
|
||||
/**
|
||||
* @param nbt NBT to deserialize
|
||||
* @return the recovered ILabel
|
||||
* @return the recovered label
|
||||
* A typical NBT structure of an {@link ILabel} is as follows:
|
||||
* <pre>{@code
|
||||
* {
|
||||
@@ -168,23 +169,36 @@ public interface ILabel {
|
||||
}
|
||||
|
||||
class RegistryConverter<T> {
|
||||
List<Function<List<T>, List<ILabel>>> handlers = new ArrayList<>();
|
||||
List<Function<List<T>, List<ILabel>>> handlersGuess = new ArrayList<>();
|
||||
List<Function<T, ILabel>> handlersRaw = new ArrayList<>();
|
||||
|
||||
private RegistryConverter() {
|
||||
}
|
||||
|
||||
public ILabel toEntry(T ingredient) {
|
||||
return handlers.stream().map(h -> h.apply(Collections.singletonList(ingredient)))
|
||||
.filter(l -> !l.isEmpty()).findFirst().orElse(Collections.singletonList(ILabel.EMPTY)).get(0);
|
||||
/**
|
||||
* @param ingredient the ingredient, possibly itemStack or FluidStack
|
||||
* @return the identical representation
|
||||
*/
|
||||
public ILabel toLabel(T ingredient) {
|
||||
return handlersRaw.stream().map(h -> h.apply(ingredient))
|
||||
.filter(Objects::nonNull).findFirst().orElse(ILabel.EMPTY);
|
||||
}
|
||||
|
||||
public List<ILabel> toEntry(List<T> ingredients) {
|
||||
return new ReversedIterator<>(handlers).stream().flatMap(h -> h.apply(ingredients).stream())
|
||||
.collect(Collectors.toList());
|
||||
/**
|
||||
* @param ingredients the list of ingredient, possibly itemStack or FluidStack
|
||||
* @return list of guessed representation, sorted by possibility
|
||||
*/
|
||||
public List<ILabel> toLabel(List<T> ingredients) {
|
||||
return new ReversedIterator<>(handlersGuess).stream().flatMap(h -> h.apply(ingredients).stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
void register(Function<List<T>, List<ILabel>> handler) {
|
||||
handlers.add(handler);
|
||||
void registerGuess(Function<List<T>, List<ILabel>> handler) {
|
||||
handlersGuess.add(handler);
|
||||
}
|
||||
|
||||
void registerRaw(Function<T, ILabel> handler) {
|
||||
handlersRaw.add(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,14 +208,14 @@ public interface ILabel {
|
||||
static {
|
||||
INSTANCE = new RegistryConverterItem();
|
||||
|
||||
INSTANCE.register(RegistryConverterItem::convertRawItemStack);
|
||||
INSTANCE.registerRaw(RegistryConverterItem::convertRawItemStack);
|
||||
}
|
||||
|
||||
private RegistryConverterItem() {
|
||||
}
|
||||
|
||||
public static List<ILabel> convertRawItemStack(List<ItemStack> iss) {
|
||||
return iss.stream().map(LabelItemStack::new).collect(Collectors.toList());
|
||||
public static ILabel convertRawItemStack(ItemStack is) {
|
||||
return new LabelItemStack(is);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class LabelItemStack extends LabelSimpleAmount {
|
||||
|
||||
@Override
|
||||
public List<String> getToolTip(List<String> existing) {
|
||||
existing.add("&9" + Utilities.getModName(itemStack));
|
||||
existing.add(FORMAT_BLUE + FORMAT_ITALIC + Utilities.getModName(itemStack));
|
||||
return existing;
|
||||
}
|
||||
|
||||
@@ -68,4 +68,9 @@ public class LabelItemStack extends LabelSimpleAmount {
|
||||
public void drawEntry(JecGui gui) {
|
||||
gui.drawItemStack(0, 0, itemStack, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof LabelItemStack && itemStack.equals(((LabelItemStack) obj).itemStack);
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ public class LabelOreDict extends LabelSimpleAmount {
|
||||
|
||||
@Override
|
||||
public List<String> getToolTip(List<String> existing) {
|
||||
existing.add("\u00A79\u00A7o" + JustEnoughCalculation.Reference.MODNAME);
|
||||
existing.add(FORMAT_BLUE + FORMAT_ITALIC + JustEnoughCalculation.Reference.MODNAME);
|
||||
return existing;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,48 +1,21 @@
|
||||
package me.towdium.jecalculation.nei;
|
||||
|
||||
import codechicken.nei.PositionedStack;
|
||||
import codechicken.nei.api.IOverlayHandler;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import me.towdium.jecalculation.JustEnoughCalculation;
|
||||
import me.towdium.jecalculation.client.gui.JecGui;
|
||||
import me.towdium.jecalculation.client.gui.guis.GuiRecipe;
|
||||
import me.towdium.jecalculation.utils.ItemStackHelper;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JecaOverlayHandler implements IOverlayHandler {
|
||||
@Override
|
||||
public void overlayRecipe(GuiContainer firstGui, IRecipeHandler recipe, int recipeIndex, boolean shift) {
|
||||
if(firstGui instanceof JecGui) {
|
||||
List<ItemStack> inputStacks = new ArrayList<>();
|
||||
LOOP:
|
||||
for (PositionedStack positionedStack : recipe.getIngredientStacks(recipeIndex)) {
|
||||
ItemStack itemStack = positionedStack.items[0];
|
||||
for (ItemStack exist : inputStacks) {
|
||||
if (ItemStackHelper.isTypeEqual(exist, itemStack)) {
|
||||
exist.stackSize += itemStack.stackSize;
|
||||
continue LOOP;
|
||||
}
|
||||
}
|
||||
inputStacks.add(itemStack.copy());
|
||||
JustEnoughCalculation.logger.info("shift ? " + shift);
|
||||
if (shift && firstGui instanceof JecGui) {
|
||||
JecGui gui = (JecGui) firstGui;
|
||||
if (gui.root instanceof GuiRecipe) {
|
||||
((GuiRecipe) gui.root).transfer(recipe, recipeIndex);
|
||||
}
|
||||
ItemStack outputStack = recipe.getResultStack(recipeIndex).items[0];
|
||||
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
JecGui.displayGui(new GuiRecipe());
|
||||
GuiContainer myGuiContainer = (GuiContainer) mc.currentScreen;
|
||||
if (myGuiContainer != null) {
|
||||
myGuiContainer.inventorySlots.getSlot(0).putStack(outputStack);
|
||||
for (int i = 8; i <= 8 + inputStacks.size() - 1 && i <= 19; i++) {
|
||||
myGuiContainer.inventorySlots.getSlot(i).putStack(inputStacks.get(i - 8));
|
||||
}
|
||||
//crafting item, nei may not have
|
||||
// myGuiContainer.inventorySlots.getSlot(4).putStack();
|
||||
}
|
||||
|
||||
} else {
|
||||
System.out.println(firstGui.getClass().toString());
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class NEIPlugin {
|
||||
|
||||
public static ILabel getEntryUnderMouse() {
|
||||
if (NEIPlugin.currentItemStack == null) return ILabel.EMPTY;
|
||||
else return registryItem.toEntry(NEIPlugin.currentItemStack);
|
||||
else return registryItem.toLabel(NEIPlugin.currentItemStack);
|
||||
}
|
||||
|
||||
public static void setEntryUnderMouse(ItemStack itemStack) {
|
||||
|
||||
Reference in new issue
Block a user