aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/plugin/fixes/vanilla/music/MusicTocker.java
blob: ed1d9890d797af50b804fd9bf375327648a63f5f (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
package gtPlusPlus.plugin.fixes.vanilla.music;

import java.lang.reflect.Field;
import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.MusicTicker;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.util.MathHelper;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.objects.XSTR;
import gtPlusPlus.api.interfaces.IPlugin;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import gtPlusPlus.preloader.CORE_Preloader;

@SideOnly(Side.CLIENT)
public class MusicTocker extends MusicTicker implements Runnable {

    private final Random mRandom = new XSTR();
    private final Minecraft mMinecraft;
    private final IPlugin mPlugin;
    private ISound mSound;
    private int mTimeUntilNextTrack = 100;

    public boolean mVanillaManager = false;

    public MusicTocker(IPlugin aPlugin) {
        super(Minecraft.getMinecraft());
        mPlugin = aPlugin;
        mMinecraft = Minecraft.getMinecraft();
        mPlugin.log("[BGM] Created BGM Watchdog with a delay of " + getDelay() + " ticks.");
        inject();
    }

    private static int getDelay() {
        return CORE_Preloader.enableWatchdogBGM;
    }

    private boolean inject() {
        mPlugin.log("[BGM] Inject new Watchdog into Minecraft instance.");
        ReflectionUtils.setField(Minecraft.getMinecraft(), "mcMusicTicker", this);
        mPlugin.log("[BGM] Verifying...");
        Field f = ReflectionUtils.getField(Minecraft.class, "mcMusicTicker");
        try {
            Object m = f.get(mMinecraft);
            if (m != null) {
                if (m instanceof MusicTocker || m.getClass().isAssignableFrom(getClass())) {
                    mPlugin.log("[BGM] Success.");
                    return true;
                } else if (m instanceof MusicTicker || m.getClass().isAssignableFrom(MusicTicker.class)) {
                    mPlugin.log("[BGM] Found Vanilla MusicTicker, but may be instance of MusicTocker.");
                    return true;
                }
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {}

        mPlugin.log("[BGM] Failed.");
        return false;
    }

    private void updateInternalNumber() {
        if (ReflectionUtils.doesFieldExist(getClass(), "field_147676_d")) {
            ReflectionUtils.setField(this, "field_147676_d", mTimeUntilNextTrack);
        }
    }

    private void updateInternalSound(ISound aSound) {
        if (ReflectionUtils.doesFieldExist(getClass(), "field_147678_c")) {
            ReflectionUtils.setField(this, "field_147678_c", aSound);
        }
    }

    /**
     * Updates the JList with a new model.
     */
    @Override
    public void update() {
        run();
        mVanillaManager = true;
    }

    @Override
    public void run() {
        MusicType musictype = this.mMinecraft.func_147109_W();

        if (this.mSound != null) {
            if (!musictype.getMusicTickerLocation().equals(this.mSound.getPositionedSoundLocation())) {
                this.mMinecraft.getSoundHandler().stopSound(this.mSound);
                this.mTimeUntilNextTrack = MathHelper.getRandomIntegerInRange(this.mRandom, 0, getDelay() / 2);
                updateInternalNumber();
                Logger.INFO("[BGM] Adjusted BGM delay 1");
            }
            if (!this.mMinecraft.getSoundHandler().isSoundPlaying(this.mSound)) {
                this.mSound = null;
                updateInternalSound(null);
                this.mTimeUntilNextTrack = Math.min(
                        MathHelper.getRandomIntegerInRange(this.mRandom, getDelay(), getDelay() * 2),
                        this.mTimeUntilNextTrack);
                updateInternalNumber();
                Logger.INFO("[BGM] Adjusted BGM delay 2");
            }
        } else if (this.mSound == null && this.mTimeUntilNextTrack-- <= 0) {
            this.mSound = PositionedSoundRecord.func_147673_a(musictype.getMusicTickerLocation());
            updateInternalSound(mSound);
            this.mMinecraft.getSoundHandler().playSound(this.mSound);
            this.mTimeUntilNextTrack = getDelay();
            updateInternalNumber();
            Logger.INFO("[BGM] Adjusted BGM 3");
        }

        /*
         * try { // Get Value stored in underlying object. Integer aRealDelay = (Integer)
         * ReflectionUtils.getField(getClass(), "field_147676_d").get(this); if (aRealDelay == null) { return; } else {
         * if (aRealDelay > getDelay() || aRealDelay <= 0) { this.mTimeUntilNextTrack = getDelay();
         * updateInternalNumber(); } else { this.mTimeUntilNextTrack -= 5 * 20; updateInternalNumber(); } aRealDelay =
         * (Integer) ReflectionUtils.getField(getClass(), "field_147676_d").get(this);
         * Logger.INFO("[BGM] Adjusted BGM - "+aRealDelay); } } catch (IllegalArgumentException | IllegalAccessException
         * e) { }
         */
    }
}