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
|
package gregtech.common.tileentities.machines;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_HATCH;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_HATCH_ACTIVE;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import appeng.api.AEApi;
import appeng.api.implementations.IPowerChannelState;
import appeng.api.networking.GridFlags;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
import appeng.items.storage.ItemBasicStorageCell;
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.IGridProxyable;
import appeng.util.IWideReadableNumberConverter;
import appeng.util.Platform;
import appeng.util.ReadableNumberConverter;
import gregtech.GTMod;
import gregtech.api.enums.ItemList;
import gregtech.api.gui.modularui.GTUIInfos;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.implementations.MTEHatchOutputBus;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;
public class MTEHatchOutputBusME extends MTEHatchOutputBus implements IPowerChannelState {
private long baseCapacity = 1_600;
private BaseActionSource requestSource = null;
private @Nullable AENetworkProxy gridProxy = null;
final IItemList<IAEItemStack> itemCache = AEApi.instance()
.storage()
.createItemList();
long lastOutputTick = 0;
long lastInputTick = 0;
long tickCounter = 0;
boolean additionalConnection = false;
public MTEHatchOutputBusME(int aID, String aName, String aNameRegional) {
super(
aID,
aName,
aNameRegional,
3,
new String[] { "Item Output for Multiblocks", "Stores directly into ME", "Can cache 1600 items by default",
"Change cache size by inserting a storage cell",
"Change ME connection behavior by right-clicking with wire cutter" },
1);
}
public MTEHatchOutputBusME(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
super(aName, aTier, 1, aDescription, aTextures);
}
@Override
public MetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTEHatchOutputBusME(mName, mTier, mDescriptionArray, mTextures);
}
@Override
public ITexture[] getTexturesActive(ITexture aBaseTexture) {
return new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_ME_HATCH_ACTIVE) };
}
@Override
public ITexture[] getTexturesInactive(ITexture aBaseTexture) {
return new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_ME_HATCH) };
}
@Override
public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) {
super.onFirstTick(aBaseMetaTileEntity);
getProxy().onReady();
}
@Override
public boolean storeAll(ItemStack aStack) {
aStack.stackSize = store(aStack);
return aStack.stackSize == 0;
}
private long getCachedAmount() {
long itemAmount = 0;
for (IAEItemStack item : itemCache) {
itemAmount += item.getStackSize();
}
return itemAmount;
}
private long getCacheCapacity() {
ItemStack upgradeItemStack = mInventory[0];
if (upgradeItemStack != null && upgradeItemStack.getItem() instanceof ItemBasicStorageCell) {
return ((ItemBasicStorageCell) upgradeItemStack.getItem()).getBytesLong(upgradeItemStack) * 8;
}
return baseCapacity;
}
/**
* Check if the internal cache can still fit more items in it
*/
public boolean canAcceptItem() {
if (getCachedAmount() < getCacheCapacity()) {
return true;
}
return false;
}
/**
* Attempt to store items in connected ME network. Returns how many items did not fit (if the network was down e.g.)
*
* @param stack input stack
* @return amount of items left over
*/
public int store(final ItemStack stack) {
// Always allow insertion on the same tick so we can output the entire recipe
if (canAcceptItem() || (lastInputTick == tickCounter)) {
itemCache.add(
AEApi.instance()
.storage()
.createItemStack(stack));
lastInputTick = tickCounter;
return 0;
}
return stack.stackSize;
}
private BaseActionSource getRequest() {
if (requestSource == null) requestSource = new MachineSource((IActionHost) getBaseMetaTileEntity());
return requestSource;
}
@Override
public AECableType getCableConnectionType(ForgeDirection forgeDirection) {
return isOutputFacing(forgeDirection) ? AECableType.SMART : AECableType.NONE;
}
private void updateValidGridProxySides() {
if (additionalConnection) {
getProxy().setValidSides(EnumSet.complementOf(EnumSet.of(ForgeDirection.UNKNOWN)));
} else {
getProxy().setValidSides(EnumSet.of(getBaseMetaTileEntity().getFrontFacing()));
}
}
@Override
public void onFacingChange() {
updateValidGridProxySides();
}
@Override
public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
GTUIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer);
return true;
}
@Override
public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (!getBaseMetaTileEntity().getCoverInfoAtSide(side)
.isGUIClickable()) return;
}
@Override
public boolean onWireCutterRightClick(ForgeDirection side, ForgeDirection wrenchingSide, EntityPlayer aPlayer,
float aX, float aY, float aZ) {
additionalConnection = !additionalConnection;
updateValidGridProxySides();
aPlayer.addChatComponentMessage(
new ChatComponentTranslation("GT5U.hatch.additionalConnection." + additionalConnection));
return true;
}
@Override
public AENetworkProxy getProxy() {
if (gridProxy == null) {
if (getBaseMetaTileEntity() instanceof IGridProxyable) {
gridProxy = new AENetworkProxy(
(IGridProxyable) getBaseMetaTileEntity(),
"proxy",
ItemList.Hatch_Output_Bus_ME.get(1),
true);
gridProxy.setFlags(GridFlags.REQUIRE_CHANNEL);
updateValidGridProxySides();
if (getBaseMetaTileEntity().getWorld() != null) gridProxy.setOwner(
getBaseMetaTileEntity().getWorld()
.getPlayerEntityByName(getBaseMetaTileEntity().getOwnerName()));
}
}
return this.gridProxy;
}
private void flushCachedStack() {
AENetworkProxy proxy = getProxy();
if (proxy == null) {
return;
}
try {
IMEMonitor<IAEItemStack> sg = proxy.getStorage()
.getItemInventory();
for (IAEItemStack s : itemCache) {
if (s.getStackSize() == 0) continue;
IAEItemStack rest = Platform.poweredInsert(proxy.getEnergy(), sg, s, getRequest());
if (rest != null && rest.getStackSize() > 0) {
s.setStackSize(rest.getStackSize());
break;
}
s.setStackSize(0);
}
} catch (final GridAccessException ignored) {
}
lastOutputTick = tickCounter;
}
@Override
public boolean isPowered() {
return getProxy() != null && getProxy().isPowered();
}
@Override
public boolean isActive() {
return getProxy() != null && getProxy().isActive();
}
@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
if (getBaseMetaTileEntity().isServerSide()) {
tickCounter = aTick;
if (tickCounter > (lastOutputTick + 40)) flushCachedStack();
if (tickCounter % 20 == 0) getBaseMetaTileEntity().setActive(isActive());
}
super.onPostTick(aBaseMetaTileEntity, aTick);
}
@Override
public void addAdditionalTooltipInformation(ItemStack stack, List<String> tooltip) {
if (stack.hasTagCompound() && stack.stackTagCompound.hasKey("baseCapacity")) {
tooltip.add(
"Current cache capacity: " + EnumChatFormatting.YELLOW
+ ReadableNumberConverter.INSTANCE
.toWideReadableForm(stack.stackTagCompound.getLong("baseCapacity")));
}
}
@Override
public void setItemNBT(NBTTagCompound aNBT) {
super.setItemNBT(aNBT);
aNBT.setLong("baseCapacity", baseCapacity);
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
NBTTagList items = new NBTTagList();
for (IAEItemStack s : itemCache) {
if (s.getStackSize() == 0) continue;
NBTTagCompound tag = new NBTTagCompound();
tag.setTag("itemStack", GTUtility.saveItem(s.getItemStack()));
tag.setLong("size", s.getStackSize());
items.appendTag(tag);
}
aNBT.setBoolean("additionalConnection", additionalConnection);
aNBT.setTag("cachedItems", items);
aNBT.setLong("baseCapacity", baseCapacity);
getProxy().writeToNBT(aNBT);
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
NBTBase t = aNBT.getTag("cachedStack"); // legacy
if (t instanceof NBTTagCompound) itemCache.add(
AEApi.instance()
.storage()
.createItemStack(GTUtility.loadItem((NBTTagCompound) t)));
t = aNBT.getTag("cachedItems");
if (t instanceof NBTTagList l) {
for (int i = 0; i < l.tagCount(); ++i) {
NBTTagCompound tag = l.getCompoundTagAt(i);
if (!tag.hasKey("itemStack")) { // legacy #868
itemCache.add(
AEApi.instance()
.storage()
.createItemStack(GTUtility.loadItem(l.getCompoundTagAt(i))));
continue;
}
NBTTagCompound tagItemStack = tag.getCompoundTag("itemStack");
final IAEItemStack s = AEApi.instance()
.storage()
.createItemStack(GTUtility.loadItem(tagItemStack));
if (s != null) {
s.setStackSize(tag.getLong("size"));
itemCache.add(s);
} else {
GTMod.GT_FML_LOGGER.warn(
"An error occurred while loading contents of ME Output Bus. This item has been voided: "
+ tagItemStack);
}
}
}
additionalConnection = aNBT.getBoolean("additionalConnection");
baseCapacity = aNBT.getLong("baseCapacity");
// Set the base capacity of existing hatches to be infinite
if (baseCapacity == 0) {
baseCapacity = Long.MAX_VALUE;
}
getProxy().readFromNBT(aNBT);
}
@Override
public boolean isGivingInformation() {
return true;
}
@Override
public String[] getInfoData() {
List<String> ss = new ArrayList<>();
ss.add(
"The bus is " + ((getProxy() != null && getProxy().isActive()) ? EnumChatFormatting.GREEN + "online"
: EnumChatFormatting.RED + "offline" + getAEDiagnostics()) + EnumChatFormatting.RESET);
IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE;
ss.add("Item cache capacity: " + nc.toWideReadableForm(getCacheCapacity()));
if (itemCache.isEmpty()) {
ss.add("The bus has no cached items");
} else {
ss.add(String.format("The bus contains %d cached stacks: ", itemCache.size()));
int counter = 0;
for (IAEItemStack s : itemCache) {
ss.add(
s.getItem()
.getItemStackDisplayName(s.getItemStack()) + ": "
+ EnumChatFormatting.GOLD
+ nc.toWideReadableForm(s.getStackSize())
+ EnumChatFormatting.RESET);
if (++counter > 100) break;
}
}
return ss.toArray(new String[itemCache.size() + 2]);
}
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
getBaseMetaTileEntity().add1by1Slot(builder);
}
@Override
public boolean acceptsItemLock() {
return false;
}
}
|