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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
package gregtech.api.metatileentity.implementations;
import static gregtech.api.enums.GTValues.V;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.widget.SlotGroup;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GTUIInfos;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.modularui.IAddUIWidgets;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.items.MetaBaseItem;
import gregtech.api.util.GTModHandler;
import gregtech.api.util.GTUtility;
import ic2.api.item.IElectricItem;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
/**
* NEVER INCLUDE THIS FILE IN YOUR MOD!!!
* <p/>
* This is the main construct for my Basic Machines such as the Automatic Extractor Extend this class to make a simple
* Machine
*/
public class MTEBasicBatteryBuffer extends MTETieredMachineBlock implements IAddUIWidgets {
public boolean mCharge = false, mDecharge = false;
public int mBatteryCount = 0, mChargeableCount = 0;
private long count = 0;
private long mStored = 0;
private long mMax = 0;
public MTEBasicBatteryBuffer(int aID, String aName, String aNameRegional, int aTier, String aDescription,
int aSlotCount) {
super(aID, aName, aNameRegional, aTier, aSlotCount, aDescription);
}
public MTEBasicBatteryBuffer(String aName, int aTier, String aDescription, ITexture[][][] aTextures,
int aSlotCount) {
super(aName, aTier, aSlotCount, aDescription, aTextures);
}
public MTEBasicBatteryBuffer(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures,
int aSlotCount) {
super(aName, aTier, aSlotCount, aDescription, aTextures);
}
@Override
public String[] getDescription() {
String[] desc = new String[mDescriptionArray.length + 1];
System.arraycopy(mDescriptionArray, 0, desc, 0, mDescriptionArray.length);
desc[mDescriptionArray.length] = mInventory.length + " Slots";
return desc;
}
@Override
public ITexture[][][] getTextureSet(ITexture[] aTextures) {
ITexture[][][] rTextures = new ITexture[2][17][];
for (byte i = -1; i < 16; i++) {
rTextures[0][i + 1] = new ITexture[] { Textures.BlockIcons.MACHINE_CASINGS[mTier][i + 1] };
rTextures[1][i + 1] = new ITexture[] { Textures.BlockIcons.MACHINE_CASINGS[mTier][i + 1],
mInventory.length == 16 ? Textures.BlockIcons.OVERLAYS_ENERGY_OUT_POWER[mTier]
: mInventory.length > 4 ? Textures.BlockIcons.OVERLAYS_ENERGY_OUT_MULTI[mTier]
: Textures.BlockIcons.OVERLAYS_ENERGY_OUT[mTier] };
}
return rTextures;
}
@Override
public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, ForgeDirection side, ForgeDirection aFacing,
int colorIndex, boolean aActive, boolean redstoneLevel) {
return mTextures[side == aFacing ? 1 : 0][colorIndex + 1];
}
@Override
public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTEBasicBatteryBuffer(mName, mTier, mDescriptionArray, mTextures, mInventory.length);
}
@Override
public boolean isSimpleMachine() {
return false;
}
@Override
public boolean isElectric() {
return true;
}
@Override
public boolean isValidSlot(int aIndex) {
return true;
}
@Override
public boolean isFacingValid(ForgeDirection facing) {
return true;
}
@Override
public boolean isEnetInput() {
return true;
}
@Override
public boolean isEnetOutput() {
return true;
}
@Override
public boolean isInputFacing(ForgeDirection side) {
return side != getBaseMetaTileEntity().getFrontFacing();
}
@Override
public boolean isOutputFacing(ForgeDirection side) {
return side == getBaseMetaTileEntity().getFrontFacing();
}
@Override
public boolean isTeleporterCompatible() {
return false;
}
@Override
public long getMinimumStoredEU() {
return V[mTier] * 16L * mInventory.length;
}
@Override
public long maxEUStore() {
return V[mTier] * 64L * mInventory.length;
}
@Override
public long maxEUInput() {
return V[mTier];
}
@Override
public long maxEUOutput() {
return V[mTier];
}
@Override
public long maxAmperesIn() {
return mChargeableCount * 2L;
}
@Override
public long maxAmperesOut() {
return mBatteryCount;
}
@Override
public int rechargerSlotStartIndex() {
return 0;
}
@Override
public int dechargerSlotStartIndex() {
return 0;
}
@Override
public int rechargerSlotCount() {
return mCharge ? mInventory.length : 0;
}
@Override
public int dechargerSlotCount() {
return mDecharge ? mInventory.length : 0;
}
@Override
public int getProgresstime() {
return (int) getBaseMetaTileEntity().getUniversalEnergyStored();
}
@Override
public int maxProgresstime() {
return (int) getBaseMetaTileEntity().getUniversalEnergyCapacity();
}
@Override
public boolean isAccessAllowed(EntityPlayer aPlayer) {
return true;
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
//
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
//
}
@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
GTUIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer);
return true;
}
@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
if (aBaseMetaTileEntity.isServerSide()) {
mCharge = aBaseMetaTileEntity.getStoredEU() / 2 > aBaseMetaTileEntity.getEUCapacity() / 3;
mDecharge = aBaseMetaTileEntity.getStoredEU() < aBaseMetaTileEntity.getEUCapacity() / 3;
mBatteryCount = 0;
mChargeableCount = 0;
for (ItemStack tStack : mInventory) if (GTModHandler.isElectricItem(tStack, mTier)) {
if (GTModHandler.isChargerItem(tStack)) mBatteryCount++;
mChargeableCount++;
}
}
count++;
}
@Override
public boolean allowPullStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
if (GTModHandler.isElectricItem(aStack) && aStack.getUnlocalizedName()
.startsWith("gt.metaitem.01.")) {
String name = aStack.getUnlocalizedName();
if (name.equals("gt.metaitem.01.32510") || name.equals("gt.metaitem.01.32511")
|| name.equals("gt.metaitem.01.32520")
|| name.equals("gt.metaitem.01.32521")
|| name.equals("gt.metaitem.01.32530")
|| name.equals("gt.metaitem.01.32531")) {
return ic2.api.item.ElectricItem.manager.getCharge(aStack) == 0;
}
}
return false;
}
@Override
public boolean allowPutStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
if (!GTUtility.isStackValid(aStack)) {
return false;
}
return mInventory[aIndex] == null && GTModHandler.isElectricItem(aStack, this.mTier);
}
@Override
public int getInventoryStackLimit() {
return 1;
}
public long[] getStoredEnergy() {
boolean scaleOverflow = false;
boolean storedOverflow = false;
long tScale = getBaseMetaTileEntity().getEUCapacity();
long tStored = getBaseMetaTileEntity().getStoredEU();
long tStep = 0;
if (mInventory != null) {
for (ItemStack aStack : mInventory) {
if (GTModHandler.isElectricItem(aStack)) {
if (aStack.getItem() instanceof MetaBaseItem) {
Long[] stats = ((MetaBaseItem) aStack.getItem()).getElectricStats(aStack);
if (stats != null) {
if (stats[0] > Long.MAX_VALUE / 2) {
scaleOverflow = true;
}
tScale = tScale + stats[0];
tStep = ((MetaBaseItem) aStack.getItem()).getRealCharge(aStack);
if (tStep > Long.MAX_VALUE / 2) {
storedOverflow = true;
}
tStored = tStored + tStep;
}
} else if (aStack.getItem() instanceof IElectricItem) {
tStored = tStored + (long) ic2.api.item.ElectricItem.manager.getCharge(aStack);
tScale = tScale + (long) ((IElectricItem) aStack.getItem()).getMaxCharge(aStack);
}
}
}
}
if (scaleOverflow) {
tScale = Long.MAX_VALUE;
}
if (storedOverflow) {
tStored = Long.MAX_VALUE;
}
return new long[] { tStored, tScale };
}
@Override
public String[] getInfoData() {
updateStorageInfo();
return new String[] { EnumChatFormatting.BLUE + getLocalName() + EnumChatFormatting.RESET, "Stored Items:",
EnumChatFormatting.GREEN + GTUtility.formatNumbers(mStored)
+ EnumChatFormatting.RESET
+ " EU / "
+ EnumChatFormatting.YELLOW
+ GTUtility.formatNumbers(mMax)
+ EnumChatFormatting.RESET
+ " EU",
"Average input:", GTUtility.formatNumbers(getBaseMetaTileEntity().getAverageElectricInput()) + " EU/t",
"Average output:", GTUtility.formatNumbers(getBaseMetaTileEntity().getAverageElectricOutput()) + " EU/t" };
}
private void updateStorageInfo() {
if (mMax == 0 || (count > 20)) {
long[] tmp = getStoredEnergy();
mStored = tmp[0];
mMax = tmp[1];
count = 0;
}
}
@Override
public void getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor,
IWailaConfigHandler config) {
NBTTagCompound tag = accessor.getNBTData();
currenttip.add(
StatCollector.translateToLocalFormatted(
"GT5U.waila.energy.stored",
GTUtility.formatNumbers(tag.getLong("mStored")),
GTUtility.formatNumbers(tag.getLong("mMax"))));
long avgIn = tag.getLong("AvgIn");
long avgOut = tag.getLong("AvgOut");
currenttip.add(
StatCollector.translateToLocalFormatted(
"GT5U.waila.energy.avg_in_with_amperage",
GTUtility.formatNumbers(avgIn),
GTUtility.getAmperageForTier(avgIn, (byte) getInputTier()),
GTUtility.getColoredTierNameFromTier((byte) getInputTier())));
currenttip.add(
StatCollector.translateToLocalFormatted(
"GT5U.waila.energy.avg_out_with_amperage",
GTUtility.formatNumbers(avgOut),
GTUtility.getAmperageForTier(avgOut, (byte) getOutputTier()),
GTUtility.getColoredTierNameFromTier((byte) getOutputTier())));
super.getWailaBody(itemStack, currenttip, accessor, config);
}
@Override
public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompound tag, World world, int x, int y,
int z) {
updateStorageInfo();
super.getWailaNBTData(player, tile, tag, world, x, y, z);
tag.setLong("mStored", mStored);
tag.setLong("mMax", mMax);
tag.setLong("AvgIn", getBaseMetaTileEntity().getAverageElectricInput());
tag.setLong("AvgOut", getBaseMetaTileEntity().getAverageElectricOutput());
}
@Override
public boolean isGivingInformation() {
return true;
}
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
switch (mInventory.length) {
case 4 -> builder.widget(
SlotGroup.ofItemHandler(inventoryHandler, 2)
.startFromSlot(0)
.endAtSlot(3)
.slotCreator(index -> new BaseSlot(inventoryHandler, index) {
@Override
public int getSlotStackLimit() {
return 1;
}
})
.background(getGUITextureSet().getItemSlot())
.build()
.setPos(70, 25));
case 9 -> builder.widget(
SlotGroup.ofItemHandler(inventoryHandler, 3)
.startFromSlot(0)
.endAtSlot(8)
.slotCreator(index -> new BaseSlot(inventoryHandler, index) {
@Override
public int getSlotStackLimit() {
return 1;
}
})
.background(getGUITextureSet().getItemSlot())
.build()
.setPos(61, 16));
case 16 -> builder.widget(
SlotGroup.ofItemHandler(inventoryHandler, 4)
.startFromSlot(0)
.endAtSlot(15)
.slotCreator(index -> new BaseSlot(inventoryHandler, index) {
@Override
public int getSlotStackLimit() {
return 1;
}
})
.background(getGUITextureSet().getItemSlot())
.build()
.setPos(52, 7));
default -> builder.widget(
SlotGroup.ofItemHandler(inventoryHandler, 1)
.startFromSlot(0)
.endAtSlot(0)
.slotCreator(index -> new BaseSlot(inventoryHandler, index) {
@Override
public int getSlotStackLimit() {
return 1;
}
})
.background(getGUITextureSet().getItemSlot())
.build()
.setPos(79, 34));
}
}
}
|