aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/net/GT_Packet_SetLockedFluid.java
blob: 04f147b20821b25763faa8516708382870543b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package gregtech.api.net;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

import com.google.common.io.ByteArrayDataInput;

import gregtech.api.interfaces.metatileentity.IFluidLockable;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.GT_LanguageManager;
import gregtech.api.util.GT_Utility;
import io.netty.buffer.ByteBuf;

public class GT_Packet_SetLockedFluid extends GT_Packet_New {

    protected int mX;
    protected short mY;
    protected int mZ;

    protected int mFluidID;

    private EntityPlayerMP mPlayer;

    public GT_Packet_SetLockedFluid() {
        super(true);
    }

    public GT_Packet_SetLockedFluid(IGregTechTileEntity aTile, FluidStack aSource) {
        this(aTile.getXCoord(), aTile.getYCoord(), aTile.getZCoord(), aSource.getFluidID());
    }

    public GT_Packet_SetLockedFluid(int x, short y, int z, int aFluidID) {
        super(false);

        this.mX = x;
        this.mY = y;
        this.mZ = z;

        this.mFluidID = aFluidID;
    }

    @Override
    public byte getPacketID() {
        return 14;
    }

    @Override
    public void encode(ByteBuf aOut) {
        aOut.writeInt(mX);
        aOut.writeShort(mY);
        aOut.writeInt(mZ);

        aOut.writeInt(mFluidID);
    }

    @Override
    public void setINetHandler(INetHandler aHandler) {
        if (aHandler instanceof NetHandlerPlayServer) {
            mPlayer = ((NetHandlerPlayServer) aHandler).playerEntity;
        }
    }

    @Override
    public GT_Packet_New decode(ByteArrayDataInput aData) {
        return new GT_Packet_SetLockedFluid(aData.readInt(), aData.readShort(), aData.readInt(), aData.readInt());
    }

    @Override
    public void process(IBlockAccess aWorld) {
        if (mPlayer == null) return;
        World world = mPlayer.worldObj;
        TileEntity tile = world.getTileEntity(mX, mY, mZ);
        if (!(tile instanceof IGregTechTileEntity) || ((IGregTechTileEntity) tile).isDead()) return;
        IMetaTileEntity mte = ((IGregTechTileEntity) tile).getMetaTileEntity();
        if (!(mte instanceof IFluidLockable mteToLock)) return;
        Fluid tFluid = FluidRegistry.getFluid(mFluidID);
        if (tFluid == null) return;
        if (!mteToLock.allowChangingLockedFluid(tFluid.getName())) return;

        mteToLock.lockFluid(true);
        mteToLock.setLockedFluidName(tFluid.getName());
        GT_Utility.sendChatToPlayer(
            mPlayer,
            String.format(
                GT_LanguageManager.addStringLocalization(
                    "Interaction_DESCRIPTION_Index_151.4",
                    "Successfully locked Fluid to %s",
                    false),
                new FluidStack(tFluid, 1).getLocalizedName()));

        mteToLock.onFluidLockPacketReceived(tFluid.getName());
    }
}