From 41dc1d75b1f52363fe69303284d5c568da5f9bf2 Mon Sep 17 00:00:00 2001 From: P3pp3rF1y Date: Thu, 26 Sep 2024 00:54:01 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fixed=20Anvil=20upgrade?= =?UTF-8?q?=20to=20not=20allow=20dupes=20when=20multiple=20players=20are?= =?UTF-8?q?=20using=20one=20upgrade=20at=20the=20same=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../upgrades/anvil/AnvilUpgradeContainer.java | 97 +++++++++++++------ .../upgrades/anvil/AnvilUpgradeTab.java | 30 +----- .../resources/META-INF/accesstransformer.cfg | 4 +- 4 files changed, 77 insertions(+), 56 deletions(-) diff --git a/gradle.properties b/gradle.properties index 14764722..7dc02697 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ loader_version_range=[4,) mod_id=sophisticatedbackpacks mod_name=Sophisticated Backpacks mod_license=GNU General Public License v3.0 -mod_version=3.20.13 +mod_version=3.20.14 mod_group_id=sophisticatedbackpacks mod_authors=P3pp3rF1y, Ridanisaurus mod_description=Fancy and functional backpacks. diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeContainer.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeContainer.java index 0b89e835..be1e7e11 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeContainer.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeContainer.java @@ -2,15 +2,15 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.world.Container; +import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.AnvilMenu; -import net.minecraft.world.inventory.ContainerLevelAccess; -import net.minecraft.world.inventory.ResultSlot; -import net.minecraft.world.inventory.Slot; +import net.minecraft.world.inventory.*; import net.minecraft.world.item.ItemStack; +import net.p3pp3rf1y.sophisticatedcore.common.gui.SlotSuppliedHandler; import net.p3pp3rf1y.sophisticatedcore.common.gui.UpgradeContainerBase; import net.p3pp3rf1y.sophisticatedcore.common.gui.UpgradeContainerType; +import net.p3pp3rf1y.sophisticatedcore.upgrades.IUpgradeWrapper; import net.p3pp3rf1y.sophisticatedcore.util.NBTHelper; import javax.annotation.Nullable; @@ -20,12 +20,11 @@ public class AnvilUpgradeContainer extends UpgradeContainerBase {}; + private Runnable nameChangeListener = () -> {}; private boolean processingOnTakeLogic = false; public AnvilUpgradeContainer(Player player, int upgradeContainerId, AnvilUpgradeWrapper upgradeWrapper, UpgradeContainerType type) { super(player, upgradeContainerId, upgradeWrapper, type); - - anvilMenuDelegate = new PersistableAnvilMenu(new Inventory(player), upgradeWrapper); + anvilMenuDelegate = new PersistableAnvilMenu(new Inventory(player)); slots.add(anvilMenuDelegate.getSlot(0)); slots.add(anvilMenuDelegate.getSlot(1)); @@ -33,8 +32,8 @@ public AnvilUpgradeContainer(Player player, int upgradeContainerId, AnvilUpgrade slots.add(resultSlot); } - public void setSlotsChangeListener(Runnable slotsChangeListener) { - this.slotsChangeListener = slotsChangeListener; + public void setNameChangeListener(Runnable nameChangeListener) { + this.nameChangeListener = nameChangeListener; } @Override @@ -46,6 +45,14 @@ public void handlePacket(CompoundTag data) { } } + @Override + public void setUpgradeWrapper(IUpgradeWrapper updatedUpgradeWrapper) { + super.setUpgradeWrapper(updatedUpgradeWrapper); + anvilMenuDelegate.setItemName(upgradeWrapper.getItemName()); + anvilMenuDelegate.createResult(); + nameChangeListener.run(); + } + public boolean shouldShiftClickIntoStorage() { return upgradeWrapper.shouldShiftClickIntoStorage(); } @@ -81,37 +88,69 @@ public int getCost() { @Nullable public String getItemName() { - return anvilMenuDelegate.getItemName(); + return upgradeWrapper.getItemName(); } private class PersistableAnvilMenu extends AnvilMenu { - private final AnvilUpgradeWrapper wrapper; - private boolean initializing = true; - public PersistableAnvilMenu(Inventory playerInventory, AnvilUpgradeWrapper wrapper) { + public PersistableAnvilMenu(Inventory playerInventory) { super(0, playerInventory, ContainerLevelAccess.create(playerInventory.player.level(), playerInventory.player.blockPosition())); - this.wrapper = wrapper; - inputSlots.setItem(0, wrapper.getInventory().getStackInSlot(0)); - inputSlots.setItem(1, wrapper.getInventory().getStackInSlot(1)); + super.setItemName(upgradeWrapper.getItemName()); + } - super.setItemName(wrapper.getItemName()); - initializing = false; + @Override + protected void createInputSlots(ItemCombinerMenuSlotDefinition itemCombinerMenuSlotDefinition) { + for(final ItemCombinerMenuSlotDefinition.SlotDefinition slotDefinition : itemCombinerMenuSlotDefinition.getSlots()) { + this.addSlot(new SlotSuppliedHandler(upgradeWrapper::getInventory, slotDefinition.slotIndex(), 0, 0) { + @Override + public void setChanged() { + super.setChanged(); + slotsChanged(inputSlots); + if (slotDefinition.slotIndex() == 0) { + if (upgradeWrapper.getItemName().isEmpty() != getItem().isEmpty()) { + String newItemName = getItem().isEmpty() ? "" : getItem().getHoverName().getString(); + upgradeWrapper.setItemName(newItemName); + setItemName(newItemName); + nameChangeListener.run(); + } + if (getItem().isEmpty()) { + setItemName(""); + upgradeWrapper.setItemName(""); + } + } + } + + @Override + public boolean mayPlace(ItemStack p_267156_) { + return slotDefinition.mayPlace().test(p_267156_); + } + }); + } } - @Nullable - public String getItemName() { - return itemName; + @Override + protected SimpleContainer createContainer(int size) { + return new SimpleContainer(size) { + public void setChanged() { + super.setChanged(); + slotsChanged(this); + } + + @Override + public ItemStack getItem(int pIndex) { + return upgradeWrapper.getInventory().getStackInSlot(pIndex); + } + + @Override + public void setItem(int pIndex, ItemStack pStack) { + upgradeWrapper.getInventory().setStackInSlot(pIndex, pStack); + } + }; } @Override - public void slotsChanged(Container inventory) { - super.slotsChanged(inventory); - if (initializing) { - return; - } - wrapper.getInventory().setStackInSlot(0, inputSlots.getItem(0)); - wrapper.getInventory().setStackInSlot(1, inputSlots.getItem(1)); - slotsChangeListener.run(); + public void slotsChanged(Container pInventory) { + createResult(); } @Override diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java index 8daaa7b8..aee20567 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java @@ -3,11 +3,9 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.Component; import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.inventory.Slot; -import net.minecraft.world.item.ItemStack; import net.p3pp3rf1y.sophisticatedbackpacks.client.gui.SBPTranslationHelper; import net.p3pp3rf1y.sophisticatedcore.client.gui.StorageScreenBase; import net.p3pp3rf1y.sophisticatedcore.client.gui.UpgradeSettingsTab; @@ -25,7 +23,6 @@ public class AnvilUpgradeTab extends UpgradeSettingsTab { public static final TextureBlitData RED_CROSS = new TextureBlitData(GuiHelper.GUI_CONTROLS, Dimension.SQUARE_256, new UV(113, 216), new Dimension(15, 15)); private static final Component TOO_EXPENSIVE_TEXT = Component.translatable("container.repair.expensive"); private final TextBox itemNameTextBox; - private ItemStack firstItemCache = ItemStack.EMPTY; public AnvilUpgradeTab(AnvilUpgradeContainer upgradeContainer, Position position, StorageScreenBase screen) { super(upgradeContainer, position, screen, SBPTranslationHelper.INSTANCE.translUpgrade("anvil"), SBPTranslationHelper.INSTANCE.translUpgradeTooltip("anvil")); @@ -46,45 +43,28 @@ protected void renderBg(GuiGraphics guiGraphics, Minecraft minecraft, int mouseX TextureBlitData textureBlitData = getContainer().getSlots().get(0).hasItem() ? EDIT_ITEM_NAME_BACKGROUND : EDIT_ITEM_NAME_BACKGROUND_DISABLED; GuiHelper.blit(guiGraphics, getX() - 4, getY() - ((getHeight() - 8) / 2) - 1, textureBlitData, getWidth() + 12, getHeight() + 2); - } }; + itemNameTextBox.setTextColor(-1); itemNameTextBox.setTextColorUneditable(-1); itemNameTextBox.setBordered(false); itemNameTextBox.setMaxLength(50); itemNameTextBox.setResponder(this::onNameChanged); - itemNameTextBox.setValue(getInitialNameValue()); + itemNameTextBox.setValue(getContainer().getItemName()); addHideableChild(itemNameTextBox); itemNameTextBox.setEditable(!upgradeContainer.getSlots().get(0).getItem().isEmpty()); - getContainer().setSlotsChangeListener(() -> { - ItemStack firstItem = getContainer().getSlots().get(0).getItem(); - if (!ItemStack.matches(firstItem, firstItemCache) || itemNameTextBox.getValue().isEmpty() != firstItem.isEmpty()) { - firstItemCache = firstItem; - itemNameTextBox.setValue(firstItem.isEmpty() ? "" : firstItem.getHoverName().getString()); - itemNameTextBox.setEditable(!firstItem.isEmpty()); - } + getContainer().setNameChangeListener(() -> { + itemNameTextBox.setValue(getContainer().getItemName()); + itemNameTextBox.setEditable(!getContainer().getSlots().get(0).getItem().isEmpty()); }); } - private String getInitialNameValue() { - ItemStack firstItem = getContainer().getSlots().get(0).getItem(); - String itemName = getContainer().getItemName(); - if (!firstItem.isEmpty() && itemName != null && !itemName.isEmpty()) { - return itemName; - } - return firstItem.isEmpty() ? "" : firstItem.getHoverName().getString(); - } - private void onNameChanged(String name) { if (getContainer().isProcessingOnTakeLogic()) { return; } - ItemStack firstItem = getContainer().getSlots().get(0).getItem(); - if (!firstItem.has(DataComponents.CUSTOM_NAME) && name.equals(firstItem.getHoverName().getString())) { - name = ""; - } getContainer().setItemName(name); } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index d17a3e01..deb27054 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -11,4 +11,6 @@ public net.minecraft.world.item.Item BASE_ATTACK_DAMAGE_UUID public net.minecraft.world.entity.item.ItemEntity pickupDelay public net.minecraft.world.item.RecordItem BY_NAME public net.minecraft.world.item.SpawnEggItem BY_ID -public net.minecraft.client.renderer.entity.EntityRenderDispatcher renderers \ No newline at end of file +public net.minecraft.client.renderer.entity.EntityRenderDispatcher renderers +protected net.minecraft.world.inventory.ItemCombinerMenu createInputSlots(Lnet/minecraft/world/inventory/ItemCombinerMenuSlotDefinition;)V +protected net.minecraft.world.inventory.ItemCombinerMenu createContainer(I)Lnet/minecraft/world/SimpleContainer; \ No newline at end of file