Skip to content

Commit 13882ab

Browse files
authored
Support AE2's Network Tool (#15)
* refactor: ContainerWirelessCellTerminal and ContainerWUTCellTerminal merge * Show base AE2's network tool's upgrade card inventory in the cell terminal
1 parent 19b5d5f commit 13882ab

9 files changed

Lines changed: 292 additions & 302 deletions

src/main/java/com/cellterminal/container/ContainerCellTerminalBase.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
import java.util.List;
88
import java.util.Map;
99

10+
11+
import appeng.container.slot.SlotRestrictedInput;
12+
import appeng.helpers.WirelessTerminalGuiObject;
13+
import appeng.items.contents.NetworkToolViewer;
14+
import appeng.items.tools.ToolNetworkTool;
1015
import net.minecraft.entity.player.EntityPlayer;
16+
import net.minecraft.inventory.IInventory;
1117
import net.minecraft.util.math.BlockPos;
1218
import net.minecraft.util.text.TextComponentTranslation;
1319
import net.minecraft.entity.player.EntityPlayerMP;
@@ -90,8 +96,76 @@ public abstract class ContainerCellTerminalBase extends AEBaseContainer {
9096
protected long currentNetworkId = 0;
9197
protected IGrid currentNetworkGrid = null; // Cached grid for current network (main or subnet)
9298

99+
// AE2's code mostly refers to the network tool as the "toolbox" so we use that name
100+
// to distinguish it from our network tools
101+
private int toolboxSlot;
102+
private NetworkToolViewer toolboxInventory;
103+
93104
public ContainerCellTerminalBase(InventoryPlayer ip, IPart part) {
94105
super(ip, null, part);
106+
107+
this.setupToolbox();
108+
}
109+
110+
public ContainerCellTerminalBase(
111+
InventoryPlayer ip,
112+
WirelessTerminalGuiObject guiObject
113+
) {
114+
115+
super(ip, guiObject);
116+
117+
if (Platform.isServer()) {
118+
IGridNode node = guiObject.getActionableNode();
119+
if (node != null && node.isActive()) {
120+
this.grid = node.getGrid();
121+
}
122+
}
123+
124+
this.setupToolbox();
125+
}
126+
127+
public void setupToolbox() {
128+
129+
final IInventory pi = this.getPlayerInv();
130+
ItemStack toolbox = null;
131+
for (int x = 0; x < pi.getSizeInventory(); x++) {
132+
final ItemStack pii = pi.getStackInSlot(x);
133+
if (!pii.isEmpty() && pii.getItem() instanceof ToolNetworkTool) {
134+
this.lockPlayerInventorySlot(x);
135+
this.toolboxSlot = x;
136+
toolbox = pii;
137+
break;
138+
}
139+
}
140+
141+
if (toolbox != null) {
142+
IGridNode node = this.getActionHost().getActionableNode();
143+
this.toolboxInventory = new NetworkToolViewer(
144+
toolbox,
145+
node != null ? node.getMachine() : null
146+
);
147+
148+
149+
for (int v = 0; v < 3; v++) {
150+
for (int u = 0; u < 3; u++) {
151+
this.addSlotToContainer(
152+
new SlotRestrictedInput(
153+
SlotRestrictedInput.PlacableItemType.UPGRADES,
154+
this.toolboxInventory.getInternalInventory(),
155+
u + v * 3,
156+
8 + 9 * 18 + 14 + 18 + 1 + u * 18,
157+
v * 18,
158+
this.getInventoryPlayer()
159+
).setPlayerSide()
160+
);
161+
}
162+
}
163+
}
164+
}
165+
166+
public boolean hasToolbox() {
167+
168+
return this.toolboxInventory != null;
95169
}
96170

97171
@Override
@@ -124,6 +198,32 @@ public void detectAndSendChanges() {
124198

125199
this.pendingData = new NBTTagCompound();
126200
}
201+
202+
this.checkToolbox();
203+
}
204+
205+
public void checkToolbox() {
206+
207+
if (hasToolbox()) {
208+
final ItemStack currentItem = this.getPlayerInv().getStackInSlot(this.toolboxSlot);
209+
210+
if (currentItem != this.toolboxInventory.getItemStack()) {
211+
if (!currentItem.isEmpty()) {
212+
if (ItemStack.areItemsEqual(this.toolboxInventory.getItemStack(), currentItem)) {
213+
this
214+
.getPlayerInv()
215+
.setInventorySlotContents(
216+
this.toolboxSlot,
217+
this.toolboxInventory.getItemStack()
218+
);
219+
} else {
220+
this.setValidContainer(false);
221+
}
222+
} else {
223+
this.setValidContainer(false);
224+
}
225+
}
226+
}
127227
}
128228

