This commit is contained in:
Gig 2015-04-24 14:20:09 +01:00
parent 6e0ec1d861
commit 4ac26ac086
137 changed files with 10339 additions and 7322 deletions

View file

@ -1,71 +1,97 @@
package techreborn.packets;
import cpw.mods.fml.common.network.FMLEmbeddedChannel;
import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec;
import cpw.mods.fml.common.network.FMLOutboundHandler;
import cpw.mods.fml.relauncher.Side;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.Packet;
import net.minecraft.world.World;
import java.io.IOException;
import java.util.EnumMap;
import java.util.logging.Logger;
public class PacketHandler extends FMLIndexedMessageToMessageCodec<SimplePacket> {
private static EnumMap<Side, FMLEmbeddedChannel> channels;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.Packet;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.FMLEmbeddedChannel;
import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec;
import cpw.mods.fml.common.network.FMLOutboundHandler;
import cpw.mods.fml.relauncher.Side;
public PacketHandler() {
public class PacketHandler extends
FMLIndexedMessageToMessageCodec<SimplePacket> {
private static EnumMap<Side, FMLEmbeddedChannel> channels;
}
public PacketHandler()
{
public static EnumMap<Side, FMLEmbeddedChannel> getChannels() {
return channels;
}
}
public static void setChannels(EnumMap<Side, FMLEmbeddedChannel> _channels) {
channels = _channels;
}
public static EnumMap<Side, FMLEmbeddedChannel> getChannels()
{
return channels;
}
public static void sendPacketToServer(SimplePacket packet) {
PacketHandler.getChannels().get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
PacketHandler.getChannels().get(Side.CLIENT).writeOutbound(packet);
}
public static void setChannels(EnumMap<Side, FMLEmbeddedChannel> _channels)
{
channels = _channels;
}
public static void sendPacketToPlayer(SimplePacket packet, EntityPlayer player) {
PacketHandler.getChannels().get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
PacketHandler.getChannels().get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
PacketHandler.getChannels().get(Side.SERVER).writeOutbound(packet);
}
public static void sendPacketToServer(SimplePacket packet)
{
PacketHandler.getChannels().get(Side.CLIENT)
.attr(FMLOutboundHandler.FML_MESSAGETARGET)
.set(FMLOutboundHandler.OutboundTarget.TOSERVER);
PacketHandler.getChannels().get(Side.CLIENT).writeOutbound(packet);
}
public static void sendPacketToAllPlayers(SimplePacket packet) {
PacketHandler.getChannels().get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
PacketHandler.getChannels().get(Side.SERVER).writeOutbound(packet);
}
public static void sendPacketToPlayer(SimplePacket packet,
EntityPlayer player)
{
PacketHandler.getChannels().get(Side.SERVER)
.attr(FMLOutboundHandler.FML_MESSAGETARGET)
.set(FMLOutboundHandler.OutboundTarget.PLAYER);
PacketHandler.getChannels().get(Side.SERVER)
.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
PacketHandler.getChannels().get(Side.SERVER).writeOutbound(packet);
}
public static void sendPacketToAllPlayers(Packet packet, World world) {
for (Object player : world.playerEntities) {
if (player instanceof EntityPlayerMP)
if (player != null)
((EntityPlayerMP) player).playerNetServerHandler.sendPacket(packet);
}
}
public static void sendPacketToAllPlayers(SimplePacket packet)
{
PacketHandler.getChannels().get(Side.SERVER)
.attr(FMLOutboundHandler.FML_MESSAGETARGET)
.set(FMLOutboundHandler.OutboundTarget.ALL);
PacketHandler.getChannels().get(Side.SERVER).writeOutbound(packet);
}
@Override
public void encodeInto(ChannelHandlerContext ctx, SimplePacket msg, ByteBuf target) throws Exception {
msg.writePacketData(target);
}
public static void sendPacketToAllPlayers(Packet packet, World world)
{
for (Object player : world.playerEntities)
{
if (player instanceof EntityPlayerMP)
if (player != null)
((EntityPlayerMP) player).playerNetServerHandler
.sendPacket(packet);
}
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, SimplePacket msg) {
try {
msg.readPacketData(source);
msg.execute();
} catch (IOException e) {
Logger.getLogger("Network").warning("Something caused a Protocol Exception!");
}
}
@Override
public void encodeInto(ChannelHandlerContext ctx, SimplePacket msg,
ByteBuf target) throws Exception
{
msg.writePacketData(target);
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf source,
SimplePacket msg)
{
try
{
msg.readPacketData(source);
msg.execute();
} catch (IOException e)
{
Logger.getLogger("Network").warning(
"Something caused a Protocol Exception!");
}
}
}

View file

@ -1,7 +1,9 @@
package techreborn.packets;
import com.google.common.base.Charsets;
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
@ -9,110 +11,133 @@ import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import java.io.IOException;
import com.google.common.base.Charsets;
public abstract class SimplePacket {
protected EntityPlayer player;
protected byte mode;
protected EntityPlayer player;
protected byte mode;
public SimplePacket(EntityPlayer _player) {
player = _player;
}
public SimplePacket(EntityPlayer _player)
{
player = _player;
}
@SuppressWarnings("unused")
public SimplePacket() {
player = null;
}
@SuppressWarnings("unused")
public SimplePacket()
{
player = null;
}
public static String readString(ByteBuf in) throws IOException {
byte[] stringBytes = new byte[in.readInt()];
in.readBytes(stringBytes);
return new String(stringBytes, Charsets.UTF_8);
}
public static String readString(ByteBuf in) throws IOException
{
byte[] stringBytes = new byte[in.readInt()];
in.readBytes(stringBytes);
return new String(stringBytes, Charsets.UTF_8);
}
public static void writeString(String string, ByteBuf out) throws IOException {
byte[] stringBytes;
stringBytes = string.getBytes(Charsets.UTF_8);
out.writeInt(stringBytes.length);
out.writeBytes(stringBytes);
}
public static void writeString(String string, ByteBuf out)
throws IOException
{
byte[] stringBytes;
stringBytes = string.getBytes(Charsets.UTF_8);
out.writeInt(stringBytes.length);
out.writeBytes(stringBytes);
}
public static World readWorld(ByteBuf in) throws IOException {
return DimensionManager.getWorld(in.readInt());
}
public static World readWorld(ByteBuf in) throws IOException
{
return DimensionManager.getWorld(in.readInt());
}
public static void writeWorld(World world, ByteBuf out) throws IOException {
out.writeInt(world.provider.dimensionId);
}
public static void writeWorld(World world, ByteBuf out) throws IOException
{
out.writeInt(world.provider.dimensionId);
}
public static EntityPlayer readPlayer(ByteBuf in) throws IOException {
if (!in.readBoolean())
return null;
World playerWorld = readWorld(in);
return playerWorld.getPlayerEntityByName(readString(in));
}
public static EntityPlayer readPlayer(ByteBuf in) throws IOException
{
if (!in.readBoolean())
return null;
World playerWorld = readWorld(in);
return playerWorld.getPlayerEntityByName(readString(in));
}
public static void writePlayer(EntityPlayer player, ByteBuf out) throws IOException {
if (player == null) {
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
writeWorld(player.worldObj, out);
writeString(player.getCommandSenderName(), out);
}
public static void writePlayer(EntityPlayer player, ByteBuf out)
throws IOException
{
if (player == null)
{
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
writeWorld(player.worldObj, out);
writeString(player.getCommandSenderName(), out);
}
public static TileEntity readTileEntity(ByteBuf in) throws IOException {
return readWorld(in).getTileEntity(in.readInt(), in.readInt(), in.readInt());
}
public static TileEntity readTileEntity(ByteBuf in) throws IOException
{
return readWorld(in).getTileEntity(in.readInt(), in.readInt(),
in.readInt());
}
public static void writeTileEntity(TileEntity tileEntity, ByteBuf out) throws IOException {
writeWorld(tileEntity.getWorldObj(), out);
out.writeInt(tileEntity.xCoord);
out.writeInt(tileEntity.yCoord);
out.writeInt(tileEntity.zCoord);
}
public static void writeTileEntity(TileEntity tileEntity, ByteBuf out)
throws IOException
{
writeWorld(tileEntity.getWorldObj(), out);
out.writeInt(tileEntity.xCoord);
out.writeInt(tileEntity.yCoord);
out.writeInt(tileEntity.zCoord);
}
public static Fluid readFluid(ByteBuf in) throws IOException {
return FluidRegistry.getFluid(readString(in));
}
public static Fluid readFluid(ByteBuf in) throws IOException
{
return FluidRegistry.getFluid(readString(in));
}
public static void writeFluid(Fluid fluid, ByteBuf out) throws IOException {
if (fluid == null) {
writeString("", out);
return;
}
writeString(fluid.getName(), out);
}
public static void writeFluid(Fluid fluid, ByteBuf out) throws IOException
{
if (fluid == null)
{
writeString("", out);
return;
}
writeString(fluid.getName(), out);
}
public void writePacketData(ByteBuf out) throws IOException {
out.writeByte(mode);
writePlayer(player, out);
writeData(out);
}
public void writePacketData(ByteBuf out) throws IOException
{
out.writeByte(mode);
writePlayer(player, out);
writeData(out);
}
public abstract void writeData(ByteBuf out) throws IOException;
public abstract void writeData(ByteBuf out) throws IOException;
public void readPacketData(ByteBuf in) throws IOException {
mode = in.readByte();
player = readPlayer(in);
readData(in);
}
public void readPacketData(ByteBuf in) throws IOException
{
mode = in.readByte();
player = readPlayer(in);
readData(in);
}
public abstract void readData(ByteBuf in) throws IOException;
public abstract void readData(ByteBuf in) throws IOException;
public abstract void execute();
public abstract void execute();
public void sendPacketToServer() {
PacketHandler.sendPacketToServer(this);
}
public void sendPacketToServer()
{
PacketHandler.sendPacketToServer(this);
}
public void sendPacketToPlayer(EntityPlayer player) {
PacketHandler.sendPacketToPlayer(this, player);
}
public void sendPacketToPlayer(EntityPlayer player)
{
PacketHandler.sendPacketToPlayer(this, player);
}
public void sendPacketToAllPlayers() {
PacketHandler.sendPacketToAllPlayers(this);
}
public void sendPacketToAllPlayers()
{
PacketHandler.sendPacketToAllPlayers(this);
}
}