aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/covers/CoverArm.java
blob: fef9ef1327af243779a67a9e3a9ed41fd2341f01 (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
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
package gregtech.common.covers;

import java.text.FieldPosition;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;

import com.gtnewhorizons.modularui.api.NumberFormatMUI;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.common.widget.TextWidget;

import gregtech.api.gui.modularui.CoverUIBuildContext;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.interfaces.tileentity.IMachineProgress;
import gregtech.api.util.CoverBehavior;
import gregtech.api.util.GTUtility;
import gregtech.api.util.ISerializableObject;
import gregtech.common.gui.modularui.widget.CoverDataControllerWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollowerNumericWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollowerToggleButtonWidget;

public class CoverArm extends CoverBehavior {

    public final int mTickRate;
    // msb converted, 2nd : direction (1=export)
    // right 14 bits: internalSlot, next 14 bits adjSlot, 0 = all, slot = -1
    protected static final int EXPORT_MASK = 0x40000000;
    protected static final int SLOT_ID_MASK = 0x3FFF;
    protected static final int SLOT_ID_MIN = 0;
    protected static final int CONVERTED_BIT = 0x80000000;

    public CoverArm(int aTickRate, ITexture coverTexture) {
        super(coverTexture);
        this.mTickRate = aTickRate;
    }

    @Override
    public boolean isRedstoneSensitive(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
        long aTimer) {
        return false;
    }

    @Override
    public int doCoverThings(ForgeDirection side, byte aInputRedstone, int aCoverID, int aCoverVariable,
        ICoverable aTileEntity, long aTimer) {
        if ((((aTileEntity instanceof IMachineProgress)) && (!((IMachineProgress) aTileEntity).isAllowedToWork()))) {
            return aCoverVariable;
        }

        // Convert from ver. 5.09.33.50, check if 3 last bits are equal
        if ((aCoverVariable >>> 29) == 0) {
            aCoverVariable = CONVERTED_BIT | (((aCoverVariable + 1) & SLOT_ID_MASK) << 14) | EXPORT_MASK;
        } else if ((aCoverVariable >>> 29) == 7) {
            aCoverVariable = CONVERTED_BIT | Math.min(Math.abs(aCoverVariable - 1), SLOT_ID_MASK);
        }

        final TileEntity toTile;
        final TileEntity fromTile;
        final int toSlot;
        final int fromSlot;

        if ((aCoverVariable & EXPORT_MASK) > 0) {
            fromTile = (TileEntity) aTileEntity;
            toTile = aTileEntity.getTileEntityAtSide(side);
            fromSlot = aCoverVariable & SLOT_ID_MASK;
            toSlot = (aCoverVariable >> 14) & SLOT_ID_MASK;
        } else {
            fromTile = aTileEntity.getTileEntityAtSide(side);
            toTile = (TileEntity) aTileEntity;
            fromSlot = (aCoverVariable >> 14) & SLOT_ID_MASK;
            toSlot = aCoverVariable & SLOT_ID_MASK;
        }

        if (fromSlot > 0 && toSlot > 0) {
            if (fromTile instanceof IInventory fromInventory && toTile instanceof IInventory toInventory)
                GTUtility.moveFromSlotToSlot(
                    fromInventory,
                    toInventory,
                    fromSlot - 1,
                    toSlot - 1,
                    null,
                    false,
                    (byte) 64,
                    (byte) 1,
                    (byte) 64,
                    (byte) 1);
        } else if (toSlot > 0) {
            final ForgeDirection toSide;
            if ((aCoverVariable & EXPORT_MASK) > 0) toSide = side;
            else toSide = side.getOpposite();
            GTUtility.moveOneItemStackIntoSlot(
                fromTile,
                toTile,
                toSide,
                toSlot - 1,
                null,
                false,
                (byte) 64,
                (byte) 1,
                (byte) 64,
                (byte) 1);
        } else if (fromSlot > 0) {
            final ForgeDirection toSide;
            if ((aCoverVariable & EXPORT_MASK) > 0) toSide = side;
            else toSide = side.getOpposite();
            if (fromTile instanceof IInventory) GTUtility.moveFromSlotToSide(
                (IInventory) fromTile,
                toTile,
                fromSlot - 1,
                toSide,
                null,
                false,
                (byte) 64,
                (byte) 1,
                (byte) 64,
                (byte) 1);
        } else {
            final ForgeDirection fromSide;
            final ForgeDirection toSide;
            if ((aCoverVariable & EXPORT_MASK) > 0) {
                fromSide = side;
                toSide = side.getOpposite();
            } else {
                fromSide = side.getOpposite();
                toSide = side;
            }
            GTUtility.moveOneItemStack(
                fromTile,
                toTile,
                fromSide,
                toSide,
                null,
                false,
                (byte) 64,
                (byte) 1,
                (byte) 64,
                (byte) 1);
        }

        return aCoverVariable;
    }

    @Override
    public int onCoverScrewdriverclick(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
        EntityPlayer aPlayer, float aX, float aY, float aZ) {
        int step = 0;
        if (GTUtility.getClickedFacingCoords(side, aX, aY, aZ)[0] >= 0.5F) {
            step += aPlayer.isSneaking() ? 256 : 16;
        } else {
            step -= aPlayer.isSneaking() ? 256 : 16;
        }
        aCoverVariable = getNewVar(aCoverVariable, step);
        sendMessageToPlayer(aPlayer, aCoverVariable);
        return aCoverVariable;
    }

    @Override
    protected boolean onCoverRightClickImpl(ForgeDirection side, int aCoverID,
        ISerializableObject.LegacyCoverData aCoverVariable, ICoverable aTileEntity, EntityPlayer aPlayer, float aX,
        float aY, float aZ) {
        int step = (GTUtility.getClickedFacingCoords(side, aX, aY, aZ)[0] >= 0.5F) ? 1 : -1;
        int tCoverVariable = getNewVar(aCoverVariable.get(), step);
        sendMessageToPlayer(aPlayer, tCoverVariable);
        aCoverVariable.set(tCoverVariable);
        return true;
    }

    @Override
    @SuppressWarnings("deprecation")
    public boolean onCoverRightclick(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
        EntityPlayer aPlayer, float aX, float aY, float aZ) {
        final int step = (GTUtility.getClickedFacingCoords(side, aX, aY, aZ)[0] >= 0.5F) ? 1 : -1;
        aCoverVariable = getNewVar(aCoverVariable, step);
        sendMessageToPlayer(aPlayer, aCoverVariable);
        aTileEntity.setCoverDataAtSide(side, aCoverVariable);
        return true;
    }

    @Override
    protected boolean isGUIClickableImpl(ForgeDirection side, int aCoverID,
        ISerializableObject.LegacyCoverData aCoverVariable, ICoverable aTileEntity) {
        return false;
    }

    private void sendMessageToPlayer(EntityPlayer aPlayer, int var) {
        if ((var & EXPORT_MASK) != 0) GTUtility.sendChatToPlayer(
            aPlayer,
            GTUtility.trans("001", "Puts out into adjacent Slot #") + (((var >> 14) & SLOT_ID_MASK) - 1));
        else GTUtility
            .sendChatToPlayer(aPlayer, GTUtility.trans("002", "Grabs in for own Slot #") + ((var & SLOT_ID_MASK) - 1));
    }

    private int getNewVar(int var, int step) {
        int intSlot = (var & SLOT_ID_MASK);
        int adjSlot = (var >> 14) & SLOT_ID_MASK;
        if ((var & EXPORT_MASK) == 0) {
            int x = (intSlot + step);
            if (x > SLOT_ID_MASK) return createVar(0, SLOT_ID_MASK, 0);
            else if (x < 1) return createVar(-step - intSlot + 1, 0, EXPORT_MASK);
            else return createVar(0, x, 0);
        } else {
            int x = (adjSlot - step);
            if (x > SLOT_ID_MASK) return createVar(SLOT_ID_MASK, 0, EXPORT_MASK);
            else if (x < 1) return createVar(0, step - adjSlot + 1, 0);
            else return createVar(x, 0, EXPORT_MASK);
        }
    }

    private int createVar(int adjSlot, int intSlot, int export) {
        return CONVERTED_BIT | export | ((adjSlot & SLOT_ID_MASK) << 14) | (intSlot & SLOT_ID_MASK);
    }

    @Override
    public boolean letsRedstoneGoIn(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsRedstoneGoOut(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsEnergyIn(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsEnergyOut(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsFluidIn(ForgeDirection side, int aCoverID, int aCoverVariable, Fluid aFluid,
        ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsFluidOut(ForgeDirection side, int aCoverID, int aCoverVariable, Fluid aFluid,
        ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsItemsIn(ForgeDirection side, int aCoverID, int aCoverVariable, int aSlot,
        ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean letsItemsOut(ForgeDirection side, int aCoverID, int aCoverVariable, int aSlot,
        ICoverable aTileEntity) {
        return true;
    }

    @Override
    public boolean alwaysLookConnected(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return true;
    }

    @Override
    public int getTickRate(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
        return this.mTickRate;
    }

    // GUI stuff

    @Override
    public boolean hasCoverGUI() {
        return true;
    }

    @Override
    public ModularWindow createWindow(CoverUIBuildContext buildContext) {
        return new ArmUIFactory(buildContext).createWindow();
    }

    private class ArmUIFactory extends UIFactory {

        private static final int startX = 10;
        private static final int startY = 25;
        private static final int spaceX = 18;
        private static final int spaceY = 18;

        private int maxSlot;

        /**
         * Display the text "Any" instead of a number when the slot is set to -1.
         */
        protected static final NumberFormatMUI numberFormatAny = new NumberFormatMUI() {

            @Override
            public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
                if (number < 0) {
                    return toAppendTo.append(GTUtility.trans("ANY", "Any"));
                } else {
                    return super.format(number, toAppendTo, pos);
                }
            }
        };

        protected ArmUIFactory(CoverUIBuildContext buildContext) {
            super(buildContext);
        }

        @SuppressWarnings("PointlessArithmeticExpression")
        @Override
        protected void addUIWidgets(ModularWindow.Builder builder) {
            maxSlot = getMaxSlot();
            builder.widget(
                new CoverDataControllerWidget<>(this::getCoverData, this::setCoverData, CoverArm.this).addFollower(
                    CoverDataFollowerToggleButtonWidget.ofDisableable(),
                    coverData -> getFlagExport(convert(coverData)) > 0,
                    (coverData, state) -> {
                        if (state) {
                            return new ISerializableObject.LegacyCoverData(
                                convert(coverData) | EXPORT_MASK | CONVERTED_BIT);
                        } else {
                            return new ISerializableObject.LegacyCoverData(
                                convert(coverData) & ~EXPORT_MASK | CONVERTED_BIT);
                        }
                    },
                    widget -> widget.setStaticTexture(GTUITextures.OVERLAY_BUTTON_EXPORT)
                        .addTooltip(GTUtility.trans("006", "Export"))
                        .setPos(spaceX * 0, spaceY * 0))
                    .addFollower(
                        CoverDataFollowerToggleButtonWidget.ofDisableable(),
                        coverData -> getFlagExport(convert(coverData)) == 0,
                        (coverData, state) -> {
                            if (state) {
                                return new ISerializableObject.LegacyCoverData(
                                    convert(coverData) & ~EXPORT_MASK | CONVERTED_BIT);
                            } else {
                                return new ISerializableObject.LegacyCoverData(
                                    convert(coverData) | EXPORT_MASK | CONVERTED_BIT);
                            }
                        },
                        widget -> widget.setStaticTexture(GTUITextures.OVERLAY_BUTTON_IMPORT)
                            .addTooltip(GTUtility.trans("007", "Import"))
                            .setPos(spaceX * 1, spaceY * 0))
                    .addFollower(
                        new CoverDataFollowerNumericWidget<>(),
                        coverData -> (double) (getFlagInternalSlot(convert(coverData)) - 1),
                        (coverData, state) -> {
                            final int coverVariable = convert(coverData);
                            return new ISerializableObject.LegacyCoverData(
                                getFlagExport(coverVariable) | ((state.intValue() + 1) & SLOT_ID_MASK)
                                    | (getFlagAdjacentSlot(coverVariable) << 14)
                                    | CONVERTED_BIT);
                        },
                        widget -> widget.setBounds(-1, maxSlot)
                            .setDefaultValue(-1)
                            .setScrollValues(1, 100, 10)
                            .setNumberFormat(numberFormatAny)
                            .setPos(spaceX * 0, spaceY * 1 + 2)
                            .setSize(spaceX * 2 + 5, 12))
                    .addFollower(
                        new CoverDataFollowerNumericWidget<>(),
                        coverData -> (double) (getFlagAdjacentSlot(convert(coverData)) - 1),
                        (coverData, state) -> {
                            final int coverVariable = convert(coverData);
                            return new ISerializableObject.LegacyCoverData(
                                getFlagExport(coverVariable) | getFlagInternalSlot(coverVariable)
                                    | (((state.intValue() + 1) & SLOT_ID_MASK) << 14)
                                    | CONVERTED_BIT);
                        },
                        widget -> widget.setValidator(val -> {
                            // We need to check the adjacent inventory here, and can't simply set a maximum value,
                            // because it can change while this cover is alive.
                            final int adjacentMaxSlot;
                            final ICoverable tile = getUIBuildContext().getTile();
                            if (tile instanceof TileEntity && !tile.isDead()) {
                                TileEntity adj = tile.getTileEntityAtSide(getUIBuildContext().getCoverSide());
                                if (adj instanceof IInventory)
                                    adjacentMaxSlot = ((IInventory) adj).getSizeInventory() - 1;
                                else adjacentMaxSlot = -1;
                            } else {
                                adjacentMaxSlot = -1;
                            }
                            return Math.min(val, adjacentMaxSlot);
                        })
                            .setMinValue(-1)
                            .setDefaultValue(-1)
                            .setScrollValues(1, 100, 10)
                            .setNumberFormat(numberFormatAny)
                            .setPos(spaceX * 0, spaceY * 2 + 2)
                            .setSize(spaceX * 2 + 5, 12))
                    .setPos(startX, startY))
                .widget(
                    new TextWidget()
                        .setStringSupplier(
                            () -> (convert(getCoverData()) & EXPORT_MASK) > 0 ? GTUtility.trans("006", "Export")
                                : GTUtility.trans("007", "Import"))
                        .setDefaultColor(COLOR_TEXT_GRAY.get())
                        .setPos(startX + spaceX * 3, 4 + startY + spaceY * 0))
                .widget(
                    new TextWidget(GTUtility.trans("254.1", "Internal slot#")).setDefaultColor(COLOR_TEXT_GRAY.get())
                        .setPos(startX + spaceX * 3, 4 + startY + spaceY * 1))
                .widget(
                    new TextWidget(GTUtility.trans("255", "Adjacent slot#")).setDefaultColor(COLOR_TEXT_GRAY.get())
                        .setPos(startX + spaceX * 3, 4 + startY + spaceY * 2));
        }

        private int getMaxSlot() {
            final ICoverable tile = getUIBuildContext().getTile();
            if (tile instanceof TileEntity && !tile.isDead()) {
                return tile.getSizeInventory() - 1;
            } else {
                return -1;
            }
        }

        private int getFlagExport(int coverVariable) {
            return coverVariable & EXPORT_MASK;
        }

        private int getFlagInternalSlot(int coverVariable) {
            return coverVariable & SLOT_ID_MASK;
        }

        private int getFlagAdjacentSlot(int coverVariable) {
            return (coverVariable >> 14) & SLOT_ID_MASK;
        }
    }
}