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
|
package gregtech.common.tileentities.machines.basic;
import static gregtech.api.enums.GTValues.V;
import static gregtech.api.enums.GTValues.debugBlockMiner;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
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.ChunkPosition;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizons.modularui.api.drawable.FallbackableUITexture;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GTUITextures;
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.metatileentity.implementations.MTEBasicMachine;
import gregtech.api.recipe.BasicUIProperties;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTUtility;
import gregtech.common.blocks.BlockOresAbstract;
import gregtech.common.blocks.TileEntityOres;
import gregtech.common.misc.DrillingLogicDelegate;
import gregtech.common.misc.IDrillingLogicDelegateOwner;
public class MTEMiner extends MTEBasicMachine implements IDrillingLogicDelegateOwner, IAddUIWidgets {
static final int[] RADIUS = { 8, 8, 16, 24, 32 }; // Miner radius per tier
static final int[] SPEED = { 160, 160, 80, 40, 20 }; // Miner cycle time per tier
static final int[] ENERGY = { 8, 8, 32, 128, 512 }; // Miner energy consumption per tier
/** Miner configured radius */
private int radiusConfig;
/** Found ore blocks cache of current drill depth */
private final ArrayList<ChunkPosition> oreBlockPositions = new ArrayList<>();
/** General pipe accessor */
private final DrillingLogicDelegate pipe = new DrillingLogicDelegate(this);
private final int mSpeed;
public MTEMiner(int aID, String aName, String aNameRegional, int aTier) {
super(
aID,
aName,
aNameRegional,
aTier,
1,
new String[] { "Digging ore instead of you", "Use Screwdriver to regulate work area",
"Use Soft Mallet to disable and retract the pipe",
String.format("%d EU/t, %d sec per block, no stuttering", ENERGY[aTier], SPEED[aTier] / 20),
String.format("Maximum work area %dx%d", (RADIUS[aTier] * 2 + 1), (RADIUS[aTier] * 2 + 1)),
String.format("Fortune bonus of %d", aTier) },
2,
2,
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_SIDE_ACTIVE")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_SIDE_ACTIVE_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_SIDE")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_SIDE_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_FRONT_ACTIVE")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_FRONT_ACTIVE_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_FRONT")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_FRONT_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_TOP_ACTIVE")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_TOP_ACTIVE_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_TOP")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_TOP_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_BOTTOM_ACTIVE")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_BOTTOM_ACTIVE_GLOW"))
.glow()
.build()),
TextureFactory.of(
TextureFactory.of(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_BOTTOM")),
TextureFactory.builder()
.addIcon(new Textures.BlockIcons.CustomIcon("basicmachines/miner/OVERLAY_BOTTOM_GLOW"))
.glow()
.build()));
mSpeed = SPEED[aTier];
radiusConfig = RADIUS[mTier];
}
public MTEMiner(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
super(aName, aTier, 1, aDescription, aTextures, 2, 2);
mSpeed = SPEED[aTier];
radiusConfig = RADIUS[mTier];
}
@Override
public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTEMiner(mName, mTier, mDescriptionArray, mTextures);
}
@Override
public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) {
pipe.findTipDepth();
fillOreList(aBaseMetaTileEntity);
}
@Override
protected boolean allowPutStackValidated(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
return super.allowPutStackValidated(aBaseMetaTileEntity, aIndex, side, aStack) //
&& aStack.getItem() == DrillingLogicDelegate.MINING_PIPE_STACK.getItem();
}
/** Both output slots must be free to work */
public boolean hasFreeSpace() {
for (int i = getOutputSlot(); i < getOutputSlot() + 2; i++) {
if (mInventory[i] != null) {
return false;
}
}
return true;
}
@Override
public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
super.onScrewdriverRightClick(side, aPlayer, aX, aY, aZ);
if (side != getBaseMetaTileEntity().getFrontFacing() && side != mMainFacing) {
if (aPlayer.isSneaking()) {
if (radiusConfig >= 0) {
radiusConfig--;
}
if (radiusConfig < 0) {
radiusConfig = RADIUS[mTier];
}
} else {
if (radiusConfig <= RADIUS[mTier]) {
radiusConfig++;
}
if (radiusConfig > RADIUS[mTier]) {
radiusConfig = 0;
}
}
GTUtility.sendChatToPlayer(
aPlayer,
String.format(
"%s %dx%d",
StatCollector.translateToLocal("GT5U.machines.workareaset"),
(radiusConfig * 2 + 1),
(radiusConfig * 2 + 1)));
// Rebuild ore cache after change config
fillOreList(getBaseMetaTileEntity());
}
}
@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
super.onPostTick(aBaseMetaTileEntity, aTick);
if (!aBaseMetaTileEntity.isServerSide()) {
return;
}
// Pipe workaround
pipe.onOwnerPostTick(aBaseMetaTileEntity, aTick);
if (!aBaseMetaTileEntity.isAllowedToWork()) {
mMaxProgresstime = 0;
if (debugBlockMiner) {
GTLog.out.println("MINER: Disabled");
}
return;
}
if (!hasFreeSpace()) {
mMaxProgresstime = 0;
if (debugBlockMiner) {
GTLog.out.println("MINER: No free space");
}
return;
}
if (!aBaseMetaTileEntity.isUniversalEnergyStored((long) ENERGY[mTier] * (mSpeed - mProgresstime))) {
mMaxProgresstime = 0;
if (debugBlockMiner) {
GTLog.out.println(
"MINER: Not enough energy yet, want " + (ENERGY[mTier] * mSpeed)
+ " have "
+ aBaseMetaTileEntity.getUniversalEnergyStored());
}
return;
}
/* Checks if machine are waiting new mining pipe item */
if (!pipe.canContinueDrilling(aTick)) {
mMaxProgresstime = 0;
return;
}
mMaxProgresstime = mSpeed;
aBaseMetaTileEntity.decreaseStoredEnergyUnits(ENERGY[mTier], true);
// Real working only when progress done. TODO some legacy code... refactorings needed
if (mProgresstime == mSpeed - 1) {
if (pipe.getTipDepth() == 0 || oreBlockPositions.isEmpty()) {
boolean descends = pipe.descent(aBaseMetaTileEntity);
if (descends) {
fillOreList(aBaseMetaTileEntity);
}
} else {
int x;
int y;
int z;
Block oreBlock;
boolean isOre;
do {
ChunkPosition oreBlockPos = oreBlockPositions.remove(0);
oreBlock = aBaseMetaTileEntity
.getBlockOffset(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ);
x = aBaseMetaTileEntity.getXCoord() + oreBlockPos.chunkPosX;
y = aBaseMetaTileEntity.getYCoord() + oreBlockPos.chunkPosY;
z = aBaseMetaTileEntity.getZCoord() + oreBlockPos.chunkPosZ;
isOre = GTUtility.isOre(
oreBlock,
aBaseMetaTileEntity.getWorld()
.getBlockMetadata(x, y, z));
} // someone else might have removed the block
while (!isOre && !oreBlockPositions.isEmpty());
if (isOre) {
pipe.mineBlock(aBaseMetaTileEntity, oreBlock, x, y, z);
}
}
}
}
/** Finds the ores in current drill Y level */
private void fillOreList(IGregTechTileEntity aBaseMetaTileEntity) {
if (pipe.getTipDepth() == 0) {
return;
}
oreBlockPositions.clear();
for (int z = -radiusConfig; z <= radiusConfig; ++z) {
for (int x = -radiusConfig; x <= radiusConfig; ++x) {
Block block = aBaseMetaTileEntity.getBlockOffset(x, pipe.getTipDepth(), z);
int blockMeta = aBaseMetaTileEntity.getMetaIDOffset(x, pipe.getTipDepth(), z);
// todo some weird checks. refactorings needed
if (block instanceof BlockOresAbstract) {
TileEntity oreEntity = aBaseMetaTileEntity.getTileEntityOffset(x, pipe.getTipDepth(), z);
if (oreEntity instanceof TileEntityOres && ((TileEntityOres) oreEntity).mNatural) {
oreBlockPositions.add(new ChunkPosition(x, pipe.getTipDepth(), z));
}
} else if (GTUtility.isOre(block, blockMeta)) {
oreBlockPositions.add(new ChunkPosition(x, pipe.getTipDepth(), z));
}
}
}
}
/** Pulls (or check can pull) items from an input slots. */
@Override
public boolean pullInputs(Item item, int count, boolean simulate) {
for (int i = 0; i < mInputSlotCount; i++) {
ItemStack stack = getInputAt(i);
if (stack != null && stack.getItem() == item && stack.stackSize >= count) {
if (simulate) {
return true;
}
stack.stackSize -= count;
if (stack.stackSize == 0) {
mInventory[getInputSlot() + i] = null;
}
return true;
}
}
return false;
}
/** Pushes (or check can push) item to output slots. */
@Override
public boolean pushOutputs(ItemStack stack, int count, boolean simulate, boolean allowInputSlots) {
return allowInputSlots && pushOutput(getInputSlot(), getInputSlot() + mInputSlotCount, stack, count, simulate)
|| pushOutput(getOutputSlot(), getOutputSlot() + mOutputItems.length, stack, count, simulate);
}
private boolean pushOutput(int startIndex, int endIndex, ItemStack stack, int count, boolean simulate) {
for (int i = startIndex; i < endIndex; i++) {
ItemStack slot = mInventory[i];
if (slot == null || slot.stackSize == 0) {
if (!simulate) {
ItemStack copy = stack.copy();
copy.stackSize = count;
mInventory[i] = copy;
}
return true;
} else if (GTUtility.areStacksEqual(slot, stack) && slot.stackSize <= slot.getMaxStackSize() - count) {
if (!simulate) {
slot.stackSize += count;
}
return true;
}
}
return false;
}
@Override
public long maxEUStore() {
return Math.max(V[mTier] * 64, 4096);
}
@Override
public void setItemNBT(NBTTagCompound aNBT) {
super.setItemNBT(aNBT);
aNBT.setInteger("radiusConfig", radiusConfig);
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
aNBT.setInteger("radiusConfig", radiusConfig);
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
if (aNBT.hasKey("radiusConfig")) {
int newRadius = aNBT.getInteger("radiusConfig");
if (RADIUS[mTier] <= newRadius && newRadius > 0) {
radiusConfig = newRadius;
}
}
}
@Override
public String[] getInfoData() {
return new String[] {
String.format(
"%s%s%s",
EnumChatFormatting.BLUE,
StatCollector.translateToLocal("GT5U.machines.miner"),
EnumChatFormatting.RESET),
String.format(
"%s: %s%d%s %s",
StatCollector.translateToLocal("GT5U.machines.workarea"),
EnumChatFormatting.GREEN,
(radiusConfig * 2 + 1),
EnumChatFormatting.RESET,
StatCollector.translateToLocal("GT5U.machines.blocks")) };
}
@Override
public int getMachineTier() {
return mTier;
}
@Override
public int getMachineSpeed() {
return mSpeed;
}
public DrillingLogicDelegate getPipe() {
return pipe;
}
private static final FallbackableUITexture progressBarTexture = GTUITextures
.fallbackableProgressbar("miner", GTUITextures.PROGRESSBAR_CANNER);
@Override
protected BasicUIProperties getUIProperties() {
return super.getUIProperties().toBuilder()
.progressBarTexture(progressBarTexture)
.build();
}
}
|