129229
/**

src/main/java/com/cellterminal/container/ContainerWUTCellTerminal.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/main/java/com/cellterminal/container/ContainerWirelessCellTerminal.java

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,46 @@
11
package com.cellterminal.container;
22

3+
import appeng.api.config.Actionable;
4+
import appeng.api.config.PowerMultiplier;
5+
import com.cellterminal.integration.AE2WUTIntegration;
6+
import com.cellterminal.items.ItemWirelessCellTerminal;
7+
import com.cellterminal.util.AE2OldVersionSupport;
38
import net.minecraft.entity.player.EntityPlayer;
49
import net.minecraft.entity.player.InventoryPlayer;
510
import net.minecraft.item.ItemStack;
611
import net.minecraftforge.fml.common.Optional;
712

813
import appeng.api.AEApi;
9-
import appeng.api.config.Actionable;
10-
import appeng.api.features.ILocatable;
1114
import appeng.api.features.IWirelessTermHandler;
12-
import appeng.api.networking.IGridNode;
13-
import appeng.api.networking.security.IActionHost;
1415
import appeng.core.AEConfig;
1516
import appeng.core.localization.PlayerMessages;
16-
import appeng.util.Platform;
17+
import appeng.helpers.WirelessTerminalGuiObject;
1718

1819
import baubles.api.BaublesApi;
1920

20-
import com.cellterminal.items.ItemWirelessCellTerminal;
21-
2221

2322
/**
2423
* Container for the Wireless Cell Terminal GUI.
2524
* Similar to ContainerCellTerminal but works with wireless connection.
2625
*/
26+
@Optional.Interface(iface = "appeng.helpers.WirelessTerminalGuiObject", modid = "ae2wut")
2727
public class ContainerWirelessCellTerminal extends ContainerCellTerminalBase {
2828

29+
private final WirelessTerminalGuiObject wirelessTerminalGuiObject;
2930
private final int slot;
3031
private final boolean isBauble;
31-
private final ItemStack terminalStack;
3232

3333
private int ticks = 0;
3434

35-
public ContainerWirelessCellTerminal(InventoryPlayer ip, int slot, boolean isBauble) {
36-
super(ip, null);
37-
this.slot = slot;
38-
this.isBauble = isBauble;
39-
40-
if (isBauble) {
41-
this.terminalStack = getBaubleStack(ip.player, slot);
42-
} else {
43-
this.terminalStack = ip.getStackInSlot(slot);
44-
this.lockPlayerInventorySlot(slot);
45-
}
46-
47-
if (Platform.isServer() && !terminalStack.isEmpty()) {
48-
IWirelessTermHandler handler = AEApi.instance().registries().wireless().getWirelessTerminalHandler(terminalStack);
49-
50-
if (handler != null) {
51-
String encKey = handler.getEncryptionKey(terminalStack);
35+
public ContainerWirelessCellTerminal(InventoryPlayer ip, WirelessTerminalGuiObject wth) {
36+
super(ip, wth);
5237

53-
try {
54-
long parsedKey = Long.parseLong(encKey);
55-
ILocatable obj = AEApi.instance().registries().locatable().getLocatableBy(parsedKey);
38+
this.wirelessTerminalGuiObject = wth;
39+
this.slot = wth.getInventorySlot();
40+
this.isBauble = AE2OldVersionSupport.wirelessTerminalGuiObjectIsBauble(wth, ip);
5641

57-
if (obj instanceof IActionHost) {
58-
IGridNode n = ((IActionHost) obj).getActionableNode();
59-
if (n != null) {
60-
this.grid = n.getGrid();
61-
}
62-
}
63-
} catch (NumberFormatException e) {
64-
// Invalid key
65-
}
66-
}
67-
}
42+
// Lock the slot if it's in the main inventory
43+
if (!isBauble && slot >= 0 && slot < ip.mainInventory.size()) this.lockPlayerInventorySlot(slot);
6844

6945
this.bindPlayerInventory(ip, 0, 0);
7046
}
@@ -73,14 +49,27 @@ public ContainerWirelessCellTerminal(InventoryPlayer ip, int slot, boolean isBau
7349
private ItemStack getBaubleStack(EntityPlayer player, int baubleSlot) {
7450
return BaublesApi.getBaublesHandler(player).getStackInSlot(baubleSlot);
7551
}
52+
/**
53+
* Get the WirelessTerminalGuiObject for this container.
54+
* Used by the GUI to access WUT functionality.
55+
*/
56+
public WirelessTerminalGuiObject getWirelessTerminalGuiObject() {
57+
return this.wirelessTerminalGuiObject;
58+
}
59+
60+
private ItemStack getTerminalStack() {
61+
if (isBauble) return getBaubleStack(getPlayerInv().player, slot);
62+
63+
return getPlayerInv().getStackInSlot(slot);
64+
}
65+
66+
7667

7768
@Override
7869
protected boolean canSendUpdates() {
79-
// Check terminal is still valid
80-
ItemStack currentStack = isBauble ? getBaubleStack(getPlayerInv().player, slot)
81-
: getPlayerInv().getStackInSlot(slot);
70+
ItemStack currentStack = getTerminalStack();
8271

83-
if (currentStack.isEmpty() || !(currentStack.getItem() instanceof ItemWirelessCellTerminal)) {
72+
if (currentStack.isEmpty() || !(currentStack.getItem() instanceof ItemWirelessCellTerminal || AE2WUTIntegration.isWirelessUniversalTerminal(currentStack))) {
8473
this.setValidContainer(false);
8574

8675
return false;

0 commit comments

Comments
 (0)