aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/net/GT_Packet_SetConfigurationCircuit.java
blob: 2bfdbca9d26142532e3170600e8788306a06a8e5 (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
102
103
104
105
106
107
108
109
110
package gregtech.api.net;

import net.minecraft.item.ItemStack;
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.common.DimensionManager;

import com.google.common.io.ByteArrayDataInput;

import cpw.mods.fml.common.network.ByteBufUtils;
import gregtech.api.interfaces.IConfigurationCircuitSupport;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.interfaces.tileentity.IHasInventory;
import gregtech.api.metatileentity.BaseTileEntity;
import gregtech.api.util.GT_Utility;
import gregtech.api.util.ISerializableObject;
import io.netty.buffer.ByteBuf;

/**
 * Client -> Server: Update machine configuration data
 */
public class GT_Packet_SetConfigurationCircuit extends GT_Packet_New {

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

    protected ItemStack circuit;

    public GT_Packet_SetConfigurationCircuit() {
        super(true);
    }

    public GT_Packet_SetConfigurationCircuit(IGregTechTileEntity tile, ItemStack circuit) {
        this(tile.getXCoord(), tile.getYCoord(), tile.getZCoord(), circuit);
    }

    public GT_Packet_SetConfigurationCircuit(BaseTileEntity tile, ItemStack circuit) {
        this(tile.getXCoord(), tile.getYCoord(), tile.getZCoord(), circuit);
    }

    public GT_Packet_SetConfigurationCircuit(int x, short y, int z, ItemStack circuit) {
        super(false);

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

        this.circuit = circuit;
    }

    @Override
    public byte getPacketID() {
        return GT_PacketTypes.SET_CONFIGURATION_CIRCUIT.id;
    }

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

        // no null check needed. ByteBufUtils will handle it
        ByteBufUtils.writeItemStack(aOut, this.circuit);
    }

    @Override
    public void setINetHandler(INetHandler aHandler) {
        if (aHandler instanceof NetHandlerPlayServer) {
            dimId = ((NetHandlerPlayServer) aHandler).playerEntity.dimension;
        } else {
            // packet sent to wrong side, so we need to ignore this one
            // but there is no way to disrupt packet pipeline
            // so we will instead go find world -2, which (hopefully) doesn't exist
            // then we will fail silently in process()
            dimId = -2;
        }
    }

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

    @Override
    public void process(IBlockAccess aWorld) {
        final World world = DimensionManager.getWorld(dimId);
        if (world == null) return;

        final TileEntity tile = world.getTileEntity(mX, mY, mZ);
        if (!(tile instanceof BaseTileEntity) || ((BaseTileEntity) tile).isDead()) return;

        final IConfigurationCircuitSupport machine = ((BaseTileEntity) tile).getConfigurationCircuitSupport();
        if (machine == null) return;
        if (!machine.allowSelectCircuit()) return;
        machine.getConfigurationCircuits()
            .stream()
            .filter(stack -> GT_Utility.areStacksEqual(stack, circuit))
            .findFirst()
            .ifPresent(stack -> ((IHasInventory) tile).setInventorySlotContents(machine.getCircuitSlot(), stack));
    }
}