Started work on the farm, some changes to the idsu, its still broken
This commit is contained in:
parent
be33bdc148
commit
c160abf176
26 changed files with 626 additions and 194 deletions
50
src/main/java/techreborn/packets/AbstractPacket.java
Normal file
50
src/main/java/techreborn/packets/AbstractPacket.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
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);
|
||||
}
|
|
@ -21,8 +21,6 @@ public class PacketHandler extends
|
|||
|
||||
public PacketHandler() {
|
||||
addDiscriminator(0, PacketAesu.class);
|
||||
addDiscriminator(1, PacketIdsu.class);
|
||||
addDiscriminator(2, PacketSendIDSUManager.class);
|
||||
}
|
||||
|
||||
public static EnumMap<Side, FMLEmbeddedChannel> getChannels()
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
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 techreborn.tiles.idsu.TileIDSU;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by mark on 17/06/15.
|
||||
*/
|
||||
public class PacketIdsu extends SimplePacket {
|
||||
|
||||
|
||||
public class PacketIdsu extends AbstractPacket {
|
||||
public PacketIdsu() {
|
||||
}
|
||||
|
||||
|
@ -21,6 +18,8 @@ public class PacketIdsu extends SimplePacket {
|
|||
|
||||
TileIDSU idsu;
|
||||
|
||||
int x, y, z;
|
||||
|
||||
public PacketIdsu(int buttonID, TileIDSU idsu, int channel, String newName) {
|
||||
this.idsu = idsu;
|
||||
this.buttonID = buttonID;
|
||||
|
@ -28,28 +27,45 @@ public class PacketIdsu extends SimplePacket {
|
|||
this.newName = newName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(ByteBuf out) throws IOException {
|
||||
SimplePacket.writeTileEntity(idsu, out);
|
||||
out.writeInt(buttonID);
|
||||
out.writeInt(channel);
|
||||
PacketBuffer buffer = new PacketBuffer(out);
|
||||
buffer.writeStringToBuffer(newName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readData(ByteBuf in) throws IOException {
|
||||
this.idsu = (TileIDSU) SimplePacket.readTileEntity(in);
|
||||
buttonID = in.readInt();
|
||||
channel = in.readInt();
|
||||
PacketBuffer buffer = new PacketBuffer(in);
|
||||
newName = buffer.readStringFromBuffer(9);
|
||||
}
|
||||
@Override
|
||||
public void encodeInto(ChannelHandlerContext ctx, ByteBuf out) {
|
||||
out.writeInt(idsu.xCoord);
|
||||
out.writeInt(idsu.yCoord);
|
||||
out.writeInt(idsu.zCoord);
|
||||
out.writeInt(buttonID);
|
||||
out.writeInt(channel);
|
||||
PacketBuffer buffer = new PacketBuffer(out);
|
||||
try {
|
||||
buffer.writeStringToBuffer(newName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
if(!idsu.getWorldObj().isRemote){
|
||||
idsu.handleGuiInputFromClient(buttonID, channel, player, newName);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void decodeInto(ChannelHandlerContext ctx, ByteBuf in) {
|
||||
this.x = in.readInt();
|
||||
this.y = in.readInt();
|
||||
this.z = in.readInt();
|
||||
buttonID = in.readInt();
|
||||
channel = in.readInt();
|
||||
PacketBuffer buffer = new PacketBuffer(in);
|
||||
try {
|
||||
newName = buffer.readStringFromBuffer(9999999);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleClientSide(EntityPlayer player) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleServerSide(EntityPlayer player) {
|
||||
idsu = (TileIDSU) player.getEntityWorld().getTileEntity(x, y, z);
|
||||
idsu.handleGuiInputFromClient(buttonID, channel, player, newName);
|
||||
}
|
||||
}
|
||||
|
|
229
src/main/java/techreborn/packets/PacketPipeline.java
Normal file
229
src/main/java/techreborn/packets/PacketPipeline.java
Normal file
|
@ -0,0 +1,229 @@
|
|||
package techreborn.packets;
|
||||
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 initalise() {
|
||||
this.channels = NetworkRegistry.INSTANCE.newChannel("TechRebornPackPipeLine", this);
|
||||
registerPackets();
|
||||
}
|
||||
|
||||
private void registerPackets() {
|
||||
registerPacket(PacketIdsu.class);
|
||||
registerPacket(PacketSendIDSUManager.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
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 net.minecraft.world.World;
|
||||
|
@ -11,34 +12,39 @@ import techreborn.tiles.idsu.IDSUManager;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
public class PacketSendIDSUManager extends SimplePacket {
|
||||
public class PacketSendIDSUManager extends AbstractPacket {
|
||||
|
||||
String json;
|
||||
|
||||
public PacketSendIDSUManager() {
|
||||
}
|
||||
|
||||
public PacketSendIDSUManager(String json, EntityPlayer player) {
|
||||
public PacketSendIDSUManager(String json) {
|
||||
this.json = json;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(ByteBuf out) throws IOException {
|
||||
public void encodeInto(ChannelHandlerContext ctx, ByteBuf out) {
|
||||
PacketBuffer buffer = new PacketBuffer(out);
|
||||
buffer.writeStringToBuffer(json);
|
||||
writePlayer(player, out);
|
||||
}
|
||||
try {
|
||||
buffer.writeStringToBuffer(json);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readData(ByteBuf in) throws IOException {
|
||||
public void decodeInto(ChannelHandlerContext ctx, ByteBuf in) {
|
||||
PacketBuffer buffer = new PacketBuffer(in);
|
||||
json = buffer.readStringFromBuffer(Integer.MAX_VALUE / 4);
|
||||
player = readPlayer(in);
|
||||
}
|
||||
try {
|
||||
json = buffer.readStringFromBuffer(9999999);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
public void handleClientSide(EntityPlayer player) {
|
||||
ClientSideIDSUManager.CLIENT.loadFromString(json, player.worldObj);
|
||||
|
||||
if(player.worldObj != null){
|
||||
|
@ -57,4 +63,9 @@ public class PacketSendIDSUManager extends SimplePacket {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleServerSide(EntityPlayer player) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue