-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1506 from Dimach/flask_fix
Fix flask crash on server
- Loading branch information
Showing
3 changed files
with
45 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 42 additions & 17 deletions
59
src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,67 @@ | ||
package gregtech.common.net; | ||
|
||
import cpw.mods.fml.common.network.simpleimpl.IMessage; | ||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; | ||
import cpw.mods.fml.common.network.simpleimpl.MessageContext; | ||
import com.google.common.io.ByteArrayDataInput; | ||
import com.google.common.io.ByteArrayDataOutput; | ||
import com.google.common.io.ByteStreams; | ||
import gregtech.api.net.GT_Packet; | ||
import gregtech.common.items.GT_VolumetricFlask; | ||
import io.netty.buffer.ByteBuf; | ||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.world.IBlockAccess; | ||
import net.minecraft.world.World; | ||
import net.minecraftforge.common.DimensionManager; | ||
|
||
public final class MessageSetFlaskCapacity implements IMessage { | ||
private int capacity; | ||
public final class MessageSetFlaskCapacity extends GT_Packet { | ||
private int capacity, dimID, playerID; | ||
|
||
public MessageSetFlaskCapacity() { | ||
super(true); | ||
} | ||
|
||
public MessageSetFlaskCapacity(int capacity) { | ||
public MessageSetFlaskCapacity(int capacity, int dimID, int playerID) { | ||
super(false); | ||
this.capacity = capacity; | ||
this.dimID = dimID; | ||
this.playerID = playerID; | ||
} | ||
|
||
public MessageSetFlaskCapacity(int capacity, EntityPlayer p) { | ||
super(false); | ||
this.capacity = capacity; | ||
this.dimID = p.worldObj.provider.dimensionId; | ||
this.playerID = p.getEntityId(); | ||
} | ||
|
||
@Override | ||
public byte getPacketID() { | ||
return 5; | ||
} | ||
|
||
public void fromBytes(ByteBuf buf) { | ||
this.capacity = buf.readInt(); | ||
@Override | ||
public byte[] encode() { | ||
ByteArrayDataOutput tOut = ByteStreams.newDataOutput(10); | ||
tOut.writeInt(capacity); | ||
tOut.writeInt(dimID); | ||
tOut.writeInt(playerID); | ||
return tOut.toByteArray(); | ||
} | ||
|
||
public void toBytes(ByteBuf buf) { | ||
buf.writeInt(this.capacity); | ||
@Override | ||
public GT_Packet decode(ByteArrayDataInput aData) { | ||
return new MessageSetFlaskCapacity(aData.readInt(), aData.readInt(), aData.readInt()); | ||
} | ||
|
||
public static final class Handler | ||
implements IMessageHandler<MessageSetFlaskCapacity, IMessage> { | ||
public IMessage onMessage(MessageSetFlaskCapacity message, MessageContext ctx) { | ||
ItemStack stack = ctx.getServerHandler().playerEntity.getHeldItem(); | ||
@Override | ||
public void process(IBlockAccess aWorld) { | ||
World w = DimensionManager.getWorld(dimID); | ||
if (w != null && w.getEntityByID(playerID) instanceof EntityPlayer) { | ||
ItemStack stack = ((EntityPlayer) w.getEntityByID(playerID)).getHeldItem(); | ||
if ((stack != null) && (stack.stackSize > 0)) { | ||
Item item = stack.getItem(); | ||
if ((item instanceof GT_VolumetricFlask)) | ||
((GT_VolumetricFlask) item).setCapacity(stack, message.capacity); | ||
((GT_VolumetricFlask) item).setCapacity(stack, capacity); | ||
} | ||
return null; | ||
} | ||
} | ||
} |