aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/GoodGenerator/Common/Container/NeutronActivatorGUIContainer.java
blob: 793949e1ff66244e51a5b920be1f6a90a7741d28 (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
package GoodGenerator.Common.Container;

import GoodGenerator.Blocks.TEs.NeutronActivator;
import com.github.technus.tectech.thing.metaTileEntity.multi.base.GT_Container_MultiMachineEM;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ICrafting;

import java.nio.ByteBuffer;

public class NeutronActivatorGUIContainer extends GT_Container_MultiMachineEM {

    private int KineticE;

    private ByteBuffer buffer;

    public NeutronActivatorGUIContainer(InventoryPlayer inventoryPlayer, IGregTechTileEntity aTileEntity) {
        super(inventoryPlayer, aTileEntity, true, true, true);
    }

    @Override
    public void addCraftingToCrafters(ICrafting clientHandle) {
        buffer.putInt(0, KineticE);
        sendStateUpdate(clientHandle);
        super.addCraftingToCrafters(clientHandle);
    }

    @Override
    public void detectAndSendChanges() {
        super.detectAndSendChanges();
        if (buffer == null) {
            buffer = ByteBuffer.allocate(Integer.BYTES);
        }
        if(mTileEntity.isServerSide()) {
            NeutronActivator tile = (NeutronActivator) mTileEntity.getMetaTileEntity();
            int currentKineticE = tile.getCurrentNeutronKineticEnergy();
            boolean isUpdated = false;
            if (currentKineticE != KineticE) {
                KineticE = currentKineticE;
                buffer.putInt(0, KineticE);
                isUpdated = true;
            }
            for (Object clientHandle : this.crafters) {
                if (isUpdated) {
                    sendStateUpdate((ICrafting) clientHandle);
                }
            }
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return true;
    }

    private void sendStateUpdate(ICrafting clientHandle) {
        final int bytes = Integer.BYTES;
        for (int i = 0; i < bytes; i++) {
            clientHandle.sendProgressBarUpdate(this, i + 21, buffer.get(i));
        }
    }

    @SideOnly(Side.CLIENT)
    public void updateProgressBar(int index, int value) {
        super.updateProgressBar(index, value);
        index = index - 21;
        if(index >= 0 && index < buffer.capacity()) {
            buffer.put(index, (byte) value);
        }
    }

    @SideOnly(Side.CLIENT)
    public int getKineticE() {
        return buffer.getInt(0);
    }
}