Skip to content

Commit

Permalink
Correct split client-server code
Browse files Browse the repository at this point in the history
  • Loading branch information
Laiff committed Dec 23, 2023
1 parent db8aee2 commit 6e96cba
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,18 @@ protected void actionPerformed(final GuiButton btn) {
}
}

public GuiType getOriginalGui() {
return originalGui;
}

public void switchGui() {
FluidCraft.proxy.netHandler.sendToServer(
new CPacketLevelTerminalCommands(
CPacketLevelTerminalCommands.Action.BACK,
originalBlockPos.x,
originalBlockPos.y,
originalBlockPos.z,
originalBlockPos.getDimension(),
originalBlockPos.getSide()));
CPacketLevelTerminalCommands message = new CPacketLevelTerminalCommands(
CPacketLevelTerminalCommands.Action.BACK,
originalBlockPos.x,
originalBlockPos.y,
originalBlockPos.z,
originalBlockPos.getDimension(),
originalBlockPos.getSide());
if (originalGui != null) {
message.setOriginalGui(originalGui.ordinal());
}
FluidCraft.proxy.netHandler.sendToServer(message);
}

@Override
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/glodblock/github/client/gui/GuiRenamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class GuiRenamer extends AEBaseGui implements IDropToFillTextField {

protected ItemStack icon = null;

protected GuiType originalGui;

public GuiRenamer(InventoryPlayer ip, ITerminalHost monitorable) {
super(new ContainerRenamer(ip, monitorable));
this.host = monitorable;
Expand All @@ -51,12 +53,16 @@ public void initGui() {
FluidCraft.proxy.netHandler.sendToServer(new CPacketRenamer(CPacketRenamer.Action.GET_TEXT));
if (host instanceof PartLevelTerminal) {
icon = ItemAndBlockHolder.LEVEL_TERMINAL.stack();
originalGui = GuiType.LEVEL_TERMINAL;
} else if (host instanceof IWirelessTerminal terminal && terminal.isUniversal(host)) {
icon = ItemAndBlockHolder.WIRELESS_ULTRA_TERM.stack();
originalGui = ItemWirelessUltraTerminal.readMode(terminal.getItemStack());
} else if (host instanceof WirelessLevelTerminalInventory) {
icon = ItemAndBlockHolder.WIRELESS_LEVEL_TERM.stack();
originalGui = GuiType.WIRELESS_LEVEL_TERMINAL;
} else if (host instanceof WirelessInterfaceTerminalInventory) {
icon = ItemAndBlockHolder.WIRELESS_INTERFACE_TERM.stack();
originalGui = GuiType.WIRELESS_INTERFACE_TERMINAL;
}
if (this.icon != null) {
this.buttonList.add(
Expand Down Expand Up @@ -114,13 +120,9 @@ protected void actionPerformed(GuiButton button) {
}

public void switchGui() {
if (host instanceof PartLevelTerminal) InventoryHandler.switchGui(GuiType.LEVEL_TERMINAL);
else if (host instanceof IWirelessTerminal terminal && terminal.isUniversal(host))
InventoryHandler.switchGui(ItemWirelessUltraTerminal.readMode(terminal.getItemStack()));
else if (host instanceof WirelessInterfaceTerminalInventory)
InventoryHandler.switchGui(GuiType.WIRELESS_INTERFACE_TERMINAL);
else if (host instanceof WirelessLevelTerminalInventory)
InventoryHandler.switchGui(GuiType.WIRELESS_LEVEL_TERMINAL);
if (originalGui != null) {
InventoryHandler.switchGui(originalGui);
}
}

public void setTextFieldValue(final String displayName, final int mousex, final int mousey, final ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ public void readFromNBTEvent(NBTTagCompound data) {
}

private ItemStack removeRecursion(ItemStack itemStack) {
if (itemStack.hasTagCompound() && itemStack.getTagCompound().hasKey(TLMTags.Stack.tagName)) {
if (itemStack != null && itemStack.hasTagCompound()
&& itemStack.getTagCompound().hasKey(TLMTags.Stack.tagName)) {
return removeRecursion(loadItemStackFromTag(itemStack));
}
return itemStack;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/glodblock/github/loader/ChannelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.glodblock.github.network.SPacketMEItemInvUpdate;
import com.glodblock.github.network.SPacketSetItemAmount;
import com.glodblock.github.network.SPacketStringUpdate;
import com.glodblock.github.network.SPacketSwitchBack;
import com.glodblock.github.network.wrapper.FCNetworkWrapper;

import cpw.mods.fml.relauncher.Side;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void run() {
netHandler.registerMessage(new SPacketSetItemAmount.Handler(), SPacketSetItemAmount.class, id++, Side.CLIENT);
netHandler.registerMessage(new CPacketRenamer.Handler(), CPacketRenamer.class, id++, Side.SERVER);
netHandler.registerMessage(new SPacketStringUpdate.Handler(), SPacketStringUpdate.class, id++, Side.CLIENT);
netHandler.registerMessage(new SPacketSwitchBack.Handler(), SPacketSwitchBack.class, id++, Side.CLIENT);
netHandler.registerMessage(
new SPacketLevelTerminalUpdate.Handler(),
SPacketLevelTerminalUpdate.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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;
Expand All @@ -28,6 +25,7 @@ public class CPacketLevelTerminalCommands implements IMessage {
private int z;
private int dim;
private ForgeDirection side;
private int originalGui = -1;

public enum Action {
EDIT,
Expand All @@ -49,16 +47,29 @@ public CPacketLevelTerminalCommands(Action action, int x, int y, int z, int dim,
this.side = side;
}

public CPacketLevelTerminalCommands setOriginalGui(int originalGui) {
this.originalGui = originalGui;
return this;
}

@Override
public void fromBytes(ByteBuf buf) {
action = Action.values()[buf.readInt()];
switch (action) {
case EDIT, BACK -> {
case EDIT -> {
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
dim = buf.readInt();
side = ForgeDirection.getOrientation(buf.readInt());
}
case BACK -> {
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
dim = buf.readInt();
side = ForgeDirection.getOrientation(buf.readInt());
originalGui = buf.readInt();
}
case ENABLE -> {}
case DISABLE -> {}
Expand All @@ -71,13 +82,21 @@ public void fromBytes(ByteBuf buf) {
public void toBytes(ByteBuf buf) {
buf.writeInt(action.ordinal());
switch (action) {
case EDIT, BACK -> {
case EDIT -> {
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(dim);
buf.writeInt(side.ordinal());
}
case BACK -> {
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(dim);
buf.writeInt(side.ordinal());
buf.writeInt(originalGui);
}
case ENABLE -> {}
case DISABLE -> {}
case ENABLE_ALL -> {}
Expand All @@ -103,11 +122,7 @@ public IMessage onMessage(CPacketLevelTerminalCommands message, MessageContext c
GuiType.LEVEL_MAINTAINER);
}
case BACK -> {
GuiType originalGui = null;
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiLevelMaintainer guiLevelMaintainer) {
originalGui = guiLevelMaintainer.getOriginalGui();
}
GuiType originalGui = GuiType.getByOrdinal(message.originalGui);
if (originalGui == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
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.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.IClickableInTerminal;
Expand Down Expand Up @@ -179,10 +176,7 @@ public IMessage onMessage(CPacketRenamer message, MessageContext ctx) {
.getTileEntity(intMsg.x, intMsg.y, intMsg.z);
this.setName(tile, intMsg.getSide(), message.text);

final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiRenamer guiRenamer) {
guiRenamer.switchGui();
}
FluidCraft.proxy.netHandler.sendTo(new SPacketSwitchBack(), player);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/glodblock/github/network/SPacketSwitchBack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.glodblock.github.network;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;

import com.glodblock.github.client.gui.GuiRenamer;

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 SPacketSwitchBack implements IMessage {

public SPacketSwitchBack() {}

@Override
public void fromBytes(ByteBuf buf) {}

@Override
public void toBytes(ByteBuf buf) {}

public static class Handler implements IMessageHandler<SPacketSwitchBack, IMessage> {

@Override
public IMessage onMessage(SPacketSwitchBack message, MessageContext ctx) {
final GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if (gs instanceof GuiRenamer) {
((GuiRenamer) gs).switchGui();
}
return null;
}
}
}

0 comments on commit 6e96cba

Please sign in to comment.