Skip to content

Commit

Permalink
fix: Fixed Anvil upgrade to not allow dupes when multiple players are…
Browse files Browse the repository at this point in the history
… using one upgrade at the same time
  • Loading branch information
P3pp3rF1y committed Sep 25, 2024
1 parent 35c19ce commit 66d779b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 53 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 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 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();
}
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 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 66d779b

Please sign in to comment.