From 16d7e58b8a24a3555abdcffe2c43c0d49faf081f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Tue, 18 Jun 2024 16:07:43 +0200 Subject: [PATCH] Update polymer --- gradle.properties | 8 +- .../brn/server_storage/lib/CraftingEntry.java | 3 + .../server_storage/lib/MyCraftingRecipe.java | 15 ++++ .../server_storage/lib/StorageOperations.java | 4 +- .../systems/brn/server_storage/lib/Util.java | 85 +++++++++++++------ .../screens/CraftingScreen.java | 2 +- src/main/resources/fabric.mod.json | 4 +- 7 files changed, 89 insertions(+), 32 deletions(-) create mode 100644 src/main/java/systems/brn/server_storage/lib/MyCraftingRecipe.java diff --git a/gradle.properties b/gradle.properties index ac5f55b..7e9c50b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,18 +4,18 @@ org.gradle.jvmargs=-Xmx1G # check these on https://modmuss50.me/fabric.html minecraft_version=1.21 -yarn_mappings=1.21+build.1 +yarn_mappings=1.21+build.2 loader_version=0.15.11 # Fabric API -fabric_version=0.100.1+1.21 +fabric_version=0.100.3+1.21 # Mod Properties -mod_version=2.6 +mod_version=2.6.1 maven_group=systems.brn archives_base_name=Server_storage # Dependencies -polymer_version=0.9.0+1.21-rc1 +polymer_version=0.9.2+1.21 server_translations_api_version=2.3.1+1.21-pre2 servergui_version=1.6.0+1.21 \ No newline at end of file diff --git a/src/main/java/systems/brn/server_storage/lib/CraftingEntry.java b/src/main/java/systems/brn/server_storage/lib/CraftingEntry.java index b36389e..4332141 100644 --- a/src/main/java/systems/brn/server_storage/lib/CraftingEntry.java +++ b/src/main/java/systems/brn/server_storage/lib/CraftingEntry.java @@ -4,9 +4,12 @@ import net.minecraft.item.ItemStack; import net.minecraft.recipe.CraftingRecipe; import net.minecraft.recipe.RecipeEntry; +import java.util.ArrayList; + public class CraftingEntry { public final ItemStack itemStack; public final RecipeEntry recipeEntry; + public final ArrayList myCraftingRecipeEntries; public CraftingEntry(ItemStack itemStack, RecipeEntry recipeEntry) { this.itemStack = itemStack; this.recipeEntry = recipeEntry; diff --git a/src/main/java/systems/brn/server_storage/lib/MyCraftingRecipe.java b/src/main/java/systems/brn/server_storage/lib/MyCraftingRecipe.java new file mode 100644 index 0000000..3db8e74 --- /dev/null +++ b/src/main/java/systems/brn/server_storage/lib/MyCraftingRecipe.java @@ -0,0 +1,15 @@ +package systems.brn.server_storage.lib; + +import net.minecraft.item.ItemStack; +import net.minecraft.recipe.CraftingRecipe; +import net.minecraft.recipe.RecipeEntry; + +import java.util.HashMap; + +public class MyCraftingRecipe { + HashMap inputs = new HashMap<>(); + HashMap outputs = new HashMap<>(); + MyCraftingRecipe(RecipeEntry recipeEntry) { + recipeEntry.value().getIngredients() + } +} diff --git a/src/main/java/systems/brn/server_storage/lib/StorageOperations.java b/src/main/java/systems/brn/server_storage/lib/StorageOperations.java index 8926a5f..b8c0b83 100644 --- a/src/main/java/systems/brn/server_storage/lib/StorageOperations.java +++ b/src/main/java/systems/brn/server_storage/lib/StorageOperations.java @@ -26,7 +26,9 @@ public class StorageOperations { } else { // Sort by count in descending order sortedMap = new TreeMap<>((o1, o2) -> { - int countCompare = Integer.compare(o2.getCount(), o1.getCount()); + int count1 = itemStackMap.get(o1); + int count2 = itemStackMap.get(o2); + int countCompare = Integer.compare(count2, count1); // If counts are equal, compare items alphabetically by name return countCompare == 0 ? String.valueOf(o1.getItem()).compareToIgnoreCase(String.valueOf(o2.getItem())) : countCompare; }); diff --git a/src/main/java/systems/brn/server_storage/lib/Util.java b/src/main/java/systems/brn/server_storage/lib/Util.java index f869182..781c85b 100644 --- a/src/main/java/systems/brn/server_storage/lib/Util.java +++ b/src/main/java/systems/brn/server_storage/lib/Util.java @@ -13,7 +13,9 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class Util { @@ -24,7 +26,7 @@ public class Util { ItemStack slotStack = inventory.getStack(i); if (!slotStack.isEmpty()) { Item itemName = slotStack.getItem(); - pages.add(String.valueOf(itemName) + " " + slotStack.getCount()); + pages.add(itemName + " " + slotStack.getCount()); } } return stringToBookContent(String.join(System.lineSeparator(), pages)); @@ -112,46 +114,81 @@ public class Util { int maxAmount = -1; boolean canMake = true; for (Ingredient ingredient : recipe.value().getIngredients()) { + HashMap inputsTemp = new HashMap<>(); for (ItemStack stack : ingredient.getMatchingStacks()) { if (stack.isEmpty()) { break; } else { //if inventory contains stack boolean foundUsableStack = false; - for (int i = 0; i < inventory.size(); i++) { - if (inventory.getStack(i).getItem().equals(stack.getItem())) { - if (maxAmount == -1) { - maxAmount = inventory.getStack(i).getCount(); - } else { - maxAmount = Math.min(maxAmount, inventory.getStack(i).getCount()); - } + for (Map.Entry entry : inputsTemp.entrySet()) { + ItemStack stackIn = entry.getKey(); + Integer count = entry.getValue(); + if (stackIn.getItem() == stack.getItem()) { + count += stack.getCount(); + entry.setValue(count); // Update the value in the map foundUsableStack = true; break; } } if (!foundUsableStack) { - canMake = false; - break; + inputsTemp.put(stack, stack.getCount()); } } } - } - if (maxAmount > 1 && canMake) { - Item outputItem = recipe.value().getResult(server.getRegistryManager()).getItem(); - CraftingEntry entry = new CraftingEntry(new ItemStack(outputItem, maxAmount), recipe); - boolean needToAdd = true; - for (int i = 0; i < craftingEntries.size(); i++) { - CraftingEntry entryLoop = craftingEntries.get(i); - if (entryLoop.itemStack.getItem().equals(outputItem)) { - needToAdd = false; - if (maxAmount > entryLoop.itemStack.getCount()) { - craftingEntries.set(i, entry); - break; + HashMap inputs = new HashMap<>(); + for (Map.Entry entry : inputsTemp.entrySet()) { + ItemStack stackIn = entry.getKey(); + Integer count = entry.getValue(); + stackIn.setCount(count); + inputs.put(stackIn, stackIn.getCount()); + } + for (int i = 0; i < inventory.size(); i++) { + ItemStack slotStack = inventory.getStack(i); + Item slotItem = slotStack.getItem(); + int slotCount = slotStack.getCount(); + for (Map.Entry entry : inputs.entrySet()) { + ItemStack stackIn = entry.getKey(); + Integer count = entry.getValue(); + if (stackIn.getItem() == slotItem) { + count -= slotCount; + entry.setValue(count); } } } - if (needToAdd) { - craftingEntries.add(entry); + for (Map.Entry entry : inputs.entrySet()) { + ItemStack stackIn = entry.getKey(); + Integer count = entry.getValue(); + if (count > 0) { + canMake = false; + } else { + int thisMaxAmount = Math.floorDivExact(Math.abs(count), stackIn.getCount()); + if (maxAmount == -1) { + maxAmount = thisMaxAmount; + } else { + maxAmount = Math.min(maxAmount, thisMaxAmount); + } + } + } + if (maxAmount > 1 && canMake) { + Item outputItem = recipe.value().getResult(server.getRegistryManager()).getItem(); + CraftingEntry entry = new CraftingEntry(new ItemStack(outputItem, maxAmount), recipe); + boolean needToAdd = true; + for (int i = 0; i < craftingEntries.size(); i++) { + CraftingEntry entryLoop = craftingEntries.get(i); + if (entryLoop.itemStack.getItem().equals(outputItem)) { + needToAdd = false; + if (maxAmount > entryLoop.itemStack.getCount()) { + craftingEntries.set(i, entry); + break; + } + } + } + if (needToAdd) { + craftingEntries.add(entry); + } + } else { + break; } } } diff --git a/src/main/java/systems/brn/server_storage/screens/CraftingScreen.java b/src/main/java/systems/brn/server_storage/screens/CraftingScreen.java index 9f816a3..6c8c867 100644 --- a/src/main/java/systems/brn/server_storage/screens/CraftingScreen.java +++ b/src/main/java/systems/brn/server_storage/screens/CraftingScreen.java @@ -117,7 +117,7 @@ public class CraftingScreen extends PagedGui { } // Add the result to the appropriate inventory - ItemStack outputStack = recipeEntry.value().getResult(storageScreen.getPlayer().getRegistryManager()); + ItemStack outputStack = recipeEntry.value().getResult(storageScreen.getPlayer().getRegistryManager()).copy(); if (toPlayerInventory) { if (canInsertItemIntoPlayerInventory(player, outputStack) == outputStack.getCount()) { player.getInventory().insertStack(outputStack); diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 4bd896d..1176a5f 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -18,7 +18,7 @@ }, "depends": { "fabricloader": ">=${loader_version}", - "fabric": "*", - "minecraft": "${minecraft_version}" + "fabric": ">=${fabric_version}", + "minecraft": ">=${minecraft_version}" } }