Moved to RebornCore

This commit is contained in:
modmuss50 2015-11-08 12:15:45 +00:00
parent 50a830a101
commit 8abf6e5282
313 changed files with 3987 additions and 16508 deletions

View file

@ -1,50 +0,0 @@
package techreborn.packets;
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
*
* @param ctx channel context
* @param buffer the buffer to encode into
* @link{cpw.mods.fml.common.network.ByteBuffUtils )
*/
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
*
* @param ctx channel context
* @param buffer the buffer to decode from
* @link{cpw.mods.fml.common.network.ByteBuffUtils )
*/
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 powerSink side. Note this occurs after decoding has
* completed.
*
* @param player the player reference
*/
public abstract void handleServerSide(EntityPlayer player);
}

View file

@ -1,6 +1,7 @@
package techreborn.packets;
import io.netty.buffer.ByteBuf;
import reborncore.common.packets.SimplePacket;
import techreborn.tiles.TileAesu;
import java.io.IOException;

View file

@ -1,84 +0,0 @@
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;
public PacketHandler() {
addDiscriminator(0, PacketAesu.class);
}
public static EnumMap<Side, FMLEmbeddedChannel> getChannels() {
return channels;
}
public static void setChannels(EnumMap<Side, FMLEmbeddedChannel> _channels) {
channels = _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 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(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 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 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,71 +1,42 @@
package techreborn.packets;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.PacketBuffer;
import reborncore.common.packets.SimplePacket;
import techreborn.tiles.idsu.TileIDSU;
import java.io.IOException;
public class PacketIdsu extends AbstractPacket {
public class PacketIdsu extends SimplePacket {
public PacketIdsu() {
}
int buttonID, channel;
String newName;
int buttonID;
TileIDSU idsu;
int x, y, z;
public PacketIdsu(int buttonID, TileIDSU idsu, int channel, String newName) {
this.idsu = idsu;
public PacketIdsu(int buttonID, TileIDSU aesu) {
this.idsu = aesu;
this.buttonID = buttonID;
this.channel = channel;
this.newName = newName;
}
@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf out) {
out.writeInt(idsu.xCoord);
out.writeInt(idsu.yCoord);
out.writeInt(idsu.zCoord);
public void writeData(ByteBuf out) throws IOException {
SimplePacket.writeTileEntity(idsu, out);
out.writeInt(buttonID);
out.writeInt(channel);
PacketBuffer buffer = new PacketBuffer(out);
try {
buffer.writeStringToBuffer(newName);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf in) {
this.x = in.readInt();
this.y = in.readInt();
this.z = in.readInt();
public void readData(ByteBuf in) throws IOException {
this.idsu = (TileIDSU) SimplePacket.readTileEntity(in);
buttonID = in.readInt();
channel = in.readInt();
PacketBuffer buffer = new PacketBuffer(in);
try {
newName = buffer.readStringFromBuffer(9999999);
} catch (IOException e) {
e.printStackTrace();
}
@Override
public void execute() {
if (!idsu.getWorldObj().isRemote) {
idsu.handleGuiInputFromClient(buttonID);
}
}
@Override
public void handleClientSide(EntityPlayer player) {
}
@Override
public void handleServerSide(EntityPlayer player) {
idsu = (TileIDSU) player.getEntityWorld().getTileEntity(x, y, z);
idsu.handleGuiInputFromClient(buttonID);
}
}

View file

@ -1,224 +0,0 @@
package techreborn.packets;
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;
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 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 java.util.*;
/**
* 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 initalise() {
this.channels = NetworkRegistry.INSTANCE.newChannel("TechRebornPackPipeLine", this);
registerPackets();
}
private void registerPackets() {
registerPacket(PacketIdsu.class);
}
// Method to call from FMLPostInitializationEvent
// Ensures that packet discriminators are common between powerSink 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 powerSink.
* <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);
}
}

View file

@ -1,122 +0,0 @@
package techreborn.packets;
import com.google.common.base.Charsets;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import java.io.IOException;
public abstract class SimplePacket {
protected EntityPlayer player;
protected byte mode;
public SimplePacket(EntityPlayer _player) {
player = _player;
}
@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 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 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 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 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 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 abstract void writeData(ByteBuf out) throws IOException;
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 execute();
public void sendPacketToServer() {
PacketHandler.sendPacketToServer(this);
}
public void sendPacketToPlayer(EntityPlayer player) {
PacketHandler.sendPacketToPlayer(this, player);
}
public void sendPacketToAllPlayers() {
PacketHandler.sendPacketToAllPlayers(this);
}
}