Skip to content

Commit

Permalink
Fix packet corruption with level emitters
Browse files Browse the repository at this point in the history
  • Loading branch information
Laiff committed Oct 18, 2023
1 parent 4570d46 commit 3b8e474
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.glodblock.github.common.item.ItemBaseWirelessTerminal;
import com.glodblock.github.common.parts.PartFluidPatternTerminal;
import com.glodblock.github.common.parts.PartFluidPatternTerminalEx;
import com.glodblock.github.common.parts.PartLevelTerminal;
import com.glodblock.github.inventory.InventoryHandler;
import com.glodblock.github.inventory.gui.GuiType;
import com.glodblock.github.inventory.item.IWirelessTerminal;
Expand All @@ -35,6 +36,9 @@ public void switchToOriginalGUI() {
if (ah instanceof PartFluidPatternTerminalEx) {
originalGui = GuiType.FLUID_PATTERN_TERMINAL_EX;
}
if (ah instanceof PartLevelTerminal) {
originalGui = GuiType.LEVEL_TERMINAL;
}
if (ah instanceof IWirelessTerminal) {
ItemStack terminal = ((IWirelessTerminal) ah).getItemStack();
if (terminal.getItem() instanceof ItemBaseWirelessTerminal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ContainerLevelTerminal extends FCBaseContainer {
*/
private int nextId = 0;

private final Map<ILevelViewable, ContainerLevelTerminal.InvTracker> tracked = new HashMap<>();
private final Map<IGridHost, ContainerLevelTerminal.InvTracker> tracked = new HashMap<>();
private final Map<Long, ContainerLevelTerminal.InvTracker> trackedById = new HashMap<>();

private IGrid grid;
Expand Down Expand Up @@ -246,21 +246,22 @@ private void syncLevelTerminalSlot(ContainerLevelTerminal.InvTracker inv, long i
private SPacketLevelTerminalUpdate updateList() {
SPacketLevelTerminalUpdate update = null;
var supported = LevelTerminalRegistry.instance().getSupportedClasses();
Set<ILevelViewable> visited = new HashSet<>();
Set<IGridHost> visited = new HashSet<>();

for (Class<? extends ILevelViewable> clz : supported) {
boolean isAdopted = LevelTerminalRegistry.instance().isAdopted(clz);
Class<? extends IGridHost> machineClass = isAdopted ? LevelTerminalRegistry.instance().getAdopted(clz)
: clz;
for (IGridNode gridNode : grid.getMachines(machineClass)) {
final IGridHost gridHost = gridNode.getMachine();
final ILevelViewable machine = isAdopted
? LevelTerminalRegistry.instance().getAdapter(clz).adapt(gridNode.getMachine())
: (ILevelViewable) gridNode.getMachine();
? LevelTerminalRegistry.instance().getAdapter(clz).adapt(gridHost)
: (ILevelViewable) gridHost;

/* First check if we are already tracking this node */
if (tracked.containsKey(machine)) {
if (tracked.containsKey(gridHost)) {
/* Check for updates */
ContainerLevelTerminal.InvTracker knownTracker = tracked.get(machine);
ContainerLevelTerminal.InvTracker knownTracker = tracked.get(gridHost);

/* Name changed? */
String name = machine.getCustomName();
Expand Down Expand Up @@ -310,15 +311,15 @@ private SPacketLevelTerminalUpdate updateList() {
.setLocation(entry.x, entry.y, entry.z, entry.dim, entry.side.ordinal())
.setItems(entry.rows, entry.rowSize, entry.inventoryNbt)
.setViewItemStack(machine.getSelfItemStack(), machine.getDisplayItemStack());
tracked.put(machine, entry);
tracked.put(gridHost, entry);
trackedById.put(entry.id, entry);
}
visited.add(machine);
visited.add(gridHost);
}
}

/* Now find any entries that we need to remove */
Iterator<Map.Entry<ILevelViewable, ContainerLevelTerminal.InvTracker>> it = tracked.entrySet().iterator();
Iterator<Map.Entry<IGridHost, ContainerLevelTerminal.InvTracker>> it = tracked.entrySet().iterator();
while (it.hasNext()) {
var entry = it.next();
if (visited.contains(entry.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import com.glodblock.github.api.registries.ILevelViewableAdapter;
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.AeStackInventoryImpl;

import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.parts.automation.PartLevelEmitter;
import appeng.util.item.AEItemStack;

public class PartLevelEmitterAdapter implements ILevelViewable, ILevelViewableAdapter {

Expand All @@ -30,20 +35,21 @@ public PartLevelEmitterAdapter() {}
public ILevelViewable adapt(IGridHost gridHost) {
if (gridHost instanceof PartLevelEmitter levelEmitter) this.delegate = levelEmitter;
return this;
};
}

@NotNull
static public IInventory getPatchedInventory(IInventory inventory, long quantity, State state) {
ItemStack itemStack = inventory.getStackInSlot(SLOT_IN).copy();
ItemStack itemStack = inventory.getStackInSlot(SLOT_IN);
if (itemStack == null) return inventory;
NBTTagCompound data = !itemStack.hasTagCompound() ? new NBTTagCompound() : itemStack.getTagCompound();
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());
data.setLong(TLMTags.Quantity.tagName, quantity);
data.setInteger(TLMTags.State.tagName, state.ordinal());
itemStack.setTagCompound(data);
inventory.setInventorySlotContents(SLOT_IN, itemStack);

return inventory;
AeStackInventoryImpl<IAEItemStack> inv = new AeStackInventoryImpl<>(StorageChannel.ITEMS, 1);
inv.setStack(SLOT_IN, AEItemStack.create(itemStack));

return new AeItemStackHandler(inv);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/ae2fc/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ae2fc.tooltip.level_maintainer.link_desc=The system is currently crafting the re
ae2fc.tooltip.level_maintainer.export=§5Exporting§r
ae2fc.tooltip.level_maintainer.export_desc=The Requester is trying to export the crafting results to the system. If the Requester has this status for a long time, there is no available space for the export.
ae2fc.tooltip.level_maintainer.error=§4Errored§r
ae2fc.tooltip.level_maintainer.error_desc=The Requester is trying to schedule crafting job but it fails. Possible reasons no receipt found or insufficient resources to craft.
ae2fc.tooltip.level_maintainer.error_desc=The Requester is trying to schedule a crafting job but is failing. Possible reasons include a missing recipe or insufficient resources.
ae2fc.tooltip.level_maintainer.batch_size=§6Crafting Batch Size§r
ae2fc.tooltip.level_maintainer.batch_size.hint=When craft requests are emitted, the Requester will use the batch size for the request. Using larger batches will increase the speed of stocking up items but will require more crafting storage.
ae2fc.tooltip.level_maintainer.request_size=§6Amount to Maintain§r
Expand Down

0 comments on commit 3b8e474

Please sign in to comment.