From 102dc511c03df589c80fcf68e8b8ec4352fb0261 Mon Sep 17 00:00:00 2001 From: Raycoms Date: Wed, 11 Dec 2024 20:03:43 +0100 Subject: [PATCH] Dontcrash (#10525) Dont crash with empty stack, but log error so we can find origin more easily. Better log spam than exceptions --- .../colony/requestsystem/requestable/Stack.java | 14 +++++++++++++- .../requestable/crafting/AbstractCrafting.java | 11 +++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/Stack.java b/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/Stack.java index 6f86b2df4bd..e1a6e5dfc00 100755 --- a/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/Stack.java +++ b/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/Stack.java @@ -5,6 +5,7 @@ import com.minecolonies.api.colony.requestsystem.factory.IFactoryController; import com.minecolonies.api.crafting.ItemStorage; import com.minecolonies.api.util.ItemStackUtils; +import com.minecolonies.api.util.Log; import com.minecolonies.api.util.ReflectionUtils; import com.minecolonies.api.util.constant.TypeConstants; import net.minecraft.nbt.CompoundTag; @@ -160,7 +161,7 @@ public Stack(final ItemStack stack, final boolean matchDamage, final boolean mat { if (ItemStackUtils.isEmpty(stack)) { - throw new IllegalArgumentException("Cannot deliver Empty Stack."); + Log.getLogger().error("Created Empty Stack", new Exception()); } this.theStack = stack.copy(); @@ -221,6 +222,11 @@ public static Stack deserialize(final IFactoryController controller, final Compo minCount = compound.getInt(NBT_MINCOUNT); } + if (stack.isEmpty()) + { + Log.getLogger().error("Deserialized bad stack", compound.toString()); + } + return new Stack(stack, matchMeta, matchNBT, result, count, minCount, canBeResolved); } @@ -265,6 +271,12 @@ public static Stack deserialize(final IFactoryController controller, final Frien int count = buffer.readInt(); int minCount = buffer.readInt(); + + if (stack.isEmpty()) + { + Log.getLogger().error("Deserialized bad stack", stack.toString()); + } + return new Stack(stack, matchMeta, matchNBT, result, count, minCount, canBeResolved); } diff --git a/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/crafting/AbstractCrafting.java b/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/crafting/AbstractCrafting.java index 8a54da4f2f0..2bbb89bcd3b 100755 --- a/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/crafting/AbstractCrafting.java +++ b/src/main/java/com/minecolonies/api/colony/requestsystem/requestable/crafting/AbstractCrafting.java @@ -3,6 +3,7 @@ import com.minecolonies.api.colony.requestsystem.requestable.IRequestable; import com.minecolonies.api.colony.requestsystem.token.IToken; import com.minecolonies.api.util.ItemStackUtils; +import com.minecolonies.api.util.Log; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.NotNull; @@ -57,12 +58,14 @@ public AbstractCrafting(@NotNull final ItemStack stack, final int count, final i this.minCount = minCount; this.recipeToken = recipeToken; - if (ItemStackUtils.isEmpty(stack)) + if (stack.isEmpty()) { - throw new IllegalArgumentException("Cannot deliver Empty Stack."); + Log.getLogger().error("Created Empty Stack", new Exception()); + } + else + { + this.theStack.setCount(Math.min(this.theStack.getCount(), this.theStack.getMaxStackSize())); } - - this.theStack.setCount(Math.min(this.theStack.getCount(), this.theStack.getMaxStackSize())); } @NotNull