aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/block/machine/BlockSuperJukebox.java
blob: 9d6f76ca2c83da4ee07c55511c0854b2442bd36d (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package gtPlusPlus.core.block.machine;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.BlockJukebox;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemRecord;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.util.GTUtility;
import gtPlusPlus.GTplusplus;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.handler.GuiHandler;
import gtPlusPlus.core.inventories.InventorySuperJukebox;
import gtPlusPlus.core.util.math.MathUtils;

public class BlockSuperJukebox extends BlockJukebox {

    @SideOnly(Side.CLIENT)
    private IIcon mIcon;

    public static class SuperJukeboxItemBlock extends ItemBlock {

        public SuperJukeboxItemBlock(Block block) {
            super(block);
        }

        @Override
        public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltips, boolean f3HEnabled) {
            tooltips.add(StatCollector.translateToLocal("tile.blockSuperJukebox.deprecated"));
        }
    }

    public BlockSuperJukebox() {
        this.setBlockName("blockSuperJukebox");
        this.setCreativeTab(CreativeTabs.tabRedstone);
        setHardness(2.0F);
        setResistance(10.0F);
        setStepSound(soundTypePiston);
        setBlockTextureName("jukebox");
        GameRegistry.registerBlock(this, SuperJukeboxItemBlock.class, "blockSuperJukebox");
    }

    /**
     * Gets the block's texture. Args: side, meta
     */
    @Override
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int ordinalSide, int aMeta) {
        return ordinalSide == 1 ? this.mIcon : this.blockIcon;
    }

    /**
     * Called upon block activation (right click on the block.)
     */
    @Override
    public boolean onBlockActivated(final World world, final int x, final int y, final int z, final EntityPlayer player,
        final int side, final float lx, final float ly, final float lz) {
        if (world.isRemote) {
            return true;
        }

        final TileEntity te = world.getTileEntity(x, y, z);
        if (te instanceof TileEntitySuperJukebox) {
            player.openGui(GTplusplus.instance, GuiHandler.GUI14, world, x, y, z);
            return true;
        }
        return false;

        /*
         * if (aWorld.getBlockMetadata(aX, aY, aZ) == 0) { return false; } else { this.func_149925_e(aWorld, aX, aY,
         * aZ); return true; }
         */
    }

    /**
     * Set the record in the {@link SuperJukebox} {@link TileEntity}.
     */
    @Override
    public final void func_149926_b(World aWorld, int aX, int aY, int aZ, ItemStack aStackToSet) {
        setRecordInJukeBox(aWorld, aX, aY, aZ, aStackToSet);
    }

    public void setRecordInJukeBox(World aWorld, int aX, int aY, int aZ, ItemStack aStackToSet) {
        if (!aWorld.isRemote) {
            TileEntitySuperJukebox tileentityjukebox = (TileEntitySuperJukebox) aWorld.getTileEntity(aX, aY, aZ);
            if (tileentityjukebox != null && aStackToSet.getItem() instanceof ItemRecord) {
                tileentityjukebox.setCurrentRecord(aStackToSet.copy());
                // aWorld.setBlockMetadataWithNotify(aX, aY, aZ, 1, 2);
            }
        }
    }

    /**
     * Function to handle playing of records.
     */
    @Override
    public final void func_149925_e(World aWorld, int aX, int aY, int aZ) {
        playJukeboxRecord(aWorld, aX, aY, aZ);
    }

    public void playJukeboxRecord(World aWorld, int aX, int aY, int aZ) {
        if (!aWorld.isRemote) {
            TileEntitySuperJukebox tileentityjukebox = (TileEntitySuperJukebox) aWorld.getTileEntity(aX, aY, aZ);

            if (tileentityjukebox != null) {
                ItemStack itemstack = tileentityjukebox.func_145856_a();

                if (itemstack != null) {

                    aWorld.playAuxSFX(1005, aX, aY, aZ, Item.getIdFromItem(itemstack.getItem()));
                    /*
                     * float f = 0.7F; double d0 = (double) (aWorld.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
                     * double d1 = (double) (aWorld.rand.nextFloat() * f) + (double) (1.0F - f) * 0.2D + 0.6D; double d2
                     * = (double) (aWorld.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D; ItemStack itemstack1 =
                     * itemstack.copy(); EntityItem entityitem = new EntityItem(aWorld, (double) aX + d0, (double) aY +
                     * d1, (double) aZ + d2, itemstack1); entityitem.delayBeforeCanPickup = 10;
                     * aWorld.spawnEntityInWorld(entityitem);
                     */
                }
            }
        }
    }

    @Override
    public void breakBlock(World p_149749_1_, int p_149749_2_, int p_149749_3_, int p_149749_4_, Block p_149749_5_,
        int p_149749_6_) {
        this.func_149925_e(p_149749_1_, p_149749_2_, p_149749_3_, p_149749_4_);
        super.breakBlock(p_149749_1_, p_149749_2_, p_149749_3_, p_149749_4_, p_149749_5_, p_149749_6_);
    }

    /**
     * Drops the block items with a specified chance of dropping the specified items
     */
    @Override
    public void dropBlockAsItemWithChance(World p_149690_1_, int p_149690_2_, int p_149690_3_, int p_149690_4_,
        int p_149690_5_, float p_149690_6_, int p_149690_7_) {
        if (!p_149690_1_.isRemote) {
            super.dropBlockAsItemWithChance(
                p_149690_1_,
                p_149690_2_,
                p_149690_3_,
                p_149690_4_,
                p_149690_5_,
                p_149690_6_,
                0);
        }
    }

    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    @Override
    public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
        return new TileEntitySuperJukebox();
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerBlockIcons(IIconRegister p_149651_1_) {
        this.blockIcon = p_149651_1_.registerIcon(this.getTextureName() + "_side");
        this.mIcon = p_149651_1_.registerIcon(this.getTextureName() + "_top");
    }

    public static class TileEntitySuperJukebox extends TileEntityJukebox implements ISidedInventory {

        /** The number of players currently using this chest */
        public int numPlayersUsing;

        private ItemStack mCurrentlyPlayingStack;
        private final InventorySuperJukebox inventoryContents;
        private String customName;

        /*
         * Important Data
         */

        public int a_TEST_INT_VAR_1;
        public int a_TEST_INT_VAR_2;
        public int a_TEST_INT_VAR_3;
        public int a_TEST_INT_VAR_4;

        public boolean mIsPlaying = false;
        public boolean mIsLooping = false;
        public boolean a_TEST_BOOL_VAR_3;
        public boolean a_TEST_BOOL_VAR_4;

        public TileEntitySuperJukebox() {
            this.inventoryContents = new InventorySuperJukebox();
        }

        @Override
        public void readFromNBT(NBTTagCompound aNBT) {
            super.readFromNBT(aNBT);

            if (aNBT.hasKey("RecordItem", 10)) {
                this.func_145857_a(ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag("RecordItem")));
            } else if (aNBT.getInteger("Record") > 0) {
                this.func_145857_a(new ItemStack(Item.getItemById(aNBT.getInteger("Record")), 1, 0));
            }

            this.inventoryContents.readFromNBT(aNBT.getCompoundTag("ContentsChest"));
            if (aNBT.hasKey("CustomName", 8)) {
                this.setCustomName(aNBT.getString("CustomName"));
            }

            mIsPlaying = aNBT.getBoolean("mIsPlaying");
            mIsLooping = aNBT.getBoolean("mIsLooping");
        }

        @Override
        public void writeToNBT(NBTTagCompound aNBT) {
            super.writeToNBT(aNBT);

            if (this.getCurrentRecord() != null) {
                aNBT.setTag(
                    "RecordItem",
                    this.func_145856_a()
                        .writeToNBT(new NBTTagCompound()));
                aNBT.setInteger(
                    "Record",
                    Item.getIdFromItem(
                        this.func_145856_a()
                            .getItem()));
            }

            final NBTTagCompound chestData = new NBTTagCompound();
            this.inventoryContents.writeToNBT(chestData);
            aNBT.setTag("ContentsChest", chestData);
            if (this.hasCustomInventoryName()) {
                aNBT.setString("CustomName", this.getCustomName());
            }

            aNBT.setBoolean("mIsPlaying", mIsPlaying);
            aNBT.setBoolean("mIsLooping", mIsLooping);
        }

        /**
         * Called to get the internal stack
         */
        @Override
        public ItemStack func_145856_a() {
            return this.mCurrentlyPlayingStack;
        }

        /**
         * Called to get the internal stack, wraps vanilla function {@link func_145856_a}.
         */
        public ItemStack getCurrentRecord() {
            return func_145856_a();
        }

        /**
         * Called to set the internal stack
         */
        @Override
        public void func_145857_a(ItemStack p_145857_1_) {
            this.mCurrentlyPlayingStack = p_145857_1_;
            this.markDirty();
        }

        /**
         * Called to set the internal stack, wraps vanilla function {@link func_145857_a}.
         */
        public void setCurrentRecord(ItemStack aStack) {
            func_145857_a(aStack);
            this.markDirty();
        }

        public InventorySuperJukebox getInventory() {
            return this.inventoryContents;
        }

        public boolean playRecord(ItemStack aRecord) {

            return false;
        }

        public boolean stopRecord() {
            return openDiscDrive();
        }

        public void setLoopState(boolean isShufflingForever) {}

        // Play button pressed
        public boolean jukeboxLogicUpdate() {

            if (this.worldObj.isRemote) {
                return true;
            }

            Logger.INFO("a");
            if (this.mIsPlaying || this.mIsLooping) {
                return selectRecordToPlayFromInventoryAndSetViaVanillaHandler();
            } else {
                return stopRecord();
            }
        }

        // Determine which record to play
        public boolean selectRecordToPlayFromInventoryAndSetViaVanillaHandler() {
            ArrayList<ItemStack> mValidRecords = new ArrayList<>();
            for (ItemStack g : this.getInventory()
                .getInventory()) {
                if (g != null) {
                    if (g.getItem() instanceof ItemRecord) {
                        mValidRecords.add(g);
                    }
                }
            }

            Logger.INFO("b1");
            // Select First Record
            ItemStack aRecordToPlay;
            if (mValidRecords.isEmpty()) {
                Logger.INFO("bX");
                return false;
            } else {
                aRecordToPlay = mValidRecords.get(!mIsLooping ? 0 : MathUtils.randInt(0, (mValidRecords.size() - 1)));
            }
            Logger.INFO("b2 - " + aRecordToPlay.getDisplayName());

            int aSlotCounter = 0;
            for (ItemStack g : this.getInventory()
                .getInventory()) {
                if (g != null && aSlotCounter <= 17) {
                    Logger.INFO("b3 - " + g.getDisplayName());
                    if (GTUtility.areStacksEqual(g, aRecordToPlay, true)) {
                        IInventory aThisInv = this.getInventory();
                        if (aThisInv.getStackInSlot(20) != null) {
                            openDiscDrive();
                        }

                        GTUtility.moveStackFromSlotAToSlotB(
                            aThisInv,
                            aThisInv,
                            aSlotCounter,
                            20,
                            (byte) 1,
                            (byte) 1,
                            (byte) 1,
                            (byte) 1);
                        setCurrentRecord(aThisInv.getStackInSlot(20));

                        World aWorld = this.worldObj;
                        int aX = this.xCoord;
                        int aY = this.yCoord;
                        int aZ = this.zCoord;
                        if (!aWorld.isRemote) {
                            aRecordToPlay = this.func_145856_a();
                            if (aRecordToPlay != null) {
                                aWorld.playAuxSFX(1005, aX, aY, aZ, Item.getIdFromItem(aRecordToPlay.getItem()));
                                this.markDirty();
                                return true;
                            }
                        }

                        Logger.INFO("b++");
                        this.markDirty();
                        return false;
                    }
                }
                aSlotCounter++;
            }

            Logger.INFO("b4");
            this.markDirty();
            return false;
        }

        public boolean genericMethodThree(Object a1, Object a2, Object a3, Object a4) {
            return false;
        }

        public void vanillaStopJukebox() {
            World aWorld = this.worldObj;
            int aX = this.xCoord;
            int aY = this.yCoord;
            int aZ = this.zCoord;
            if (!aWorld.isRemote) {
                TileEntitySuperJukebox tileentityjukebox = (TileEntitySuperJukebox) aWorld.getTileEntity(aX, aY, aZ);
                if (tileentityjukebox != null) {
                    ItemStack aRecordToPlay = tileentityjukebox.func_145856_a();
                    if (aRecordToPlay != null) {
                        aWorld.playAuxSFX(1005, aX, aY, aZ, 0);
                        aWorld.playRecord(null, aX, aY, aZ);
                        tileentityjukebox.func_145857_a(null);
                        this.markDirty();
                    }
                }
            }
        }

        public boolean openDiscDrive() {
            int aSlotCounter = 17;

            ItemStack g;

            for (int i = 17; i >= 0; i--) {
                g = this.getInventory()
                    .getInventory()[i];
                if (g == null && aSlotCounter >= 0) {
                    IInventory aThisInv = this.getInventory();
                    GTUtility
                        .moveStackFromSlotAToSlotB(aThisInv, aThisInv, 20, i, (byte) 1, (byte) 1, (byte) 1, (byte) 1);
                    vanillaStopJukebox();
                    Logger.INFO("b++");
                    this.markDirty();
                    return true;
                }
            }

            /*
             * for (ItemStack g : this.getInventory().getInventory()) { if (g == null && aSlotCounter >= 0) { IInventory
             * aThisInv = this.getInventory(); GT_Utility.moveStackFromSlotAToSlotB(aThisInv, aThisInv, 20,
             * aSlotCounter, (byte) 1, (byte) 1, (byte) 1, (byte) 1); vanillaStopJukebox(); Logger.INFO("b++"); return
             * true; } aSlotCounter--; }
             */
            this.markDirty();
            return false;
        }

        public boolean anyPlayerInRange() {
            return this.worldObj.getClosestPlayer(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, 32)
                != null;
        }

        public NBTTagCompound getTag(final NBTTagCompound nbt, final String tag) {
            if (!nbt.hasKey(tag)) {
                nbt.setTag(tag, new NBTTagCompound());
            }
            return nbt.getCompoundTag(tag);
        }

        @Override
        public int getSizeInventory() {
            return this.getInventory()
                .getSizeInventory() - 3;
        }

        @Override
        public ItemStack getStackInSlot(final int slot) {
            return this.getInventory()
                .getStackInSlot(slot);
        }

        @Override
        public ItemStack decrStackSize(final int slot, final int count) {
            return this.getInventory()
                .decrStackSize(slot, count);
        }

        @Override
        public ItemStack getStackInSlotOnClosing(final int slot) {
            return this.getInventory()
                .getStackInSlotOnClosing(slot);
        }

        @Override
        public void setInventorySlotContents(final int slot, final ItemStack stack) {
            this.getInventory()
                .setInventorySlotContents(slot, stack);
        }

        @Override
        public int getInventoryStackLimit() {
            return 1;
        }

        @Override
        public boolean isUseableByPlayer(final EntityPlayer entityplayer) {
            return this.getInventory()
                .isUseableByPlayer(entityplayer);
        }

        @Override
        public void openInventory() {
            if (this.numPlayersUsing < 0) {
                this.numPlayersUsing = 0;
            }
            if (!this.worldObj.isRemote) {
                this.numPlayersUsing++;
            }
            this.worldObj
                .addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
            this.getInventory()
                .openInventory();
        }

        @Override
        public void closeInventory() {
            if (!this.worldObj.isRemote) {
                this.numPlayersUsing--;
            }
            this.worldObj
                .addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
            this.getInventory()
                .closeInventory();
        }

        @Override
        public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) {
            if (slot >= 18) {
                return false;
            }
            return this.getInventory()
                .isItemValidForSlot(slot, itemstack);
        }

        private static final int[] SIDED_SLOTS = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
            17 };

        @Override
        public int[] getAccessibleSlotsFromSide(final int p_94128_1_) {
            return SIDED_SLOTS;
        }

        @Override
        public boolean canInsertItem(final int p_102007_1_, final ItemStack p_102007_2_, final int p_102007_3_) {
            if (p_102007_1_ >= 18) {
                return false;
            }
            return this.getInventory()
                .isItemValidForSlot(p_102007_1_, p_102007_2_);
        }

        @Override
        public boolean canExtractItem(final int p_102008_1_, final ItemStack p_102008_2_, final int p_102008_3_) {
            if (p_102008_1_ >= 18) {
                return false;
            }
            return this.getInventory()
                .isItemValidForSlot(p_102008_1_, p_102008_2_);
        }

        public String getCustomName() {
            return this.customName;
        }

        public void setCustomName(final String customName) {
            this.customName = customName;
        }

        @Override
        public String getInventoryName() {
            return this.hasCustomInventoryName() ? this.customName : "container.SuperJukebox";
        }

        @Override
        public boolean hasCustomInventoryName() {
            return (this.customName != null) && !this.customName.isEmpty();
        }
    }
}