Skip to content

Commit

Permalink
Doesn't crash
Browse files Browse the repository at this point in the history
  • Loading branch information
UselessBullets committed Dec 25, 2023
1 parent f19da30 commit 0ad0cb2
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 206 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ dependencies {

// Mods
modImplementation "ModMenu:ModMenu:2.0.3"
//modImplementation "BTA_Babric_SpawnEggs:spawneggs:1.3.0"
modImplementation "BTA_Babric_SpawnEggs:spawneggs:2.0.0-7.1"
// modImplementation "BTANoNameDyes:noname_dyes:${project.dyes_version}"
// modImplementation "potato-logistics:PotatoLogistics:${project.potato_logistics_version}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import net.minecraft.core.achievement.stat.StatsCounter;
import net.minecraft.core.block.Block;
import net.minecraft.core.crafting.legacy.CraftingManager;
import net.minecraft.core.data.registry.recipe.RecipeSymbol;
import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCrafting;
import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCraftingShaped;
import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCraftingShapeless;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.player.gamemode.Gamemode;
Expand All @@ -24,7 +28,9 @@
import useless.legacyui.Sorting.Recipe.RecipeCost;
import useless.legacyui.Sorting.Recipe.RecipeGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LegacyContainerCrafting extends Container {
public InventoryCrafting craftMatrix;
Expand Down Expand Up @@ -112,7 +118,7 @@ public void setRecipes(EntityPlayer player, StatsCounter statCounter, boolean sh
RecipeCategory category = LegacyCategoryManager.getRecipeCategories().get(categoryIndex);

for (RecipeGroup group : category.getRecipeGroups(isInInventory)){
LegacyUI.LOGGER.debug("CategoryGroup: " + group.getContainer(0, isInInventory).inventorySlots.get(0).getStack().getItem().getKey());
LegacyUI.LOGGER.debug("CategoryGroup: " + group.getOutputStack(0, isInInventory).getItemName());
}
LegacyUI.LOGGER.debug("Category: " + category + " | slotId: " + currentSlotId + " | currentScroll: " + currentScrollAmount + " | craftPreview: " + showCraftingPreview);
this.inventorySlots.clear();
Expand All @@ -125,74 +131,75 @@ public void setRecipes(EntityPlayer player, StatsCounter statCounter, boolean sh

int index = 0;
for (RecipeGroup group : craftingGroups){
ContainerGuidebookRecipeCrafting currentContainer;
RecipeEntryCrafting<?, ?> recipe;
boolean craftable;
if (index == currentSlotId){ // special rendering for scrolling and recipe preview
currentContainer = group.getContainer(currentScrollAmount, isInInventory);
craftable = canCraft(player, new RecipeCost(currentContainer));
recipe = group.getRecipe(currentScrollAmount, isInInventory);
craftable = canCraft(player, new RecipeCost(recipe));

// Recipebar preview
item = currentContainer.inventorySlots.get(0).getStack();
item = (ItemStack) recipe.getOutput();
discovered = isDicovered(item, statCounter, player);
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 12 + 18 * index, 56, currentContainer.inventorySlots.get(0).getStack(), discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 12 + 18 * index, 56, item, discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value));

if (group.getRecipes(isInInventory).length > 1) { // If multiple Items in recipe group
if (group.getRecipes(isInInventory).size() > 1) { // If multiple Items in recipe group
int idUpper = currentScrollAmount + 1; // Next item in group
int idLower = currentScrollAmount - 1; // Last item in group

// Next item preview
item = group.getContainer(idUpper, isInInventory).inventorySlots.get(0).getStack();
item = group.getOutputStack(idUpper, isInInventory);
discovered = isDicovered(item, statCounter, player);
craftable = canCraft(player, new RecipeCost(group.getContainer(idUpper, isInInventory)));
craftable = canCraft(player, new RecipeCost(group.getRecipe(idUpper, isInInventory)));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 12 + 18 * index, 56 + 21, item, discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value));

// Previous item preview
item = group.getContainer(idLower, isInInventory).inventorySlots.get(0).getStack();
item = group.getOutputStack(idUpper, isInInventory);
discovered = isDicovered(item, statCounter, player);
craftable = canCraft(player, new RecipeCost(group.getContainer(idLower, isInInventory)));
craftable = canCraft(player, new RecipeCost(group.getRecipe(idLower, isInInventory)));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 12 + 18 * index, 56 - 21, item, discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value));

}

// Crafting table result preview
int offset = showCraftingPreview ? 0:5000;
RecipeCost cost = new RecipeCost(currentContainer);
RecipeCost cost = new RecipeCost(recipe);
craftable = canCraft(player, cost);
item = currentContainer.inventorySlots.get(0).getStack();
item = (ItemStack) recipe.getOutput();
discovered = isDicovered(item, statCounter, player);
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 103, 123+offset, item, discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value, 26));

