-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileLaserTableBase.java
230 lines (193 loc) · 5.87 KB
/
TileLaserTableBase.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.silicon;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.BuildCraftCore;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.power.ILaserTarget;
import buildcraft.core.IMachine;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.triggers.ActionMachineControl;
import buildcraft.core.utils.AverageUtil;
public abstract class TileLaserTableBase extends TileBuildCraft implements ILaserTarget, IInventory, IActionReceptor, IMachine {
public double clientRequiredEnergy = 0;
protected SimpleInventory inv = new SimpleInventory(getSizeInventory(), "inv", 64);
protected ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private double energy = 0;
private int recentEnergyAverage;
private AverageUtil recentEnergyAverageUtil = new AverageUtil(20);
@Override
public void updateEntity() {
super.updateEntity();
recentEnergyAverageUtil.tick();
}
public double getEnergy() {
return energy;
}
public void setEnergy(double energy) {
this.energy = energy;
}
public void addEnergy(double energy) {
this.energy += energy;
}
public void subtractEnergy(double energy) {
this.energy -= energy;
}
public abstract double getRequiredEnergy();
public int getProgressScaled(int ratio) {
if (clientRequiredEnergy == 0.0) {
return 0;
} else if (energy >= clientRequiredEnergy) {
return ratio;
} else {
return (int) (energy / clientRequiredEnergy * ratio);
}
}
public int getRecentEnergyAverage() {
return recentEnergyAverage;
}
public abstract boolean canCraft();
@Override
public boolean requiresLaserEnergy() {
return canCraft() && energy < getRequiredEnergy() * 5F;
}
@Override
public void receiveLaserEnergy(double energy) {
this.energy += energy;
recentEnergyAverageUtil.push(energy);
}
@Override
public boolean isInvalidTarget() {
return isInvalid();
}
@Override
public double getXCoord() {
return xCoord;
}
@Override
public double getYCoord() {
return yCoord;
}
@Override
public double getZCoord() {
return zCoord;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inv.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
return inv.decrStackSize(slot, amount);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inv.setInventorySlotContents(slot, stack);
}
@Override
public int getInventoryStackLimit() {
return inv.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && !isInvalid();
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
inv.writeToNBT(nbt, "inv");
nbt.setDouble("energy", energy);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
inv.readFromNBT(nbt, "inv");
energy = nbt.getDouble("energy");
}
public void getGUINetworkData(int id, int data) {
int currentStored = (int) (energy * 100.0);
int requiredEnergy = (int) (clientRequiredEnergy * 100.0);
switch (id) {
case 0:
requiredEnergy = (requiredEnergy & 0xFFFF0000) | (data & 0xFFFF);
clientRequiredEnergy = requiredEnergy / 100.0f;
break;
case 1:
currentStored = (currentStored & 0xFFFF0000) | (data & 0xFFFF);
energy = currentStored / 100.0f;
break;
case 2:
requiredEnergy = (requiredEnergy & 0xFFFF) | ((data & 0xFFFF) << 16);
clientRequiredEnergy = requiredEnergy / 100.0f;
break;
case 3:
currentStored = (currentStored & 0xFFFF) | ((data & 0xFFFF) << 16);
energy = currentStored / 100.0f;
break;
case 4:
recentEnergyAverage = recentEnergyAverage & 0xFFFF0000 | (data & 0xFFFF);
break;
case 5:
recentEnergyAverage = (recentEnergyAverage & 0xFFFF) | ((data & 0xFFFF) << 16);
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int requiredEnergy = (int) (getRequiredEnergy() * 100.0);
int currentStored = (int) (energy * 100.0);
int lRecentEnergy = (int) (recentEnergyAverageUtil.getAverage() * 100f);
iCrafting.sendProgressBarUpdate(container, 0, requiredEnergy & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 1, currentStored & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 2, (requiredEnergy >>> 16) & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 3, (currentStored >>> 16) & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 4, lRecentEnergy & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 5, (lRecentEnergy >>> 16) & 0xFFFF);
}
@Override
public boolean isActive() {
return lastMode != ActionMachineControl.Mode.Off;
}
@Override
public boolean manageFluids() {
return false;
}
@Override
public boolean manageSolids() {
return false;
}
@Override
public boolean allowAction(IAction action) {
return action == BuildCraftCore.actionOn || action == BuildCraftCore.actionOff;
}
@Override
public void actionActivated(IAction action) {
if (action == BuildCraftCore.actionOn) {
lastMode = ActionMachineControl.Mode.On;
} else if (action == BuildCraftCore.actionOff) {
lastMode = ActionMachineControl.Mode.Off;
}
}
}