From 556fa487bec903e3224f3e5ad3742bf0ce803741 Mon Sep 17 00:00:00 2001 From: Mrbysco Date: Thu, 17 Oct 2024 21:17:23 +0200 Subject: [PATCH] Fix Enervator and Energizer gui crashing --- .../mod/inventory/ContainerEnergizer.java | 31 +--------- .../mod/inventory/ContainerEnervator.java | 24 +------- .../mod/inventory/gui/GuiEnergizer.java | 4 -- .../mod/inventory/gui/GuiEnervator.java | 5 -- .../mod/inventory/slot/ArmorSlot.java | 56 +++++++++++++++++++ 5 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/ArmorSlot.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnergizer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnergizer.java index 136e07f27..dd042f29c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnergizer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnergizer.java @@ -10,7 +10,7 @@ package de.ellpeck.actuallyadditions.mod.inventory; -import com.mojang.datafixers.util.Pair; +import de.ellpeck.actuallyadditions.mod.inventory.slot.ArmorSlot; import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotItemHandlerUnconditioned; import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotOutput; import de.ellpeck.actuallyadditions.mod.tile.TileEntityEnergizer; @@ -22,12 +22,8 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.enchantment.EnchantmentEffectComponents; -import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.neoforged.neoforge.capabilities.Capabilities; -import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -63,29 +59,8 @@ public class ContainerEnergizer extends AbstractContainerMenu { for (int k = 0; k < 4; ++k) { EquipmentSlot slot = VALID_EQUIPMENT_SLOTS[k]; - this.addSlot(new Slot(inventory, 36 + 3 - k, 102, 19 + k * 18) { - @Override - public int getMaxStackSize() { - return 1; - } - - @Override - public boolean mayPlace(ItemStack stack) { - return !stack.isEmpty() && stack.getItem() instanceof ArmorItem; - } - - @Override - public boolean mayPickup(Player player) { - ItemStack itemstack = this.getItem(); - return (itemstack.isEmpty() || player.isCreative() || !EnchantmentHelper.has(itemstack, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE)) && super.mayPickup(player); - } - - @Nullable - @Override - public Pair getNoItemIcon() { - return Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.TEXTURE_EMPTY_SLOTS.get(slot.getIndex())); - } - }); + ResourceLocation resourcelocation = InventoryMenu.TEXTURE_EMPTY_SLOTS.get(slot); + this.addSlot(new ArmorSlot(inventory, slot, 36 + 3 - k, 102, 19 + k * 18, resourcelocation)); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnervator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnervator.java index 96c20e317..1e7d318f9 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnervator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/ContainerEnervator.java @@ -10,7 +10,7 @@ package de.ellpeck.actuallyadditions.mod.inventory; -import com.mojang.datafixers.util.Pair; +import de.ellpeck.actuallyadditions.mod.inventory.slot.ArmorSlot; import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotItemHandlerUnconditioned; import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotOutput; import de.ellpeck.actuallyadditions.mod.tile.TileEntityEnervator; @@ -23,10 +23,8 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.ItemStack; import net.neoforged.neoforge.capabilities.Capabilities; -import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -56,24 +54,8 @@ public class ContainerEnervator extends AbstractContainerMenu { for (int k = 0; k < 4; ++k) { EquipmentSlot slot = ContainerEnergizer.VALID_EQUIPMENT_SLOTS[k]; - this.addSlot(new Slot(inventory, 36 + 3 - k, 102, 19 + k * 18) { - @Override - public int getMaxStackSize() { - return 1; - } - - // TODO: [port] validate that this is correct - @Override - public boolean mayPlace(ItemStack stack) { - return StackUtil.isValid(stack) && stack.getItem() instanceof ArmorItem; - } - - @Nullable - @Override - public Pair getNoItemIcon() { - return Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.TEXTURE_EMPTY_SLOTS.get(slot.getIndex())); - } - }); + ResourceLocation resourcelocation = InventoryMenu.TEXTURE_EMPTY_SLOTS.get(slot); + this.addSlot(new ArmorSlot(inventory, slot, 36 + 3 - k, 102, 19 + k * 18, resourcelocation)); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnergizer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnergizer.java index c7af08032..ed2b82c07 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnergizer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnergizer.java @@ -10,7 +10,6 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui; -import com.mojang.blaze3d.systems.RenderSystem; import de.ellpeck.actuallyadditions.mod.inventory.ContainerEnergizer; import de.ellpeck.actuallyadditions.mod.tile.TileEntityEnergizer; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; @@ -21,7 +20,6 @@ import net.minecraft.world.entity.player.Inventory; import javax.annotation.Nonnull; - public class GuiEnergizer extends AAScreen { private static final ResourceLocation RES_LOC = AssetUtil.getGuiLocation("gui_energizer"); @@ -49,8 +47,6 @@ public class GuiEnergizer extends AAScreen { @Override public void renderBg(GuiGraphics guiGraphics, float f, int x, int y) { - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - guiGraphics.blit(AssetUtil.GUI_INVENTORY_LOCATION, this.leftPos, this.topPos + 93, 0, 0, 176, 86); guiGraphics.blit(RES_LOC, this.leftPos, this.topPos, 0, 0, 176, 93); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnervator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnervator.java index bdc62641a..fb2098e57 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnervator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/GuiEnervator.java @@ -10,7 +10,6 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui; -import com.mojang.blaze3d.systems.RenderSystem; import de.ellpeck.actuallyadditions.mod.inventory.ContainerEnervator; import de.ellpeck.actuallyadditions.mod.tile.TileEntityEnervator; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; @@ -21,8 +20,6 @@ import net.minecraft.world.entity.player.Inventory; import javax.annotation.Nonnull; - - public class GuiEnervator extends AAScreen { private static final ResourceLocation RES_LOC = AssetUtil.getGuiLocation("gui_energizer"); @@ -50,8 +47,6 @@ public class GuiEnervator extends AAScreen { @Override public void renderBg(GuiGraphics guiGraphics, float f, int x, int y) { - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - guiGraphics.blit(AssetUtil.GUI_INVENTORY_LOCATION, this.leftPos, this.topPos + 93, 0, 0, 176, 86); guiGraphics.blit(RES_LOC, this.leftPos, this.topPos, 0, 0, 176, 93); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/ArmorSlot.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/ArmorSlot.java new file mode 100644 index 000000000..73a0c8bbf --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/slot/ArmorSlot.java @@ -0,0 +1,56 @@ +package de.ellpeck.actuallyadditions.mod.inventory.slot; + +import com.mojang.datafixers.util.Pair; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.Container; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.InventoryMenu; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ArmorItem; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.enchantment.EnchantmentEffectComponents; +import net.minecraft.world.item.enchantment.EnchantmentHelper; +import org.jetbrains.annotations.Nullable; + +/** + * A copy of vanilla's ArmorSlot without the owner entity + * Vanilla's ArmorSlot class is not public, so we have to copy it + */ +public class ArmorSlot extends Slot { + private final EquipmentSlot slot; + @Nullable + private final ResourceLocation emptyIcon; + + public ArmorSlot( + Container container, EquipmentSlot slot, int slotIndex, int x, int y, @Nullable ResourceLocation emptyIcon + ) { + super(container, slotIndex, x, y); + this.slot = slot; + this.emptyIcon = emptyIcon; + } + + @Override + public int getMaxStackSize() { + return 1; + } + + @Override + public boolean mayPlace(ItemStack stack) { + return !stack.isEmpty() && stack.getItem() instanceof ArmorItem; + } + + @Override + public boolean mayPickup(Player player) { + ItemStack itemstack = this.getItem(); + return !itemstack.isEmpty() && !player.isCreative() && EnchantmentHelper.has(itemstack, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE) + ? false + : super.mayPickup(player); + } + + @Override + public Pair getNoItemIcon() { + return this.emptyIcon != null ? Pair.of(InventoryMenu.BLOCK_ATLAS, this.emptyIcon) : super.getNoItemIcon(); + } +}