for (int j = 1; j < currentContainer.inventorySlots.size(); j++) {
item = currentContainer.inventorySlots.get(j).getStack();
for (int j = 1; j < recipe.getRecipeSize(); j++) {
item = (ItemStack) recipe.getOutput();
discovered = isDicovered(item, statCounter, player);

RecipeSymbol[] costSymbols = cost.costMap.keySet().toArray(new RecipeSymbol[0]);
int k = 0;
if (item != null){
for (int i = 0; i < cost.itemStacks.length; i++){
if (cost.itemStacks[i].getItem() == item.getItem()){
cost.quantity[i] -= 1;
for (int i = 0; i < costSymbols.length; i++){
if (costSymbols[i].matches(item)){
cost.costMap.put(costSymbols[i], cost.costMap.get(costSymbols[i]) - 1);
k = i;
break;
}
}
}

if (currentContainer.inventorySlots.size() > 5){
int num = cost.costMap.get(costSymbols[k]);
if (recipe.getRecipeSize() > 5){
// Render 3x3 crafting grid
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 20 + 18 * ((j - 1) % 3), 109 + offset + 18 * ((j - 1) / 3), item, discovered, InventoryHelper.itemsInInventory(inventoryPlayer, item, cost.useAlts) <= cost.quantity[k] && item != null, LegacyUI.modSettings.getHighlightColor().value.value));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 20 + 18 * ((j - 1) % 3), 109 + offset + 18 * ((j - 1) / 3), item, discovered, InventoryHelper.itemsInInventory(inventoryPlayer, item) <= num && item != null, LegacyUI.modSettings.getHighlightColor().value.value));
}
else {
// Render 2x2 crafting gird
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 29 + 18 * ((j - 1) % 2), 118 + offset + 18 * ((j - 1) / 2), item, discovered, InventoryHelper.itemsInInventory(inventoryPlayer, item, cost.useAlts) <= cost.quantity[k] && item != null, LegacyUI.modSettings.getHighlightColor().value.value));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 29 + 18 * ((j - 1) % 2), 118 + offset + 18 * ((j - 1) / 2), item, discovered, InventoryHelper.itemsInInventory(inventoryPlayer, item) <= num && item != null, LegacyUI.modSettings.getHighlightColor().value.value));
}


}
}
else { // Renders first Slot of none selected groups
item = group.getContainer(0, isInInventory).inventorySlots.get(0).getStack();
item = group.getOutputStack(0, isInInventory);
discovered = isDicovered(item, statCounter, player);
craftable = canCraft(player, new RecipeCost(group.getContainer(0, isInInventory)));
craftable = canCraft(player, new RecipeCost(group.getRecipe(0, isInInventory)));
this.addSlot(new SlotCraftingDisplayLegacy(this.inventorySlots.size(), 12 + 18 * index, 56, item, discovered, !craftable, LegacyUI.modSettings.getHighlightColor().value.value));
}

