Skip to content

Commit

Permalink
Removed PacketMode (powered spawner mode)
Browse files Browse the repository at this point in the history
* re #4189
  • Loading branch information
HenryLoenwind committed May 1, 2017
1 parent 6c96d36 commit 772567c
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 63 deletions.
13 changes: 11 additions & 2 deletions src/main/java/crazypants/enderio/GuiID.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.annotation.Nonnull;

import crazypants.enderio.network.IRemoteExec;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -213,7 +214,11 @@ public static void init() {
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
IGuiHandler handler = byID(id).handler;
if (handler != null && world != null && world.isBlockLoaded(new BlockPos(x, y, z))) {
return handler.getServerGuiElement(id, player, world, x, y, z);
final Object guiElement = handler.getServerGuiElement(id, player, world, x, y, z);
if (guiElement instanceof IRemoteExec) {
((IRemoteExec) guiElement).setGuiID(id);
}
return guiElement;
}
return null;
}
Expand All @@ -222,7 +227,11 @@ public Object getServerGuiElement(int id, EntityPlayer player, World world, int
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
IGuiHandler handler = byID(id).handler;
if (handler != null) {
return handler.getClientGuiElement(id, player, world, x, y, z);
final Object guiElement = handler.getClientGuiElement(id, player, world, x, y, z);
if (guiElement instanceof IRemoteExec) {
((IRemoteExec) guiElement).setGuiID(id);
}
return guiElement;
}
return null;
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/crazypants/enderio/gui/GuiContainerBaseEIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.enderio.core.client.render.RenderUtil;

import crazypants.enderio.EnderIO;
import crazypants.enderio.network.IRemoteExec;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
Expand All @@ -26,7 +27,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public abstract class GuiContainerBaseEIO extends GuiContainerBase {
public abstract class GuiContainerBaseEIO extends GuiContainerBase implements IRemoteExec.IGui {

private final List<ResourceLocation> guiTextures = new ArrayList<ResourceLocation>();

Expand Down Expand Up @@ -176,4 +177,19 @@ private void renderTabPart(VertexBuffer tes, int x, int y, int u, int v, int w,
tes.pos(x, y + 0, 0).tex(minU, minV).endVertex();
}

@Override
public void setGuiID(int id) {
if (inventorySlots instanceof IRemoteExec.IContainer) {
((IRemoteExec.IContainer) inventorySlots).setGuiID(id);
}
}

@Override
public int getGuiID() {
if (inventorySlots instanceof IRemoteExec.IContainer) {
return ((IRemoteExec.IContainer) inventorySlots).getGuiID();
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,15 @@ public int getEnd() {
return end;
}
}

private int guiID = -1;

public void setGuiID(int id) {
guiID = id;
}

public int getGuiID() {
return guiID;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public class BlockPoweredSpawner extends AbstractMachineBlock<TilePoweredSpawner
public static BlockPoweredSpawner create() {
MachineRecipeRegistry.instance.registerRecipe(ModObject.blockPoweredSpawner.getUnlocalisedName(), new DummyRecipe());

PacketHandler.INSTANCE.registerMessage(PacketMode.class, PacketMode.class, PacketHandler.nextID(), Side.SERVER);
PacketHandler.INSTANCE.registerMessage(PacketUpdateNotification.class, PacketUpdateNotification.class, PacketHandler.nextID(), Side.CLIENT);

//Ensure costs are loaded at startup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import com.enderio.core.client.gui.widget.GhostSlot;

import crazypants.enderio.machine.gui.AbstractMachineContainer;
import crazypants.enderio.network.GuiPacket;
import crazypants.enderio.network.IRemoteExec;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import static crazypants.enderio.ModObject.itemSoulVessel;

public class ContainerPoweredSpawner extends AbstractMachineContainer<TilePoweredSpawner> {
public class ContainerPoweredSpawner extends AbstractMachineContainer<TilePoweredSpawner> implements IRemoteExec.IContainer {

private Slot slotInput;
private Slot slotOutput;
Expand Down Expand Up @@ -50,4 +52,15 @@ public void setSlotVisibility(boolean visible) {
slotOutput.yDisplayPosition = visible ? 42 : -3000;
}

@Override
public void networkExec(int id, GuiPacket message) {
switch (id) {
case 0:
getInv().setSpawnMode(message.getBoolean(0));
break;
default:
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import crazypants.enderio.EnderIO;
import crazypants.enderio.gui.IconEIO;
import crazypants.enderio.machine.gui.GuiPoweredMachineBase;
import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.network.GuiPacket;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void initGui() {
protected void actionPerformed(GuiButton par1GuiButton) throws IOException {
if(par1GuiButton == modeB) {
getTileEntity().setSpawnMode(!getTileEntity().isSpawnMode());
PacketHandler.INSTANCE.sendToServer(new PacketMode(getTileEntity()));
GuiPacket.send(this, 0, getTileEntity().isSpawnMode());
} else if (par1GuiButton == showRangeB) {
getTileEntity().setShowRange(showRangeB.isSelected());
} else {
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/crazypants/enderio/machine/spawner/PacketMode.java

This file was deleted.

180 changes: 180 additions & 0 deletions src/main/java/crazypants/enderio/network/GuiPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package crazypants.enderio.network;

import crazypants.enderio.Log;
import io.netty.buffer.ByteBuf;
import net.minecraft.inventory.Container;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

/**
* A generic network packet that allows a client-side GUI (or container) to talk to its server-side container.
* <p>
* Usage:
* <ul>
* <li>On the GUI implement IRemoteExec.IGui (already done on GuiContainerBaseEIO).
* <li>On the container implement IRemoteExec.IContainer.
* <li>If needed, add a new send() method and PATTERN to GuiPacket for the correct number and type of parameters.
* <li>If needed, add a new DataType value and handling code to GuiPacket (for things that cannot be transmitted as int or long).
* <li>In the GUI, call the appropriate send() method. Chose a unique (per container) message ID.
* <li>In the container, implement networkExec() and switch on the message ID. You can get the parameters with the same index numbers as the send() method put
* them in. Be careful to match the data types used or you'll get either 0 values.
* <li>Done.
* </ul>
*
* The GuiPacket's handler will make sure that calls are only delivered to the correct container. If that container isn't open, the message will be silently
* ignored.
* <p>
* The GUI and container are responsible to agree on the data transferred.
*
*/
public class GuiPacket implements IMessage {

public static void send(IRemoteExec gui, int msgID) {
GuiPacket p = new GuiPacket(gui, msgID, 0, null, null);
p.send();
}

public static void send(IRemoteExec gui, int msgID, int data) {
GuiPacket p = new GuiPacket(gui, msgID, 1, new int[] { data }, null);
p.send();
}

public static void send(IRemoteExec gui, int msgID, Enum<?> data) {
GuiPacket p = new GuiPacket(gui, msgID, 1, new int[] { data.ordinal() }, null);
p.send();
}

public static void send(IRemoteExec gui, int msgID, boolean data) {
GuiPacket p = new GuiPacket(gui, msgID, 1, new int[] { data ? 1 : 0 }, null);
p.send();
}

// ------------------------------------------------------------------

private static enum DataType {
INT,
LONG;
}

private static final DataType[][] PATTERN = { {}, { DataType.INT } };

private int guiID, msgID, pattern;
private int[] ints;
private long[] longs;

private GuiPacket(IRemoteExec gui, int msgID, int pattern, int[] ints, long[] longs) {
this.guiID = gui.getGuiID();
this.msgID = msgID;
this.pattern = pattern;
this.ints = ints;
this.longs = longs;
}

public GuiPacket() {
}

@Override
public void toBytes(ByteBuf buf) {
buf.writeShort(guiID);
buf.writeShort(msgID);
buf.writeShort(pattern);
int idx = 0;
for (DataType dt : PATTERN[pattern]) {
switch (dt) {
case INT:
buf.writeInt(ints[idx++]);
break;
case LONG:
buf.writeLong(longs[idx++]);
break;
}
}
}

@Override
public void fromBytes(ByteBuf buf) {
guiID = buf.readShort();
msgID = buf.readShort();
pattern = buf.readShort();
if (pattern < 0 || pattern >= PATTERN.length) {
Log.warn("Invalid network packet received (" + guiID + "/" + msgID + "/" + pattern + "): p invalid");
return;
}
int idx = 0;
for (DataType dt : PATTERN[pattern]) {
switch (dt) {
case INT:
if (ints == null)
ints = new int[PATTERN[pattern].length];
ints[idx++] = buf.readInt();
break;
case LONG:
if (longs == null)
longs = new long[PATTERN[pattern].length];
longs[idx++] = buf.readLong();
break;
}
}
}

private boolean checkAccess(int idx, DataType type) {
if (idx >= PATTERN[pattern].length) {
Log.warn("Invalid network packet received (" + guiID + "/" + msgID + "/" + pattern + "): idx" + idx + " high");
return false;
}
if (PATTERN[pattern][idx] != type) {
Log.warn("Invalid network packet received (" + guiID + "/" + msgID + "/" + pattern + "): idx" + idx + " not " + type);
return false;
}
if ((type == DataType.INT && ints == null) || (type == DataType.LONG && longs == null)) {
Log.warn(
"Invalid network packet received (" + guiID + "/" + msgID + "/" + pattern + "): idx" + idx + " no " + type + " data");
return false;
}
return true;
}

private void send() {
PacketHandler.INSTANCE.sendToServer(this);
}

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

@Override
public IMessage onMessage(GuiPacket message, MessageContext ctx) {
final Container openContainer = ctx.getServerHandler().playerEntity.openContainer;
if (openContainer instanceof IRemoteExec.IContainer && ((IRemoteExec.IContainer) openContainer).getGuiID() == message.guiID && message.guiID >= 0) {
((IRemoteExec.IContainer) openContainer).networkExec(message.msgID, message);
}
return null;
}

}

// GETTERS

public int getInt(int idx) {
return checkAccess(idx, DataType.INT) ? ints[idx] : 0;
}

public <E extends Enum<?>> E getEnum(int idx, Class<E> clazz) {
E[] enumConstants = clazz.getEnumConstants();
return enumConstants[MathHelper.clamp_int(getInt(idx), 0, enumConstants.length - 1)];
}

public boolean getBoolean(int idx) {
return getInt(idx) != 0;
}

public long getLong(int idx) {
return checkAccess(idx, DataType.LONG) ? longs[idx] : 0L;
}

public BlockPos getBlockPos(int idx) {
return BlockPos.fromLong(getLong(idx));
}

}
Loading

0 comments on commit 772567c

Please sign in to comment.