aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/thing/metaTileEntity/multi/base/SoundLoopAnyBlock.java
blob: 15b08afb5cb76181c50df18484df9d75725ace02 (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
package tectech.thing.metaTileEntity.multi.base;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.MovingSound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;

@SideOnly(Side.CLIENT)
public class SoundLoopAnyBlock extends MovingSound {

    private final boolean stopWhenBlockActive;
    private final boolean stopWhenBlockInactive;
    private final int worldID;
    private boolean fadeOut = false;
    private int tileX;
    private int tileY;
    private int tileZ;
    private Block blockToTriggerEnd = null;

    /**
     * Constructs a SoundLoopAnyBlock.
     *
     * @param soundResource    the sound file location
     * @param tileEntity       the tile entity associated with this sound
     * @param stopWhenActive   flag to stop the sound when the block is active
     * @param stopWhenInactive flag to stop the sound when the block is inactive
     * @param offset           positional offset for sound origin from the tile entity [x, y, z]
     * @param blockCheck       block that ends the sound when matched at the sound location
     */
    public SoundLoopAnyBlock(ResourceLocation soundResource, IGregTechTileEntity tileEntity, boolean stopWhenActive,
        boolean stopWhenInactive, int[] offset, Block blockCheck) {
        super(soundResource);
        this.stopWhenBlockActive = stopWhenActive;
        this.stopWhenBlockInactive = stopWhenInactive;
        tileX = tileEntity.getXCoord();
        tileY = tileEntity.getYCoord();
        tileZ = tileEntity.getZCoord();
        xPosF = tileX + offset[0];
        yPosF = tileY + offset[1];
        zPosF = tileZ + offset[2];
        worldID = tileEntity.getWorld().provider.dimensionId;
        repeat = true;
        volume = 0.0625f;
        blockToTriggerEnd = blockCheck;
    }

    @Override
    public void update() {
        if (donePlaying) {
            return;
        }

        if (fadeOut) {
            volume -= 0.0625f;
            if (volume <= 0) {
                volume = 0;
                donePlaying = true;
            }
        } else if (volume < 1) {
            volume += 0.0625f;
        }

        World world = Minecraft.getMinecraft().thePlayer.worldObj;
        donePlaying = world.provider.dimensionId != worldID
            || !world.checkChunksExist((int) xPosF, (int) yPosF, (int) zPosF, (int) xPosF, (int) yPosF, (int) zPosF);

        if (donePlaying) return;

        Block blockAtSoundLocation = world.getBlock((int) xPosF, (int) yPosF, (int) zPosF);
        if (blockToTriggerEnd != null) {
            donePlaying = blockAtSoundLocation == blockToTriggerEnd;
        }

        if (donePlaying) return;

        TileEntity tile = world.getTileEntity(tileX, tileY, tileZ);
        donePlaying = tile == null;

        if (donePlaying) return;

        // Adjust fading based on the activity state of the tile entity
        fadeOut |= ((IGregTechTileEntity) tile).isActive() ? stopWhenBlockActive : stopWhenBlockInactive;
    }
}