Skip to content

Commit

Permalink
Fixed the crash bug when attempting to swap fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Mar 15, 2014
1 parent ca4dfc6 commit 854f92b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
Expand Up @@ -141,6 +141,7 @@ public void registerPackets ()
registerPacket(PacketSmeltery.class);
registerPacket(PacketStencilTable.class);
registerPacket(PacketToolStation.class);
registerPacket(PacketUpdateTE.class);
}

// Method to call from FMLPostInitializationEvent
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/tconstruct/util/network/packet/PacketSmeltery.java
@@ -1,13 +1,16 @@
package tconstruct.util.network.packet;

import cpw.mods.fml.common.FMLCommonHandler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import tconstruct.TConstruct;
import tconstruct.blocks.logic.SmelteryLogic;
import cpw.mods.fml.common.FMLCommonHandler;

public class PacketSmeltery extends AbstractPacket
{
Expand Down Expand Up @@ -65,16 +68,12 @@ public void handleServerSide (EntityPlayer player)
World world = player.worldObj;

TileEntity te = world.getTileEntity(x, y, z);

if (te instanceof SmelteryLogic)
{
FluidStack temp = null;

for (FluidStack liquid : ((SmelteryLogic) te).moltenMetal)
{// TODO
// update
// fluid
// stuffs
{
if (liquid.fluidID == fluidID)
{
temp = liquid;
Expand All @@ -89,11 +88,10 @@ public void handleServerSide (EntityPlayer player)
else
((SmelteryLogic) te).moltenMetal.add(0, temp);
}
// TODO check if this works like it should
// Old code:
// PacketDispatcher.sendPacketToAllInDimension(te.getDescriptionPacket(),
// dimension);
FMLCommonHandler.instance().getClientToServerNetworkManager().scheduleOutboundPacket(te.getDescriptionPacket());

NBTTagCompound data = new NBTTagCompound();
te.writeToNBT(data);
TConstruct.packetPipeline.sendToDimension(new PacketUpdateTE(x, y, z, data), dimension);
}
}

Expand Down
69 changes: 69 additions & 0 deletions src/main/java/tconstruct/util/network/packet/PacketUpdateTE.java
@@ -0,0 +1,69 @@
package tconstruct.util.network.packet;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;

public class PacketUpdateTE extends AbstractPacket {
private int x, y, z;
private NBTTagCompound data;

public PacketUpdateTE() {

}

public PacketUpdateTE(int x, int y, int z, NBTTagCompound data) {
this.x = x;
this.y = y;
this.z = z;
this.data = data;
}

@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
System.out.println("Encoding...");
PacketBuffer pbuff = new PacketBuffer(buffer);
pbuff.writeInt(x);
pbuff.writeShort(y);
pbuff.writeInt(z);
try {
pbuff.writeNBTTagCompoundToBuffer(data);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
System.out.println("Decoding...");
PacketBuffer pbuff = new PacketBuffer(buffer);
x = pbuff.readInt();
y = pbuff.readShort();
z = pbuff.readInt();
try {
data = pbuff.readNBTTagCompoundFromBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void handleClientSide(EntityPlayer player) {
System.out.println("Handling client...");
TileEntity te = player.worldObj.getTileEntity(x, y, z);

if (te != null) {
te.readFromNBT(data);
}
}

@Override
public void handleServerSide(EntityPlayer player) {
System.out.println("Handling server...");
}

}

0 comments on commit 854f92b

Please sign in to comment.