aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/goodgenerator/common/container/YOTTankGUIContainer.java
blob: 0a425e358e5fd19227fa08525c95e268ae4f2df2 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package goodgenerator.common.container;

import goodgenerator.blocks.tileEntity.YottaFluidTank;
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 YOTTankGUIContainer extends GT_Container_MultiMachineEM {

    private String currentStore = "";
    private String store = "";
    private String fluidName = "";

    private ByteBuffer buffer;

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

    @Override
    public void addCraftingToCrafters(ICrafting clientHandle) {
        buffer.putInt(0, currentStore.length());
        buffer.putInt(Integer.BYTES, store.length());
        buffer.putInt(Integer.BYTES * 2, fluidName.length());
        for (int i = 0; i < currentStore.length(); ++i) {
            buffer.putChar(Integer.BYTES * 3 + Character.BYTES * i, currentStore.charAt(i));
        }
        for (int i = 0; i < store.length(); ++i) {
            buffer.putChar(Integer.BYTES * 3 + Character.BYTES * (i + currentStore.length()), store.charAt(i));
        }
        for (int i = 0; i < fluidName.length(); ++i) {
            buffer.putChar(Integer.BYTES * 3 + Character.BYTES * (i + currentStore.length() + store.length()), fluidName.charAt(i));
        }
        sendStateUpdate(clientHandle);
        super.addCraftingToCrafters(clientHandle);
    }

    @Override
    public void detectAndSendChanges() {
        super.detectAndSendChanges();
        if (buffer == null) {
            buffer = ByteBuffer.allocate(8192);
        }
        if(mTileEntity.isServerSide()) {
            YottaFluidTank tile = (YottaFluidTank) mTileEntity.getMetaTileEntity();
            if (tile == null) return;
            String newStored = tile.getStored();
            String newCap = tile.getCap();
            String newFluid = tile.getFluidName();
            boolean isUpdated = false;
            if (!newStored.equals(currentStore) || !newCap.equals(store) || !newFluid.equals(fluidName)) {
                currentStore = newStored;
                store = newCap;
                fluidName = newFluid;
                buffer.putInt(0, currentStore.length());
                buffer.putInt(Integer.BYTES, store.length());
                buffer.putInt(Integer.BYTES * 2, fluidName.length());
                for (int i = 0; i < currentStore.length(); ++i) {
                    buffer.putChar(Integer.BYTES * 3 + Character.BYTES * i, currentStore.charAt(i));
                }
                for (int i = 0; i < store.length(); ++i) {
                    buffer.putChar(Integer.BYTES * 3 + Character.BYTES * (i + currentStore.length()), store.charAt(i));
                }
                for (int i = 0; i < fluidName.length(); ++i) {
                    buffer.putChar(Integer.BYTES * 3 + Character.BYTES * (i + currentStore.length() + store.length()), fluidName.charAt(i));
                }
                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 * 3 + Character.BYTES * (currentStore.length() + store.length() + fluidName.length());
        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 String getStorage() {
        StringBuilder sb = new StringBuilder();
        int startP = Integer.BYTES * 3;
        for (int i = 0; i < buffer.getInt(0); ++i) {
            sb.append(buffer.getChar(startP + Character.BYTES * i));
        }
        return sb.toString();
    }

    @SideOnly(Side.CLIENT)
    public String getCap() {
        StringBuilder sb = new StringBuilder();
        int startP = Integer.BYTES * 3 + Character.BYTES * buffer.getInt(0);
        for (int i = 0; i < buffer.getInt(Integer.BYTES); ++i) {
            sb.append(buffer.getChar(startP + Character.BYTES * i));
        }
        return sb.toString();
    }

    @SideOnly(Side.CLIENT)
    public String getFluidName() {
        StringBuilder sb = new StringBuilder();
        int startP = Integer.BYTES * 3 + Character.BYTES * (buffer.getInt(0) + buffer.getInt(Integer.BYTES));
        for (int i = 0; i < buffer.getInt(Integer.BYTES * 2); ++i) {
            sb.append(buffer.getChar(startP + Character.BYTES * i));
        }
        return sb.toString();
    }
}