Compare commits

..
Author SHA1 Message Date
Martin Robertz 096363a7ca Merge remote-tracking branch 'origin/master' into dev 2023-03-30 22:21:12 +02:00
chochem 54781c290e quantum fluid fix (#114) 2023-03-30 22:21:00 +02:00
Firenoo 78d88fb9c3 Network throttle: Force ME update packets to be sent once every 50ms.
1. Item and fluid packets are separated to their own classes, as they are
not even allowed to be mixed together in the first place. This will enforce a strict
separation of the two.

2. New network worker thread for handling ME inventory updates. Instead of
creating packets directly, classes should schedule packet(s) to be sent at the
next 50ms boundary. The packet has a limit of 256 items to prevent killing the
client, but this can be changed in the config. In fact, the packet send rate is
also configurable.

3. Upon opening a terminal GUI, ME update packets are split. Only the last received
packet will trigger a terminal sorting, so if the ME system is large, it may take
several seconds (!) to load all the items.

(cherry picked from commit 4513a83d6d)
2023-03-25 20:40:36 +01:00
Martin Robertz a120bd560b Fix fluid terminal interactions (#112)
Now the math is properly calculated.

Co-authored-by: Firenoo <[email protected]>
(cherry picked from commit a921d5b9ce)
2023-03-25 20:40:18 +01:00
11 changed files with 463 additions and 198 deletions

No files matched your search

@@ -14,6 +14,7 @@ import com.glodblock.github.loader.ChannelLoader;
import com.glodblock.github.loader.ItemAndBlockHolder;
import com.glodblock.github.loader.RecipeLoader;
import com.glodblock.github.loader.filter.FluidFilter;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.proxy.CommonProxy;
import com.glodblock.github.util.ModAndClassUtil;
@@ -82,6 +83,16 @@ public class FluidCraft {
proxy.postInit(event);
}
@Mod.EventHandler
public void onServerStart(FMLServerStartingEvent event) {
SPacketMEUpdateBuffer.init();
}
@Mod.EventHandler
public void onServerStop(FMLServerStoppedEvent event) {
SPacketMEUpdateBuffer.disable();
}
@Mod.EventHandler
public void onLoadComplete(FMLLoadCompleteEvent event) {}
@@ -1,7 +1,8 @@
package com.glodblock.github.client.gui.container;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
@@ -14,8 +15,6 @@ import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.IFluidContainerItem;
import org.apache.commons.lang3.tuple.MutablePair;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.implementations.tiles.IViewCellStorage;
@@ -43,7 +42,7 @@ import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.network.CPacketFluidUpdate;
import com.glodblock.github.network.SPacketFluidUpdate;
import com.glodblock.github.network.SPacketMEFluidInvUpdate;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.util.Util;
public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
@@ -126,33 +125,22 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
protected void processItemList() {
if (!this.fluids.isEmpty()) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack is : this.fluids) {
final IAEFluidStack send = monitorCache.findPrecise(is);
try {
if (send != null) {
packet.appendFluid(send);
} else {
is.setStackSize(0);
packet.appendFluid(is);
}
} catch (BufferOverflowException e) {
for (final Object c : this.crafters) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
packet = new SPacketMEFluidInvUpdate();
if (send != null) {
packet.appendFluid(send);
} else {
is.setStackSize(0);
packet.appendFluid(is);
}
if (send != null) {
toSend.add(send);
} else {
is.setStackSize(0);
toSend.add(is);
}
}
for (final Object c : this.crafters) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
if (c instanceof EntityPlayerMP) {
SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
}
}
this.fluids.resetStatus();
}
}
@@ -160,17 +148,11 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayerMP && this.monitor != null) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack is : monitorCache) {
try {
packet.appendFluid(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEFluidInvUpdate();
packet.appendFluid(is);
}
toSend.add(is);
}
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
}
}
@@ -210,122 +192,275 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
this.dropItem(is);
}
/**
* Two operations, insert and extract.
*
* @param slotIndex The slot. If index == -1, the cursor held itemstack. Else, slotIndex indicates which slot was
* shift clicked on.
*/
public void postChange(Iterable<IAEFluidStack> change, ItemStack fluidContainer, EntityPlayer player,
int slotIndex) {
int cursorItems;
if (slotIndex == -1) {
cursorItems = player.inventory.getItemStack().stackSize;
} else {
cursorItems = player.inventory.getStackInSlot(slotIndex).stackSize;
}
for (IAEFluidStack fluid : change) {
IAEFluidStack nfluid = this.monitor.getStorageList().findPrecise(fluid);
ItemStack out = fluidContainer.copy();
out.stackSize = 1;
// The primary output itemstack
if (Util.FluidUtil.isEmpty(fluidContainer) && fluid != null) {
// add fluid to tanks
if (nfluid.getStackSize() <= 0) continue;
final IAEFluidStack toExtract = nfluid.copy();
MutablePair<Integer, ItemStack> fillStack = Util.FluidUtil.fillStack(out, toExtract.getFluidStack());
if (fillStack.right == null || fillStack.left <= 0) continue;
toExtract.setStackSize((long) fillStack.left * fluidContainer.stackSize);
// The real result may don't match simulation result.
if (fillStack.right.getItem() instanceof IFluidContainerItem) {
IAEFluidStack real = this.host.getFluidInventory()
.extractItems(toExtract, Actionable.MODULATE, this.getActionSource());
if (real == null) continue;
// Refill the container
out = fluidContainer.copy();
out.stackSize = 1;
fillStack = Util.FluidUtil.fillStack(out, real.getFluidStack());
fillStack.right.stackSize = (int) (real.getStackSize() / fillStack.left);
out.stackSize = fillStack.right.stackSize;
if ((int) (real.getStackSize() % fillStack.left) > 0) {
this.dropItem(
Util.FluidUtil.setFluidContainerAmount(
fillStack.right,
(int) (real.getStackSize() % fillStack.left)),
1);
out.stackSize++;
}
} else if (FluidContainerRegistry.isContainer(fillStack.right)) {
IAEFluidStack real = this.host.getFluidInventory()
.extractItems(toExtract, Actionable.MODULATE, this.getActionSource());
if (real == null) continue;
// Refill the container
out = fluidContainer.copy();
out.stackSize = 1;
fillStack = Util.FluidUtil.fillStack(out, real.getFluidStack());
fillStack.right.stackSize = (int) (real.getStackSize() / fillStack.left);
out.stackSize = fillStack.right.stackSize;
toExtract.setStackSize((long) fillStack.right.stackSize * fillStack.left);
}
this.dropItem(fillStack.right);
// Situation 1.a: Empty fluid container, and nonnull slot
extractFluid(fluid, fluidContainer, player, cursorItems);
} else if (!Util.FluidUtil.isEmpty(fluidContainer)) {
// add fluid to ae network
AEFluidStack fluidStack = Util.getAEFluidFromItem(fluidContainer);
final IAEFluidStack aeFluidStack = fluidStack.copy();
// simulate result is incorrect. so I'm using other solution and ec2 both mod have same issues
final IAEFluidStack notInserted = this.host.getFluidInventory()
.injectItems(aeFluidStack, Actionable.MODULATE, this.getActionSource());
MutablePair<Integer, ItemStack> drainStack = Util.FluidUtil
.drainStack(out.copy(), aeFluidStack.getFluidStack());
if (notInserted != null && notInserted.getStackSize() > 0) {
if (fluidStack.getStackSize() == notInserted.getStackSize()) continue;
aeFluidStack.decStackSize(notInserted.getStackSize());
if (drainStack.left > aeFluidStack.getStackSize()
&& FluidContainerRegistry.isContainer(drainStack.right)) {
aeFluidStack.setStackSize(drainStack.left - aeFluidStack.getStackSize());
this.host.getFluidInventory()
.extractItems(aeFluidStack, Actionable.MODULATE, this.getActionSource());
continue;
}
this.dropItem(drainStack.right, (int) (aeFluidStack.getStackSize() / drainStack.left)); // drop
// empty
// item
out.stackSize = (int) (notInserted.getStackSize() / drainStack.left);
if (drainStack.right.getItem() instanceof IFluidContainerItem) {
if (notInserted.getStackSize() % drainStack.left > 0) {
fluidStack.setStackSize((notInserted.getStackSize() % drainStack.left));
((IFluidContainerItem) drainStack.right.getItem())
.fill(drainStack.right, fluidStack.getFluidStack(), true);
this.dropItem(drainStack.right, 1);
}
} else if (FluidContainerRegistry.isContainer(drainStack.right)) {
if (notInserted.getStackSize() % drainStack.left > 0) {
aeFluidStack.setStackSize(aeFluidStack.getStackSize() % drainStack.left);
this.host.getFluidInventory()
.extractItems(aeFluidStack, Actionable.MODULATE, this.getActionSource());
out.stackSize++;
}
}
if (slotIndex == -1) out.stackSize = fluidContainer.stackSize - out.stackSize;
} else {
if (slotIndex == -1) {
out.stackSize = fluidContainer.stackSize;
} else {
out.stackSize = (int) (fluidContainer.stackSize
- (aeFluidStack.getStackSize() / drainStack.left));
}
this.dropItem(drainStack.right, fluidContainer.stackSize); // drop empty item
}
} else {
continue;
}
if (slotIndex == -1) {
player.inventory.getItemStack().stackSize = player.inventory.getItemStack().stackSize - out.stackSize;
if (player.inventory.getItemStack().stackSize > 0) {
FluidCraft.proxy.netHandler.sendTo(
new SPacketFluidUpdate(new HashMap<>(), player.inventory.getItemStack()),
(EntityPlayerMP) player);
} else {
player.inventory.setItemStack(null);
FluidCraft.proxy.netHandler
.sendTo(new SPacketFluidUpdate(new HashMap<>()), (EntityPlayerMP) player);
}
} else {
player.inventory.setInventorySlotContents(slotIndex, out.stackSize > 0 ? out : null);
// Situation 2.a: We are holding a non-empty container.
insertFluid(fluidContainer, player, slotIndex, cursorItems);
// End of situation 2.a
}
// No op (Any other situation)
}
this.detectAndSendChanges();
}
/**
* The insert operation. For input, we have an filled container stack. For outputs, we have the following: 1.
* Leftover filled container stack - primary output. 2. Empty containers 3. Partially filled container x1 In order
* above, the itemstack at `slotIndex` is transformed into the output.
*/
private void insertFluid(ItemStack fluidContainer, EntityPlayer player, int slotIndex, int heldContainers) {
IAEFluidStack fluid;
int fluidInTank;
if (fluidContainer.getItem() instanceof IFluidContainerItem) {
fluid = AEFluidStack.create(((IFluidContainerItem) fluidContainer.getItem()).getFluid(fluidContainer));
fluidInTank = (int) fluid.getStackSize();
} else if (FluidContainerRegistry.isContainer(fluidContainer)) {
fluid = AEFluidStack.create(FluidContainerRegistry.getFluidForFilledItem(fluidContainer));
fluidInTank = (int) fluid.getStackSize();
} else {
return;
}
// Simulate to know how much will not be inserted
long toInsert = (long) fluidInTank * fluidContainer.stackSize;
IAEFluidStack canSend = fluid.copy();
canSend.setStackSize(toInsert);
final IAEFluidStack notInserted = this.host.getFluidInventory()
.injectItems(canSend, Actionable.SIMULATE, this.getActionSource());
int emptyTanks = fluidContainer.stackSize;
ItemStack partialStack = fluidContainer.copy();
if (notInserted != null && notInserted.getStackSize() > 0) {
// Here, we cannot insert everything into the system.
if (canSend.getStackSize() == notInserted.getStackSize()) return; // System was full, no op
// Now "refill" the buckets that couldn't enter the system
int refilled = (int) (notInserted.getStackSize() / fluidInTank); // # of full tanks left
emptyTanks -= refilled;
toInsert -= (long) fluidInTank * refilled;
final int remainder = (int) (notInserted.getStackSize() % fluidInTank); // partial tank
// Now handle remaining if applicable
if (remainder > 0) {
notInserted.setStackSize(remainder);
partialStack.stackSize = 1;
if (partialStack.getItem() instanceof IFluidContainerItem) {
int filled = ((IFluidContainerItem) partialStack.getItem())
.fill(partialStack, notInserted.getFluidStack(), true);
toInsert -= filled;
assert filled == remainder;
emptyTanks--;
} else if (FluidContainerRegistry.isContainer(fluidContainer)) {
// For "whole containers" (need to round down, no remainders)
ItemStack container = FluidContainerRegistry
.fillFluidContainer(notInserted.getFluidStack(), fluidContainer);
if (container == null) {
// whatever, lets just not have the ME system get filled with any of its fluid
emptyTanks++;
toInsert -= remainder;
partialStack.stackSize = 0;
} else {
toInsert -= FluidContainerRegistry.getContainerCapacity(partialStack);
partialStack = container;
}
}
} else {
partialStack.stackSize = 0;
}
} else {
partialStack.stackSize = 0;
}
// Now that the 3 outputs are handled, we can now actually put the fluids in
canSend.setStackSize(toInsert);
IAEFluidStack extra = this.host.getFluidInventory()
.injectItems(canSend, Actionable.MODULATE, this.getActionSource());
assert extra == null || extra.getStackSize() <= 0;
ItemStack emptyStack = fluidContainer.copy();
emptyStack.stackSize = 1;
if (emptyStack.getItem() instanceof IFluidContainerItem) {
((IFluidContainerItem) emptyStack.getItem()).drain(emptyStack, fluidInTank, true);
} else {
emptyStack = FluidContainerRegistry.drainFluidContainer(emptyStack);
}
emptyStack.stackSize = emptyTanks;
int extraTanks = heldContainers - emptyTanks;
// Done. Put the output in the inventory or ground, and update stack size.
boolean shouldSendStack = true;
if (slotIndex == -1) {
if (extraTanks > 0) {
player.inventory.getItemStack().stackSize = extraTanks;
dropItem(emptyStack);
dropItem(partialStack);
} else if (emptyTanks != 0) {
player.inventory.setItemStack(emptyStack);
dropItem(partialStack);
} else if (partialStack.stackSize != 0) {
player.inventory.setItemStack(partialStack);
} else {
player.inventory.setItemStack(null);
shouldSendStack = false;
}
} else {
ItemStack extraTanksStack = player.inventory.getStackInSlot(slotIndex);
if (extraTanks > 0) {
if (extraTanks > extraTanksStack.getMaxStackSize()) {
dropItem(extraTanksStack, extraTanksStack.stackSize - extraTanksStack.getMaxStackSize());
extraTanksStack.stackSize = extraTanksStack.getMaxStackSize();
}
dropItem(emptyStack);
dropItem(partialStack);
} else if (emptyTanks != 0) {
if (emptyTanks > emptyStack.getMaxStackSize()) {
dropItem(emptyStack, emptyTanks - emptyStack.getMaxStackSize());
emptyStack.stackSize = emptyStack.getMaxStackSize();
}
player.inventory.setInventorySlotContents(slotIndex, emptyStack);
dropItem(partialStack);
} else if (partialStack.stackSize != 0) {
player.inventory.setInventorySlotContents(slotIndex, partialStack);
} else {
player.inventory.setItemStack(null);
shouldSendStack = false;
}
}
if (shouldSendStack) {
FluidCraft.proxy.netHandler.sendTo(
new SPacketFluidUpdate(new HashMap<>(), player.inventory.getItemStack()),
(EntityPlayerMP) player);
} else {
FluidCraft.proxy.netHandler.sendTo(new SPacketFluidUpdate(new HashMap<>()), (EntityPlayerMP) player);
}
}
/**
* The extract operation. For input, we have an empty container stack. For outputs, we have the following: 1.
* Leftover empty container stack - primary output. 2. Filled containers (full) 3. Partially filled container x1 In
* order above, the itemstack at `slotIndex` is transformed into the output.
*/
private void extractFluid(IAEFluidStack fluid, ItemStack fluidContainer, EntityPlayer player, int heldContainers) {
IAEFluidStack storedFluid = this.monitor.getStorageList().findPrecise(fluid);
if (storedFluid == null || storedFluid.getStackSize() <= 0) return;
int capacity;
if (fluidContainer.getItem() instanceof IFluidContainerItem) {
capacity = ((IFluidContainerItem) fluidContainer.getItem()).getCapacity(fluidContainer);
} else if (FluidContainerRegistry.isContainer(fluidContainer)) {
// Fill with fluid container registry, or abort if it failed
ItemStack dummy = FluidContainerRegistry.fillFluidContainer(storedFluid.getFluidStack(), fluidContainer);
capacity = FluidContainerRegistry.getContainerCapacity(dummy);
} else {
return;
}
if (capacity == 0) return;
// The fluidstack that we will try to extract from the system.
final IAEFluidStack canExtract = storedFluid.copy();
// Maximum amount of fluid we can extract with the given container(s). Guarantees that we won't
// run into issues w/ remainder fluid later.
canExtract.setStackSize((long) capacity * fluidContainer.stackSize);
IAEFluidStack actualExtract = this.host.getFluidInventory()
.extractItems(canExtract, Actionable.SIMULATE, this.getActionSource());
if (actualExtract == null) return;
// Calculate the number of full fluid containers we extracted
long toExtract = actualExtract.getStackSize();
int filledTanks = (int) (toExtract / capacity);
int remainder = (int) (toExtract % capacity);
int emptyTanks = heldContainers - filledTanks;
// Fill the filled tanks
ItemStack filledStack = fluidContainer.copy();
filledStack.stackSize = 1;
if (filledTanks > 0) {
if (filledStack.getItem() instanceof IFluidContainerItem) {
int amt = ((IFluidContainerItem) filledStack.getItem())
.fill(filledStack, actualExtract.getFluidStack(), true);
assert amt == capacity;
} else {
// Should not be NPE since we affirmed that we have enough capacity.
filledStack = FluidContainerRegistry.fillFluidContainer(actualExtract.getFluidStack(), filledStack);
assert capacity == FluidContainerRegistry.getContainerCapacity(filledStack);
}
filledStack.stackSize = filledTanks;
} else {
filledStack.stackSize = 0;
}
// Calculate the remaining fluid to extract, if any
ItemStack partialStack = fluidContainer.copy();
if (remainder > 0) {
actualExtract.setStackSize(remainder);
partialStack.stackSize = 1;
if (partialStack.getItem() instanceof IFluidContainerItem) {
int amt = ((IFluidContainerItem) partialStack.getItem())
.fill(partialStack, actualExtract.getFluidStack(), true);
assert amt == remainder;
emptyTanks--;
} else {
ItemStack dummy = FluidContainerRegistry
.fillFluidContainer(actualExtract.getFluidStack(), partialStack);
if (dummy == null) {
// Failed to fill partial...
partialStack.stackSize = 0;
toExtract -= remainder;
} else {
partialStack = dummy;
toExtract -= FluidContainerRegistry.getContainerCapacity(partialStack);
emptyTanks--;
}
}
} else {
partialStack.stackSize = 0;
}
// Now perform the real extraction.
actualExtract.setStackSize(toExtract);
this.host.getFluidInventory().extractItems(canExtract, Actionable.MODULATE, this.getActionSource());
// Done. Put the output in the inventory or ground, and update stack size.
// We can assume slotIndex == -1, since we don't actually allow extraction via shift click.
boolean shouldSendStack = true;
if (emptyTanks > 0) {
ItemStack emptyStack = player.inventory.getItemStack();
if (emptyTanks > emptyStack.stackSize) {
dropItem(emptyStack, emptyStack.stackSize - emptyStack.getMaxStackSize());
emptyStack.stackSize = emptyStack.getMaxStackSize();
}
dropItem(filledStack);
dropItem(partialStack);
} else if (filledStack.stackSize != 0) {
if (filledStack.stackSize > filledStack.getMaxStackSize()) {
dropItem(filledStack, filledStack.stackSize - filledStack.getMaxStackSize());
filledStack.stackSize = filledStack.getMaxStackSize();
}
player.inventory.setItemStack(filledStack);
dropItem(partialStack);
} else if (partialStack.stackSize != 0) {
player.inventory.setItemStack(partialStack);
} else {
player.inventory.setItemStack(null);
shouldSendStack = false;
}
if (shouldSendStack) {
FluidCraft.proxy.netHandler.sendTo(
new SPacketFluidUpdate(new HashMap<>(), player.inventory.getItemStack()),
(EntityPlayerMP) player);
} else {
FluidCraft.proxy.netHandler.sendTo(new SPacketFluidUpdate(new HashMap<>()), (EntityPlayerMP) player);
}
}
protected boolean isEssentiaMode() {
return false;
}
@@ -1,6 +1,7 @@
package com.glodblock.github.client.gui.container;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -25,9 +26,8 @@ import appeng.container.slot.SlotRestrictedInput;
import appeng.me.helpers.ChannelPowerSrc;
import appeng.util.Platform;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.network.SPacketMEItemInvUpdate;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
@@ -95,34 +95,19 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
protected void processItemList() {
if (!this.items.isEmpty()) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack is : this.items) {
final IAEItemStack send = monitorCache.findPrecise(is);
try {
if (send != null) {
packet.appendItem(send);
} else {
is.setStackSize(0);
packet.appendItem(is);
}
} catch (BufferOverflowException e) {
for (final Object c : this.crafters) {
if (c instanceof EntityPlayerMP) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}
packet = new SPacketMEItemInvUpdate();
if (send != null) {
packet.appendItem(send);
} else {
is.setStackSize(0);
packet.appendItem(is);
}
if (send != null) {
toSend.add(send);
} else {
is.setStackSize(0);
toSend.add(is);
}
}
for (final Object c : this.crafters) {
if (c instanceof EntityPlayerMP) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
if (c instanceof EntityPlayer) {
SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
}
}
this.items.resetStatus();
@@ -133,17 +118,11 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack is : monitorCache) {
try {
packet.appendItem(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEItemInvUpdate();
packet.appendItem(is);
}
toSend.add(is);
}
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
}
}
@@ -152,6 +131,9 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
super.removeCraftingFromCrafters(c);
if (this.crafters.isEmpty() && this.monitor != null) {
this.monitor.removeListener(this);
if (c instanceof EntityPlayerMP) {
SPacketMEUpdateBuffer.clear((EntityPlayerMP) c);
}
}
}
@@ -30,6 +30,7 @@ import appeng.util.IConfigManagerHost;
import appeng.util.Platform;
import com.glodblock.github.inventory.item.IWirelessTerminal;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseContainer
implements IConfigManagerHost, IConfigurableObject, IMEMonitorHandlerReceiver<T> {
@@ -158,6 +159,10 @@ public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseCo
super.onContainerClosed(player);
if (this.monitor != null) {
this.monitor.removeListener(this);
if (player instanceof EntityPlayerMP && Platform.isServer()) {
SPacketMEUpdateBuffer.clear((EntityPlayerMP) player);
}
}
}
@@ -18,6 +18,7 @@ public class Config {
public static boolean blacklistEssentiaGas;
public static double portableCellBattery;
public static int packetSize;
public static int packetRate;
public static void run() {
loadCategory();
@@ -55,6 +56,10 @@ public class Config {
packetSize = Config.get("Fluid Craft for AE2", "packetSize", 256, "Number of items to be sent per packet")
.getInt();
if (packetSize <= 0) packetSize = 256;
packetRate = Config
.get("Fluid Craft for AE2", "packetRate", 50, "Period at which packets are dispatched, in ms.")
.getInt();
if (packetRate <= 0) packetRate = 50;
if (Config.hasChanged()) Config.save();
}
@@ -81,7 +81,7 @@ public class ItemAndBlockHolder {
NameConst.ITEM_QUANTUM_FLUID_STORAGE,
Integer.MAX_VALUE / 16,
8,
5,
1,
4.5).register();
public static ItemFluidExtremeStorageCell SINGULARITY_CELL = new ItemFluidExtremeStorageCell(
NameConst.ITEM_SINGULARITY_FLUID_STORAGE,
@@ -1,5 +1,8 @@
package com.glodblock.github.network;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -9,7 +12,6 @@ import net.minecraft.nbt.NBTTagCompound;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.ContainerLevelMaintainer;
import com.glodblock.github.common.tile.TileLevelMaintainer;
@@ -88,7 +90,7 @@ public class CPacketLevelMaintainer implements IMessage {
public static class Handler implements IMessageHandler<CPacketLevelMaintainer, IMessage> {
private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) {
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
List<IAEItemStack> 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);
@@ -96,7 +98,7 @@ public class CPacketLevelMaintainer implements IMessage {
if (is1 != null) {
NBTTagCompound data;
data = is1.getItemStack().getTagCompound();
packet.appendItem(
toSend.add(
setTag(
is,
is1.getStackSize(),
@@ -104,11 +106,11 @@ public class CPacketLevelMaintainer implements IMessage {
data.getBoolean("Enable"),
cca.getTile().requests.getState(i).ordinal()));
} else {
packet.appendItem(setTag(is, 0, i, true, 0));
toSend.add(setTag(is, 0, i, true, 0));
}
}
}
FluidCraft.proxy.netHandler.sendTo(packet, player);
SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend);
}
@Nullable
@@ -1,7 +1,6 @@
package com.glodblock.github.network;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.List;
@@ -13,7 +12,6 @@ import appeng.util.item.AEFluidStack;
import com.glodblock.github.client.gui.GuiFluidCraftConfirm;
import com.glodblock.github.client.gui.GuiFluidMonitor;
import com.glodblock.github.common.Config;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
@@ -40,8 +38,12 @@ public class SPacketMEFluidInvUpdate implements IMessage {
/**
* If resort, call "updateView()". Used when multiple packets are sent to open an inventory; only the last packet
* should resort.
* should resort. <<<<<<< HEAD
*
* =======
*
* >>>>>>> 669eb96... Network throttle: Force ME update packets to be sent once every 50ms.
*
* @param resort whether this packet should resort the term or not
*/
public SPacketMEFluidInvUpdate(boolean resort) {
@@ -82,12 +84,8 @@ public class SPacketMEFluidInvUpdate implements IMessage {
this.resort = resort;
}
public void appendFluid(final IAEFluidStack is) throws BufferOverflowException {
if (list.size() <= Config.packetSize) {
list.add(is);
} else {
throw new BufferOverflowException();
}
public void appendFluid(final IAEFluidStack is) {
list.add(is);
}
public boolean isEmpty() {
@@ -1,7 +1,6 @@
package com.glodblock.github.network;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.List;
@@ -14,7 +13,6 @@ import appeng.util.item.AEItemStack;
import com.glodblock.github.client.gui.GuiFluidCraftConfirm;
import com.glodblock.github.client.gui.GuiItemMonitor;
import com.glodblock.github.client.gui.GuiLevelMaintainer;
import com.glodblock.github.common.Config;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
@@ -83,12 +81,8 @@ public class SPacketMEItemInvUpdate implements IMessage {
this.resort = resort;
}
public void appendItem(final IAEItemStack is) throws BufferOverflowException {
if (list.size() <= Config.packetSize) {
list.add(is);
} else {
throw new BufferOverflowException();
}
public void appendItem(final IAEItemStack is) {
list.add(is);
}
public boolean isEmpty() {
@@ -0,0 +1,130 @@
package com.glodblock.github.network;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import net.minecraft.entity.player.EntityPlayerMP;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.common.Config;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
/**
* Only should exist on the server side! Schedules packets to be sent every `packetRate` ms.
*/
public class SPacketMEUpdateBuffer {
private static ScheduledExecutorService executor;
private static ScheduledFuture<?> task;
private static final Map<EntityPlayerMP, LinkedHashSet<IAEItemStack>> itemBuffer = new HashMap<>();
private static final Map<EntityPlayerMP, LinkedHashSet<IAEFluidStack>> fluidBuffer = new HashMap<>();
public static void init() {
executor = Executors.newSingleThreadScheduledExecutor(r -> {
Thread thread = new Thread(r);
thread.setName("AE2FC Network worker");
thread.setDaemon(true);
thread.setPriority(6);
return thread;
});
task = executor
.scheduleAtFixedRate(SPacketMEUpdateBuffer::sendBuffer, 0, Config.packetRate, TimeUnit.MILLISECONDS);
}
public static void disable() {
task.cancel(false);
executor.shutdown();
}
public static void scheduleItemUpdate(EntityPlayerMP player, List<IAEItemStack> stacks) {
synchronized (itemBuffer) {
if (!itemBuffer.containsKey(player)) {
itemBuffer.put(player, new LinkedHashSet<>(1024));
}
LinkedHashSet<IAEItemStack> buffer = itemBuffer.get(player);
stacks.forEach(s -> buffer.add(s.copy()));
}
}
public static void scheduleFluidUpdate(EntityPlayerMP player, List<IAEFluidStack> stacks) {
synchronized (itemBuffer) {
if (!fluidBuffer.containsKey(player)) {
fluidBuffer.put(player, new LinkedHashSet<>(1024));
}
LinkedHashSet<IAEFluidStack> buffer = fluidBuffer.get(player);
stacks.forEach(s -> buffer.add(s.copy()));
}
}
public static void sendBuffer() {
synchronized (itemBuffer) {
itemBuffer.forEach((player, updates) -> {
if (updates.isEmpty()) return;
int i = 0;
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate(false);
Iterator<IAEItemStack> it = updates.iterator();
while (it.hasNext()) {
if (i < Config.packetSize) {
packet.appendItem(it.next());
it.remove();
i++;
} else {
FluidCraft.proxy.netHandler.sendTo(packet, player);
packet = new SPacketMEItemInvUpdate(false);
i = 0;
}
}
packet.setResort(true);
FluidCraft.proxy.netHandler.sendTo(packet, player);
});
fluidBuffer.forEach((player, updates) -> {
if (updates.isEmpty()) return;
int i = 0;
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate(false);
Iterator<IAEFluidStack> it = updates.iterator();
while (it.hasNext()) {
if (i < Config.packetSize) {
packet.appendFluid(it.next());
it.remove();
i++;
} else {
FluidCraft.proxy.netHandler.sendTo(packet, player);
packet = new SPacketMEFluidInvUpdate(false);
i = 0;
}
}
packet.setResort(true);
FluidCraft.proxy.netHandler.sendTo(packet, player);
});
}
}
public static void clear(EntityPlayerMP player) {
synchronized (itemBuffer) {
if (itemBuffer.containsKey(player)) {
itemBuffer.get(player).clear();
}
if (fluidBuffer.containsKey(player)) {
fluidBuffer.get(player).clear();
}
}
}
@SubscribeEvent
public void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event) {
if (event.player instanceof EntityPlayerMP) {
clear((EntityPlayerMP) event.player);
}
}
}
@@ -11,8 +11,10 @@ import com.glodblock.github.common.Config;
import com.glodblock.github.common.tile.TileWalrus;
import com.glodblock.github.inventory.external.AEFluidInterfaceHandler;
import com.glodblock.github.loader.ItemAndBlockHolder;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.util.ModAndClassUtil;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
@@ -27,6 +29,7 @@ public class CommonProxy {
public void init(FMLInitializationEvent event) {
this.registerMovables();
FMLCommonHandler.instance().bus().register(SPacketMEUpdateBuffer.class);
}
public void postInit(FMLPostInitializationEvent event) {