Expand All @@ -208,34 +215,32 @@ public boolean craft(Minecraft mc, int windowId){

RecipeCategory category = LegacyCategoryManager.getRecipeCategories().get(categoryIndex);

ContainerGuidebookRecipeCrafting recipe = category.getRecipeGroups(isInInventory)[currentSlotId].getContainer(currentScrollAmount, isInInventory);
RecipeEntryCrafting<?, ?> recipe = category.getRecipeGroups(isInInventory)[currentSlotId].getRecipe(currentScrollAmount, isInInventory);
RecipeCost recipeCost = new RecipeCost(recipe);

RecipeSymbol[] recipeInput = InventoryHelper.getRecipeInput(recipe);
if (canCraft(mc.thePlayer, recipeCost)){
for (int i = 1; i < recipe.inventorySlots.size(); i++){
ItemStack itemStack = recipe.inventorySlots.get(i).getStack();
if (itemStack != null){
int slotId = InventoryHelper.findStackIndex(mc.thePlayer.inventory.mainInventory, itemStack, recipeCost.useAlts); // Finds Slot index of an inventory Slot with a desired item
if (slotId == -1) {continue;}
if (slotId < 9){ slotId += 36;}
for (int i = 0; i < recipeInput.length; i++){
int slotId = InventoryHelper.findStackIndex(mc.thePlayer.inventory.mainInventory, recipeInput[i]); // Finds Slot index of an inventory Slot with a desired item
if (slotId == -1) {continue;}
if (slotId < 9){ slotId += 36;}


if (isInInventory){
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId}, mc.thePlayer); // Puts down stack
}
else if (recipe.inventorySlots.size() > 5){// 3x3 crafting
int offset = 1;
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + offset}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + offset}, mc.thePlayer); // Puts down stack
}
else {// 2x2 crafting
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + 1}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i + (i/3)}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + 1}, mc.thePlayer); // Puts down stack
}
if (isInInventory){
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId}, mc.thePlayer); // Puts down stack
}
else if (recipe.getRecipeSize() > 5){// 3x3 crafting
int offset = 1;
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + offset}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + offset}, mc.thePlayer); // Puts down stack
}
else {// 2x2 crafting
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + 1}, mc.thePlayer); // Picks up stack
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_RIGHT, new int[]{i + (i/3)}, mc.thePlayer); // Places one item
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.CLICK_LEFT, new int[]{slotId + 1}, mc.thePlayer); // Puts down stack
}
}
mc.playerController.handleInventoryMouseClick(windowId, InventoryAction.MOVE_STACK, new int[]{0}, mc.thePlayer);
Expand All @@ -246,8 +251,8 @@ else if (recipe.inventorySlots.size() > 5){// 3x3 crafting
}
private boolean canCraft(EntityPlayer player, RecipeCost cost){
boolean canCraft = true;
for (int i = 0; i < cost.itemStacks.length; i++){
canCraft = canCraft && InventoryHelper.itemsInInventory(player.inventory, cost.itemStacks[i], cost.useAlts) >= cost.quantity[i];
for (Map.Entry<RecipeSymbol, Integer> entry : cost.costMap.entrySet()){
canCraft = canCraft && InventoryHelper.itemsInInventory(player.inventory, entry.getKey()) >= entry.getValue();
}
return canCraft;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import net.minecraft.client.input.controller.Button;
import net.minecraft.client.input.controller.ControllerInput;
import net.minecraft.core.crafting.legacy.recipe.IRecipe;
import net.minecraft.core.data.registry.recipe.entry.RecipeEntryCrafting;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.lang.I18n;
import org.lwjgl.input.Keyboard;
import useless.legacyui.Gui.Containers.LegacyContainerCrafting;
Expand Down Expand Up @@ -110,7 +112,7 @@ public void scrollGroup(int direction){
public void selectScrollGroup(int value){
int initialScroll = currentScroll;
currentScroll = value;
int groupSize = currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).length;
int groupSize = currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).size();
if (currentScroll > groupSize-1){
currentScroll -= groupSize;
} else if (currentScroll < 0){
Expand Down Expand Up @@ -284,7 +286,7 @@ public void init() {
}
public void setContainerRecipes(){
RecipeGroup[] recipeGroups = currentCategory().getRecipeGroups(isSmall());
if (recipeGroups[currentSlot].getRecipes(isSmall()).length > 1){ // If scroll bar active
if (recipeGroups[currentSlot].getRecipes(isSmall()).size() > 1){ // If scroll bar active
scrollUp.enabled = true;
scrollDown.enabled = true;
} else {
Expand Down Expand Up @@ -341,17 +343,18 @@ protected void drawGuiContainerBackgroundLayer(float partialTick) {

UtilGui.drawTexturedModalRect(this, GUIx + (tabWidth-1) * (currentTab % 8), GUIy - 2, (tabWidth) * (currentTab % 8),229, tabWidth, 30, 1f/guiTextureWidth); // Render Selected Tab

IRecipe currentRecipe = (IRecipe) currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall())[currentScroll];
if ((currentCategory().getRecipeGroups(isSmall())[currentSlot].getContainer(currentScroll, isSmall()).inventorySlots.size() <= 5 && showCraftDisplay) || isSmall()){ // 2x2 Crafting overlay

RecipeEntryCrafting<?, ?> currentRecipe = currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).get(currentScroll);
if ((currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipe(currentScroll, isSmall()).getRecipeSize() <= 5 && showCraftDisplay) || isSmall()){ // 2x2 Crafting overlay
UtilGui.drawTexturedModalRect(this, GUIx + 19, GUIy + 108, 61, 175, 54, 54, 1f/guiTextureWidth);
}

drawStringCenteredNoShadow(fontRenderer, I18n.getInstance().translateKey("legacyui.guilabel.inventory"),GUIx + 204, GUIy + 97, LegacyUI.modSettings.getGuiLabelColor().value.value);

String craftingString; // Text above crafting table
if (LegacyUI.modSettings.getShowCraftingItemNamePreview().value && showCraftDisplay){ // If crafting display rendered and render item names enabled
craftingString = currentRecipe.getRecipeOutput().getDisplayName(); // Get Item name
if (!LegacyContainerCrafting.isDicovered(currentRecipe.getRecipeOutput(), mc.statsCounter, mc.thePlayer)){ // If undiscovered obscure it
craftingString = ((ItemStack)currentRecipe.getOutput()).getDisplayName(); // Get Item name
if (!LegacyContainerCrafting.isDicovered((ItemStack) currentRecipe.getOutput(), mc.statsCounter, mc.thePlayer)){ // If undiscovered obscure it
craftingString = craftingString.replaceAll("[^ ]", "?");
}
if (craftingString.length() > 21){ // If too long then cap to 21 characters
Expand Down Expand Up @@ -381,7 +384,7 @@ protected void drawGuiContainerBackgroundLayer(float partialTick) {
private void drawSelectionCursorForeground(){
int x = 8 + 18*currentSlot;
int y = 52;
if (currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).length > 1){
if (currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).size() > 1){
UtilGui.drawTexturedModalRect(this, x - 1,y,35, 175, 26, 24, 1f/guiTextureWidth);
UtilGui.drawTexturedModalRect(this, x - 1,y - 31, 115, 175, 26,31, 1f/guiTextureWidth);
UtilGui.drawTexturedModalRect(this, x - 1,y + 24, 141, 175, 26,31, 1f/guiTextureWidth);
Expand All @@ -392,7 +395,7 @@ private void drawSelectionCursorForeground(){
private void drawSelectionCursorBackground(){
int x = 12 + 18*currentSlot;
int y = 51;
if (currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).length > 1){
if (currentCategory().getRecipeGroups(isSmall())[currentSlot].getRecipes(isSmall()).size() > 1){
UtilGui.drawTexturedModalRect(this,GUIx + x - 1,GUIy + y - 17,167, 175, 18, 18, 1f/guiTextureWidth);
UtilGui.drawTexturedModalRect(this,GUIx + x - 1,GUIy + y + 25, 167, 175, 18,18, 1f/guiTextureWidth);
}
Expand Down
Loading

0 comments on commit 0ad0cb2

Please # to comment.