switch to SimpleNetworkWrapper

This commit is contained in:
Azanor committed 2014-06-05 10:08:04 +02:00
1 parent 78db21ece3
commit f73a213e20
10 files changed
+56 -292

No files matched your search

+5 -6
View File
@@ -9,7 +9,7 @@ import org.apache.logging.log4j.Logger;
import baubles.common.event.EventHandlerEntity;
import baubles.common.network.EventHandlerNetwork;
import baubles.common.network.PacketPipeline;
import baubles.common.network.PacketHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
@@ -20,13 +20,13 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.12.0.1056,);")
@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.12.1.1110,);")
public class Baubles {
public static final String MODID = "Baubles";
public static final String MODNAME = "Baubles";
public static final String VERSION = "1.0.0.15";
public static final String VERSION = "1.0.0.16";
@SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy")
public static CommonProxy proxy;
@@ -39,7 +39,6 @@ public class Baubles {
public EventHandlerEntity entityEventHandler;
public File modDir;
public static final PacketPipeline packetPipeline = new PacketPipeline();
public static final Logger log = LogManager.getLogger("Baubles");
public static final int GUI = 0;
@@ -56,6 +55,8 @@ public class Baubles {
if (Config.config!=null) Config.save();
}
PacketHandler.init();
entityEventHandler = new EventHandlerEntity();
MinecraftForge.EVENT_BUS.register(entityEventHandler);
@@ -69,7 +70,6 @@ public class Baubles {
@EventHandler
public void init(FMLInitializationEvent evt) {
packetPipeline.initialise();
NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy);
proxy.registerKeyBindings();
}
@@ -77,7 +77,6 @@ public class Baubles {
@EventHandler
public void postInit(FMLPostInitializationEvent evt) {
Config.initRecipe();
packetPipeline.postInitialise();
}
}
@@ -15,6 +15,7 @@ import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.common.Baubles;
import baubles.common.lib.PlayerHandler;
import baubles.common.network.PacketHandler;
import baubles.common.network.PacketSyncBauble;
public class InventoryBaubles implements IInventory {
@@ -252,7 +253,7 @@ public class InventoryBaubles implements IInventory {
public void syncSlotToClients(int slot) {
try {
if (Baubles.proxy.getClientWorld()==null) {
Baubles.packetPipeline.sendToAll(new PacketSyncBauble(player.get(),slot));
PacketHandler.INSTANCE.sendToAll(new PacketSyncBauble(player.get(),slot));
}
} catch (Exception e) { e.printStackTrace(); }
}
@@ -4,7 +4,7 @@ import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
import baubles.common.Baubles;
import baubles.common.network.PacketHandler;
import baubles.common.network.PacketOpenBaublesInventory;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.client.registry.ClientRegistry;
@@ -29,7 +29,7 @@ public class KeyHandler {
if (event.side == Side.SERVER) return;
if (event.phase == Phase.START ) {
if (key.getIsKeyPressed() && FMLClientHandler.instance().getClient().inGameHasFocus) {
Baubles.packetPipeline.sendToServer(new PacketOpenBaublesInventory(event.player));
PacketHandler.INSTANCE.sendToServer(new PacketOpenBaublesInventory(event.player));
}
}
}
@@ -1,43 +0,0 @@
package baubles.common.network;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
/**
* AbstractPacket class. Should be the parent of all packets wishing to use the PacketPipeline.
* @author sirgingalot
*/
public abstract class AbstractPacket {
/**
* Encode the packet data into the ByteBuf stream. Complex data sets may need specific data handlers (See @link{cpw.mods.fml.common.network.ByteBuffUtils})
*
* @param ctx channel context
* @param buffer the buffer to encode into
*/
public abstract void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer);
/**
* Decode the packet data from the ByteBuf stream. Complex data sets may need specific data handlers (See @link{cpw.mods.fml.common.network.ByteBuffUtils})
*
* @param ctx channel context
* @param buffer the buffer to decode from
*/
public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer);
/**
* Handle a packet on the client side. Note this occurs after decoding has completed.
*
* @param player the player reference
*/
public abstract void handleClientSide(EntityPlayer player);
/**
* Handle a packet on the server side. Note this occurs after decoding has completed.
*
* @param player the player reference
*/
public abstract void handleServerSide(EntityPlayer player);
}
@@ -0,0 +1,19 @@
package baubles.common.network;
import baubles.common.Baubles;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
public class PacketHandler
{
public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(Baubles.MODID.toLowerCase());
public static void init()
{
INSTANCE.registerMessage(PacketOpenBaublesInventory.class, PacketOpenBaublesInventory.class, 0, Side.SERVER);
INSTANCE.registerMessage(PacketSyncBauble.class, PacketSyncBauble.class, 1, Side.CLIENT);
}
}
@@ -1,28 +1,28 @@
package baubles.common.network;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import baubles.common.Baubles;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public class PacketOpenBaublesInventory extends AbstractPacket {
public class PacketOpenBaublesInventory implements IMessage, IMessageHandler<PacketOpenBaublesInventory, IMessage> {
public PacketOpenBaublesInventory() {}
public PacketOpenBaublesInventory(EntityPlayer player) {}
@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {}
public void toBytes(ByteBuf buffer) {}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {}
public void fromBytes(ByteBuf buffer) {}
@Override
public void handleClientSide(EntityPlayer player) {}
@Override
public void handleServerSide(EntityPlayer player) {
player.openGui(Baubles.instance, Baubles.GUI, player.worldObj, (int)player.posX, (int)player.posY, (int)player.posZ);
public IMessage onMessage(PacketOpenBaublesInventory message, MessageContext ctx) {
ctx.getServerHandler().playerEntity.openGui(Baubles.instance, Baubles.GUI, ctx.getServerHandler().playerEntity.worldObj, (int)ctx.getServerHandler().playerEntity.posX, (int)ctx.getServerHandler().playerEntity.posY, (int)ctx.getServerHandler().playerEntity.posZ);
return null;
}
@@ -1,217 +0,0 @@
package baubles.common.network;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import cpw.mods.fml.common.FMLCommonHandler;
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.internal.FMLProxyPacket;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* Packet pipeline class. Directs all registered packet data to be handled by the packets themselves.
* @author sirgingalot
* some code from: cpw
*/
@ChannelHandler.Sharable
public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket> {
private EnumMap<Side, FMLEmbeddedChannel> channels;
private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>();
private boolean isPostInitialised = false;
/**
* Register your packet with the pipeline. Discriminators are automatically set.
*
* @param clazz the class to register
*
* @return whether registration was successful. Failure may occur if 256 packets have been registered or if the registry already contains this packet
*/
public boolean registerPacket(Class<? extends AbstractPacket> clazz) {
if (this.packets.size() > 256) {
// You should log here!!
return false;
}
if (this.packets.contains(clazz)) {
// You should log here!!
return false;
}
if (this.isPostInitialised) {
// You should log here!!
return false;
}
this.packets.add(clazz);
return true;
}
// In line encoding of the packet, including discriminator setting
@Override
protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception {
ByteBuf buffer = Unpooled.buffer();
Class<? extends AbstractPacket> clazz = msg.getClass();
if (!this.packets.contains(msg.getClass())) {
throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName());
}
byte discriminator = (byte) this.packets.indexOf(clazz);
buffer.writeByte(discriminator);
msg.encodeInto(ctx, buffer);
FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
out.add(proxyPacket);
}
// In line decoding and handling of the packet
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
ByteBuf payload = msg.payload();
byte discriminator = payload.readByte();
Class<? extends AbstractPacket> clazz = this.packets.get(discriminator);
if (clazz == null) {
throw new NullPointerException("No packet registered for discriminator: " + discriminator);
}
AbstractPacket pkt = clazz.newInstance();
pkt.decodeInto(ctx, payload.slice());
EntityPlayer player;
switch (FMLCommonHandler.instance().getEffectiveSide()) {
case CLIENT:
player = this.getClientPlayer();
pkt.handleClientSide(player);
break;
case SERVER:
INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
player = ((NetHandlerPlayServer) netHandler).playerEntity;
pkt.handleServerSide(player);
break;
default:
}
out.add(pkt);
}
// Method to call from FMLInitializationEvent
public void initialise() {
this.channels = NetworkRegistry.INSTANCE.newChannel("BAUBLE", this);
//packets
registerPacket(PacketOpenBaublesInventory.class);
registerPacket(PacketSyncBauble.class);
}
// Method to call from FMLPostInitializationEvent
// Ensures that packet discriminators are common between server and client by using logical sorting
public void postInitialise() {
if (this.isPostInitialised) {
return;
}
this.isPostInitialised = true;
Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() {
@Override
public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2) {
int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName());
if (com == 0) {
com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName());
}
return com;
}
});
}
@SideOnly(Side.CLIENT)
private EntityPlayer getClientPlayer() {
return Minecraft.getMinecraft().thePlayer;
}
/**
* Send this message to everyone.
* <p/>
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
*
* @param message The message to send
*/
public void sendToAll(AbstractPacket message) {
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
this.channels.get(Side.SERVER).writeAndFlush(message);
}
/**
* Send this message to the specified player.
* <p/>
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
*
* @param message The message to send
* @param player The player to send it to
*/
public void sendTo(AbstractPacket message, EntityPlayerMP player) {
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
this.channels.get(Side.SERVER).writeAndFlush(message);
}
/**
* Send this message to everyone within a certain range of a point.
* <p/>
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
*
* @param message The message to send
* @param point The {@link cpw.mods.fml.common.network.NetworkRegistry.TargetPoint} around which to send
*/
public void sendToAllAround(AbstractPacket message, NetworkRegistry.TargetPoint point) {
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
this.channels.get(Side.SERVER).writeAndFlush(message);
}
/**
* Send this message to everyone within the supplied dimension.
* <p/>
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
*
* @param message The message to send
* @param dimensionId The dimension id to target
*/
public void sendToDimension(AbstractPacket message, int dimensionId) {
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId);
this.channels.get(Side.SERVER).writeAndFlush(message);
}
/**
* Send this message to the server.
* <p/>
* Adapted from CPW's code in cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper
*
* @param message The message to send
*/
public void sendToServer(AbstractPacket message) {
this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
this.channels.get(Side.CLIENT).writeAndFlush(message);
}
}
@@ -1,7 +1,6 @@
package baubles.common.network;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException;
@@ -10,9 +9,13 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.world.World;
import baubles.common.Baubles;
import baubles.common.lib.PlayerHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
public class PacketSyncBauble extends AbstractPacket {
public class PacketSyncBauble implements IMessage, IMessageHandler<PacketSyncBauble, IMessage> {
int slot;
int playerId;
@@ -27,7 +30,7 @@ public class PacketSyncBauble extends AbstractPacket {
}
@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
public void toBytes(ByteBuf buffer) {
buffer.writeByte(slot);
buffer.writeInt(playerId);
PacketBuffer pb = new PacketBuffer(buffer);
@@ -35,7 +38,7 @@ public class PacketSyncBauble extends AbstractPacket {
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer)
public void fromBytes(ByteBuf buffer)
{
slot = buffer.readByte();
playerId = buffer.readInt();
@@ -44,17 +47,15 @@ public class PacketSyncBauble extends AbstractPacket {
}
@Override
public void handleClientSide(EntityPlayer player) {
World world = player.worldObj;
if (world==null) return;
Entity p = world.getEntityByID(playerId);
public IMessage onMessage(PacketSyncBauble message, MessageContext ctx) {
World world = Baubles.proxy.getClientWorld();
if (world==null) return null;
Entity p = world.getEntityByID(message.playerId);
if (p !=null && p instanceof EntityPlayer) {
PlayerHandler.getPlayerBaubles((EntityPlayer) p).stackList[slot]=bauble;
PlayerHandler.getPlayerBaubles((EntityPlayer) p).stackList[message.slot]=message.bauble;
}
return null;
}
@Override
public void handleServerSide(EntityPlayer player) {}
}