Compare commits

...
Author SHA1 Message Date
Firenoo 708cdece79 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 a packet to be sent at the
next 50ms boundary. The packet has a limit of 256 items to prevent killing the
client; should be adjusted until a good balance between startup time (see below)
and client lag is reached.

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.
2023-03-21 08:36:45 -07:00
12 changed files with 334 additions and 115 deletions

No files matched your search

@@ -14,15 +14,13 @@ import com.glodblock.github.loader.ChannelLoader;
import com.glodblock.github.loader.ItemAndBlockHolder; import com.glodblock.github.loader.ItemAndBlockHolder;
import com.glodblock.github.loader.RecipeLoader; import com.glodblock.github.loader.RecipeLoader;
import com.glodblock.github.loader.filter.FluidFilter; import com.glodblock.github.loader.filter.FluidFilter;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.proxy.CommonProxy; import com.glodblock.github.proxy.CommonProxy;
import com.glodblock.github.util.ModAndClassUtil; import com.glodblock.github.util.ModAndClassUtil;
import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.*;
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.NetworkRegistry;
@Mod( @Mod(
@@ -85,6 +83,16 @@ public class FluidCraft {
proxy.postInit(event); proxy.postInit(event);
} }
@Mod.EventHandler
public void onServerStart(FMLServerStartingEvent event) {
SPacketMEUpdateBuffer.init();
}
@Mod.EventHandler
public void onServerStop(FMLServerStoppedEvent event) {
SPacketMEUpdateBuffer.disable();
}
@Mod.EventHandler @Mod.EventHandler
public void onLoadComplete(FMLLoadCompleteEvent event) {} public void onLoadComplete(FMLLoadCompleteEvent event) {}
@@ -25,13 +25,16 @@ public class GuiFluidMonitor extends FCGuiMonitor<IAEFluidStack> {
this.repo = new FluidRepo(getScrollBar(), this); this.repo = new FluidRepo(getScrollBar(), this);
} }
public void postUpdate(final List<IAEFluidStack> list) { @Override
public void postUpdate(final List<IAEFluidStack> list, boolean resort) {
for (final IAEFluidStack is : list) { for (final IAEFluidStack is : list) {
IAEItemStack stack = AEItemStack.create(ItemFluidDrop.newDisplayStack(is.getFluidStack())); IAEItemStack stack = AEItemStack.create(ItemFluidDrop.newDisplayStack(is.getFluidStack()));
stack.setStackSize(is.getStackSize()); stack.setStackSize(is.getStackSize());
this.repo.postUpdate(stack); this.repo.postUpdate(stack);
} }
this.repo.updateView(); if (resort) {
this.repo.updateView();
}
this.setScrollBar(); this.setScrollBar();
} }
@@ -22,11 +22,14 @@ public class GuiItemMonitor extends FCGuiMonitor<IAEItemStack> {
this.repo = new ItemRepo(getScrollBar(), this); this.repo = new ItemRepo(getScrollBar(), this);
} }
public void postUpdate(final List<IAEItemStack> list) { @Override
public void postUpdate(final List<IAEItemStack> list, boolean resort) {
for (final IAEItemStack is : list) { for (final IAEItemStack is : list) {
this.repo.postUpdate(is); this.repo.postUpdate(is);
} }
this.repo.updateView(); if (resort) {
this.repo.updateView();
}
this.setScrollBar(); this.setScrollBar();
} }
@@ -107,7 +107,7 @@ public abstract class FCGuiMonitor<T extends IAEStack<T>> extends FCBaseMEGui
offsetY = y; offsetY = y;
} }
public abstract void postUpdate(final List<T> list); public abstract void postUpdate(final List<T> list, boolean resort);
protected void setScrollBar() { protected void setScrollBar() {
this.getScrollBar().setTop(18).setLeft(175).setHeight(this.rows * 18 - 2); this.getScrollBar().setTop(18).setLeft(175).setHeight(this.rows * 18 - 2);
@@ -1,7 +1,8 @@
package com.glodblock.github.client.gui.container; package com.glodblock.github.client.gui.container;
import java.nio.BufferOverflowException; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@@ -43,7 +44,7 @@ import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.common.item.ItemFluidDrop; import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.network.CPacketFluidUpdate; import com.glodblock.github.network.CPacketFluidUpdate;
import com.glodblock.github.network.SPacketFluidUpdate; import com.glodblock.github.network.SPacketFluidUpdate;
import com.glodblock.github.network.SPacketMEInventoryUpdate; import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.util.Util; import com.glodblock.github.util.Util;
public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> { public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
@@ -126,46 +127,34 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
protected void processItemList() { protected void processItemList() {
if (!this.fluids.isEmpty()) { if (!this.fluids.isEmpty()) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList(); final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
final SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(true); List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack is : this.fluids) { for (final IAEFluidStack is : this.fluids) {
final IAEFluidStack send = monitorCache.findPrecise(is); final IAEFluidStack send = monitorCache.findPrecise(is);
if (send == null) { if (send != null) {
is.setStackSize(0); toSend.add(send);
piu.appendFluid(is);
} else { } else {
piu.appendFluid(send); is.setStackSize(0);
toSend.add(is);
} }
} }
for (final Object c : this.crafters) {
if (!piu.isEmpty()) { if (c instanceof EntityPlayerMP) {
this.fluids.resetStatus(); SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
for (final Object c : this.crafters) {
if (c instanceof EntityPlayer) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
}
} }
} }
this.fluids.resetStatus();
} }
} }
@Override @Override
protected void queueInventory(final ICrafting c) { protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) { if (Platform.isServer() && c instanceof EntityPlayerMP && this.monitor != null) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(true);
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList(); final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack send : monitorCache) { for (final IAEFluidStack is : monitorCache) {
try { toSend.add(is);
piu.appendFluid(send);
} catch (final BufferOverflowException boe) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
piu = new SPacketMEInventoryUpdate(true);
piu.appendFluid(send);
}
} }
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c); SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
} }
} }
@@ -1,6 +1,7 @@
package com.glodblock.github.client.gui.container; 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.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
@@ -25,9 +26,8 @@ import appeng.container.slot.SlotRestrictedInput;
import appeng.me.helpers.ChannelPowerSrc; import appeng.me.helpers.ChannelPowerSrc;
import appeng.util.Platform; import appeng.util.Platform;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.base.FCContainerMonitor; import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.network.SPacketMEInventoryUpdate; import com.glodblock.github.network.SPacketMEUpdateBuffer;
public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> { public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
@@ -95,46 +95,34 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
protected void processItemList() { protected void processItemList() {
if (!this.items.isEmpty()) { if (!this.items.isEmpty()) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList(); final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
final SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(); List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack is : this.items) { for (final IAEItemStack is : this.items) {
final IAEItemStack send = monitorCache.findPrecise(is); final IAEItemStack send = monitorCache.findPrecise(is);
if (send == null) { if (send != null) {
is.setStackSize(0); toSend.add(send);
piu.appendItem(is);
} else { } else {
piu.appendItem(send); is.setStackSize(0);
toSend.add(is);
} }
} }
for (final Object c : this.crafters) {
if (!piu.isEmpty()) { if (c instanceof EntityPlayer) {
this.items.resetStatus(); SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
for (final Object c : this.crafters) {
if (c instanceof EntityPlayer) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
}
} }
} }
this.items.resetStatus();
} }
} }
@Override @Override
protected void queueInventory(final ICrafting c) { protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) { if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate();
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList(); final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack send : monitorCache) { for (final IAEItemStack is : monitorCache) {
try { toSend.add(is);
piu.appendItem(send);
} catch (final BufferOverflowException boe) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
piu = new SPacketMEInventoryUpdate();
piu.appendItem(send);
}
} }
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c); SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
} }
} }
@@ -143,6 +131,9 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
super.removeCraftingFromCrafters(c); super.removeCraftingFromCrafters(c);
if (this.crafters.isEmpty() && this.monitor != null) { if (this.crafters.isEmpty() && this.monitor != null) {
this.monitor.removeListener(this); 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 appeng.util.Platform;
import com.glodblock.github.inventory.item.IWirelessTerminal; import com.glodblock.github.inventory.item.IWirelessTerminal;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseContainer public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseContainer
implements IConfigManagerHost, IConfigurableObject, IMEMonitorHandlerReceiver<T> { implements IConfigManagerHost, IConfigurableObject, IMEMonitorHandlerReceiver<T> {
@@ -158,6 +159,10 @@ public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseCo
super.onContainerClosed(player); super.onContainerClosed(player);
if (this.monitor != null) { if (this.monitor != null) {
this.monitor.removeListener(this); this.monitor.removeListener(this);
if (player instanceof EntityPlayerMP && Platform.isServer()) {
SPacketMEUpdateBuffer.clear((EntityPlayerMP) player);
}
} }
} }
@@ -25,9 +25,11 @@ public class ChannelLoader implements Runnable {
Side.SERVER); Side.SERVER);
FluidCraft.proxy.netHandler FluidCraft.proxy.netHandler
.registerMessage(new CPacketEncodePattern.Handler(), CPacketEncodePattern.class, id++, Side.SERVER); .registerMessage(new CPacketEncodePattern.Handler(), CPacketEncodePattern.class, id++, Side.SERVER);
FluidCraft.proxy.netHandler
.registerMessage(new SPacketMEItemInvUpdate.Handler(), SPacketMEItemInvUpdate.class, id++, Side.CLIENT);
FluidCraft.proxy.netHandler.registerMessage( FluidCraft.proxy.netHandler.registerMessage(
new SPacketMEInventoryUpdate.Handler(), new SPacketMEFluidInvUpdate.Handler(),
SPacketMEInventoryUpdate.class, SPacketMEFluidInvUpdate.class,
id++, id++,
Side.CLIENT); Side.CLIENT);
FluidCraft.proxy.netHandler FluidCraft.proxy.netHandler
@@ -1,5 +1,8 @@
package com.glodblock.github.network; package com.glodblock.github.network;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
@@ -9,7 +12,6 @@ import net.minecraft.nbt.NBTTagCompound;
import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack; import appeng.util.item.AEItemStack;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.ContainerLevelMaintainer; import com.glodblock.github.client.gui.container.ContainerLevelMaintainer;
import com.glodblock.github.common.tile.TileLevelMaintainer; import com.glodblock.github.common.tile.TileLevelMaintainer;
@@ -88,7 +90,7 @@ public class CPacketLevelMaintainer implements IMessage {
public static class Handler implements IMessageHandler<CPacketLevelMaintainer, IMessage> { public static class Handler implements IMessageHandler<CPacketLevelMaintainer, IMessage> {
private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) { private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(false); List<IAEItemStack> toSend = new ArrayList<>(TileLevelMaintainer.REQ_COUNT);
for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) { for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) {
IAEItemStack is = cca.getTile().requests.getRequestQtyStack(i); IAEItemStack is = cca.getTile().requests.getRequestQtyStack(i);
IAEItemStack is1 = cca.getTile().requests.getRequestBatches().getStack(i); IAEItemStack is1 = cca.getTile().requests.getRequestBatches().getStack(i);
@@ -96,7 +98,7 @@ public class CPacketLevelMaintainer implements IMessage {
if (is1 != null) { if (is1 != null) {
NBTTagCompound data; NBTTagCompound data;
data = is1.getItemStack().getTagCompound(); data = is1.getItemStack().getTagCompound();
piu.appendItem( toSend.add(
setTag( setTag(
is, is,
is1.getStackSize(), is1.getStackSize(),
@@ -104,11 +106,11 @@ public class CPacketLevelMaintainer implements IMessage {
data.getBoolean("Enable"), data.getBoolean("Enable"),
cca.getTile().requests.getState(i).ordinal())); cca.getTile().requests.getState(i).ordinal()));
} else { } else {
piu.appendItem(setTag(is, 0, i, true, 0)); toSend.add(setTag(is, 0, i, true, 0));
} }
} }
} }
FluidCraft.proxy.netHandler.sendTo(piu, player); SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend);
} }
@Nullable @Nullable
@@ -2,62 +2,60 @@ package com.glodblock.github.network;
import java.io.IOException; import java.io.IOException;
import java.nio.BufferOverflowException; import java.nio.BufferOverflowException;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiScreen;
import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEFluidStack; import appeng.util.item.AEFluidStack;
import appeng.util.item.AEItemStack;
import com.glodblock.github.client.gui.GuiFluidCraftConfirm; import com.glodblock.github.client.gui.GuiFluidCraftConfirm;
import com.glodblock.github.client.gui.GuiFluidMonitor; import com.glodblock.github.client.gui.GuiFluidMonitor;
import com.glodblock.github.client.gui.GuiItemMonitor;
import com.glodblock.github.client.gui.GuiLevelMaintainer;
import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext; import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
public class SPacketMEInventoryUpdate implements IMessage { /**
* Dedicated packet for fluid updates.
*/
public class SPacketMEFluidInvUpdate implements IMessage {
private List<Object> list; private List<IAEFluidStack> list = new ArrayList<>();
private byte ref; private byte ref = (byte) 0;
private boolean isFluid = false; private boolean resort = true;
public SPacketMEInventoryUpdate() { public SPacketMEFluidInvUpdate() {}
ref = 0;
list = new LinkedList<>();
}
public SPacketMEInventoryUpdate(Boolean isFluid) { /**
ref = 0; * Used for the GUI to confirm crafting. 0 = available 1 = pending 2 = missing
list = new LinkedList<>(); */
this.isFluid = isFluid; public SPacketMEFluidInvUpdate(byte b) {
}
public SPacketMEInventoryUpdate(byte b) {
ref = b; ref = b;
list = new LinkedList<>(); }
/**
* If resort, call "updateView()". Used when multiple packets are sent to open an inventory; only the last packet
* should resort.
*
* @param resort whether this packet should resort the term or not
*/
public SPacketMEFluidInvUpdate(boolean resort) {
this.resort = resort;
} }
@Override @Override
public void fromBytes(ByteBuf buf) { public void fromBytes(ByteBuf buf) {
isFluid = buf.readBoolean();
long amount = buf.readLong();
ref = buf.readByte(); ref = buf.readByte();
list = new LinkedList<>(); resort = buf.readBoolean();
long amount = buf.readLong();
list = new ArrayList<>();
try { try {
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
if (isFluid) { list.add(AEFluidStack.loadFluidStackFromPacket(buf));
list.add(AEFluidStack.loadFluidStackFromPacket(buf));
} else {
list.add(AEItemStack.loadItemStackFromPacket(buf));
}
} }
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(); io.printStackTrace();
@@ -66,21 +64,20 @@ public class SPacketMEInventoryUpdate implements IMessage {
@Override @Override
public void toBytes(ByteBuf buf) { public void toBytes(ByteBuf buf) {
buf.writeBoolean(isFluid);
buf.writeLong(list.size());
buf.writeByte(ref); buf.writeByte(ref);
buf.writeBoolean(resort);
buf.writeLong(list.size());
try { try {
for (Object is : list) { for (IAEFluidStack is : list) {
if (is instanceof IAEItemStack) ((IAEItemStack) is).writeToPacket(buf); is.writeToPacket(buf);
if (is instanceof IAEFluidStack) ((IAEFluidStack) is).writeToPacket(buf);
} }
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(); io.printStackTrace();
} }
} }
public void appendItem(final IAEItemStack is) throws BufferOverflowException { public void setResort(boolean resort) {
list.add(is); this.resort = resort;
} }
public void appendFluid(final IAEFluidStack is) throws BufferOverflowException { public void appendFluid(final IAEFluidStack is) throws BufferOverflowException {
@@ -91,20 +88,16 @@ public class SPacketMEInventoryUpdate implements IMessage {
return this.list.isEmpty(); return this.list.isEmpty();
} }
public static class Handler implements IMessageHandler<SPacketMEInventoryUpdate, IMessage> { public static class Handler implements IMessageHandler<SPacketMEFluidInvUpdate, IMessage> {
@Override @Override
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public IMessage onMessage(SPacketMEInventoryUpdate message, MessageContext ctx) { public IMessage onMessage(SPacketMEFluidInvUpdate message, MessageContext ctx) {
final GuiScreen gs = Minecraft.getMinecraft().currentScreen; final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiFluidMonitor) { if (gs instanceof GuiFluidMonitor) {
((GuiFluidMonitor) gs).postUpdate((List) message.list); ((GuiFluidMonitor) gs).postUpdate(message.list, message.resort);
} else if (gs instanceof GuiItemMonitor) {
((GuiItemMonitor) gs).postUpdate((List) message.list);
} else if (gs instanceof GuiFluidCraftConfirm) { } else if (gs instanceof GuiFluidCraftConfirm) {
((GuiFluidCraftConfirm) gs).postUpdate((List) message.list, message.ref); ((GuiFluidCraftConfirm) gs).postUpdate((List) message.list, message.ref);
} else if (gs instanceof GuiLevelMaintainer) {
((GuiLevelMaintainer) gs).postUpdate((List) message.list);
} }
return null; return null;
} }
@@ -0,0 +1,109 @@
package com.glodblock.github.network;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import appeng.api.storage.data.IAEItemStack;
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 cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
/**
* Packet dedicated to item inventory update.
*/
public class SPacketMEItemInvUpdate implements IMessage {
private List<IAEItemStack> list = new ArrayList<>();
private byte ref = (byte) 0;
private boolean resort = true;
public SPacketMEItemInvUpdate() {}
/**
* Used for the GUI to confirm crafting. 0 = available 1 = pending 2 = missing
*/
public SPacketMEItemInvUpdate(byte b) {
ref = b;
list = new ArrayList<>();
}
/**
* If resort, call "updateView()". Used when multiple packets are sent to open an inventory; only the last packet
* should resort.
*
* @param resort whether this packet should resort the term or not
*/
public SPacketMEItemInvUpdate(boolean resort) {
this.resort = resort;
}
@Override
public void fromBytes(ByteBuf buf) {
ref = buf.readByte();
resort = buf.readBoolean();
long amount = buf.readLong();
list = new ArrayList<>();
try {
for (int i = 0; i < amount; i++) {
list.add(AEItemStack.loadItemStackFromPacket(buf));
}
} catch (IOException io) {
io.printStackTrace();
}
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeByte(ref);
buf.writeBoolean(resort);
buf.writeLong(list.size());
try {
for (IAEItemStack is : list) {
is.writeToPacket(buf);
}
} catch (IOException io) {
io.printStackTrace();
}
}
public void setResort(boolean resort) {
this.resort = resort;
}
public void appendItem(final IAEItemStack is) throws BufferOverflowException {
list.add(is);
}
public boolean isEmpty() {
return this.list.isEmpty();
}
public static class Handler implements IMessageHandler<SPacketMEItemInvUpdate, IMessage> {
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public IMessage onMessage(SPacketMEItemInvUpdate message, MessageContext ctx) {
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiItemMonitor) {
((GuiItemMonitor) gs).postUpdate(message.list, message.resort);
} else if (gs instanceof GuiFluidCraftConfirm) {
((GuiFluidCraftConfirm) gs).postUpdate(message.list, message.ref);
} else if (gs instanceof GuiLevelMaintainer) {
((GuiLevelMaintainer) gs).postUpdate(message.list);
}
return null;
}
}
}
@@ -0,0 +1,114 @@
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;
/**
* Only should exist on the server side!
*/
public class SPacketMEUpdateBuffer {
private static ScheduledExecutorService executor;
private static ScheduledFuture<?> task;
private static final Map<EntityPlayerMP, LinkedHashMap<IAEItemStack, IAEItemStack>> itemBuffer = new HashMap<>();
private static final Map<EntityPlayerMP, Map<IAEFluidStack, 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, 50, 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 LinkedHashMap<>(1024));
}
Map<IAEItemStack, IAEItemStack> buffer = itemBuffer.get(player);
stacks.forEach(s -> buffer.put(s, s));
}
}
public static void scheduleFluidUpdate(EntityPlayerMP player, List<IAEFluidStack> stacks) {
synchronized (itemBuffer) {
if (!fluidBuffer.containsKey(player)) {
fluidBuffer.put(player, new LinkedHashMap<>(1024));
}
Map<IAEFluidStack, IAEFluidStack> buffer = fluidBuffer.get(player);
stacks.forEach(s -> buffer.put(s, s));
}
}
public static void sendBuffer() {
synchronized (itemBuffer) {
itemBuffer.forEach((player, updates) -> {
if (updates.isEmpty()) return;
int i = 0;
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
Iterator<IAEItemStack> it = updates.keySet().iterator();
while (it.hasNext()) {
if (i < 256) {
packet.appendItem(it.next());
it.remove();
} else {
packet.setResort(false);
break;
}
i++;
}
FluidCraft.proxy.netHandler.sendTo(packet, player);
});
fluidBuffer.forEach((player, updates) -> {
if (updates.isEmpty()) return;
int i = 0;
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
Iterator<IAEFluidStack> it = updates.keySet().iterator();
while (it.hasNext()) {
if (i < 256) {
packet.appendFluid(it.next());
it.remove();
} else {
packet.setResort(false);
break;
}
i++;
}
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();
}
}
}
}