Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Fixed Anvil upgrade to not allow dupes when multiple players a… #1159

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,21 +20,20 @@ public class AnvilUpgradeContainer extends UpgradeContainerBase<AnvilUpgradeWrap
private final Slot resultSlot;

private final PersistableAnvilMenu anvilMenuDelegate;
private Runnable slotsChangeListener = () -> {};
private Runnable nameChangeListener = () -> {};
private boolean processingOnTakeLogic = false;
public AnvilUpgradeContainer(Player player, int upgradeContainerId, AnvilUpgradeWrapper upgradeWrapper, UpgradeContainerType<AnvilUpgradeWrapper, AnvilUpgradeContainer> 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));
resultSlot = anvilMenuDelegate.getSlot(2);
slots.add(resultSlot);
}

public void setSlotsChangeListener(Runnable slotsChangeListener) {
this.slotsChangeListener = slotsChangeListener;
public void setNameChangeListener(Runnable nameChangeListener) {
this.nameChangeListener = nameChangeListener;
}

@Override
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +23,6 @@ public class AnvilUpgradeTab extends UpgradeSettingsTab<AnvilUpgradeContainer> {
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"));
Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
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;