From f26d18e3213d48cc8fa3d7395638a875476a3379 Mon Sep 17 00:00:00 2001 From: Andrei Laiff Date: Thu, 12 Oct 2023 12:40:57 +0300 Subject: [PATCH] feat: Rework scheduling and data storage in `TileLevelMaintainer` functionality --- .../github/client/gui/GuiLevelMaintainer.java | 106 ++-- .../container/ContainerLevelMaintainer.java | 94 ++- .../com/glodblock/github/common/Config.java | 8 + .../common/parts/PartFluidLevelEmitter.java | 5 +- .../common/tile/TileLevelMaintainer.java | 584 +++++++++++------- .../crossmod/waila/TileWailaDataProvider.java | 4 +- ... => LevelMaintainerWailaDataProvider.java} | 12 +- ...erminal.java => IClickableInTerminal.java} | 2 +- .../network/CPacketLevelMaintainer.java | 127 ++-- .../github/network/CPacketRenamer.java | 107 ++-- .../com/glodblock/github/util/Updater.java | 7 + 11 files changed, 640 insertions(+), 416 deletions(-) rename src/main/java/com/glodblock/github/crossmod/waila/tile/{LevelMaintainerWailaDataProvide.java => LevelMaintainerWailaDataProvider.java} (76%) rename src/main/java/com/glodblock/github/inventory/item/{IWirelessInterfaceTerminal.java => IClickableInTerminal.java} (74%) create mode 100644 src/main/java/com/glodblock/github/util/Updater.java 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 5f37fc5f..b3933681 100644 --- a/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/client/gui/GuiLevelMaintainer.java @@ -1,5 +1,7 @@ package com.glodblock.github.client.gui; +import static com.glodblock.github.client.gui.container.ContainerLevelMaintainer.createLevelValues; + import java.awt.Rectangle; import java.util.ArrayList; import java.util.Collections; @@ -19,11 +21,15 @@ import org.lwjgl.input.Keyboard; import com.glodblock.github.FluidCraft; import com.glodblock.github.client.gui.container.ContainerLevelMaintainer; import com.glodblock.github.common.tile.TileLevelMaintainer; +import com.glodblock.github.common.tile.TileLevelMaintainer.State; +import com.glodblock.github.common.tile.TileLevelMaintainer.TLMTags; import com.glodblock.github.inventory.gui.MouseRegionManager; import com.glodblock.github.inventory.slot.SlotFluidConvertingFake; import com.glodblock.github.inventory.slot.SlotSingleItem; import com.glodblock.github.network.CPacketLevelMaintainer; +import com.glodblock.github.network.CPacketLevelMaintainer.Action; import com.glodblock.github.util.Ae2ReflectClient; +import com.glodblock.github.util.FCGuiColors; import com.glodblock.github.util.NameConst; import appeng.api.storage.data.IAEItemStack; @@ -67,19 +73,20 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { public void postUpdate(List list) { for (Slot slot : this.cont.getRequestSlots()) { if (!slot.getHasStack()) { - component[slot.getSlotIndex()].setState(TileLevelMaintainer.State.Nothing); + component[slot.getSlotIndex()].setState(State.None); } } for (IAEItemStack is : list) { NBTTagCompound data = is.getItemStack().getTagCompound(); - long size = data.getLong("Batch"); - int i = data.getInteger("Index"); - boolean isEnable = data.getBoolean("Enable"); - TileLevelMaintainer.State state = TileLevelMaintainer.State.values()[data.getInteger("State")]; - component[i].getQty().textField.setText(String.valueOf(is.getStackSize())); - component[i].getBatch().textField.setText(String.valueOf(size)); - component[i].setEnable(isEnable); - component[i].setState(state); + long batch = data.getLong(TLMTags.Batch.tagName); + long quantity = data.getLong(TLMTags.Quantity.tagName); + int idx = data.getInteger(TLMTags.Index.tagName); + boolean isEnable = data.getBoolean(TLMTags.Enable.tagName); + State state = State.values()[data.getInteger(TLMTags.State.tagName)]; + component[idx].getQty().textField.setText(String.valueOf(quantity)); + component[idx].getBatch().textField.setText(String.valueOf(batch)); + component[idx].setEnable(isEnable); + component[idx].setState(state); } } @@ -92,19 +99,19 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { new FCGuiTextField(this.fontRendererObj, guiLeft + 46, guiTop + 19 + 19 * i, 52, 14), NameConst.TT_LEVEL_MAINTAINER_REQUEST_SIZE, i, - "TileLevelMaintainer.Quantity"), + Action.Quantity), new Widget( new FCGuiTextField(this.fontRendererObj, guiLeft + 100, guiTop + 19 + 19 * i, 52, 14), NameConst.TT_LEVEL_MAINTAINER_BATCH_SIZE, i, - "TileLevelMaintainer.Batch"), + Action.Batch), new GuiFCImgButton(guiLeft + 105 + 47, guiTop + 17 + 19 * i, "SUBMIT", "SUBMIT", false), new GuiFCImgButton(guiLeft + 9, guiTop + 20 + 19 * i, "ENABLE", "ENABLE", false), new GuiFCImgButton(guiLeft + 9, guiTop + 20 + 19 * i, "DISABLE", "DISABLE", false), new FCGuiLineField(fontRendererObj, guiLeft + 47, guiTop + 33 + 19 * i, 125), this.buttonList); } - FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer("TileLevelMaintainer.refresh")); + FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Refresh)); } public void drawScreen(final int mouseX, final int mouseY, final float btn) { @@ -124,7 +131,7 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { int tick = this.refreshTick; int interval = 20; if (tick > lastWorkingTick + interval && this.input == null) { - FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer("TileLevelMaintainer.refresh")); + FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Refresh)); lastWorkingTick = this.refreshTick; } for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) { @@ -158,7 +165,7 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { stackSizeRenderer.renderItemOverlayIntoGUI( fontRendererObj, mc.getTextureManager(), - stack.getItemStack(), + fake.getItemStack(), slot.xDisplayPosition, slot.yDisplayPosition); return false; @@ -256,12 +263,10 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { public boolean handleDragNDrop(GuiContainer gui, int mouseX, int mouseY, ItemStack draggedStack, int button) { for (SlotFluidConvertingFake slot : this.cont.getRequestSlots()) { if (getSlotArea(slot).contains(mouseX, mouseY)) { - slot.putStack(draggedStack); - NetworkHandler.instance.sendToServer(new PacketNEIDragClick(draggedStack, slot.getSlotIndex())); - if (draggedStack != null) { - this.updateAmount(slot.getSlotIndex(), draggedStack.stackSize); - draggedStack.stackSize = 0; - } + ItemStack itemStack = createLevelValues(draggedStack.copy()); + slot.putStack(itemStack); + NetworkHandler.instance.sendToServer(new PacketNEIDragClick(itemStack, slot.getSlotIndex())); + this.updateAmount(slot.getSlotIndex(), itemStack.stackSize); return true; } } @@ -285,8 +290,9 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { private final GuiFCImgButton enable; private final GuiFCImgButton submit; private final FCGuiLineField line; - private TileLevelMaintainer.State state; + private State state; + @SuppressWarnings("unchecked") public Component(Widget qtyInput, Widget batchInput, GuiFCImgButton submitBtn, GuiFCImgButton enableBtn, GuiFCImgButton disableBtn, FCGuiLineField line, List buttonList) { this.qty = qtyInput; @@ -295,14 +301,14 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { this.disable = disableBtn; this.submit = submitBtn; this.line = line; - this.state = TileLevelMaintainer.State.Nothing; + this.state = State.None; buttonList.add(this.submit); buttonList.add(this.enable); buttonList.add(this.disable); } public int getIndex() { - return this.getQty().idx; + return this.qty.idx; } public void setEnable(boolean enable) { @@ -332,20 +338,18 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { if (this.send(this.getQty())) this.send(this.getBatch()); didSomething = true; } else if (this.enable == btn) { - FluidCraft.proxy.netHandler - .sendToServer(new CPacketLevelMaintainer("TileLevelMaintainer.Enable", this.getIndex())); + FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Enable, this.getIndex())); didSomething = true; } else if (this.disable == btn) { - FluidCraft.proxy.netHandler - .sendToServer(new CPacketLevelMaintainer("TileLevelMaintainer.Disable", this.getIndex())); + FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Disable, this.getIndex())); didSomething = true; } return didSomething; } public FCGuiTextField isMouseIn(final int xCoord, final int yCoord) { - if (this.getQty().textField.isMouseIn(xCoord, yCoord)) return this.getQty().textField; - if (this.getBatch().textField.isMouseIn(xCoord, yCoord)) return this.getBatch().textField; + if (this.qty.textField.isMouseIn(xCoord, yCoord)) return this.getQty().textField; + if (this.batch.textField.isMouseIn(xCoord, yCoord)) return this.getBatch().textField; return null; } @@ -362,33 +366,39 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { } public void draw() { - this.getQty().draw(); - this.getBatch().draw(); + this.qty.draw(); + this.batch.draw(); ArrayList message = new ArrayList<>(); message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_TITLE) + "\n"); switch (this.state) { - case Idling -> { - this.line.setColor(0xFF55FF55); + case Idle -> { + this.line.setColor(FCGuiColors.StateIdle.getColor()); message.add( - NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " " + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_IDLE)); } - case Crafting -> { - this.line.setColor(0xFFFFFF55); + case Craft -> { + this.line.setColor(FCGuiColors.StateCraft.getColor()); message.add( - NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " " + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_LINK)); } - case Exporting -> { - this.line.setColor(0xFFAA00AA); + case Export -> { + this.line.setColor(FCGuiColors.StateExport.getColor()); message.add( - NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " " + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_EXPORT)); } - default -> { - this.line.setColor(0); + case Error -> { + this.line.setColor(FCGuiColors.StateError.getColor()); message.add( - NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " " + + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_ERROR)); + } + default -> { + this.line.setColor(FCGuiColors.StateNone.getColor()); + message.add( + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " " + NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_NONE)); } } @@ -399,7 +409,9 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_LINK)); message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_LINK_DESC) + "\n"); message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_EXPORT)); - message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_EXPORT_DESC)); + message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_EXPORT_DESC) + "\n"); + message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_ERROR)); + message.add(NameConst.i18n(NameConst.TT_LEVEL_MAINTAINER_ERROR_DESC)); } else { message.add(NameConst.i18n(NameConst.TT_SHIFT_FOR_MORE)); } @@ -415,7 +427,7 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { } } - public void setState(TileLevelMaintainer.State state) { + public void setState(State state) { this.state = state; } } @@ -423,11 +435,11 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler { private class Widget { public final int idx; - public final String action; + public final Action action; private final FCGuiTextField textField; private final String tooltip; - public Widget(FCGuiTextField textField, String tooltip, int idx, String action) { + public Widget(FCGuiTextField textField, String tooltip, int idx, Action action) { this.textField = textField; this.textField.setEnableBackgroundDrawing(false); this.textField.setText("0"); 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 8a31cb7f..f469ca6c 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 @@ -1,16 +1,25 @@ package com.glodblock.github.client.gui.container; +import java.util.Objects; + import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import com.glodblock.github.common.tile.TileLevelMaintainer; +import com.glodblock.github.common.tile.TileLevelMaintainer.State; +import com.glodblock.github.common.tile.TileLevelMaintainer.TLMTags; import com.glodblock.github.inventory.AeItemStackHandler; import com.glodblock.github.inventory.slot.SlotFluidConvertingFake; +import com.glodblock.github.util.Util; +import appeng.api.config.SecurityPermissions; import appeng.container.AEBaseContainer; -import appeng.util.item.AEItemStack; +import appeng.helpers.InventoryAction; +import appeng.util.Platform; public class ContainerLevelMaintainer extends AEBaseContainer { @@ -37,15 +46,96 @@ public class ContainerLevelMaintainer extends AEBaseContainer { return this.requestSlots; } + @Override + public void doAction(EntityPlayerMP player, InventoryAction action, int slotId, long id) { + Slot slot = getSlot(slotId); + if (slot instanceof SlotFluidConvertingFake) { + final ItemStack stack = player.inventory.getItemStack(); + switch (action) { + case PICKUP_OR_SET_DOWN -> { + if (stack == null) { + slot.putStack(null); + } else { + ((SlotFluidConvertingFake) slot).putConvertedStack(createLevelValues(stack.copy())); + } + } + case PLACE_SINGLE -> { + if (stack != null) { + ((SlotFluidConvertingFake) slot).putConvertedStack( + createLevelValues(Objects.requireNonNull(Util.copyStackWithSize(stack, 1)))); + } + } + case SPLIT_OR_PLACE_SINGLE -> { + ItemStack inSlot = slot.getStack(); + if (inSlot != null) { + if (stack == null) { + slot.putStack( + createLevelValues( + Objects.requireNonNull( + Util.copyStackWithSize( + inSlot, + Math.max(1, inSlot.stackSize - 1))))); + } else if (stack.isItemEqual(inSlot)) { + slot.putStack( + createLevelValues( + Objects.requireNonNull( + Util.copyStackWithSize( + inSlot, + Math.min( + inSlot.getMaxStackSize(), + inSlot.stackSize + 1))))); + } else { + ((SlotFluidConvertingFake) slot).putConvertedStack( + createLevelValues(Objects.requireNonNull(Util.copyStackWithSize(stack, 1)))); + } + } else if (stack != null) { + ((SlotFluidConvertingFake) slot).putConvertedStack( + createLevelValues(Objects.requireNonNull(Util.copyStackWithSize(stack, 1)))); + } + } + } + } else { + super.doAction(player, action, slotId, id); + } + } + @Override public ItemStack transferStackInSlot(EntityPlayer player, int idx) { + if (Platform.isClient()) { + return null; + } + for (SlotFluidConvertingFake slot : this.getRequestSlots()) { if (!slot.getHasStack()) { - slot.setAeStack(AEItemStack.create(((Slot) this.inventorySlots.get(idx)).getStack()), true); + ItemStack itemStack = ((Slot) this.inventorySlots.get(idx)).getStack(); + slot.putConvertedStack(createLevelValues(itemStack.copy())); break; } } + this.detectAndSendChanges(); return null; } + + @Override + public void detectAndSendChanges() { + this.verifyPermissions(SecurityPermissions.BUILD, false); + + super.detectAndSendChanges(); + } + + 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); + data.setInteger(TLMTags.State.tagName, State.None.ordinal()); + itemStack.setTagCompound(data); + + return itemStack; + } + } diff --git a/src/main/java/com/glodblock/github/common/Config.java b/src/main/java/com/glodblock/github/common/Config.java index 558f4195..b8a74dda 100644 --- a/src/main/java/com/glodblock/github/common/Config.java +++ b/src/main/java/com/glodblock/github/common/Config.java @@ -20,6 +20,8 @@ public class Config { public static int packetSize; public static int packetRate; public static boolean replaceEC2; + public static int levelMaintainerMinTicks; + public static int levelMaintainerMaxTicks; public static void run() { loadCategory(); @@ -66,6 +68,12 @@ public class Config { "Fluid Craft for AE2", true, "Set true to handle missing item mappings from EC2. Note to work properly, you must have all relevant parts."); + + levelMaintainerMinTicks = Config.get("LevelMaintainer", "minTick", 5, "Number on ticks for minimal request") + .getInt(); + levelMaintainerMaxTicks = Config.get("LevelMaintainer", "maxTick", 120, "Number on ticks for maximal request") + .getInt(); + if (Config.hasChanged()) Config.save(); } diff --git a/src/main/java/com/glodblock/github/common/parts/PartFluidLevelEmitter.java b/src/main/java/com/glodblock/github/common/parts/PartFluidLevelEmitter.java index 3abab719..147ed28f 100644 --- a/src/main/java/com/glodblock/github/common/parts/PartFluidLevelEmitter.java +++ b/src/main/java/com/glodblock/github/common/parts/PartFluidLevelEmitter.java @@ -17,6 +17,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; +import com.glodblock.github.api.registries.ILevelViewable; import com.glodblock.github.common.item.ItemFluidPacket; import com.glodblock.github.inventory.InventoryHandler; import com.glodblock.github.inventory.gui.GuiType; @@ -62,8 +63,8 @@ import appeng.util.Platform; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -public class PartFluidLevelEmitter extends PartUpgradeable - implements IStackWatcherHost, ICraftingWatcherHost, IMEMonitorHandlerReceiver, IGridTickable { +public class PartFluidLevelEmitter extends PartUpgradeable implements IStackWatcherHost, ICraftingWatcherHost, + IMEMonitorHandlerReceiver, IGridTickable, ILevelViewable { private static final int FLAG_ON = 8; 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 733673a0..7a8b3000 100644 --- a/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/common/tile/TileLevelMaintainer.java @@ -5,11 +5,16 @@ import java.util.concurrent.Future; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.util.ForgeDirection; +import com.glodblock.github.api.registries.ILevelViewable; +import com.glodblock.github.common.Config; import com.glodblock.github.common.item.ItemFluidDrop; import com.glodblock.github.inventory.AeItemStackHandler; import com.glodblock.github.inventory.AeStackInventory; import com.glodblock.github.inventory.AeStackInventoryImpl; +import com.glodblock.github.util.Updater; import com.google.common.collect.ImmutableSet; import appeng.api.AEApi; @@ -37,7 +42,6 @@ import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; import appeng.core.AELog; -import appeng.crafting.CraftingLink; import appeng.helpers.NonNullArrayIterator; import appeng.me.GridAccessException; import appeng.tile.TileEvent; @@ -49,135 +53,146 @@ import appeng.util.item.AEItemStack; import io.netty.buffer.ByteBuf; public class TileLevelMaintainer extends AENetworkTile - implements IAEAppEngInventory, IGridTickable, ICraftingRequester, IPowerChannelState { + implements IAEAppEngInventory, IGridTickable, ICraftingRequester, IPowerChannelState, ILevelViewable { public static final int REQ_COUNT = 5; public final InventoryRequest requests = new InventoryRequest(this); private final BaseActionSource source; - private final IInventory inv = new AeItemStackHandler(requests.getRequestStacks()); + private final IInventory inv = new AeItemStackHandler(requests.requestStacks); private boolean isPowered = false; public TileLevelMaintainer() { getProxy().setIdlePowerUsage(1D); getProxy().setFlags(GridFlags.REQUIRE_CHANNEL); - this.source = new MachineSource(this); + source = new MachineSource(this); } public AeStackInventory getRequestSlots() { - return requests.getRequestStacks(); + return requests.requestStacks; } @Override public ImmutableSet getRequestedJobs() { - return ImmutableSet.copyOf(new NonNullArrayIterator<>(this.requests.getLinks())); - } - - private IAEItemStack setState(ICraftingLink link, IAEItemStack items) { - if (items != null) { - requests.setState(link, State.Exporting); - } else { - requests.setState(link, State.Crafting); - } - return items; + return ImmutableSet.copyOf(new NonNullArrayIterator<>(requests.links)); } @Override public IAEItemStack injectCraftedItems(ICraftingLink link, IAEItemStack items, Actionable mode) { + int idx = requests.getIdxByLink(link); try { - if (this.getProxy().isActive()) { - final IEnergyGrid energy = this.getProxy().getEnergy(); + if (getProxy().isActive()) { + final IEnergyGrid energy = getProxy().getEnergy(); final double power = Math .ceil(ItemFluidDrop.isFluidStack(items) ? items.getStackSize() / 1000D : items.getStackSize()); if (energy.extractAEPower(power, mode, PowerMultiplier.CONFIG) > power - 0.01) { if (ItemFluidDrop.isFluidStack(items)) { - IAEFluidStack notInserted = this.getProxy().getStorage().getFluidInventory() - .injectItems(ItemFluidDrop.getAeFluidStack(items), mode, this.source); - if (notInserted != null) { - items.setStackSize(notInserted.getStackSize()); - return setState(link, items); + IAEFluidStack notInjectedItems = getProxy().getStorage().getFluidInventory() + .injectItems(ItemFluidDrop.getAeFluidStack(items), mode, source); + if (notInjectedItems != null) { + items.setStackSize(notInjectedItems.getStackSize()); + requests.updateState(idx, State.Export); + + return items; } else { return null; } } else { - return setState( - link, - this.getProxy().getStorage().getItemInventory().injectItems(items, mode, this.source)); + return getProxy().getStorage().getItemInventory().injectItems(items, mode, source); } } } } catch (GridAccessException e) { AELog.debug(e); } - return setState(link, items); + + return items; } @Override public void jobStateChange(ICraftingLink link) { - if (this.requests.getLinks() != null) { - for (int x = 0; x < this.requests.getLinks().length; x++) { - if (this.requests.getLinks()[x] == link) { - this.requests.getLinks()[x] = link; - return; - } + for (int x = 0; x < REQ_COUNT; x++) { + if (requests.getLink(x) == link) { + requests.updateLink(x, link); + return; } } } @Override public TickingRequest getTickingRequest(IGridNode node) { - return new TickingRequest(5, 120, false, true); + return new TickingRequest(Config.levelMaintainerMinTicks, Config.levelMaintainerMaxTicks, false, true); } @Override public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) { - return this.canDoBusWork() ? this.doWork() : TickRateModulation.IDLE; + return canDoBusWork() ? doWork() : TickRateModulation.IDLE; } private TickRateModulation doWork() { - if (!this.getProxy().isActive() || !this.canDoBusWork()) { + if (!getProxy().isActive() || !canDoBusWork()) { return TickRateModulation.IDLE; } - boolean didSomething = false; try { - ICraftingGrid cg = this.getProxy().getCrafting(); - IGrid grid = this.getProxy().getGrid(); - final IItemList inv = this.getProxy().getStorage().getItemInventory().getStorageList(); + final ICraftingGrid craftingGrid = getProxy().getCrafting(); + final IGrid grid = getProxy().getGrid(); + final IItemList inv = getProxy().getStorage().getItemInventory().getStorageList(); for (int i = 0; i < REQ_COUNT; i++) { - IAEItemStack is = requests.getRequestQtyStack(i); - if (is != null && requests.getBatchSize(i) > 0) { + long quantity = requests.getQuantity(i); + long batchSize = requests.getBatchSize(i); + boolean isEnable = requests.isEnable(i); + if (!isEnable || batchSize == 0) requests.updateState(i, State.None); + if (batchSize > 0) { IAEItemStack craftItem = requests.getCraftItem(i); - if (this.requests.isDone(i)) this.requests.setState(i, State.Idling); - if (cg.canEmitFor(craftItem) || cg.isRequesting(craftItem) - || inv.findPrecise(is) == null - || !inv.findPrecise(is).isCraftable() - || inv.findPrecise(is).getStackSize() >= is.getStackSize() - || !this.requests.isDone(i)) + IAEItemStack aeItem = inv.findPrecise(craftItem); + boolean isDone = requests.isDone(i); + boolean isCraftable = aeItem != null && aeItem.isCraftable(); + boolean shouldCraft = isCraftable && aeItem.getStackSize() < quantity; + if (isDone) requests.updateState(i, State.Idle); + if (!isCraftable) requests.updateState(i, State.Error); + if (craftingGrid.canEmitFor(craftItem) || craftingGrid.isRequesting(craftItem) + || !isDone + || !shouldCraft) continue; // do crafting - Future jobTask = requests.getJobs()[i]; + Future jobTask = requests.getJob(i); if (jobTask == null) { - requests.getJobs()[i] = cg.beginCraftingJob(getWorldObj(), grid, this.source, craftItem, null); + requests.updateJob( + i, + craftingGrid.beginCraftingJob(getWorldObj(), grid, source, craftItem, null)); + requests.updateState(i, State.Craft); + + // calculate only one job per tick request + return TickRateModulation.SAME; } else if (jobTask.isDone()) { - requests.setState(i, State.Idling); + requests.updateState(i, State.Craft); try { ICraftingJob job = jobTask.get(); if (job != null) { - ICraftingLink link = cg.submitJob(job, this, null, false, this.source); - requests.jobs[i] = null; + ICraftingLink link = craftingGrid.submitJob(job, this, null, false, source); + requests.updateJob(i, null); if (link != null) { - didSomething = true; - requests.setState(i, State.Crafting); - requests.getLinks()[i] = link; + requests.updateState(i, State.Craft); + requests.updateLink(i, link); + + // submit only one job per tick request + return TickRateModulation.SAME; + } else { + requests.updateState(i, State.Error); } + } else { + requests.updateState(i, State.Error); } } catch (Exception ignored) { - + requests.updateState(i, State.Error); } } } } - } catch (final GridAccessException ignore) {} - return didSomething ? TickRateModulation.FASTER : TickRateModulation.SLOWER; + } catch (final GridAccessException ignore) { + + } + + return TickRateModulation.SAME; } @Override @@ -196,7 +211,7 @@ public class TileLevelMaintainer extends AENetworkTile } protected boolean canDoBusWork() { - return this.getProxy().isActive(); + return getProxy().isActive(); } @Override @@ -204,275 +219,364 @@ public class TileLevelMaintainer extends AENetworkTile @Override public boolean isPowered() { - return this.isPowered; + return isPowered; } @Override public boolean isActive() { - return this.isPowered; + return isPowered; } public InventoryRequest getRequestInventory() { - return this.requests; + return requests; } public IInventory getInventory() { return inv; } + public IInventory getInventoryByName(String name) { + if (name == "config") return new AeItemStackHandler(requests.requestStacks); + + return null; + } + public void updateQuantity(int idx, long size) { - this.requests.updateQuantity(idx, size); + requests.updateQuantity(idx, size); } public void updateBatchSize(int idx, long size) { - this.requests.updateBatchSize(idx, size); + requests.updateBatchSize(idx, size); } - public void setRequestStatus(int idx, boolean enable) { - this.requests.setEnable(idx, enable); + public void updateStatus(int idx, boolean enable) { + requests.updateStatus(idx, enable); } private void readLinkFromNBT(NBTTagCompound data) { + for (int i = 0; i < REQ_COUNT; i++) { + final NBTTagCompound stackData = requests.getItemStack(i).getTagCompound(); + if (stackData != null && stackData.hasNoTags() + && !stackData.hasKey(TLMTags.Link.tagName) + && stackData.getCompoundTag(TLMTags.Link.tagName).hasNoTags()) { + requests.updateLink(i, null); + requests.updateState(i, State.Idle); + } else { + NBTTagCompound linkData = stackData.getCompoundTag(TLMTags.Link.tagName); + requests.updateLink(i, AEApi.instance().storage().loadCraftingLink(linkData, this)); + if (!requests.isDone(i)) requests.updateState(i, State.Craft); + } + } + } + + /** + * @deprecated + */ + @Deprecated + private void readLinkFromNBT__old(NBTTagCompound data) { try { - for (int i = 0; i < this.requests.getLinks().length; i++) { + for (int i = 0; i < REQ_COUNT; i++) { final NBTTagCompound link = data.getCompoundTag("links-" + i); if (link != null && link.hasNoTags()) { - this.requests.getLinks()[i] = null; + requests.updateLink(i, null); } else { - this.requests.getLinks()[i] = AEApi.instance().storage().loadCraftingLink(link, this); - if (!this.requests.isDone(i)) this.requests.setState(i, State.Crafting); + requests.updateLink(i, AEApi.instance().storage().loadCraftingLink(link, this)); + if (!requests.isDone(i)) requests.updateState(i, State.Craft); } } } catch (Exception ignore) {} } - private NBTTagCompound writeLinkToNBT(NBTTagCompound data) { - for (int i = 0; i < this.requests.getLinks().length; i++) { - if (this.requests.getLinks()[i] != null) { - CraftingLink link = (CraftingLink) this.requests.getLinks()[i]; - NBTTagCompound linkData = new NBTTagCompound(); - link.writeToNBT(linkData); - data.setTag("links-" + i, linkData); - } - } - return data; - } - @TileEvent(TileEventType.WORLD_NBT_WRITE) - public NBTTagCompound writeToNBTEvent(NBTTagCompound data) { - this.requests.requestStacks.writeToNbt(data, "Inventory"); - this.requests.requestBatches.writeToNbt(data, "Batch"); - this.requests.requestQtys.writeToNbt(data, "Count"); - return writeLinkToNBT(data); + public void writeToNBTEvent(NBTTagCompound data) { + requests.requestStacks.writeToNbt(data, TLMTags.RequestStacks.tagName); } @TileEvent(TileEventType.WORLD_NBT_READ) public void readFromNBTEvent(NBTTagCompound data) { - this.requests.requestStacks.readFromNbt(data, "Inventory"); - this.requests.requestBatches.readFromNbt(data, "Batch"); - this.requests.requestQtys.readFromNbt(data, "Count"); - this.readLinkFromNBT(data); + if (data.hasKey(TLMTags.RequestStacks.tagName)) { + requests.requestStacks.readFromNbt(data, TLMTags.RequestStacks.tagName); + } else { + // Migration from old data storage + long[] batches = new long[REQ_COUNT]; + long[] quantyties = new long[REQ_COUNT]; + requests.requestStacks.readFromNbt(data, "Batch"); + for (int i = 0; i < REQ_COUNT; i++) { + IAEItemStack batchStack = requests.requestStacks.getStack(i); + batches[i] = batchStack != null ? batchStack.getStackSize() : 0; + } + requests.requestStacks.readFromNbt(data, "Count"); + for (int i = 0; i < REQ_COUNT; i++) { + IAEItemStack quantityStack = requests.requestStacks.getStack(i); + quantyties[i] = quantityStack != null ? quantityStack.getStackSize() : 0; + } + requests.requestStacks.readFromNbt(data, "Inventory"); + for (int i = 0; i < REQ_COUNT; i++) { + IAEItemStack requestsStack = requests.requestStacks.getStack(i); + if (requestsStack != null) { + requests.updateStack(i, requestsStack.getItemStack()); + requests.updateBatchSize(i, batches[i]); + requests.updateQuantity(i, quantyties[i]); + } + } + readLinkFromNBT__old(data); + } } @TileEvent(TileEventType.NETWORK_READ) public boolean readFromStream(final ByteBuf data) { - final boolean oldPower = this.isPowered; - this.isPowered = data.readBoolean(); - return this.isPowered != oldPower; + final boolean oldPower = isPowered; + isPowered = data.readBoolean(); + return isPowered != oldPower; } @TileEvent(TileEventType.NETWORK_WRITE) public void writeToStream(final ByteBuf data) { - data.writeBoolean(this.isActive()); + data.writeBoolean(isActive()); } @MENetworkEventSubscribe public void stateChange(final MENetworkPowerStatusChange p) { - this.updatePowerState(); + updatePowerState(); } @MENetworkEventSubscribe public final void bootingRender(final MENetworkBootingStatusChange c) { - this.updatePowerState(); + updatePowerState(); } private void updatePowerState() { boolean newState = false; try { - newState = this.getProxy().isActive() - && this.getProxy().getEnergy().extractAEPower(1, Actionable.SIMULATE, PowerMultiplier.CONFIG) - > 0.0001; + newState = getProxy().isActive() + && getProxy().getEnergy().extractAEPower(1, Actionable.SIMULATE, PowerMultiplier.CONFIG) > 0.0001; } catch (final GridAccessException ignored) { } - if (newState != this.isPowered) { - this.isPowered = newState; - this.markForUpdate(); + if (newState != isPowered) { + isPowered = newState; + markForUpdate(); } } - public enum State { - Nothing, - Idling, - Crafting, - Exporting + @Override + public TileEntity getTile() { + return this; } - public static class InventoryRequest { + @Override + public ForgeDirection getSide() { + return ForgeDirection.UNKNOWN; + } + + @Override + public int rowSize() { + return REQ_COUNT; + } + + @Override + public ItemStack getSelfItemStack() { + return getItemFromTile(this); + } + + public enum State { + None, + Idle, + Craft, + Export, + Error + } + + public enum TLMTags { + + RequestStacks("RequestStacks"), + Enable("Enable"), + Quantity("Quantity"), + Batch("Batch"), + Link("Link"), + State("State"), + Index("Index"), + Stack("Stack"); + + public final String tagName; + + TLMTags(String tagName) { + this.tagName = tagName; + } + } + + public static final class InventoryRequest { private final AeStackInventoryImpl requestStacks; - private final AeStackInventoryImpl requestBatches; - private final AeStackInventoryImpl requestQtys; private final Future[] jobs; private final ICraftingLink[] links; private final State[] state = new State[REQ_COUNT]; @SuppressWarnings("unchecked") public InventoryRequest(TileLevelMaintainer tile) { - this.requestStacks = new AeStackInventoryImpl<>(StorageChannel.ITEMS, REQ_COUNT, tile); - this.requestBatches = new AeStackInventoryImpl<>(StorageChannel.ITEMS, REQ_COUNT, tile); - this.requestQtys = new AeStackInventoryImpl<>(StorageChannel.ITEMS, REQ_COUNT, tile); - this.jobs = new Future[REQ_COUNT]; - this.links = new ICraftingLink[REQ_COUNT]; - } - - public void setEnable(int idx, boolean enable) { - IAEItemStack ias = this.getRequestStacks().getStack(idx); - IAEItemStack ias1 = this.getRequestBatches().getStack(idx); - if (ias != null && ias1 != null) { - ItemStack is = ias1.getItemStack(); - NBTTagCompound data = new NBTTagCompound(); - data.setBoolean("Enable", enable); - is.setTagCompound(data); - IAEItemStack i = AEItemStack.create(is); - i.setStackSize(ias1.getStackSize()); - this.getRequestBatches().setStack(idx, i); - } + requestStacks = new AeStackInventoryImpl<>(StorageChannel.ITEMS, REQ_COUNT, tile); + jobs = new Future[REQ_COUNT]; + links = new ICraftingLink[REQ_COUNT]; } public State getState(int idx) { - IAEItemStack ias = this.getRequestStacks().getStack(idx); + IAEItemStack ias = requestStacks.getStack(idx); if (ias == null) { - return State.Nothing; + return State.None; } else { - return this.state[idx] == null ? State.Idling : this.state[idx]; + return state[idx] == null ? State.Idle : state[idx]; } } - public void setState(ICraftingLink link, State state) { - int i = 0; - for (ICraftingLink link1 : this.getLinks()) { - if (link == link1) { - this.setState(i, state); - break; + public int getIdxByLink(ICraftingLink link) { + for (int i = 0; i < REQ_COUNT; i++) { + if (links[i] == link) { + return i; } - i++; - } - } - - public void setState(int idx, State state) { - this.state[idx] = state; - } - - public boolean isEnable(int idx) { - IAEItemStack ias = this.getRequestBatches().getStack(idx); - if (ias != null) { - ItemStack is = ias.getItemStack(); - try { - return is.getTagCompound().getBoolean("Enable"); - } catch (NullPointerException e) { - // support old version - return true; - } - } else { - return true; - } - } - - public boolean isDone(int index) { - if (this.getLinks()[index] != null) { - return this.getLinks()[index].isDone() || this.getLinks()[index].isCanceled(); - } - return true; - } - - public Future[] getJobs() { - return this.jobs; - } - - public ICraftingLink[] getLinks() { - return links; - } - - public AeStackInventory getRequestQtys() { - return this.requestQtys; - } - - public AeStackInventory getRequestBatches() { - return this.requestBatches; - } - - public AeStackInventory getRequestStacks() { - return this.requestStacks; - } - - public void updateQuantity(int idx, long size) { - IAEItemStack is = this.getRequestStacks().getStack(idx); - if (is != null) { - IAEItemStack i = is.copy(); - i.setStackSize(size); - this.getRequestQtys().setStack(idx, i); - } - } - - public void updateBatchSize(int idx, long size) { - IAEItemStack ias = this.getRequestStacks().getStack(idx); - if (ias != null) { - ItemStack is = ias.copy().getItemStack(); - NBTTagCompound data = new NBTTagCompound(); - data.setBoolean("Enable", true); - is.setTagCompound(data); - IAEItemStack i = AEItemStack.create(is); - i.setStackSize(size); - this.getRequestBatches().setStack(idx, i); - } - } - - public IAEItemStack getRequestQtyStack(int idx) { - IAEItemStack is = this.getRequestStacks().getStack(idx); - if (is == null) return null; - IAEItemStack qis = this.getRequestQtys().getStack(idx); - if (qis == null || !is.isSameType(qis)) return null; - return this.getRequestQtys().getStack(idx); - } - - public long getQuantity(int idx) { - IAEItemStack ias = this.getRequestQtyStack(idx); - if (ias == null) { - return 0; - } - return ias.getStackSize(); - } - - public long getBatchSize(int idx) { - if (!this.isEnable(idx)) return 0; - IAEItemStack is = this.getRequestStacks().getStack(idx); - IAEItemStack is1 = this.getRequestBatches().getStack(idx); - if (is != null && is1 != null) { - return is1.getStackSize(); } return 0; } - public IAEItemStack getCraftItem(int idx) { - IAEItemStack is = this.getRequestQtyStack(idx); - if (is != null) { - IAEItemStack ias = is.copy(); - ias.setStackSize(this.getBatchSize(idx)); - return ias; + public boolean isEnable(int idx) { + IAEItemStack ias = requestStacks.getStack(idx); + if (ias == null) { + return true; } - return null; + + ItemStack is = ias.getItemStack(); + return is.hasTagCompound() ? is.getTagCompound().getBoolean(TLMTags.Enable.tagName) : true; + } + + public boolean isDone(int index) { + if (links[index] == null) { + return true; + } + return links[index].isDone() || links[index].isCanceled(); + } + + public Future getJob(int idx) { + return jobs[idx]; + } + + public ICraftingLink getLink(int idx) { + return links[idx]; + } + + public void updateJob(int idx, Future job) { + jobs[idx] = job; + } + + private void updateField(int idx, Updater updater) { + IAEItemStack ias = requestStacks.getStack(idx); + if (ias == null) return; + + ItemStack itemStack = ias.getItemStack(); + NBTTagCompound data = itemStack.hasTagCompound() ? itemStack.getTagCompound() : new NBTTagCompound(); + data.setInteger(TLMTags.Index.tagName, idx); + itemStack.setTagCompound(updater.update(data)); + IAEItemStack newRequestStack = AEItemStack.create(itemStack); + newRequestStack.setStackSize(ias.getStackSize()); + requestStacks.setStack(idx, newRequestStack); + + } + + public void updateLink(int idx, ICraftingLink link) { + links[idx] = link; + + updateField(idx, data -> { + NBTTagCompound linkData = new NBTTagCompound(); + if (link != null) link.writeToNBT(linkData); + data.setTag(TLMTags.Link.tagName, linkData); + + return data; + }); + } + + public void updateState(int idx, State nextState) { + state[idx] = nextState; + + updateField(idx, data -> { + data.setInteger(TLMTags.State.tagName, nextState.ordinal()); + + return data; + }); + } + + public void updateStatus(int idx, boolean enable) { + updateField(idx, data -> { + State nextState = enable ? State.Idle : State.None; + + data.setBoolean(TLMTags.Enable.tagName, enable); + data.setInteger(TLMTags.State.tagName, nextState.ordinal()); + return data; + }); + } + + public void updateQuantity(int idx, long quantity) { + updateField(idx, data -> { + data.setLong(TLMTags.Quantity.tagName, quantity); + + return data; + }); + } + + public void updateBatchSize(int idx, long batch) { + updateField(idx, data -> { + data.setLong(TLMTags.Batch.tagName, batch); + data.setBoolean(TLMTags.Enable.tagName, true); + + return data; + }); + } + + public void updateStack(int idx, ItemStack itemStack) { + updateField(idx, data -> { + NBTTagCompound stackData = new NBTTagCompound(); + if (itemStack != null) itemStack.writeToNBT(stackData); + data.setTag(TLMTags.Stack.tagName, stackData); + + return data; + }); + } + + public IAEItemStack getAEItemStack(int idx) { + return requestStacks.getStack(idx); + } + + public ItemStack getItemStack(int idx) { + return requestStacks.getStack(idx).getItemStack(); + } + + public long getQuantity(int idx) { + if (!isEnable(idx)) return 0; + IAEItemStack ias = requestStacks.getStack(idx); + if (ias == null) return 0; + ItemStack itemStack = ias.getItemStack(); + if (!itemStack.hasTagCompound()) return 0; + + return itemStack.getTagCompound().getLong(TLMTags.Quantity.tagName); + } + + public long getBatchSize(int idx) { + if (!isEnable(idx)) return 0; + IAEItemStack ias = requestStacks.getStack(idx); + if (ias == null) return 0; + ItemStack itemStack = ias.getItemStack(); + if (!itemStack.hasTagCompound()) return 0; + + return itemStack.getTagCompound().getLong(TLMTags.Batch.tagName); + } + + 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)); + IAEItemStack qais = AEItemStack.create(qis); + qais.setStackSize(getBatchSize(idx)); + + return qais; } } } diff --git a/src/main/java/com/glodblock/github/crossmod/waila/TileWailaDataProvider.java b/src/main/java/com/glodblock/github/crossmod/waila/TileWailaDataProvider.java index 47fdabc7..4790ab0d 100644 --- a/src/main/java/com/glodblock/github/crossmod/waila/TileWailaDataProvider.java +++ b/src/main/java/com/glodblock/github/crossmod/waila/TileWailaDataProvider.java @@ -9,7 +9,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import com.glodblock.github.crossmod.waila.tile.FluidInvWailaDataProvider; -import com.glodblock.github.crossmod.waila.tile.LevelMaintainerWailaDataProvide; +import com.glodblock.github.crossmod.waila.tile.LevelMaintainerWailaDataProvider; import com.google.common.collect.Lists; import mcp.mobius.waila.api.IWailaConfigHandler; @@ -22,7 +22,7 @@ public class TileWailaDataProvider implements IWailaDataProvider { public TileWailaDataProvider() { final IWailaDataProvider fluidInv = new FluidInvWailaDataProvider(); - final IWailaDataProvider levelMaintainer = new LevelMaintainerWailaDataProvide(); + final IWailaDataProvider levelMaintainer = new LevelMaintainerWailaDataProvider(); this.providers = Lists.newArrayList(fluidInv, levelMaintainer); } diff --git a/src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvide.java b/src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvider.java similarity index 76% rename from src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvide.java rename to src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvider.java index 76f05581..7cc01b0d 100644 --- a/src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvide.java +++ b/src/main/java/com/glodblock/github/crossmod/waila/tile/LevelMaintainerWailaDataProvider.java @@ -16,23 +16,23 @@ import appeng.integration.modules.waila.BaseWailaDataProvider; import mcp.mobius.waila.api.IWailaConfigHandler; import mcp.mobius.waila.api.IWailaDataAccessor; -public class LevelMaintainerWailaDataProvide extends BaseWailaDataProvider { +public class LevelMaintainerWailaDataProvider extends BaseWailaDataProvider { @Override public List getWailaBody(final ItemStack itemStack, final List currentToolTip, final IWailaDataAccessor accessor, final IWailaConfigHandler config) { final TileEntity te = accessor.getTileEntity(); - if (te instanceof TileLevelMaintainer) { + if (te instanceof TileLevelMaintainer tileLevelMaintainer) { te.readFromNBT(accessor.getNBTData()); for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) { - IAEItemStack ias = ((TileLevelMaintainer) te).requests.getRequestStacks().getStack(i); + IAEItemStack ias = tileLevelMaintainer.requests.getAEItemStack(i); if (ias != null) { currentToolTip.add( Tooltip.tileLevelMaintainerFormat( ias.getItemStack().getDisplayName(), - ((TileLevelMaintainer) te).requests.getQuantity(i), - ((TileLevelMaintainer) te).requests.getBatchSize(i), - ((TileLevelMaintainer) te).requests.isEnable(i))); + tileLevelMaintainer.requests.getQuantity(i), + tileLevelMaintainer.requests.getBatchSize(i), + tileLevelMaintainer.requests.isEnable(i))); } } } diff --git a/src/main/java/com/glodblock/github/inventory/item/IWirelessInterfaceTerminal.java b/src/main/java/com/glodblock/github/inventory/item/IClickableInTerminal.java similarity index 74% rename from src/main/java/com/glodblock/github/inventory/item/IWirelessInterfaceTerminal.java rename to src/main/java/com/glodblock/github/inventory/item/IClickableInTerminal.java index 19118dd0..280486d5 100644 --- a/src/main/java/com/glodblock/github/inventory/item/IWirelessInterfaceTerminal.java +++ b/src/main/java/com/glodblock/github/inventory/item/IClickableInTerminal.java @@ -2,7 +2,7 @@ package com.glodblock.github.inventory.item; import com.glodblock.github.util.Util; -public interface IWirelessInterfaceTerminal extends IWirelessTerminal { +public interface IClickableInTerminal { void setClickedInterface(Util.DimensionalCoordSide tile); diff --git a/src/main/java/com/glodblock/github/network/CPacketLevelMaintainer.java b/src/main/java/com/glodblock/github/network/CPacketLevelMaintainer.java index 2957afd5..d79134f9 100644 --- a/src/main/java/com/glodblock/github/network/CPacketLevelMaintainer.java +++ b/src/main/java/com/glodblock/github/network/CPacketLevelMaintainer.java @@ -6,14 +6,11 @@ import java.util.List; import javax.annotation.Nullable; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import com.glodblock.github.client.gui.container.ContainerLevelMaintainer; import com.glodblock.github.common.tile.TileLevelMaintainer; import appeng.api.storage.data.IAEItemStack; -import appeng.util.item.AEItemStack; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; @@ -21,111 +18,101 @@ import io.netty.buffer.ByteBuf; public class CPacketLevelMaintainer implements IMessage { - private String action; + public enum Action { + Refresh, + Quantity, + Batch, + Enable, + Disable + } + + private Action action; private long size; private int slotIndex; - public CPacketLevelMaintainer() {} - - public CPacketLevelMaintainer(String action, int slotIndex) { - this.action = action; - this.size = 0; - this.slotIndex = slotIndex; - } - - public CPacketLevelMaintainer(String action) { - this.action = action; - this.size = 0; + public CPacketLevelMaintainer() { + this.action = Action.Refresh; this.slotIndex = 0; + this.size = 0; } - public CPacketLevelMaintainer(String action, int slotIndex, long size) { + public CPacketLevelMaintainer(Action action) { this.action = action; - this.size = size; - this.slotIndex = slotIndex; + this.slotIndex = 0; + this.size = 0; } - public CPacketLevelMaintainer(String action, int slotIndex, String size) { + public CPacketLevelMaintainer(Action action, int slotIndex) { + this.action = action; + this.slotIndex = slotIndex; + this.size = 0; + } + + public CPacketLevelMaintainer(Action action, int slotIndex, long size) { + this.action = action; + this.slotIndex = slotIndex; + this.size = size; + } + + public CPacketLevelMaintainer(Action action, int slotIndex, String size) { this.action = action; this.slotIndex = slotIndex; this.size = size.isEmpty() ? 0 : Long.parseLong(size); } - public static IAEItemStack setTag(IAEItemStack ias, long batch, int slotIndex, boolean enable, int state) { - NBTTagCompound data = new NBTTagCompound(); - ItemStack is = ias.getItemStack(); - data.setLong("Batch", batch); - data.setLong("Index", slotIndex); - data.setBoolean("Enable", enable); - data.setInteger("State", state); - is.setTagCompound(data); - IAEItemStack iaeItemStack = AEItemStack.create(is); - iaeItemStack.setStackSize(ias.getStackSize()); - return iaeItemStack; - } - @Override public void fromBytes(ByteBuf buf) { - int leAction = buf.readInt(); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < leAction; i++) { - sb.append(buf.readChar()); - } - this.action = sb.toString(); + this.action = Action.values()[buf.readInt()]; this.slotIndex = buf.readInt(); this.size = buf.readLong(); } @Override public void toBytes(ByteBuf buf) { - buf.writeInt(this.action.length()); - for (int i = 0; i < this.action.length(); i++) { - buf.writeChar(this.action.charAt(i)); - } + buf.writeInt(this.action.ordinal()); buf.writeInt(this.slotIndex); buf.writeLong(this.size); } public static class Handler implements IMessageHandler { - private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) { + private List refresh(ContainerLevelMaintainer cca) { List toSend = new ArrayList<>(TileLevelMaintainer.REQ_COUNT); for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) { - IAEItemStack is = cca.getTile().requests.getRequestQtyStack(i); - IAEItemStack is1 = cca.getTile().requests.getRequestBatches().getStack(i); - if (is != null) { - if (is1 != null) { - NBTTagCompound data; - data = is1.getItemStack().getTagCompound(); - toSend.add( - setTag( - is, - is1.getStackSize(), - i, - data.getBoolean("Enable"), - cca.getTile().requests.getState(i).ordinal())); - } else { - toSend.add(setTag(is, 0, i, true, 0)); - } + IAEItemStack ias = cca.getTile().requests.getAEItemStack(i); + if (ias != null) { + toSend.add(ias); } } - SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend); + return toSend; } @Nullable @Override public IMessage onMessage(CPacketLevelMaintainer message, MessageContext ctx) { - if (message.action.startsWith("TileLevelMaintainer.") - && ctx.getServerHandler().playerEntity.openContainer instanceof final ContainerLevelMaintainer cca) { + if (ctx.getServerHandler().playerEntity.openContainer instanceof final ContainerLevelMaintainer clm) { EntityPlayerMP player = ctx.getServerHandler().playerEntity; - switch (message.action) { - case "TileLevelMaintainer.Quantity" -> cca.getTile() - .updateQuantity(message.slotIndex, message.size); - case "TileLevelMaintainer.Batch" -> cca.getTile().updateBatchSize(message.slotIndex, message.size); - case "TileLevelMaintainer.Enable" -> cca.getTile().setRequestStatus(message.slotIndex, false); - case "TileLevelMaintainer.Disable" -> cca.getTile().setRequestStatus(message.slotIndex, true); - } - this.refresh(cca, player); + List toSend = switch (message.action) { + case Quantity -> { + clm.getTile().updateQuantity(message.slotIndex, message.size); + yield this.refresh(clm); + } + case Batch -> { + clm.getTile().updateBatchSize(message.slotIndex, message.size); + yield this.refresh(clm); + } + case Enable -> { + clm.getTile().updateStatus(message.slotIndex, false); + yield this.refresh(clm); + } + case Disable -> { + clm.getTile().updateStatus(message.slotIndex, true); + yield this.refresh(clm); + } + case Refresh -> this.refresh(clm); + }; + + SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend); } return null; } diff --git a/src/main/java/com/glodblock/github/network/CPacketRenamer.java b/src/main/java/com/glodblock/github/network/CPacketRenamer.java index 46450a49..601b0383 100644 --- a/src/main/java/com/glodblock/github/network/CPacketRenamer.java +++ b/src/main/java/com/glodblock/github/network/CPacketRenamer.java @@ -1,19 +1,22 @@ package com.glodblock.github.network; -import static com.glodblock.github.common.item.ItemWirelessUltraTerminal.getGuis; - +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.util.ForgeDirection; import com.glodblock.github.FluidCraft; +import com.glodblock.github.client.gui.GuiRenamer; import com.glodblock.github.inventory.InventoryHandler; import com.glodblock.github.inventory.gui.GuiType; -import com.glodblock.github.inventory.item.IWirelessInterfaceTerminal; +import com.glodblock.github.inventory.item.IClickableInTerminal; +import com.glodblock.github.inventory.item.IWirelessTerminal; import com.glodblock.github.util.BlockPos; import com.glodblock.github.util.Util; +import appeng.api.implementations.IUpgradeableHost; import appeng.container.AEBaseContainer; import appeng.helpers.ICustomNameObject; import appeng.tile.networking.TileCableBus; @@ -121,58 +124,70 @@ public class CPacketRenamer implements IMessage { public IMessage onMessage(CPacketRenamer message, MessageContext ctx) { EntityPlayerMP player = ctx.getServerHandler().playerEntity; AEBaseContainer con = (AEBaseContainer) player.openContainer; - TileEntity tile; - if (message.action == Action.OPEN) { - tile = DimensionManager.getWorld(message.dim).getTileEntity(message.x, message.y, message.z); - if (tile != null && con.getTarget() instanceof IWirelessInterfaceTerminal - && tile instanceof ICustomNameObject) { - String name = getName(tile, message.side); - ((IWirelessInterfaceTerminal) con.getTarget()).setClickedInterface( - new Util.DimensionalCoordSide( - message.x, - message.y, - message.z, - message.dim, + switch (message.action) { + case OPEN -> { + if (con.getTarget() instanceof IClickableInTerminal clickableInterface) { + TileEntity tile = DimensionManager.getWorld(message.dim) + .getTileEntity(message.x, message.y, message.z); + if (!(tile instanceof ICustomNameObject)) { + break; + } + + String name = getName(tile, message.side); + clickableInterface.setClickedInterface( + new Util.DimensionalCoordSide( + message.x, + message.y, + message.z, + message.dim, + message.side, + name)); + + if (con.getTarget() instanceof IWirelessTerminal terminal) { + InventoryHandler.openGui( + player, + player.worldObj, + new BlockPos( + terminal.getInventorySlot(), + Util.GuiHelper.encodeType(0, Util.GuiHelper.GuiType.ITEM), + 1), message.side, - name)); - InventoryHandler.openGui( - player, - player.worldObj, - new BlockPos( - ((IWirelessInterfaceTerminal) con.getTarget()).getInventorySlot(), - Util.GuiHelper.encodeType(0, Util.GuiHelper.GuiType.ITEM), - 1), - message.side, - GuiType.RENAMER); + GuiType.RENAMER); + } else if (con.getTarget() instanceof IUpgradeableHost host) { + InventoryHandler.openGui( + player, + player.worldObj, + new BlockPos(host.getTile()), + con.getOpenContext().getSide(), + GuiType.RENAMER); + } + } } - } else { - if (con.getTarget() instanceof IWirelessInterfaceTerminal) { - Util.DimensionalCoordSide intMsg = ((IWirelessInterfaceTerminal) con.getTarget()) - .getClickedInterface(); - tile = DimensionManager.getWorld(intMsg.getDimension()).getTileEntity(intMsg.x, intMsg.y, intMsg.z); - if (message.action == Action.GET_TEXT) { + case GET_TEXT -> { + if (con.getTarget() instanceof IClickableInTerminal clickableInterface) { + Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface(); + TileEntity tile = DimensionManager.getWorld(intMsg.getDimension()) + .getTileEntity(intMsg.x, intMsg.y, intMsg.z); FluidCraft.proxy.netHandler .sendTo(new SPacketStringUpdate(this.getName(tile, intMsg.getSide())), player); - } else { + } + } + case SET_TEXT -> { + if (con.getTarget() instanceof IClickableInTerminal clickableInterface) { + Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface(); + TileEntity tile = DimensionManager.getWorld(intMsg.getDimension()) + .getTileEntity(intMsg.x, intMsg.y, intMsg.z); this.setName(tile, intMsg.getSide(), message.text); - InventoryHandler.openGui( - player, - player.worldObj, - new BlockPos( - ((IWirelessInterfaceTerminal) con.getTarget()).getInventorySlot(), - Util.GuiHelper.encodeType( - getGuis().indexOf( - GuiType.valueOf( - GuiType.WIRELESS_INTERFACE_TERMINAL.toString())), - Util.GuiHelper.GuiType.ITEM), - 0), - ForgeDirection.UNKNOWN, - GuiType.WIRELESS_INTERFACE_TERMINAL); + + final GuiScreen gs = Minecraft.getMinecraft().currentScreen; + if (gs instanceof GuiRenamer guiRenamer) { + guiRenamer.switchGui(); + } } } } - return null; } + } } diff --git a/src/main/java/com/glodblock/github/util/Updater.java b/src/main/java/com/glodblock/github/util/Updater.java new file mode 100644 index 00000000..fff27bc9 --- /dev/null +++ b/src/main/java/com/glodblock/github/util/Updater.java @@ -0,0 +1,7 @@ +package com.glodblock.github.util; + +@FunctionalInterface +public interface Updater { + + Output update(Input data); +}