aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java
diff options
context:
space:
mode:
authorRoman / Nea <roman.graef@gmail.com>2021-12-30 14:37:02 +0100
committerGitHub <noreply@github.com>2021-12-30 14:37:02 +0100
commitb0b0b7567ec0656c60f2f3e4730c0edace353fb7 (patch)
tree0e2b23fe4c52b0242cefc8caee9ed887797542b2 /src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java
parentfc642887639d1918d68f4706e27ea59605a16fcb (diff)
downloadnotenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.tar.gz
notenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.tar.bz2
notenoughupdates-b0b0b7567ec0656c60f2f3e4730c0edace353fb7.zip
Adding support for more recipe types and forge recipes (#40)
* Foundations for support of different crafting recipe types. NeuRecipe is now a base class for a recipe which provides common concepts, such as inputs, outputs and a rendering task. GuiItemRecipe has been reworked to work with this new NeuRecipe. NeuManager now parses said recipes. This should be reworked to be a two step process (first register items, then register recipes). To keep compatibility with older repo versions, NeuRecipes are parse lenient and default to a crafting recipe. New recipes should be added in the `recipes` json field which is an array of json dictionaries, which have a `type` and other fields depending on the `type` of that recipe. This also adds support for having multiple recipes for a single item (e.g. uncrafting storage blocks). * Remove references in existing code * Recipe Generation * ring recipes * recipe generator v2 * quick forge * bugfixes and performance improvements * fix raw craft cost * reload hotm if you open the hotm tree inv * add myself to the changelog * replace quickforge formular with lookup table * do not crash anymore when opening recipes outside of skyblock * format coins differently * remove debug logs * change recipe generator so that it doesnt crash old versions
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java257
1 files changed, 113 insertions, 144 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java
index 210e1da7..e820378b 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java
@@ -1,33 +1,51 @@
package io.github.moulberry.notenoughupdates.miscgui;
-import com.google.gson.JsonObject;
+import com.google.common.collect.ImmutableList;
import io.github.moulberry.notenoughupdates.NEUManager;
+import io.github.moulberry.notenoughupdates.recipes.NeuRecipe;
+import io.github.moulberry.notenoughupdates.recipes.RecipeSlot;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
+import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
+import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
-import org.lwjgl.util.vector.Vector2f;
import java.awt.*;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.List;
public class GuiItemRecipe extends GuiScreen {
- private static final ResourceLocation resourcePacksTexture = new ResourceLocation("textures/gui/resource_packs.png");
- private static final ResourceLocation craftingTableGuiTextures = new ResourceLocation("textures/gui/container/crafting_table.png");
+ public static final ResourceLocation resourcePacksTexture = new ResourceLocation("textures/gui/resource_packs.png");
+
+ public static final int SLOT_SIZE = 16;
+ public static final int SLOT_SPACING = SLOT_SIZE + 2;
+ public static final int BUTTON_WIDTH = 7;
+ public static final int BUTTON_HEIGHT = 11;
+ public static final int BUTTON_POSITION_Y = 63;
+ public static final int BUTTON_POSITION_LEFT_X = 110;
+ public static final int BUTTON_POSITION_RIGHT_X = 147;
+ public static final int PAGE_STRING_X = 132;
+ public static final int PAGE_STRING_Y = 69;
+ public static final int TITLE_X = 28;
+ public static final int TITLE_Y = 6;
+ public static final int HOTBAR_SLOT_X = 8;
+ public static final int HOTBAR_SLOT_Y = 142;
+ public static final int PLAYER_INVENTORY_X = 8;
+ public static final int PLAYER_INVENTORY_Y = 84;
- private final List<ItemStack[]> craftMatrices;
- private final List<JsonObject> results;
private int currentIndex = 0;
private final String title;
+ private final List<NeuRecipe> craftingRecipes;
private final NEUManager manager;
public int guiLeft = 0;
@@ -35,159 +53,122 @@ public class GuiItemRecipe extends GuiScreen {
public int xSize = 176;
public int ySize = 166;
- public GuiItemRecipe(String title, List<ItemStack[]> craftMatrices, List<JsonObject> results, NEUManager manager) {
- this.craftMatrices = craftMatrices;
- this.results = results;
+ public GuiItemRecipe(String title, List<NeuRecipe> craftingRecipes, NEUManager manager) {
+ this.craftingRecipes = craftingRecipes;
this.manager = manager;
this.title = title;
}
- private String getCraftText() {
- if(results.get(currentIndex).has("crafttext")) {
- return results.get(currentIndex).get("crafttext").getAsString();
- } else {
- return "";
- }
+ public NeuRecipe getCurrentRecipe() {
+ currentIndex = MathHelper.clamp_int(currentIndex, 0, craftingRecipes.size());
+ return craftingRecipes.get(currentIndex);
+ }
+
+ public boolean isWithinRect(int x, int y, int topLeftX, int topLeftY, int width, int height) {
+ return topLeftX <= x && x <= topLeftX + width
+ && topLeftY <= y && y <= topLeftY + height;
+ }
+
+ private ImmutableList<RecipeSlot> getAllRenderedSlots() {
+ return ImmutableList.<RecipeSlot>builder()
+ .addAll(getPlayerInventory())
+ .addAll(getCurrentRecipe().getSlots()).build();
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();
-
- if(currentIndex < 0) {
- currentIndex = 0;
- } else if(currentIndex >= craftMatrices.size()) {
- currentIndex = craftMatrices.size()-1;
- }
-
FontRenderer fontRendererObj = Minecraft.getMinecraft().fontRendererObj;
this.guiLeft = (width - this.xSize) / 2;
this.guiTop = (height - this.ySize) / 2;
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
- Minecraft.getMinecraft().getTextureManager().bindTexture(craftingTableGuiTextures);
+
+ NeuRecipe currentRecipe = getCurrentRecipe();
+
+ Minecraft.getMinecraft().getTextureManager().bindTexture(currentRecipe.getBackground());
this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, this.xSize, this.ySize);
- List<String> tooltipToRender = null;
- for(int index=0; index <= 45; index++) {
- Vector2f pos = getPositionForIndex(index);
- Utils.drawItemStack(getStackForIndex(index), (int)pos.x, (int)pos.y);
-
- if(mouseX > pos.x && mouseX < pos.x+16) {
- if(mouseY > pos.y && mouseY < pos.y+16) {
- ItemStack stack = getStackForIndex(index);
- if(stack != null) {
- tooltipToRender = stack.getTooltip(Minecraft.getMinecraft().thePlayer, false);
- }
- }
- }
+ currentRecipe.drawExtraBackground(this);
+
+ List<RecipeSlot> slots = getAllRenderedSlots();
+ for (RecipeSlot slot : slots) {
+ Utils.drawItemStack(slot.getItemStack(), slot.getX(this), slot.getY(this));
}
- if(craftMatrices.size() > 1) {
- int guiX = mouseX - guiLeft;
- int guiY = mouseY - guiTop;
+ if (craftingRecipes.size() > 1) drawArrows(mouseX, mouseY);
- int buttonWidth = 7;
- int buttonHeight = 11;
+ Utils.drawStringScaledMaxWidth(title, fontRendererObj, guiLeft + TITLE_X, guiTop + TITLE_Y, false, xSize - 38, 0x404040);
- boolean leftSelected = false;
- boolean rightSelected = false;
+ currentRecipe.drawExtraInfo(this);
- if(guiY > + 63 && guiY < + 63 + buttonHeight) {
- if(guiX > + 110 && guiX < 110 + buttonWidth) {
- leftSelected = true;
- } else if(guiX > 147 && guiX < 147 + buttonWidth) {
- rightSelected = true;
- }
+ for (RecipeSlot slot : slots) {
+ if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) {
+ if (slot.getItemStack() == null) continue;
+ Utils.drawHoveringText(slot.getItemStack().getTooltip(Minecraft.getMinecraft().thePlayer, false), mouseX, mouseY, width, height, -1, fontRendererObj);
}
-
- Minecraft.getMinecraft().getTextureManager().bindTexture(resourcePacksTexture);
- //Left arrow
- Utils.drawTexturedRect(guiLeft+110, guiTop+63, 7, 11, 34/256f, 48/256f,
- 5/256f + (leftSelected ? 32/256f : 0), 27/256f + (leftSelected ? 32/256f : 0));
- //Right arrow
- Utils.drawTexturedRect(guiLeft+147, guiTop+63, 7, 11, 10/256f, 24/256f,
- 5/256f + (rightSelected ? 32/256f : 0), 27/256f + (rightSelected ? 32/256f : 0));
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
-
- String str = (currentIndex+1)+"/"+craftMatrices.size();
- Utils.drawStringCenteredScaledMaxWidth(str, fontRendererObj, guiLeft+132, guiTop+69,
- false, 24, Color.BLACK.getRGB());
}
+ currentRecipe.drawHoverInformation(this, mouseX, mouseY);
+ }
- Utils.drawStringCenteredScaledMaxWidth(getCraftText(), fontRendererObj, guiLeft+132, guiTop+25,
- false, 75, 4210752);
+ private void drawArrows(int mouseX, int mouseY) {
+ boolean leftSelected = isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_LEFT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
+ boolean rightSelected = isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_RIGHT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
- Utils.drawStringScaledMaxWidth(title, fontRendererObj, guiLeft+28, guiTop+6, title.contains("\u00a7"), xSize-38, 4210752);
+ Minecraft.getMinecraft().getTextureManager().bindTexture(resourcePacksTexture);
- if(tooltipToRender != null) {
- Utils.drawHoveringText(tooltipToRender, mouseX, mouseY, width, height, -1, fontRendererObj);
- }
- }
+ Utils.drawTexturedRect(guiLeft + BUTTON_POSITION_LEFT_X, guiTop + BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT,
+ 34 / 256f, 48 / 256f,
+ leftSelected ? 37 / 256f : 5 / 256f, leftSelected ? 59 / 256f : 27 / 256f
+ );
+ Utils.drawTexturedRect(guiLeft + BUTTON_POSITION_RIGHT_X, guiTop + BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT,
+ 10 / 256f, 24 / 256f,
+ rightSelected ? 37 / 256f : 5 / 256f, rightSelected ? 59 / 256f : 27 / 256f
+ );
+ GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
- public ItemStack getStackForIndex(int index) {
- if(index == 0) {
- return manager.jsonToStack(results.get(currentIndex));
- } else if(index >= 1 && index <= 9) {
- return craftMatrices.get(currentIndex)[index-1];
- } else {
- return Minecraft.getMinecraft().thePlayer.inventory.getStackInSlot(index-10);
- }
+ String selectedPage = (currentIndex + 1) + "/" + craftingRecipes.size();
+
+ Utils.drawStringCenteredScaledMaxWidth(selectedPage, fontRendererObj,
+ guiLeft + PAGE_STRING_X, guiTop + PAGE_STRING_Y, false, 24, Color.BLACK.getRGB());
}
- public Vector2f getPositionForIndex(int index) {
- //0 = result
- //1-9 = craft matrix
- //10-18 = hotbar
- //19-45 = player inv
-
- if(index == 0) {
- return new Vector2f(guiLeft+124, guiTop+35);
- } else if(index >= 1 && index <= 9) {
- index -= 1;
- int x = index % 3;
- int y = index / 3;
- return new Vector2f(guiLeft+30 + x*18, guiTop+17 + y * 18);
- } else if(index >= 10 && index <= 18) {
- index -= 10;
- return new Vector2f(guiLeft+8 + index*18, guiTop+142);
- } else if(index >= 19 && index <= 45) {
- index -= 19;
- int x = index % 9;
- int y = index / 9;
- return new Vector2f(guiLeft+8 + x*18, guiTop+84 + y*18);
+ public List<RecipeSlot> getPlayerInventory() {
+ List<RecipeSlot> slots = new ArrayList<>();
+ ItemStack[] inventory = Minecraft.getMinecraft().thePlayer.inventory.mainInventory;
+ int hotbarSize = InventoryPlayer.getHotbarSize();
+ for (int i = 0; i < inventory.length; i++) {
+ ItemStack item = inventory[i];
+ if (item == null || item.stackSize == 0) continue;
+ int row = i / hotbarSize;
+ int col = i % hotbarSize;
+ if (row == 0)
+ slots.add(new RecipeSlot(HOTBAR_SLOT_X + i * SLOT_SPACING, HOTBAR_SLOT_Y, item));
+ else
+ slots.add(new RecipeSlot(PLAYER_INVENTORY_X + col * SLOT_SPACING, PLAYER_INVENTORY_Y + (row - 1) * SLOT_SPACING, item));
}
- return null;
+ return slots;
}
@Override
public void handleKeyboardInput() throws IOException {
super.handleKeyboardInput();
- if(!Keyboard.getEventKeyState()) return;
-
ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
int width = scaledResolution.getScaledWidth();
int height = scaledResolution.getScaledHeight();
int mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth;
int mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1;
-
int keyPressed = Keyboard.getEventKey() == 0 ? Keyboard.getEventCharacter()+256 : Keyboard.getEventKey();
- for(int index=0; index <= 45; index++) {
- Vector2f pos = getPositionForIndex(index);
- if(mouseX > pos.x && mouseX < pos.x+16) {
- if(mouseY > pos.y && mouseY < pos.y+16) {
- ItemStack stack = getStackForIndex(index);
- if(stack != null) {
- if(keyPressed == manager.keybindViewRecipe.getKeyCode()) {
- manager.displayGuiItemRecipe(manager.getInternalNameForItem(stack), "");
- } else if(keyPressed == manager.keybindViewUsages.getKeyCode()) {
- manager.displayGuiItemUsages(manager.getInternalNameForItem(stack));
- }
- }
- return;
+ for (RecipeSlot slot : getAllRenderedSlots()) {
+ if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) {
+ ItemStack itemStack = slot.getItemStack();
+ if (keyPressed == manager.keybindViewRecipe.getKeyCode()) { // TODO: rework this so it doesnt skip recipe chains
+ manager.displayGuiItemRecipe(manager.getInternalNameForItem(itemStack), "");
+ } else if (keyPressed == manager.keybindViewUsages.getKeyCode()) {
+ manager.displayGuiItemUsages(manager.getInternalNameForItem(itemStack));
}
}
}
@@ -197,37 +178,25 @@ public class GuiItemRecipe extends GuiScreen {
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);
- int guiX = mouseX - guiLeft;
- int guiY = mouseY - guiTop;
-
- int buttonWidth = 7;
- int buttonHeight = 11;
-
- if(guiY > + 63 && guiY < + 63 + buttonHeight) {
- if(guiX > + 110 && guiX < 110 + buttonWidth) {
- currentIndex--;
- Utils.playPressSound();
- return;
- } else if(guiX > 147 && guiX < 147 + buttonWidth) {
- currentIndex++;
- Utils.playPressSound();
- return;
- }
+ if (isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_LEFT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT)) {
+ currentIndex = currentIndex == 0 ? 0 : currentIndex - 1;
+ Utils.playPressSound();
+ return;
+ }
+
+ if (isWithinRect(mouseX - guiLeft, mouseY - guiTop, BUTTON_POSITION_RIGHT_X, BUTTON_POSITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT)) {
+ currentIndex = currentIndex == craftingRecipes.size() - 1 ? currentIndex : currentIndex + 1;
+ Utils.playPressSound();
+ return;
}
- for(int index=0; index <= 45; index++) {
- Vector2f pos = getPositionForIndex(index);
- if(mouseX > pos.x && mouseX < pos.x+16) {
- if(mouseY > pos.y && mouseY < pos.y+16) {
- ItemStack stack = getStackForIndex(index);
- if(stack != null) {
- if(mouseButton == 0) {
- manager.displayGuiItemRecipe(manager.getInternalNameForItem(stack), "");
- } else if(mouseButton == 1) {
- manager.displayGuiItemUsages(manager.getInternalNameForItem(stack));
- }
- }
- return;
+ for (RecipeSlot slot : getAllRenderedSlots()) {
+ if (isWithinRect(mouseX, mouseY, slot.getX(this), slot.getY(this), SLOT_SIZE, SLOT_SIZE)) {
+ ItemStack itemStack = slot.getItemStack();
+ if (mouseButton == 0) {
+ manager.displayGuiItemRecipe(manager.getInternalNameForItem(itemStack), "");
+ } else if (mouseButton == 1) {
+ manager.displayGuiItemUsages(manager.getInternalNameForItem(itemStack));
}
}
}