From 66d779b37fa14487da5df0eb5e10dad40c85a087 Mon Sep 17 00:00:00 2001 From: P3pp3rF1y Date: Thu, 26 Sep 2024 00:54:01 +0200 Subject: [PATCH] fix: Fixed Anvil upgrade to not allow dupes when multiple players are using one upgrade at the same time --- gradle.properties | 2 +- .../upgrades/anvil/AnvilUpgradeContainer.java | 95 +++++++++++++------ .../upgrades/anvil/AnvilUpgradeTab.java | 29 +----- .../resources/META-INF/accesstransformer.cfg | 2 + 4 files changed, 75 insertions(+), 53 deletions(-) diff --git a/gradle.properties b/gradle.properties index 79f284e6..1a28ef5b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.daemon=false mod_id=sophisticatedbackpacks mod_group_id=sophisticatedbackpacks -mod_version=3.20.8 +mod_version=3.20.9 sonar_project_key=sophisticatedbackpacks:SophisticatedBackpacks github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedBackpacks 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 9790eae1..88367218 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 handleMessage(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 p_267204_) { + return new SimpleContainer(p_267204_) { + 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 pInventory) { - super.slotsChanged(pInventory); - if (initializing) { - return; - } - wrapper.getInventory().setStackInSlot(0, inputSlots.getItem(0)); - wrapper.getInventory().setStackInSlot(1, inputSlots.getItem(1)); - slotsChangeListener.run(); + 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 3084121a..e0003475 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/upgrades/anvil/AnvilUpgradeTab.java @@ -1,10 +1,8 @@ package net.p3pp3rf1y.sophisticatedbackpacks.upgrades.anvil; import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.renderer.GameRenderer; import net.minecraft.network.chat.Component; import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.inventory.Slot; @@ -51,45 +49,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.hasCustomHoverName() && 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 746066c0..d8365c74 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -35,3 +35,5 @@ public net.minecraft.world.item.crafting.SmithingTransformRecipe f_265907_ # add public net.minecraft.world.item.crafting.SmithingTransformRecipe f_266098_ # result public net.minecraft.world.item.crafting.ShapedRecipe f_44149_ # result protected net.minecraft.world.inventory.AnvilMenu f_39001_ # itemName +protected net.minecraft.world.inventory.ItemCombinerMenu m_266254_(Lnet/minecraft/world/inventory/ItemCombinerMenuSlotDefinition;)V # createInputSlots +protected net.minecraft.world.inventory.ItemCombinerMenu m_266190_(I)Lnet/minecraft/world/SimpleContainer; # createContainer