mirror of
https://github.com/GTNewHorizons/AE2FluidCraft-Rework.git
synced 2026-11-16 01:54:59 +00:00
Add LevelMaintainerTerminal rework logic inside TileLevelMaintainer (#162)
* Use new interface terminal API * Update dependencies.gradle * feat: Update core hooks and integrations * api: Add `LevelTerminalRegistry` functionality * feat: Use `customName` to display in `GuiFluidLevelEmitter` * feat: Rework scheduling and data storage in `TileLevelMaintainer` functionality * feat: Add new item `LevelTerminal` * feat: Support back navigation functionality * feat: add back navigation in `LevelMaintainer` from terminal * use `IGuiTooltipHandler` directly from ae2 include #160 * fix correct `InterfaceTerminalSupportedClassProvider` deprecation * Implement new version of `InterfaceWireless` based on `InterfaceTerminal` * Fix `LevelTerminal` data recursion and few in fixes gui * Use `aeRenderItem` directly and fix `zIndex` in custom slots * Fix rendering numbers bigger than 2.1G through `aeRenderItem` * Fix fails on `getCraftingItem` no root cause was found * Fix add packet logging and try to not fail after receive corrupted packet * Fix add logging around empty configuration * Fix packet corruption and log invalid data * Fix packet corruption with level emitters * Correct handle overwrite packet * Fix intercept dnd click from GT5 hook for `AppEngGuiHandler` keyword `net.bdew.neiaddons.appeng.AppEngGuiHandler` * update be+deps * Update dependencies.gradle * Update dependencies.gradle * Fix `zIndexes` for compatability with `DuraDisplay` * Update AE2 and fix compatability * Change `ForgeDirection` side serialization to `byte` * Fix tooltip zIndexes * Rename all rest parts related to `Level terminal` * Code cleanup after review * Remove duplicated dependency declaration --------- Co-authored-by: Firenoo <[email protected]> Co-authored-by: Martin Robertz <[email protected]>
This commit is contained in:
80 files changed
+6169
-1785
No files matched your search
@@ -6,14 +6,11 @@ import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.glodblock.github.client.gui.container.ContainerLevelMaintainer;
|
||||
import com.glodblock.github.common.tile.TileLevelMaintainer;
|
||||
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
@@ -21,111 +18,101 @@ import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class CPacketLevelMaintainer implements IMessage {
|
||||
|
||||
private String action;
|
||||
public enum Action {
|
||||
Refresh,
|
||||
Quantity,
|
||||
Batch,
|
||||
Enable,
|
||||
Disable
|
||||
}
|
||||
|
||||
private Action action;
|
||||
private long size;
|
||||
private int slotIndex;
|
||||
|
||||
public CPacketLevelMaintainer() {}
|
||||
|
||||
public CPacketLevelMaintainer(String action, int slotIndex) {
|
||||
this.action = action;
|
||||
this.size = 0;
|
||||
this.slotIndex = slotIndex;
|
||||
}
|
||||
|
||||
public CPacketLevelMaintainer(String action) {
|
||||
this.action = action;
|
||||
this.size = 0;
|
||||
public CPacketLevelMaintainer() {
|
||||
this.action = Action.Refresh;
|
||||
this.slotIndex = 0;
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
public CPacketLevelMaintainer(String action, int slotIndex, long size) {
|
||||
public CPacketLevelMaintainer(Action action) {
|
||||
this.action = action;
|
||||
this.size = size;
|
||||
this.slotIndex = slotIndex;
|
||||
this.slotIndex = 0;
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
public CPacketLevelMaintainer(String action, int slotIndex, String size) {
|
||||
public CPacketLevelMaintainer(Action action, int slotIndex) {
|
||||
this.action = action;
|
||||
this.slotIndex = slotIndex;
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
public CPacketLevelMaintainer(Action action, int slotIndex, long size) {
|
||||
this.action = action;
|
||||
this.slotIndex = slotIndex;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public CPacketLevelMaintainer(Action action, int slotIndex, String size) {
|
||||
this.action = action;
|
||||
this.slotIndex = slotIndex;
|
||||
this.size = size.isEmpty() ? 0 : Long.parseLong(size);
|
||||
}
|
||||
|
||||
public static IAEItemStack setTag(IAEItemStack ias, long batch, int slotIndex, boolean enable, int state) {
|
||||
NBTTagCompound data = new NBTTagCompound();
|
||||
ItemStack is = ias.getItemStack();
|
||||
data.setLong("Batch", batch);
|
||||
data.setLong("Index", slotIndex);
|
||||
data.setBoolean("Enable", enable);
|
||||
data.setInteger("State", state);
|
||||
is.setTagCompound(data);
|
||||
IAEItemStack iaeItemStack = AEItemStack.create(is);
|
||||
iaeItemStack.setStackSize(ias.getStackSize());
|
||||
return iaeItemStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
int leAction = buf.readInt();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < leAction; i++) {
|
||||
sb.append(buf.readChar());
|
||||
}
|
||||
this.action = sb.toString();
|
||||
this.action = Action.values()[buf.readInt()];
|
||||
this.slotIndex = buf.readInt();
|
||||
this.size = buf.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(this.action.length());
|
||||
for (int i = 0; i < this.action.length(); i++) {
|
||||
buf.writeChar(this.action.charAt(i));
|
||||
}
|
||||
buf.writeInt(this.action.ordinal());
|
||||
buf.writeInt(this.slotIndex);
|
||||
buf.writeLong(this.size);
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<CPacketLevelMaintainer, IMessage> {
|
||||
|
||||
private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) {
|
||||
private List<IAEItemStack> refresh(ContainerLevelMaintainer cca) {
|
||||
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);
|
||||
if (is != null) {
|
||||
if (is1 != null) {
|
||||
NBTTagCompound data;
|
||||
data = is1.getItemStack().getTagCompound();
|
||||
toSend.add(
|
||||
setTag(
|
||||
is,
|
||||
is1.getStackSize(),
|
||||
i,
|
||||
data.getBoolean("Enable"),
|
||||
cca.getTile().requests.getState(i).ordinal()));
|
||||
} else {
|
||||
toSend.add(setTag(is, 0, i, true, 0));
|
||||
}
|
||||
IAEItemStack ias = cca.getTile().requests.getAEItemStack(i);
|
||||
if (ias != null) {
|
||||
toSend.add(ias);
|
||||
}
|
||||
}
|
||||
SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend);
|
||||
return toSend;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IMessage onMessage(CPacketLevelMaintainer message, MessageContext ctx) {
|
||||
if (message.action.startsWith("TileLevelMaintainer.")
|
||||
&& ctx.getServerHandler().playerEntity.openContainer instanceof final ContainerLevelMaintainer cca) {
|
||||
if (ctx.getServerHandler().playerEntity.openContainer instanceof final ContainerLevelMaintainer clm) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
|
||||
switch (message.action) {
|
||||
case "TileLevelMaintainer.Quantity" -> cca.getTile()
|
||||
.updateQuantity(message.slotIndex, message.size);
|
||||
case "TileLevelMaintainer.Batch" -> cca.getTile().updateBatchSize(message.slotIndex, message.size);
|
||||
case "TileLevelMaintainer.Enable" -> cca.getTile().setRequestStatus(message.slotIndex, false);
|
||||
case "TileLevelMaintainer.Disable" -> cca.getTile().setRequestStatus(message.slotIndex, true);
|
||||
}
|
||||
this.refresh(cca, player);
|
||||
List<IAEItemStack> toSend = switch (message.action) {
|
||||
case Quantity -> {
|
||||
clm.getTile().updateQuantity(message.slotIndex, message.size);
|
||||
yield this.refresh(clm);
|
||||
}
|
||||
case Batch -> {
|
||||
clm.getTile().updateBatchSize(message.slotIndex, message.size);
|
||||
yield this.refresh(clm);
|
||||
}
|
||||
case Enable -> {
|
||||
clm.getTile().updateStatus(message.slotIndex, false);
|
||||
yield this.refresh(clm);
|
||||
}
|
||||
case Disable -> {
|
||||
clm.getTile().updateStatus(message.slotIndex, true);
|
||||
yield this.refresh(clm);
|
||||
}
|
||||
case Refresh -> this.refresh(clm);
|
||||
};
|
||||
|
||||
SPacketMEUpdateBuffer.scheduleItemUpdate(player, toSend);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.glodblock.github.network;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.DimensionManager;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import com.glodblock.github.client.gui.GuiLevelMaintainer;
|
||||
import com.glodblock.github.inventory.InventoryHandler;
|
||||
import com.glodblock.github.inventory.gui.GuiType;
|
||||
import com.glodblock.github.inventory.item.IClickableInTerminal;
|
||||
import com.glodblock.github.util.BlockPos;
|
||||
import com.glodblock.github.util.Util;
|
||||
|
||||
import appeng.container.AEBaseContainer;
|
||||
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;
|
||||
|
||||
public class CPacketLevelTerminalCommands implements IMessage {
|
||||
|
||||
private Action action;
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
private int dim;
|
||||
private ForgeDirection side;
|
||||
|
||||
public enum Action {
|
||||
EDIT,
|
||||
BACK,
|
||||
ENABLE,
|
||||
DISABLE,
|
||||
ENABLE_ALL,
|
||||
DISABLE_ALL,
|
||||
}
|
||||
|
||||
public CPacketLevelTerminalCommands() {}
|
||||
|
||||
public CPacketLevelTerminalCommands(Action action, int x, int y, int z, int dim, ForgeDirection side) {
|
||||
this.action = action;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.dim = dim;
|
||||
this.side = side;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
action = Action.values()[buf.readInt()];
|
||||
switch (action) {
|
||||
case EDIT, BACK -> {
|
||||
x = buf.readInt();
|
||||
y = buf.readInt();
|
||||
z = buf.readInt();
|
||||
dim = buf.readInt();
|
||||
side = ForgeDirection.getOrientation(buf.readInt());
|
||||
}
|
||||
case ENABLE -> {}
|
||||
case DISABLE -> {}
|
||||
case ENABLE_ALL -> {}
|
||||
case DISABLE_ALL -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeInt(action.ordinal());
|
||||
switch (action) {
|
||||
case EDIT, BACK -> {
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
buf.writeInt(dim);
|
||||
buf.writeInt(side.ordinal());
|
||||
}
|
||||
case ENABLE -> {}
|
||||
case DISABLE -> {}
|
||||
case ENABLE_ALL -> {}
|
||||
case DISABLE_ALL -> {}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<CPacketLevelTerminalCommands, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(CPacketLevelTerminalCommands message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
|
||||
AEBaseContainer con = (AEBaseContainer) player.openContainer;
|
||||
switch (message.action) {
|
||||
case EDIT -> {
|
||||
TileEntity tile = DimensionManager.getWorld(message.dim)
|
||||
.getTileEntity(message.x, message.y, message.z);
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(tile),
|
||||
message.side,
|
||||
GuiType.LEVEL_MAINTAINER);
|
||||
}
|
||||
case BACK -> {
|
||||
GuiType originalGui = null;
|
||||
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
|
||||
if (gs instanceof GuiLevelMaintainer guiLevelMaintainer) {
|
||||
originalGui = guiLevelMaintainer.getOriginalGui();
|
||||
}
|
||||
if (originalGui == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(message.x, message.y, message.z, DimensionManager.getWorld(message.dim)),
|
||||
message.side,
|
||||
originalGui);
|
||||
}
|
||||
case ENABLE -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
}
|
||||
}
|
||||
case DISABLE -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
}
|
||||
}
|
||||
case ENABLE_ALL -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
}
|
||||
}
|
||||
case DISABLE_ALL -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,22 @@
|
||||
package com.glodblock.github.network;
|
||||
|
||||
import static com.glodblock.github.common.item.ItemWirelessUltraTerminal.getGuis;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.DimensionManager;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import com.glodblock.github.FluidCraft;
|
||||
import com.glodblock.github.client.gui.GuiRenamer;
|
||||
import com.glodblock.github.inventory.InventoryHandler;
|
||||
import com.glodblock.github.inventory.gui.GuiType;
|
||||
import com.glodblock.github.inventory.item.IWirelessInterfaceTerminal;
|
||||
import com.glodblock.github.inventory.item.IClickableInTerminal;
|
||||
import com.glodblock.github.inventory.item.IWirelessTerminal;
|
||||
import com.glodblock.github.util.BlockPos;
|
||||
import com.glodblock.github.util.Util;
|
||||
|
||||
import appeng.api.implementations.IUpgradeableHost;
|
||||
import appeng.container.AEBaseContainer;
|
||||
import appeng.helpers.ICustomNameObject;
|
||||
import appeng.tile.networking.TileCableBus;
|
||||
@@ -121,58 +124,70 @@ public class CPacketRenamer implements IMessage {
|
||||
public IMessage onMessage(CPacketRenamer message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
|
||||
AEBaseContainer con = (AEBaseContainer) player.openContainer;
|
||||
TileEntity tile;
|
||||
if (message.action == Action.OPEN) {
|
||||
tile = DimensionManager.getWorld(message.dim).getTileEntity(message.x, message.y, message.z);
|
||||
if (tile != null && con.getTarget() instanceof IWirelessInterfaceTerminal
|
||||
&& tile instanceof ICustomNameObject) {
|
||||
String name = getName(tile, message.side);
|
||||
((IWirelessInterfaceTerminal) con.getTarget()).setClickedInterface(
|
||||
new Util.DimensionalCoordSide(
|
||||
message.x,
|
||||
message.y,
|
||||
message.z,
|
||||
message.dim,
|
||||
switch (message.action) {
|
||||
case OPEN -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
TileEntity tile = DimensionManager.getWorld(message.dim)
|
||||
.getTileEntity(message.x, message.y, message.z);
|
||||
if (!(tile instanceof ICustomNameObject)) {
|
||||
break;
|
||||
}
|
||||
|
||||
String name = getName(tile, message.side);
|
||||
clickableInterface.setClickedInterface(
|
||||
new Util.DimensionalCoordSide(
|
||||
message.x,
|
||||
message.y,
|
||||
message.z,
|
||||
message.dim,
|
||||
message.side,
|
||||
name));
|
||||
|
||||
if (con.getTarget() instanceof IWirelessTerminal terminal) {
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(
|
||||
terminal.getInventorySlot(),
|
||||
Util.GuiHelper.encodeType(0, Util.GuiHelper.GuiType.ITEM),
|
||||
1),
|
||||
message.side,
|
||||
name));
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(
|
||||
((IWirelessInterfaceTerminal) con.getTarget()).getInventorySlot(),
|
||||
Util.GuiHelper.encodeType(0, Util.GuiHelper.GuiType.ITEM),
|
||||
1),
|
||||
message.side,
|
||||
GuiType.RENAMER);
|
||||
GuiType.RENAMER);
|
||||
} else if (con.getTarget() instanceof IUpgradeableHost host) {
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(host.getTile()),
|
||||
con.getOpenContext().getSide(),
|
||||
GuiType.RENAMER);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (con.getTarget() instanceof IWirelessInterfaceTerminal) {
|
||||
Util.DimensionalCoordSide intMsg = ((IWirelessInterfaceTerminal) con.getTarget())
|
||||
.getClickedInterface();
|
||||
tile = DimensionManager.getWorld(intMsg.getDimension()).getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
if (message.action == Action.GET_TEXT) {
|
||||
case GET_TEXT -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
FluidCraft.proxy.netHandler
|
||||
.sendTo(new SPacketStringUpdate(this.getName(tile, intMsg.getSide())), player);
|
||||
} else {
|
||||
}
|
||||
}
|
||||
case SET_TEXT -> {
|
||||
if (con.getTarget() instanceof IClickableInTerminal clickableInterface) {
|
||||
Util.DimensionalCoordSide intMsg = clickableInterface.getClickedInterface();
|
||||
TileEntity tile = DimensionManager.getWorld(intMsg.getDimension())
|
||||
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
|
||||
this.setName(tile, intMsg.getSide(), message.text);
|
||||
InventoryHandler.openGui(
|
||||
player,
|
||||
player.worldObj,
|
||||
new BlockPos(
|
||||
((IWirelessInterfaceTerminal) con.getTarget()).getInventorySlot(),
|
||||
Util.GuiHelper.encodeType(
|
||||
getGuis().indexOf(
|
||||
GuiType.valueOf(
|
||||
GuiType.WIRELESS_INTERFACE_TERMINAL.toString())),
|
||||
Util.GuiHelper.GuiType.ITEM),
|
||||
0),
|
||||
ForgeDirection.UNKNOWN,
|
||||
GuiType.WIRELESS_INTERFACE_TERMINAL);
|
||||
|
||||
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
|
||||
if (gs instanceof GuiRenamer guiRenamer) {
|
||||
guiRenamer.switchGui();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,585 @@
|
||||
package com.glodblock.github.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import com.glodblock.github.client.gui.GuiLevelTerminal;
|
||||
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.features.AEFeature;
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
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;
|
||||
import io.netty.buffer.ByteBufInputStream;
|
||||
import io.netty.buffer.ByteBufOutputStream;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
/**
|
||||
* Packet used for level terminal updates. Packet allows the server to send an array of command packets, which are then
|
||||
* processed in order. This allows for chaining commands to produce the desired update.
|
||||
*/
|
||||
public class SPacketLevelTerminalUpdate implements IMessage {
|
||||
|
||||
public static final int CLEAR_ALL_BIT = 1;
|
||||
public static final int DISCONNECT_BIT = 2;
|
||||
|
||||
private final List<PacketEntry> commands = new ArrayList<>();
|
||||
private int statusFlags;
|
||||
|
||||
public SPacketLevelTerminalUpdate() {}
|
||||
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
this.statusFlags = buf.readByte();
|
||||
int numEntries = buf.readInt();
|
||||
|
||||
for (int i = 0; i < numEntries; ++i) {
|
||||
try {
|
||||
PacketType type = PacketType.values()[buf.readByte()];
|
||||
|
||||
switch (type) {
|
||||
case ADD -> this.commands.add(new PacketAdd(buf));
|
||||
case REMOVE -> this.commands.add(new PacketRemove(buf));
|
||||
case OVERWRITE -> this.commands.add(new PacketOverwrite(buf));
|
||||
case RENAME -> this.commands.add(new PacketRename(buf));
|
||||
default -> throw new IOException("Unknown packet type received of index " + type);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(
|
||||
"Corrupted packet commands: (" + i
|
||||
+ ") of ("
|
||||
+ numEntries
|
||||
+ ") -> "
|
||||
+ this.commands.size()
|
||||
+ " : "
|
||||
+ this.commands.stream().map(packetEntry -> packetEntry.getClass().getSimpleName())
|
||||
.collect(Collectors.groupingBy(String::new, Collectors.counting())));
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.DebugLogging)) {
|
||||
AELog.info(" <- Parsed content: " + this.commands);
|
||||
}
|
||||
}
|
||||
AELog.debug(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(
|
||||
" <- Received commands " + this.commands.size()
|
||||
+ " : "
|
||||
+ this.commands.stream().map(packetEntry -> packetEntry.getClass().getSimpleName())
|
||||
.collect(Collectors.groupingBy(String::new, Collectors.counting())));
|
||||
}
|
||||
}
|
||||
|
||||
public void toBytes(ByteBuf buf) {
|
||||
try {
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(
|
||||
" <- Sent commands " + this.commands.size()
|
||||
+ " : "
|
||||
+ this.commands.stream().map(packetEntry -> packetEntry.getClass().getSimpleName())
|
||||
.collect(Collectors.groupingBy(String::new, Collectors.counting())));
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.DebugLogging)) {
|
||||
AELog.info(" -> Sent commands: " + this.commands);
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeByte(statusFlags);
|
||||
buf.writeInt(commands.size());
|
||||
for (PacketEntry entry : commands) {
|
||||
entry.write(buf);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all entries on the terminal. This is done BEFORE any entries are processed, so you can set this to clear
|
||||
* old entries, and add new ones after in one packet.
|
||||
*/
|
||||
public void setClear() {
|
||||
statusFlags |= CLEAR_ALL_BIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* The terminal disconnected. Entries are still processed, but indicates for the GUI to darken/turn off the
|
||||
* terminal. No further updates will arrive until the terminal reconnects.
|
||||
*/
|
||||
public void setDisconnect() {
|
||||
this.statusFlags |= DISCONNECT_BIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry. Fill out the rest of the command using the {@link PacketAdd#setItems(int, int, NBTTagList)} and
|
||||
* {@link PacketAdd#setLocation(int, int, int, int, int)}.
|
||||
*
|
||||
* @return the packet, which needs to have information filled out.
|
||||
*/
|
||||
public PacketAdd addNewEntry(long id, String name, boolean online) {
|
||||
PacketAdd packet = new PacketAdd(id, name, online);
|
||||
|
||||
commands.add(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the entry with the id from the terminal.
|
||||
*/
|
||||
public void addRemovalEntry(long id) {
|
||||
commands.add(new PacketRemove(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename the entry
|
||||
*/
|
||||
public void addRenamedEntry(long id, String newName) {
|
||||
commands.add(new PacketRename(id, newName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite the entry with new items or new status
|
||||
*/
|
||||
public PacketOverwrite addOverwriteEntry(long id) {
|
||||
PacketOverwrite packet = new PacketOverwrite(id);
|
||||
|
||||
commands.add(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<SPacketLevelTerminalUpdate, IMessage> {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IMessage onMessage(SPacketLevelTerminalUpdate message, MessageContext ctx) {
|
||||
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
|
||||
|
||||
if (gs instanceof GuiLevelTerminal levelTerminal) {
|
||||
levelTerminal.postUpdate(message.commands, message.statusFlags);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
enum PacketType {
|
||||
ADD,
|
||||
REMOVE,
|
||||
OVERWRITE,
|
||||
RENAME,
|
||||
}
|
||||
|
||||
/**
|
||||
* A packet for updating an entry.
|
||||
*/
|
||||
public abstract static class PacketEntry {
|
||||
|
||||
public final long entryId;
|
||||
|
||||
protected PacketEntry(long entryId) {
|
||||
this.entryId = entryId;
|
||||
}
|
||||
|
||||
protected PacketEntry(ByteBuf buf) throws IOException {
|
||||
this.entryId = buf.readLong();
|
||||
read(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Needs to write the packet id.
|
||||
*/
|
||||
protected abstract void write(ByteBuf buf) throws IOException;
|
||||
|
||||
/**
|
||||
* Reading the entry id is not needed.
|
||||
*/
|
||||
protected abstract void read(ByteBuf buf) throws IOException;
|
||||
}
|
||||
|
||||
/**
|
||||
* A command for sending a new entry.
|
||||
*/
|
||||
public static class PacketAdd extends PacketEntry {
|
||||
|
||||
public String name;
|
||||
public int x, y, z, dim, side;
|
||||
public int rows, rowSize;
|
||||
public boolean online;
|
||||
public ItemStack selfItemStack;
|
||||
public ItemStack displayItemStack;
|
||||
public NBTTagList items;
|
||||
|
||||
PacketAdd(long id, String name, boolean online) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.online = online;
|
||||
}
|
||||
|
||||
PacketAdd(ByteBuf buf) throws IOException {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
public PacketAdd setLocation(int x, int y, int z, int dim, int side) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.dim = dim;
|
||||
this.side = side;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PacketAdd setItems(int rows, int rowSize, NBTTagList items) {
|
||||
this.rows = rows;
|
||||
this.rowSize = rowSize;
|
||||
this.items = items;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public PacketAdd setViewItemStack(ItemStack selfItemStack, ItemStack displayItemStack) {
|
||||
this.selfItemStack = selfItemStack;
|
||||
this.displayItemStack = displayItemStack;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void write(ByteBuf buf) throws IOException {
|
||||
buf.writeByte(PacketType.ADD.ordinal());
|
||||
buf.writeLong(entryId);
|
||||
ByteBufUtils.writeUTF8String(buf, this.name);
|
||||
buf.writeInt(x);
|
||||
buf.writeInt(y);
|
||||
buf.writeInt(z);
|
||||
buf.writeInt(dim);
|
||||
buf.writeByte(side);
|
||||
buf.writeInt(rows);
|
||||
buf.writeInt(rowSize);
|
||||
|
||||
ByteBuf tempBuf = Unpooled.directBuffer(256);
|
||||
try {
|
||||
try (ByteBufOutputStream stream = new ByteBufOutputStream(tempBuf)) {
|
||||
|
||||
NBTTagCompound wrapper = new NBTTagCompound();
|
||||
|
||||
if (selfItemStack != null) {
|
||||
wrapper.setTag("self", selfItemStack.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
if (displayItemStack != null) {
|
||||
wrapper.setTag("display", displayItemStack.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
wrapper.setTag("data", items);
|
||||
CompressedStreamTools.writeCompressed(wrapper, stream);
|
||||
}
|
||||
buf.writeInt(tempBuf.readableBytes());
|
||||
buf.writeBytes(tempBuf);
|
||||
} finally {
|
||||
tempBuf.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void read(ByteBuf buf) throws IOException {
|
||||
this.name = ByteBufUtils.readUTF8String(buf);
|
||||
this.x = buf.readInt();
|
||||
this.y = buf.readInt();
|
||||
this.z = buf.readInt();
|
||||
this.dim = buf.readInt();
|
||||
this.side = buf.readByte();
|
||||
this.rows = buf.readInt();
|
||||
this.rowSize = buf.readInt();
|
||||
int payloadSize = buf.readInt();
|
||||
try (ByteBufInputStream stream = new ByteBufInputStream(buf, payloadSize)) {
|
||||
NBTTagCompound payload = CompressedStreamTools.readCompressed(stream);
|
||||
int available = stream.available();
|
||||
if (available > 0) {
|
||||
byte[] left = new byte[available];
|
||||
int read = stream.read(left);
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(
|
||||
"Unread bytes detected (" + read
|
||||
+ "): "
|
||||
+ Arrays.toString(left)
|
||||
+ " at "
|
||||
+ dim
|
||||
+ "#("
|
||||
+ x
|
||||
+ ":"
|
||||
+ y
|
||||
+ ":"
|
||||
+ z
|
||||
+ ")@"
|
||||
+ ForgeDirection.getOrientation(side));
|
||||
}
|
||||
}
|
||||
|
||||
if (payload.hasKey("self", NBT.TAG_COMPOUND)) {
|
||||
this.selfItemStack = ItemStack.loadItemStackFromNBT(payload.getCompoundTag("self"));
|
||||
}
|
||||
|
||||
if (payload.hasKey("display", NBT.TAG_COMPOUND)) {
|
||||
this.displayItemStack = ItemStack.loadItemStackFromNBT(payload.getCompoundTag("display"));
|
||||
}
|
||||
|
||||
this.items = payload.getTagList("data", NBT.TAG_COMPOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PacketAdd{" + "name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", x="
|
||||
+ x
|
||||
+ ", y="
|
||||
+ y
|
||||
+ ", z="
|
||||
+ z
|
||||
+ ", dim="
|
||||
+ dim
|
||||
+ ", side="
|
||||
+ side
|
||||
+ ", rows="
|
||||
+ rows
|
||||
+ ", rowSize="
|
||||
+ rowSize
|
||||
+ ", online="
|
||||
+ online
|
||||
+ ", selfItemStack="
|
||||
+ selfItemStack
|
||||
+ ", displayItemStack="
|
||||
+ displayItemStack
|
||||
+ ", items="
|
||||
+ items
|
||||
+ ", entryId="
|
||||
+ entryId
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
public static class PacketRemove extends PacketEntry {
|
||||
|
||||
PacketRemove(long id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
PacketRemove(ByteBuf buf) throws IOException {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void write(ByteBuf buf) {
|
||||
buf.writeByte(PacketType.REMOVE.ordinal());
|
||||
buf.writeLong(entryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void read(ByteBuf buf) {}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PacketRemove{" + "entryId=" + entryId + '}';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite online status or inventory of the entry.
|
||||
*/
|
||||
public static class PacketOverwrite extends PacketEntry {
|
||||
|
||||
public static final int ONLINE_BIT = 1;
|
||||
public static final int ONLINE_VALID = 1 << 1;
|
||||
public static final int ITEMS_VALID = 1 << 2;
|
||||
public static final int ALL_ITEM_UPDATE_BIT = 1 << 3;
|
||||
public boolean onlineValid;
|
||||
public boolean online;
|
||||
public boolean itemsValid;
|
||||
public boolean allItemUpdate;
|
||||
public int[] validIndices;
|
||||
public NBTTagList items;
|
||||
|
||||
protected PacketOverwrite(long id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
protected PacketOverwrite(ByteBuf buf) throws IOException {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
public PacketOverwrite setOnline(boolean online) {
|
||||
this.onlineValid = true;
|
||||
this.online = online;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PacketOverwrite setItems(int[] validIndices, NBTTagList items) {
|
||||
this.itemsValid = true;
|
||||
this.allItemUpdate = validIndices == null || validIndices.length == 0;
|
||||
this.validIndices = validIndices;
|
||||
this.items = items;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public PacketOverwrite setItems(int newSize, NBTTagList items) {
|
||||
this.itemsValid = true;
|
||||
this.allItemUpdate = true;
|
||||
this.validIndices = new int[newSize];
|
||||
this.items = items;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void write(ByteBuf buf) throws IOException {
|
||||
buf.writeByte(PacketType.OVERWRITE.ordinal());
|
||||
buf.writeLong(entryId);
|
||||
|
||||
int flags = 0;
|
||||
|
||||
if (onlineValid) {
|
||||
flags |= ONLINE_VALID;
|
||||
flags |= online ? ONLINE_BIT : 0;
|
||||
}
|
||||
if (itemsValid) {
|
||||
flags |= ITEMS_VALID;
|
||||
if (allItemUpdate) {
|
||||
flags |= ALL_ITEM_UPDATE_BIT;
|
||||
buf.writeByte(flags);
|
||||
buf.writeInt(validIndices.length);
|
||||
} else {
|
||||
buf.writeByte(flags);
|
||||
buf.writeInt(validIndices.length);
|
||||
for (int validIndex : validIndices) {
|
||||
buf.writeInt(validIndex);
|
||||
}
|
||||
}
|
||||
ByteBuf tempBuf = Unpooled.directBuffer(256);
|
||||
try {
|
||||
try (ByteBufOutputStream stream = new ByteBufOutputStream(tempBuf)) {
|
||||
|
||||
NBTTagCompound wrapper = new NBTTagCompound();
|
||||
|
||||
wrapper.setTag("data", items);
|
||||
CompressedStreamTools.writeCompressed(wrapper, stream);
|
||||
}
|
||||
buf.writeInt(tempBuf.readableBytes());
|
||||
buf.writeBytes(tempBuf);
|
||||
} finally {
|
||||
tempBuf.release();
|
||||
}
|
||||
} else {
|
||||
buf.writeByte(flags);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void read(ByteBuf buf) throws IOException {
|
||||
int flags = buf.readByte();
|
||||
|
||||
/* Decide whether to use online flag or not */
|
||||
if ((flags & ONLINE_VALID) == ONLINE_VALID) {
|
||||
this.onlineValid = true;
|
||||
this.online = (flags & ONLINE_BIT) == ONLINE_BIT;
|
||||
}
|
||||
/* Decide whether to read item list or not */
|
||||
if ((flags & ITEMS_VALID) == ITEMS_VALID) {
|
||||
this.itemsValid = true;
|
||||
if ((flags & ALL_ITEM_UPDATE_BIT) == ALL_ITEM_UPDATE_BIT) {
|
||||
this.allItemUpdate = true;
|
||||
int numItems = buf.readInt();
|
||||
this.validIndices = new int[numItems];
|
||||
} else {
|
||||
int numItems = buf.readInt();
|
||||
this.validIndices = new int[numItems];
|
||||
for (int i = 0; i < numItems; ++i) {
|
||||
this.validIndices[i] = buf.readInt();
|
||||
}
|
||||
}
|
||||
int payloadSize = buf.readInt();
|
||||
try (ByteBufInputStream stream = new ByteBufInputStream(buf, payloadSize)) {
|
||||
this.items = CompressedStreamTools.readCompressed(stream).getTagList("data", NBT.TAG_COMPOUND);
|
||||
int available = stream.available();
|
||||
if (available > 0) {
|
||||
byte[] left = new byte[available];
|
||||
int read = stream.read(left);
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info("Unread bytes detected (" + read + "): " + Arrays.toString(left));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PacketOverwrite{" + "onlineValid="
|
||||
+ onlineValid
|
||||
+ ", online="
|
||||
+ online
|
||||
+ ", itemsValid="
|
||||
+ itemsValid
|
||||
+ ", allItemUpdate="
|
||||
+ allItemUpdate
|
||||
+ ", validIndices="
|
||||
+ Arrays.toString(validIndices)
|
||||
+ ", items="
|
||||
+ items
|
||||
+ ", entryId="
|
||||
+ entryId
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename the entry.
|
||||
*/
|
||||
public static class PacketRename extends PacketEntry {
|
||||
|
||||
public String newName;
|
||||
|
||||
protected PacketRename(long id, String newName) {
|
||||
super(id);
|
||||
this.newName = newName;
|
||||
}
|
||||
|
||||
protected PacketRename(ByteBuf buf) throws IOException {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void write(ByteBuf buf) {
|
||||
buf.writeByte(PacketType.RENAME.ordinal());
|
||||
buf.writeLong(entryId);
|
||||
ByteBufUtils.writeUTF8String(buf, newName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void read(ByteBuf buf) {
|
||||
newName = ByteBufUtils.readUTF8String(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PacketRename{" + "newName='" + newName + '\'' + ", entryId=" + entryId + '}';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.glodblock.github.network.wrapper;
|
||||
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.features.AEFeature;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public class FCIndexedCodec extends SimpleIndexedCodec {
|
||||
|
||||
static int DISCRIMINATOR_BYTE = 1;
|
||||
|
||||
@Override
|
||||
public void encodeInto(ChannelHandlerContext ctx, IMessage msg, ByteBuf target) throws Exception {
|
||||
super.encodeInto(ctx, msg, target);
|
||||
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(" -> " + msg.getClass().getName() + " : " + target.readableBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, IMessage msg) {
|
||||
if (AEConfig.instance.isFeatureEnabled(AEFeature.PacketLogging)) {
|
||||
AELog.info(" <- " + msg.getClass().getName() + " : " + (source.readableBytes() + DISCRIMINATOR_BYTE));
|
||||
}
|
||||
|
||||
super.decodeInto(ctx, source, msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package com.glodblock.github.network.wrapper;
|
||||
|
||||
import java.util.EnumMap;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.INetHandler;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
|
||||
import cpw.mods.fml.common.network.FMLEmbeddedChannel;
|
||||
import cpw.mods.fml.common.network.FMLOutboundHandler;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper;
|
||||
import cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
|
||||
/**
|
||||
* Copied from {@link cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper} to be able to override IndexedCodec
|
||||
* to add packet logging
|
||||
*/
|
||||
public class FCNetworkWrapper {
|
||||
|
||||
protected final EnumMap<Side, FMLEmbeddedChannel> channels;
|
||||
protected final FCIndexedCodec packetCodec;
|
||||
|
||||
public FCNetworkWrapper(String channelName) {
|
||||
packetCodec = new FCIndexedCodec();
|
||||
channels = NetworkRegistry.INSTANCE.newChannel(channelName, packetCodec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a message and it's associated handler. The message will have the supplied discriminator byte. The
|
||||
* message handler will be registered on the supplied side (this is the side where you want the message to be
|
||||
* processed and acted upon).
|
||||
*
|
||||
* @param messageHandler the message handler type
|
||||
* @param requestMessageType the message type
|
||||
* @param discriminator a discriminator byte
|
||||
* @param side the side for the handler
|
||||
*/
|
||||
public <REQ extends IMessage, REPLY extends IMessage> void registerMessage(
|
||||
Class<? extends IMessageHandler<REQ, REPLY>> messageHandler, Class<REQ> requestMessageType,
|
||||
int discriminator, Side side) {
|
||||
registerMessage(instantiate(messageHandler), requestMessageType, discriminator, side);
|
||||
}
|
||||
|
||||
static <REQ extends IMessage, REPLY extends IMessage> IMessageHandler<? super REQ, ? extends REPLY> instantiate(
|
||||
Class<? extends IMessageHandler<? super REQ, ? extends REPLY>> handler) {
|
||||
try {
|
||||
return handler.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
throw Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a message and it's associated handler. The message will have the supplied discriminator byte. The
|
||||
* message handler will be registered on the supplied side (this is the side where you want the message to be
|
||||
* processed and acted upon).
|
||||
*
|
||||
* @param messageHandler the message handler instance
|
||||
* @param requestMessageType the message type
|
||||
* @param discriminator a discriminator byte
|
||||
* @param side the side for the handler
|
||||
*/
|
||||
public <REQ extends IMessage, REPLY extends IMessage> void registerMessage(
|
||||
IMessageHandler<? super REQ, ? extends REPLY> messageHandler, Class<REQ> requestMessageType,
|
||||
int discriminator, Side side) {
|
||||
packetCodec.addDiscriminator(discriminator, requestMessageType);
|
||||
FMLEmbeddedChannel channel = channels.get(side);
|
||||
String type = channel.findChannelHandlerNameForType(SimpleIndexedCodec.class);
|
||||
if (side == Side.SERVER) {
|
||||
addServerHandlerAfter(channel, type, messageHandler, requestMessageType);
|
||||
} else {
|
||||
addClientHandlerAfter(channel, type, messageHandler, requestMessageType);
|
||||
}
|
||||
}
|
||||
|
||||
private <REQ extends IMessage, REPLY extends IMessage, NH extends INetHandler> void addServerHandlerAfter(
|
||||
FMLEmbeddedChannel channel, String type, IMessageHandler<? super REQ, ? extends REPLY> messageHandler,
|
||||
Class<REQ> requestType) {
|
||||
SimpleChannelHandlerWrapper<REQ, REPLY> handler = getHandlerWrapper(messageHandler, Side.SERVER, requestType);
|
||||
channel.pipeline().addAfter(type, messageHandler.getClass().getName(), handler);
|
||||
}
|
||||
|
||||
private <REQ extends IMessage, REPLY extends IMessage, NH extends INetHandler> void addClientHandlerAfter(
|
||||
FMLEmbeddedChannel channel, String type, IMessageHandler<? super REQ, ? extends REPLY> messageHandler,
|
||||
Class<REQ> requestType) {
|
||||
SimpleChannelHandlerWrapper<REQ, REPLY> handler = getHandlerWrapper(messageHandler, Side.CLIENT, requestType);
|
||||
channel.pipeline().addAfter(type, messageHandler.getClass().getName(), handler);
|
||||
}
|
||||
|
||||
private <REPLY extends IMessage, REQ extends IMessage> SimpleChannelHandlerWrapper<REQ, REPLY> getHandlerWrapper(
|
||||
IMessageHandler<? super REQ, ? extends REPLY> messageHandler, Side side, Class<REQ> requestType) {
|
||||
return new SimpleChannelHandlerWrapper<REQ, REPLY>(messageHandler, side, requestType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a minecraft packet from the supplied message. Can be used where minecraft packets are required, such as
|
||||
* {@link TileEntity#getDescriptionPacket}.
|
||||
*
|
||||
* @param message The message to translate into packet form
|
||||
* @return A minecraft {@link Packet} suitable for use in minecraft APIs
|
||||
*/
|
||||
public Packet getPacketFrom(IMessage message) {
|
||||
return channels.get(Side.SERVER).generatePacketFrom(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone. The {@link IMessageHandler} for this message type should be on the CLIENT side.
|
||||
*
|
||||
* @param message The message to send
|
||||
*/
|
||||
public void sendToAll(IMessage message) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to the specified player. The {@link IMessageHandler} for this message type should be on the
|
||||
* CLIENT side.
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param player The player to send it to
|
||||
*/
|
||||
public void sendTo(IMessage message, EntityPlayerMP player) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET)
|
||||
.set(FMLOutboundHandler.OutboundTarget.PLAYER);
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone within a certain range of a point. The {@link IMessageHandler} for this message
|
||||
* type should be on the CLIENT side.
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param point The {@link TargetPoint} around which to send
|
||||
*/
|
||||
public void sendToAllAround(IMessage message, TargetPoint point) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET)
|
||||
.set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to everyone within the supplied dimension. The {@link IMessageHandler} for this message type
|
||||
* should be on the CLIENT side.
|
||||
*
|
||||
* @param message The message to send
|
||||
* @param dimensionId The dimension id to target
|
||||
*/
|
||||
public void sendToDimension(IMessage message, int dimensionId) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET)
|
||||
.set(FMLOutboundHandler.OutboundTarget.DIMENSION);
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId);
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send this message to the server. The {@link IMessageHandler} for this message type should be on the SERVER side.
|
||||
*
|
||||
* @param message The message to send
|
||||
*/
|
||||
public void sendToServer(IMessage message) {
|
||||
channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET)
|
||||
.set(FMLOutboundHandler.OutboundTarget.TOSERVER);
|
||||
channels.get(Side.CLIENT).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user