feat: Rework scheduling and data storage in TileLevelMaintainer functionality

This commit is contained in:
Andrei Laiff committed 2023-10-21 22:41:00 +03:00
1 parent 827bbf8aa8
commit f26d18e321
11 files changed
+640 -416

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;
}
@@ -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;
}
}
}