Merge branch 'master' of https://github.com/calclavia/Resonant-Induction
This commit is contained in:
commit
717ef201fd
4 changed files with 131 additions and 16 deletions
|
@ -3,29 +3,126 @@
|
|||
*/
|
||||
package resonantinduction;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import resonantinduction.base.IPacketReceiver;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
import cpw.mods.fml.common.network.IPacketHandler;
|
||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
/**
|
||||
* @author Calclavia
|
||||
* @author AidanBrady
|
||||
*
|
||||
*/
|
||||
public class PacketHandler implements IPacketHandler
|
||||
{
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* cpw.mods.fml.common.network.IPacketHandler#onPacketData(net.minecraft.network.INetworkManager
|
||||
* , net.minecraft.network.packet.Packet250CustomPayload, cpw.mods.fml.common.network.Player)
|
||||
*/
|
||||
@Override
|
||||
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
|
||||
{
|
||||
|
||||
if(packet.channel == ResonantInduction.CHANNEL)
|
||||
{
|
||||
ByteArrayDataInput dataStream = ByteStreams.newDataInput(packet.data);
|
||||
EntityPlayer entityplayer = (EntityPlayer)player;
|
||||
World world = entityplayer.worldObj;
|
||||
|
||||
try {
|
||||
int packetType = dataStream.readInt();
|
||||
|
||||
if(packetType == PacketType.TILE.ordinal())
|
||||
{
|
||||
int x = dataStream.readInt();
|
||||
int y = dataStream.readInt();
|
||||
int z = dataStream.readInt();
|
||||
|
||||
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
|
||||
|
||||
if(tileEntity instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver)tileEntity).handle(dataStream);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
public static void encode(Object[] dataValues, DataOutputStream output)
|
||||
{
|
||||
try {
|
||||
for(Object data : dataValues)
|
||||
{
|
||||
if(data instanceof Integer)
|
||||
{
|
||||
output.writeInt((Integer)data);
|
||||
}
|
||||
else if(data instanceof Boolean)
|
||||
{
|
||||
output.writeBoolean((Boolean)data);
|
||||
}
|
||||
else if(data instanceof Double)
|
||||
{
|
||||
output.writeDouble((Double)data);
|
||||
}
|
||||
else if(data instanceof Float)
|
||||
{
|
||||
output.writeFloat((Float)data);
|
||||
}
|
||||
else if(data instanceof String)
|
||||
{
|
||||
output.writeUTF((String)data);
|
||||
}
|
||||
else if(data instanceof Byte)
|
||||
{
|
||||
output.writeByte((Byte)data);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {}
|
||||
}
|
||||
|
||||
public void sendTileEntityPacketToServer(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
PacketDispatcher.sendPacketToServer(getTileEntityPacket(tileEntity, dataValues));
|
||||
}
|
||||
|
||||
public void sendTileEntityPacketToClients(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllPlayers(getTileEntityPacket(tileEntity, dataValues));
|
||||
}
|
||||
|
||||
public Packet250CustomPayload getTileEntityPacket(TileEntity tileEntity, Object... dataValues)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try {
|
||||
data.writeInt(tileEntity.xCoord);
|
||||
data.writeInt(tileEntity.yCoord);
|
||||
data.writeInt(tileEntity.zCoord);
|
||||
|
||||
encode(dataValues, data);
|
||||
} catch(Exception e) {}
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = ResonantInduction.CHANNEL;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static enum PacketType
|
||||
{
|
||||
TILE
|
||||
}
|
||||
|
||||
}
|
||||
|
|
8
src/resonantinduction/base/IPacketReceiver.java
Normal file
8
src/resonantinduction/base/IPacketReceiver.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package resonantinduction.base;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
public interface IPacketReceiver
|
||||
{
|
||||
public void handle(ByteArrayDataInput input);
|
||||
}
|
|
@ -30,13 +30,11 @@ public class BlockEMContractor extends BlockBase implements ITileEntityProvider
|
|||
@Override
|
||||
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
|
||||
{
|
||||
if(par1World.isRemote)
|
||||
if(par1World.isRemote && !par5EntityPlayer.isSneaking())
|
||||
{
|
||||
//debug
|
||||
TileEntityEMContractor contractor = (TileEntityEMContractor)par1World.getBlockTileEntity(par2, par3, par4);
|
||||
int toSet = contractor.facing.ordinal() < 5 ? contractor.facing.ordinal()+1 : 0;
|
||||
contractor.facing = ForgeDirection.getOrientation(toSet);
|
||||
System.out.println(contractor.facing.ordinal());
|
||||
contractor.incrementFacing();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,4 +6,16 @@ import net.minecraftforge.common.ForgeDirection;
|
|||
public class TileEntityEMContractor extends TileEntity
|
||||
{
|
||||
public ForgeDirection facing = ForgeDirection.UP;
|
||||
|
||||
@Override
|
||||
public void updateEntity()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void incrementFacing()
|
||||
{
|
||||
int newOrdinal = facing.ordinal() < 5 ? facing.ordinal()+1 : 0;
|
||||
facing = ForgeDirection.getOrientation(newOrdinal);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue