mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Use per-stack IDs for batch deletes in network protocol (#141)
* Break out the ID from being stored in transporter stack (since they are shared) * Mark needsSync as private; update comments and cleanup delete loop * Fix broken packet, and remove the overlap between needsSync and deletes. Also minorly deduped some code * Turns out needSync for entityEntering is needed (as it is nextID not same ID)
This commit is contained in:
1 parent
cbd2aab888
commit
cf8a92c302
4 files changed
+154
-148
No files matched your search
@@ -1,11 +1,18 @@
|
||||
package mekanism.common.transmitters;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nonnull;
|
||||
import mekanism.api.Coord4D;
|
||||
import mekanism.api.EnumColor;
|
||||
import mekanism.api.Range4D;
|
||||
import mekanism.common.HashList;
|
||||
import mekanism.api.TileNetworkList;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.base.ILogisticalTransporter;
|
||||
import mekanism.common.capabilities.Capabilities;
|
||||
@@ -23,48 +30,100 @@ import mekanism.common.util.CapabilityUtils;
|
||||
import mekanism.common.util.InventoryUtils;
|
||||
import mekanism.common.util.MekanismUtils;
|
||||
import mekanism.common.util.TransporterUtils;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
|
||||
public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwork> implements ILogisticalTransporter {
|
||||
|
||||
public HashList<TransporterStack> transit = new HashList<>();
|
||||
private Map<Integer, TransporterStack> transit = new HashMap<>();
|
||||
|
||||
public EnumColor color;
|
||||
private int nextId = 0;
|
||||
|
||||
public Set<TransporterStack> needsSync = new HashSet<>();
|
||||
private EnumColor color;
|
||||
|
||||
private Map<Integer, TransporterStack> needsSync = new HashMap<>();
|
||||
|
||||
public TransporterImpl(TileEntityLogisticalTransporter multiPart) {
|
||||
super(multiPart);
|
||||
}
|
||||
|
||||
public Collection<TransporterStack> getTransit() {
|
||||
return Collections.unmodifiableCollection(transit.values());
|
||||
}
|
||||
|
||||
public void deleteStack(int id) {
|
||||
transit.remove(id);
|
||||
}
|
||||
|
||||
public void addStack(int id, TransporterStack s) {
|
||||
transit.put(id, s);
|
||||
}
|
||||
|
||||
public void writeToPacket(TileNetworkList data) {
|
||||
data.add(transit.size());
|
||||
for (Entry<Integer, TransporterStack> entry : transit.entrySet()) {
|
||||
data.add(entry.getKey());
|
||||
entry.getValue().write(this, data);
|
||||
}
|
||||
}
|
||||
|
||||
public void readFromPacket(ByteBuf dataStream) {
|
||||
transit.clear();
|
||||
|
||||
int count = dataStream.readInt();
|
||||
for (int i = 0; i < count; i++) {
|
||||
int id = dataStream.readInt();
|
||||
TransporterStack s = TransporterStack.readFromPacket(dataStream);
|
||||
transit.put(id, s);
|
||||
}
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound nbtTags) {
|
||||
if (nbtTags.hasKey("color")) {
|
||||
setColor(TransporterUtils.colors.get(nbtTags.getInteger("color")));
|
||||
}
|
||||
|
||||
if (nbtTags.hasKey("stacks")) {
|
||||
NBTTagList tagList = nbtTags.getTagList("stacks", NBT.TAG_COMPOUND);
|
||||
|
||||
for (int i = 0; i < tagList.tagCount(); i++) {
|
||||
TransporterStack stack = TransporterStack.readFromNBT(tagList.getCompoundTagAt(i));
|
||||
transit.put(nextId++, stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (world().isRemote) {
|
||||
for (TransporterStack stack : transit) {
|
||||
if (stack != null) {
|
||||
stack.progress = Math.min(100, stack.progress + getTileEntity().tier.speed);
|
||||
}
|
||||
for (TransporterStack stack : transit.values()) {
|
||||
stack.progress = Math.min(100, stack.progress + getTileEntity().tier.speed);
|
||||
}
|
||||
} else {
|
||||
if (getTransmitterNetwork() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<TransporterStack> deletes = new HashSet<>();
|
||||
Set<Integer> deletes = new HashSet<>();
|
||||
|
||||
getTileEntity().pullItems();
|
||||
|
||||
for (TransporterStack stack : transit) {
|
||||
for (Map.Entry<Integer, TransporterStack> entry : transit.entrySet()) {
|
||||
int stackId = entry.getKey();
|
||||
TransporterStack stack = entry.getValue();
|
||||
|
||||
if (!stack.initiatedPath) {
|
||||
if (stack.itemStack.isEmpty() || !recalculate(stack, null)) {
|
||||
deletes.add(stack);
|
||||
if (stack.itemStack.isEmpty() || !recalculate(stackId, stack, null)) {
|
||||
deletes.add(stackId);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
stack.progress += getTileEntity().tier.speed;
|
||||
|
||||
if (stack.progress > 100) {
|
||||
if (stack.progress >= 100) {
|
||||
Coord4D prevSet = null;
|
||||
|
||||
if (stack.hasPath()) {
|
||||
@@ -72,114 +131,82 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
|
||||
if (currentIndex == 0) //Necessary for transition reasons, not sure why
|
||||
{
|
||||
deletes.add(stack);
|
||||
deletes.add(stackId);
|
||||
continue;
|
||||
}
|
||||
|
||||
Coord4D next = stack.getPath().get(currentIndex - 1);
|
||||
|
||||
if (!stack.isFinal(this)) {
|
||||
if (next != null && stack.canInsertToTransporter(stack.getNext(this).getTileEntity(world()),
|
||||
if (next != null && stack.canInsertToTransporter(next.getTileEntity(world()),
|
||||
stack.getSide(this))) {
|
||||
ILogisticalTransporter nextTile = CapabilityUtils
|
||||
.getCapability(next.getTileEntity(world()),
|
||||
Capabilities.LOGISTICAL_TRANSPORTER_CAPABILITY, null);
|
||||
nextTile.entityEntering(stack, stack.progress % 100);
|
||||
deletes.add(stack);
|
||||
|
||||
deletes.add(stackId);
|
||||
continue;
|
||||
|
||||
} else if (next != null) {
|
||||
prevSet = next;
|
||||
}
|
||||
} else {
|
||||
if (stack.pathType != Path.NONE) {
|
||||
TileEntity tile = next.getTileEntity(world());
|
||||
} else if (stack.pathType != Path.NONE) {
|
||||
TileEntity tile = next.getTileEntity(world());
|
||||
|
||||
if (tile != null) {
|
||||
needsSync.add(stack);
|
||||
TransitResponse response = InventoryUtils
|
||||
.putStackInInventory(tile, TransitRequest.getFromTransport(stack),
|
||||
stack.getSide(this), stack.pathType == Path.HOME);
|
||||
if (tile != null) {
|
||||
TransitResponse response = InventoryUtils
|
||||
.putStackInInventory(tile, TransitRequest.getFromTransport(stack),
|
||||
stack.getSide(this), stack.pathType == Path.HOME);
|
||||
|
||||
if (response.getRejected(stack.itemStack).isEmpty()) {
|
||||
TransporterManager.remove(stack);
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
} else {
|
||||
needsSync.add(stack);
|
||||
stack.itemStack = response.getRejected(stack.itemStack);
|
||||
|
||||
prevSet = next;
|
||||
}
|
||||
// Nothing was rejected; remove the stack from the prediction tracker and
|
||||
// schedule this stack for deletion. Continue the loop thereafter
|
||||
if (response.getRejected(stack.itemStack).isEmpty()) {
|
||||
TransporterManager.remove(stack);
|
||||
deletes.add(stackId);
|
||||
continue;
|
||||
} else {
|
||||
// Some portion of the stack got rejected; save the remainder and
|
||||
// let the recalculate below sort out what to do next
|
||||
stack.itemStack = response.getRejected(stack.itemStack);
|
||||
prevSet = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!recalculate(stack, prevSet)) {
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
if (!recalculate(stackId, stack, prevSet)) {
|
||||
deletes.add(stackId);
|
||||
} else if (prevSet != null) {
|
||||
stack.progress = 0;
|
||||
} else {
|
||||
if (prevSet != null) {
|
||||
stack.progress = 0;
|
||||
} else {
|
||||
stack.progress = 50;
|
||||
}
|
||||
stack.progress = 50;
|
||||
}
|
||||
|
||||
} else if (stack.progress == 50) {
|
||||
if (stack.isFinal(this)) {
|
||||
if (stack.pathType == Path.DEST && (!checkSideForInsert(stack) || !InventoryUtils
|
||||
.canInsert(stack.getDest().getTileEntity(world()), stack.color, stack.itemStack,
|
||||
stack.getSide(this), false))) {
|
||||
if (!recalculate(stack, null)) {
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
}
|
||||
} else if (stack.pathType == Path.HOME && (!checkSideForInsert(stack) || !InventoryUtils
|
||||
.canInsert(stack.getDest().getTileEntity(world()), stack.color, stack.itemStack,
|
||||
stack.getSide(this), true))) {
|
||||
if (!recalculate(stack, null)) {
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
}
|
||||
} else if (stack.pathType == Path.NONE) {
|
||||
if (!recalculate(stack, null)) {
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
if (checkPath(stack, Path.DEST, false) || checkPath(stack, Path.HOME, true)
|
||||
|| stack.pathType == Path.NONE) {
|
||||
if (!recalculate(stackId, stack, null)) {
|
||||
deletes.add(stackId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TileEntity next = stack.getNext(this).getTileEntity(world());
|
||||
boolean recalculate = false;
|
||||
|
||||
if (!stack.canInsertToTransporter(next, stack.getSide(this))) {
|
||||
recalculate = true;
|
||||
}
|
||||
|
||||
if (recalculate) {
|
||||
if (!recalculate(stack, null)) {
|
||||
deletes.add(stack);
|
||||
continue;
|
||||
if (!recalculate(stackId, stack, null)) {
|
||||
deletes.add(stackId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove any packets from needsSync that are also queued up for removal
|
||||
// TODO: Verify that there's no case where we want a sync update before removal; in my testing
|
||||
// this had a surprising impact on overall efficiency, which suggests we're adding lots
|
||||
// of packets to needSync that isn't strictly necessary
|
||||
needsSync.removeIf(s -> deletes.contains(s));
|
||||
|
||||
if (deletes.size() > 0 || needsSync.size() > 0) {
|
||||
// Construct the message first; this way our indices are in sync with client
|
||||
TileEntityMessage msg = new TileEntityMessage(coord(), getTileEntity().makeBatchPacket(needsSync, deletes));
|
||||
TileEntityMessage msg = new TileEntityMessage(coord(),
|
||||
getTileEntity().makeBatchPacket(needsSync, deletes));
|
||||
|
||||
// Now remove any entries from transit that have been deleted
|
||||
for (TransporterStack stack : deletes) {
|
||||
transit.remove(stack);
|
||||
}
|
||||
deletes.forEach(id -> transit.remove(id));
|
||||
|
||||
// Clear the pending sync packets
|
||||
needsSync.clear();
|
||||
@@ -191,6 +218,12 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkPath(TransporterStack stack, Path dest, boolean home) {
|
||||
return stack.pathType == dest && (!checkSideForInsert(stack) || !InventoryUtils
|
||||
.canInsert(stack.getDest().getTileEntity(world()), stack.color, stack.itemStack,
|
||||
stack.getSide(this), home));
|
||||
}
|
||||
|
||||
private boolean checkSideForInsert(TransporterStack stack) {
|
||||
EnumFacing side = stack.getSide(this);
|
||||
|
||||
@@ -198,9 +231,7 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
|| getTileEntity().getConnectionType(side) == ConnectionType.PUSH;
|
||||
}
|
||||
|
||||
private boolean recalculate(TransporterStack stack, Coord4D from) {
|
||||
needsSync.add(stack);
|
||||
|
||||
private boolean recalculate(int stackId, TransporterStack stack, Coord4D from) {
|
||||
if (stack.pathType != Path.NONE) {
|
||||
TransitResponse ret = stack.recalculatePath(TransitRequest.getFromTransport(stack), this, 0);
|
||||
|
||||
@@ -217,6 +248,8 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
}
|
||||
}
|
||||
|
||||
//Only add to needsSync if true is being returned; otherwise it gets added to deletes
|
||||
needsSync.put(stackId, stack);
|
||||
if (from != null) {
|
||||
stack.originalLocation = from;
|
||||
}
|
||||
@@ -245,13 +278,19 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
|
||||
TransitResponse response = stack.recalculatePath(request, this, min);
|
||||
|
||||
return getTransitResponse(doEmit, stack, response);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private TransitResponse getTransitResponse(boolean doEmit, TransporterStack stack, TransitResponse response) {
|
||||
if (!response.isEmpty()) {
|
||||
stack.itemStack = response.getStack();
|
||||
|
||||
if (doEmit) {
|
||||
transit.add(stack);
|
||||
int stackId = nextId++;
|
||||
transit.put(stackId, stack);
|
||||
Mekanism.packetHandler
|
||||
.sendToReceivers(new TileEntityMessage(coord(), getTileEntity().makeSyncPacket(stack)),
|
||||
.sendToReceivers(new TileEntityMessage(coord(), getTileEntity().makeSyncPacket(stackId, stack)),
|
||||
new Range4D(coord()));
|
||||
MekanismUtils.saveChunk(getTileEntity());
|
||||
}
|
||||
@@ -278,21 +317,7 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
|
||||
TransitResponse response = stack.recalculateRRPath(request, outputter, this, min);
|
||||
|
||||
if (!response.isEmpty()) {
|
||||
stack.itemStack = response.getStack();
|
||||
|
||||
if (doEmit) {
|
||||
transit.add(stack);
|
||||
Mekanism.packetHandler
|
||||
.sendToReceivers(new TileEntityMessage(coord(), getTileEntity().makeSyncPacket(stack)),
|
||||
new Range4D(coord()));
|
||||
MekanismUtils.saveChunk(getTileEntity());
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
return TransitResponse.EMPTY;
|
||||
return getTransitResponse(doEmit, stack, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -303,9 +328,10 @@ public class TransporterImpl extends TransmitterImpl<TileEntity, InventoryNetwor
|
||||
// This code used to generate a sync message at this point, but that was a LOT
|
||||
// of bandwidth in a busy server, so by adding to needsSync, the sync will happen
|
||||
// in a batch on a per-tick basis.
|
||||
int stackId = nextId++;
|
||||
stack.progress = progress;
|
||||
transit.add(stack);
|
||||
needsSync.add(stack);
|
||||
transit.put(stackId, stack);
|
||||
needsSync.put(stackId, stack);
|
||||
|
||||
// N.B. We are not marking the chunk as dirty here! I don't believe it's needed, since
|
||||
// the next tick will generate the necessary save and if we crash before the next tick,
|
||||
|
||||
Reference in new issue
Block a user