aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/misc/GT_DrillingLogicDelegate.java
blob: 8f06f70140cc289a9b7e69eb25a5532de4035a3f (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
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
package gregtech.common.misc;

import java.util.List;

import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_ModHandler;
import gregtech.api.util.GT_Utility;
import gregtech.common.blocks.GT_TileEntity_Ores;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.FakePlayer;
import static gregtech.api.enums.GT_Values.debugBlockMiner;

/** @author Relvl on 27.01.2022 */
@SuppressWarnings("ObjectEquality")
public class GT_DrillingLogicDelegate {
    public static final ItemStack MINING_PIPE_STACK = GT_ModHandler.getIC2Item("miningPipe", 0);
    public static final Block MINING_PIPE_BLOCK = GT_Utility.getBlockFromStack(MINING_PIPE_STACK);
    public static final Block MINING_PIPE_TIP_BLOCK = GT_Utility.getBlockFromStack(GT_ModHandler.getIC2Item("miningPipeTip", 0));

    /** The owner machine pointer */
    private final GT_IDrillingLogicDelegateOwner owner;

    /** Is pipe retracting process done and halts? */
    private boolean isRetractDone;
    /** Is machine ran out of mining pipes in its inventory and halts? */
    private boolean isWaitingForPipeItem;
    /** Pipe tip depth (relative to machine Y position, NEGATIVE). */
    private int tipDepth;
    /** Cached fake player */
    private FakePlayer mFakePlayer;

    public GT_DrillingLogicDelegate(GT_IDrillingLogicDelegateOwner owner) {
        this.owner = owner;
    }

    /** Descents a pipe tip one plock deeper. */
    public boolean descent(IGregTechTileEntity te) {
        if (!te.isAllowedToWork()) {
            return false;
        }

        int xCoord = te.getXCoord();
        int zCoord = te.getZCoord();
        int yCoord = te.getYCoord();
        int checkY = yCoord + tipDepth - 1;
        boolean isHitsTheVoid = checkY < 0;
        boolean isHitsBedrock = GT_Utility.getBlockHardnessAt(te.getWorld(), xCoord, checkY, zCoord) < 0;
        boolean isFakePlayerAllowed = canFakePlayerInteract(te, xCoord, checkY, zCoord);

        if (isHitsTheVoid || isHitsBedrock || !isFakePlayerAllowed) {
            // Disable and start retracting process.
            te.disableWorking();
            if (debugBlockMiner) {
                if (isHitsTheVoid) {
                    GT_Log.out.println("MINER: Hit bottom");
                }
                if (isHitsBedrock) {
                    GT_Log.out.println("MINER: Hit block with -1 hardness");
                }
                if (!isFakePlayerAllowed) {
                    GT_Log.out.println("MINER: Unable to set mining pipe tip");
                }
            }
            return false;
        }

        // Replace the tip onto pipe
        if (te.getBlockOffset(0, tipDepth, 0) == MINING_PIPE_TIP_BLOCK) {
            te.getWorld().setBlock(xCoord, yCoord + tipDepth, zCoord, MINING_PIPE_BLOCK);
        }
        // Get and decrease pipe from the machine
        boolean pipeTaken = owner.pullInputs(MINING_PIPE_STACK.getItem(), 1, false);
        if (!pipeTaken) {
            // If there was nothing - waiting for the pipes (just for prevent unnecessary checks)
            isWaitingForPipeItem = true;
            return false;
        }

        // If there is something - mine it
        Block block = te.getBlockOffset(0, tipDepth - 1, 0);
        if (!block.isAir(te.getWorld(), xCoord, yCoord, zCoord)) {
            mineBlock(te, block, xCoord, yCoord + tipDepth - 1, zCoord);
        }

        // Descent the pipe tip
        te.getWorld().setBlock(xCoord, yCoord + tipDepth - 1, zCoord, MINING_PIPE_TIP_BLOCK);
        tipDepth--;
        return true;
    }

    public void onOwnerPostTick(IGregTechTileEntity te, long tick) {
        // If the machine was disabled - try to retract pipe
        if (!te.isAllowedToWork()) {
            onPostTickRetract(te, tick);
            return;
        }
        // If the machine was re-enabled - we should reset the retracting process
        isRetractDone = false;
    }

    /** If the machine are disabled - tried to retract pipe. */
    private void onPostTickRetract(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        if (isRetractDone) {
            return;
        }
        // If retracting process just touch the miner
        if (tipDepth == 0) {
            isRetractDone = true;
            return;
        }
        // Once per N ticks (depends on tier)
        if ((aTick % (owner.getMachineSpeed() / 5)) != 0) {
            return;
        }

        // Check we can push pipe back to machine (inputs allowed for this case!)
        boolean canPush = owner.pushOutputs(MINING_PIPE_STACK, 1, true, true);
        if (!canPush) {
            return;
        }

        // Inspect target block - it should be a pipe tip, else something went wrong.
        Block targetBlock = aBaseMetaTileEntity.getBlockOffset(0, tipDepth, 0);
        if (targetBlock != MINING_PIPE_TIP_BLOCK && targetBlock != MINING_PIPE_BLOCK) {
            return;
        }

        // Retract the pipe/tip
        int xCoord = aBaseMetaTileEntity.getXCoord();
        int yCoord = aBaseMetaTileEntity.getYCoord();
        int zCoord = aBaseMetaTileEntity.getZCoord();
        int actualDrillY = yCoord + tipDepth;
        // Move the pipe tip position
        if (actualDrillY < yCoord - 1) {
            owner.getBaseMetaTileEntity().getWorld().setBlock(xCoord, actualDrillY + 1, zCoord, MINING_PIPE_TIP_BLOCK);
        }
        // Remove the old pipe tip
        aBaseMetaTileEntity.getWorld().setBlock(xCoord, actualDrillY, zCoord, Blocks.air, 0, /*send to client without neighbour updates*/2);

        // Return the pipe back to the machine (inputs allowed for this case!)
        owner.pushOutputs(MINING_PIPE_STACK, 1, false, true);

        tipDepth++;
    }

    /** Minings the block if it is possible. */
    public void mineBlock(IGregTechTileEntity te, Block block, int x, int y, int z) {
        if (!GT_Utility.eraseBlockByFakePlayer(getFakePlayer(te), x, y, z, true)) {
            return;
        }

        List<ItemStack> drops = getBlockDrops(block, x, y, z);

        boolean canFitDrops = true;
        for (ItemStack drop : drops) {
            canFitDrops &= owner.pushOutputs(drop, drop.stackSize, true, false);
        }
        if (!canFitDrops) {
            return;
        }
        for (ItemStack drop : drops) {
            owner.pushOutputs(drop, drop.stackSize, false, false);
        }

        short metaData = 0;
        TileEntity tTileEntity = owner.getBaseMetaTileEntity().getTileEntity(x, y, z);
        if (tTileEntity instanceof GT_TileEntity_Ores) {
            metaData = ((GT_TileEntity_Ores)tTileEntity).mMetaData;
        }

        ItemStack cobble = GT_Utility.getCobbleForOre(block, metaData);
        te.getWorld().setBlock(x, y, z, Block.getBlockFromItem(cobble.getItem()), cobble.getItemDamage(), /*cause updates(1) + send to client(2)*/ 3);
    }

    /** Returns NEGATIVE (eg -5) depth of current drilling Y world level. RELATIVELY TO MINER ENTITY! This means '(miner world Y) + depth = (actual world Y)'. */
    public int getTipDepth() {
        return tipDepth;
    }

    /** Looking for the lowest continuous pipe. */
    public void findTipDepth() {
        IGregTechTileEntity ownerTe = owner.getBaseMetaTileEntity();
        if (!ownerTe.isServerSide()) {
            return;
        }
        while (true) {
            Block block = ownerTe.getBlockOffset(0, tipDepth - 1, 0);
            if (block != MINING_PIPE_BLOCK && block != MINING_PIPE_TIP_BLOCK) {
                return;
            }
            tipDepth--;
        }
    }

    /** Creates and provides the Fake Player for owners. todo maybe will provide player owner uuid? im sure some servers not allow to fakers, in griefing reasons. */
    public FakePlayer getFakePlayer(IGregTechTileEntity te) {
        if (mFakePlayer == null) {
            mFakePlayer = GT_Utility.getFakePlayer(te);
        }
        if (mFakePlayer != null) {
            mFakePlayer.setWorld(te.getWorld());
            mFakePlayer.setPosition(te.getXCoord(), te.getYCoord(), te.getZCoord());
        }
        return mFakePlayer;
    }

    public boolean canFakePlayerInteract(IGregTechTileEntity te, int xCoord, int yCoord, int zCoord) {
        return GT_Utility.setBlockByFakePlayer(getFakePlayer(te), xCoord, yCoord, zCoord, MINING_PIPE_TIP_BLOCK, 0, true);
    }

    /** Get target block drops. We need to encapsulate everyting of mining in this class. */
    private List<ItemStack> getBlockDrops(final Block oreBlock, int posX, int posY, int posZ) {
        return oreBlock.getDrops(owner.getBaseMetaTileEntity().getWorld(), posX, posY, posZ, owner.getBaseMetaTileEntity().getMetaID(posX, posY, posZ), owner.getMachineTier());
    }

    /** Can the owner continue doing its work? If we await new pipes - it cannot. */
    public boolean canContinueDrilling(long tick) {
        if (isWaitingForPipeItem) {
            if (tick % 5 != 0) {
                return false;
            }
            boolean hasPipe = owner.pullInputs(MINING_PIPE_STACK.getItem(), 1, true);
            if (hasPipe) {
                isWaitingForPipeItem = false;
            }
            return hasPipe;
        }
        return true;
    }
}