From be752e8aee8d4030bd97814a3a4c65fb0eeaacb6 Mon Sep 17 00:00:00 2001 From: Andrei Laiff Date: Sat, 14 Oct 2023 22:26:53 +0300 Subject: [PATCH] Fix `LevelTerminal` data recursion and few in fixes gui --- .../client/gui/GuiInterfaceWireless.java | 2 +- .../github/client/gui/GuiLevelMaintainer.java | 9 ++--- .../github/client/gui/GuiLevelTerminal.java | 24 ++++++------ .../container/ContainerLevelMaintainer.java | 21 +++++++--- .../gui/container/ContainerLevelTerminal.java | 2 +- .../common/tile/TileLevelMaintainer.java | 39 ++++++++++++++++++- .../adapters/PartLevelEmitterAdapter.java | 8 ++-- 7 files changed, 75 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/glodblock/github/client/gui/GuiInterfaceWireless.java b/src/main/java/com/glodblock/github/client/gui/GuiInterfaceWireless.java index 0120a66c..099f7f15 100644 --- a/src/main/java/com/glodblock/github/client/gui/GuiInterfaceWireless.java +++ b/src/main/java/com/glodblock/github/client/gui/GuiInterfaceWireless.java @@ -218,7 +218,7 @@ public class GuiInterfaceWireless extends FCBaseMEGui implements IDropToFillText protected void repositionSlots() { for (final Object obj : this.inventorySlots.inventorySlots) { if (obj instanceof final AppEngSlot slot) { - slot.yDisplayPosition = this.ySize + slot.getY() - 78 - 7; + slot.yDisplayPosition = this.ySize + slot.getY() - 78 - 4; } } } diff --git a/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java b/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java index a8db75eb..8b7b01db 100644 --- a/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java @@ -37,7 +37,6 @@ import com.glodblock.github.loader.ItemAndBlockHolder; import com.glodblock.github.network.CPacketLevelMaintainer; import com.glodblock.github.network.CPacketLevelMaintainer.Action; import com.glodblock.github.network.CPacketLevelTerminalCommands; -import com.glodblock.github.util.Ae2ReflectClient; import com.glodblock.github.util.FCGuiColors; import com.glodblock.github.util.NameConst; import com.glodblock.github.util.Util; @@ -46,7 +45,6 @@ import appeng.api.storage.data.IAEItemStack; import appeng.api.util.DimensionalCoord; import appeng.client.gui.AEBaseGui; import appeng.client.gui.widgets.GuiTabButton; -import appeng.client.render.AppEngRenderItem; import appeng.container.AEBaseContainer; import appeng.container.slot.SlotFake; import appeng.core.sync.network.NetworkHandler; @@ -64,7 +62,6 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { private final ContainerLevelMaintainer cont; private final Component[] component = new Component[TileLevelMaintainer.REQ_COUNT]; private final MouseRegionManager mouseRegions = new MouseRegionManager(this); - private final AppEngRenderItem stackSizeRenderer = Ae2ReflectClient.getStackSizeRenderer(this); private FCGuiTextField input; private int lastWorkingTick; private int refreshTick; @@ -219,7 +216,6 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { public boolean drawSlot0(Slot slot) { if (slot instanceof SlotFake) { IAEItemStack stack = ((SlotFluidConvertingFake) slot).getAeStack(); - GL11.glEnable(GL11.GL_DEPTH_TEST); super.func_146977_a(new SlotSingleItem(slot)); if (stack == null) return true; IAEItemStack fake = stack.copy(); @@ -228,16 +224,17 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { } else { fake.setStackSize(0); } + float lastZLevel = this.zLevel; this.zLevel = 0f; GL11.glTranslatef(0.0f, 0.0f, 200.0f); - stackSizeRenderer.setAeStack(fake); - stackSizeRenderer.renderItemOverlayIntoGUI( + aeRenderItem.renderItemOverlayIntoGUI( fontRendererObj, mc.getTextureManager(), fake.getItemStack(), slot.xDisplayPosition, slot.yDisplayPosition); GL11.glTranslatef(0.0f, 0.0f, -200.0f); + this.zLevel = lastZLevel; return false; } return true; diff --git a/src/main/java/com/glodblock/github/client/gui/GuiLevelTerminal.java b/src/main/java/com/glodblock/github/client/gui/GuiLevelTerminal.java index a5b3e65c..64a2f69e 100644 --- a/src/main/java/com/glodblock/github/client/gui/GuiLevelTerminal.java +++ b/src/main/java/com/glodblock/github/client/gui/GuiLevelTerminal.java @@ -51,7 +51,6 @@ import com.glodblock.github.network.CPacketInventoryAction; import com.glodblock.github.network.CPacketLevelTerminalCommands; import com.glodblock.github.network.CPacketLevelTerminalCommands.Action; import com.glodblock.github.network.CPacketRenamer; -import com.glodblock.github.util.Ae2ReflectClient; import com.glodblock.github.util.FCGuiColors; import com.glodblock.github.util.ModAndClassUtil; import com.glodblock.github.util.NameConst; @@ -69,7 +68,6 @@ import appeng.client.gui.widgets.GuiScrollbar; import appeng.client.gui.widgets.GuiTabButton; import appeng.client.gui.widgets.IDropToFillTextField; import appeng.client.gui.widgets.MEGuiTextField; -import appeng.client.render.AppEngRenderItem; import appeng.client.render.BlockPosHighlighter; import appeng.container.AEBaseContainer; import appeng.container.slot.AppEngSlot; @@ -95,7 +93,6 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel private static final ResourceLocation TEX_BG = FluidCraft.resource("textures/gui/level_terminal.png"); protected int offsetY; private static final int offsetX = 21; - private final AppEngRenderItem stackSizeRenderer = Ae2ReflectClient.getStackSizeRenderer(this); protected static String searchFieldOutputsText = ""; protected static String searchFieldNamesText = ""; protected static String currentMode = "OFF"; @@ -497,6 +494,7 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel Tessellator.instance.startDrawingQuads(); int relY = 0; final int slotLeftMargin = (VIEW_WIDTH - entry.rowSize * 18); + float lastZLevel = aeRenderItem.zLevel; entry.dispY = viewY; /* PASS 1: BG */ @@ -572,6 +570,9 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel if (stack != null) { NBTTagCompound data = stack.getTagCompound(); + ItemStack itemStack = data.hasKey(TLMTags.Stack.tagName) + ? ItemStack.loadItemStackFromNBT(data.getCompoundTag(TLMTags.Stack.tagName)) + : stack.copy(); long quantity = data.getLong(TLMTags.Quantity.tagName); long batch = data.getLong(TLMTags.Batch.tagName); State state = State.values()[data.getInteger(TLMTags.State.tagName)]; @@ -581,16 +582,16 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel GL11.glTranslatef(colLeft, viewY + rowYTop + 1, ITEM_STACK_Z); GL11.glEnable(GL12.GL_RESCALE_NORMAL); RenderHelper.enableGUIStandardItemLighting(); - stackSizeRenderer.zLevel = 3.0f - 50.0f; - stackSizeRenderer.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), stack, 0, 0); - stackSizeRenderer.zLevel = 0.0f; + aeRenderItem.zLevel = 3.0f - 50.0f; + aeRenderItem.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0); + aeRenderItem.zLevel = 0.0f; GL11.glTranslatef(0.0f, 0.0f, ITEM_STACK_OVERLAY_Z - ITEM_STACK_Z); - stack.stackSize = (int) quantity; - stackSizeRenderer.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, 0, 0); + itemStack.stackSize = (int) quantity; + aeRenderItem.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0); if (batch > 0) { - stack.stackSize = (int) batch; - stackSizeRenderer - .renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, 0, -11); + itemStack.stackSize = (int) batch; + aeRenderItem + .renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, -11); } int color = switch (state) { case Idle -> FCGuiColors.StateIdle.getColor(); @@ -635,6 +636,7 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel } } GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + aeRenderItem.zLevel = lastZLevel; return relY + 1; } diff --git a/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelMaintainer.java b/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelMaintainer.java index f469ca6c..030d68a5 100644 --- a/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelMaintainer.java @@ -126,12 +126,21 @@ public class ContainerLevelMaintainer extends AEBaseContainer { public static ItemStack createLevelValues(ItemStack itemStack) { var data = itemStack.hasTagCompound() ? itemStack.getTagCompound() : new NBTTagCompound(); - var itemStackTag = new NBTTagCompound(); - itemStack.writeToNBT(itemStackTag); - data.setTag(TLMTags.Stack.tagName, itemStackTag); - data.setLong(TLMTags.Quantity.tagName, itemStack.stackSize); - data.setLong(TLMTags.Batch.tagName, 0); - data.setBoolean(TLMTags.Enable.tagName, false); + if (!data.hasKey(TLMTags.Stack.tagName)) { + var itemStackTag = new NBTTagCompound(); + itemStack.copy().writeToNBT(itemStackTag); + data.setTag(TLMTags.Stack.tagName, itemStackTag); + } + if (!data.hasKey(TLMTags.Quantity.tagName)) { + data.setLong(TLMTags.Quantity.tagName, itemStack.stackSize); + } + if (!data.hasKey(TLMTags.Batch.tagName)) { + data.setLong(TLMTags.Batch.tagName, 0); + } + if (!data.hasKey(TLMTags.Enable.tagName)) { + data.setBoolean(TLMTags.Enable.tagName, false); + } + data.setInteger(TLMTags.State.tagName, State.None.ordinal()); itemStack.setTagCompound(data); diff --git a/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelTerminal.java b/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelTerminal.java index 5ea23e17..652ae95c 100644 --- a/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelTerminal.java +++ b/src/main/java/com/glodblock/github/client/gui/container/ContainerLevelTerminal.java @@ -205,7 +205,7 @@ public class ContainerLevelTerminal extends FCBaseContainer { } valid.add(i); } - if (valid.size() > 0) { + if (!valid.isEmpty()) { int[] validIndices = Ints.toArray(valid); NBTTagList tag = new NBTTagList(); for (int i = 0; i < valid.size(); ++i) { diff --git a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java index 7a8b3000..83d2ffb7 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -8,6 +8,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; +import org.jetbrains.annotations.Nullable; + import com.glodblock.github.api.registries.ILevelViewable; import com.glodblock.github.common.Config; import com.glodblock.github.common.item.ItemFluidDrop; @@ -49,6 +51,7 @@ import appeng.tile.events.TileEventType; import appeng.tile.grid.AENetworkTile; import appeng.tile.inventory.IAEAppEngInventory; import appeng.tile.inventory.InvOperation; +import appeng.util.Platform; import appeng.util.item.AEItemStack; import io.netty.buffer.ByteBuf; @@ -296,6 +299,27 @@ public class TileLevelMaintainer extends AENetworkTile public void readFromNBTEvent(NBTTagCompound data) { if (data.hasKey(TLMTags.RequestStacks.tagName)) { requests.requestStacks.readFromNbt(data, TLMTags.RequestStacks.tagName); + if (Platform.isServer()) { + for (int i = 0; i < REQ_COUNT; i++) { + if (requests.requestStacks.getStack(i) != null) { + ItemStack storageStack = requests.requestStacks.getStack(i).getItemStack(); + ItemStack itemStack = loadItemStackFromTag(storageStack); + ItemStack craftStack = removeRecursion(storageStack); + if (!ItemStack.areItemStacksEqual(itemStack, craftStack)) { + requests.updateStack(i, craftStack); + AELog.info( + "[TileLevelMaintainer] Replace craft stack from: " + itemStack.toString() + + ":" + + (itemStack.hasTagCompound() ? itemStack.getTagCompound() : "{no tags}") + + "; with: " + + craftStack + + ":" + + (craftStack.hasTagCompound() ? craftStack.getTagCompound() + : "{no tags}")); + } + } + } + } } else { // Migration from old data storage long[] batches = new long[REQ_COUNT]; @@ -323,6 +347,18 @@ public class TileLevelMaintainer extends AENetworkTile } } + private ItemStack removeRecursion(ItemStack itemStack) { + if (itemStack.hasTagCompound() && itemStack.getTagCompound().hasKey(TLMTags.Stack.tagName)) { + return removeRecursion(loadItemStackFromTag(itemStack)); + } + return itemStack; + } + + @Nullable + private static ItemStack loadItemStackFromTag(ItemStack itemStack) { + return ItemStack.loadItemStackFromNBT(itemStack.getTagCompound().getCompoundTag(TLMTags.Stack.tagName)); + } + @TileEvent(TileEventType.NETWORK_READ) public boolean readFromStream(final ByteBuf data) { final boolean oldPower = isPowered; @@ -571,8 +607,7 @@ public class TileLevelMaintainer extends AENetworkTile public IAEItemStack getCraftItem(int idx) { IAEItemStack is = requestStacks.getStack(idx); if (is == null) return null; - ItemStack qis = ItemStack - .loadItemStackFromNBT(is.getItemStack().getTagCompound().getCompoundTag(TLMTags.Stack.tagName)); + ItemStack qis = loadItemStackFromTag(is.getItemStack()); IAEItemStack qais = AEItemStack.create(qis); qais.setStackSize(getBatchSize(idx)); diff --git a/src/main/java/com/glodblock/github/coremod/registries/adapters/PartLevelEmitterAdapter.java b/src/main/java/com/glodblock/github/coremod/registries/adapters/PartLevelEmitterAdapter.java index a8bdb45d..af477981 100644 --- a/src/main/java/com/glodblock/github/coremod/registries/adapters/PartLevelEmitterAdapter.java +++ b/src/main/java/com/glodblock/github/coremod/registries/adapters/PartLevelEmitterAdapter.java @@ -33,11 +33,13 @@ public class PartLevelEmitterAdapter implements ILevelViewable, ILevelViewableAd }; @NotNull - static public IInventory getPatchedInventory(IInventory inventory, long value, State state) { + static public IInventory getPatchedInventory(IInventory inventory, long quantity, State state) { ItemStack itemStack = inventory.getStackInSlot(SLOT_IN).copy(); NBTTagCompound data = !itemStack.hasTagCompound() ? new NBTTagCompound() : itemStack.getTagCompound(); - data.setLong(TLMTags.Quantity.tagName, value); - data.setInteger(TLMTags.State.tagName, state.ordinal()); + if (!data.hasKey(TLMTags.Quantity.tagName) || data.getLong(TLMTags.Quantity.tagName) != quantity) + data.setLong(TLMTags.Quantity.tagName, quantity); + if (!data.hasKey(TLMTags.State.tagName) || data.getInteger(TLMTags.State.tagName) != state.ordinal()) + data.setInteger(TLMTags.State.tagName, state.ordinal()); itemStack.setTagCompound(data); inventory.setInventorySlotContents(SLOT_IN, itemStack);