SoundHandlerReloadInhibitor.java
3.74 KB
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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.client;
import java.util.List;
import com.mumfrey.liteloader.client.ducks.IReloadable;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.client.resources.SimpleReloadableResourceManager;
/**
* Manager object which handles inhibiting the sound handler's reload
* notification at startup.
*
* @author Adam Mummery-Smith
*/
public class SoundHandlerReloadInhibitor
{
/**
* Resource Manager
*/
private SimpleReloadableResourceManager resourceManager;
/**
* Sound manager
*/
private SoundHandler soundHandler;
/**
* True if inhibition is currently active
*/
private boolean inhibited;
/**
* So that we can re-insert the sound manager at the same index, we store
* the index we remove it from.
*/
private int storedIndex;
SoundHandlerReloadInhibitor(SimpleReloadableResourceManager resourceManager, SoundHandler soundHandler)
{
this.resourceManager = resourceManager;
this.soundHandler = soundHandler;
}
/**
* Inhibit the sound manager reload notification
*
* @return true if inhibit was applied
*/
public boolean inhibit()
{
try
{
if (!this.inhibited)
{
List<IResourceManagerReloadListener> reloadListeners = ((IReloadable)this.resourceManager).getReloadListeners();
if (reloadListeners != null)
{
this.storedIndex = reloadListeners.indexOf(this.soundHandler);
if (this.storedIndex > -1)
{
LiteLoaderLogger.info("Inhibiting sound handler reload");
reloadListeners.remove(this.soundHandler);
this.inhibited = true;
return true;
}
}
}
}
catch (Exception ex)
{
LiteLoaderLogger.warning("Error inhibiting sound handler reload");
}
return false;
}
/**
* Remove the sound manager reload inhibit
*
* @param reload True to reload the sound manager now
* @return true if the sound manager was successfully restored
*/
public boolean unInhibit(boolean reload)
{
try
{
if (this.inhibited)
{
List<IResourceManagerReloadListener> reloadListeners = ((IReloadable)this.resourceManager).getReloadListeners();
if (reloadListeners != null)
{
if (this.storedIndex > -1)
{
reloadListeners.add(this.storedIndex, this.soundHandler);
}
else
{
reloadListeners.add(this.soundHandler);
}
LiteLoaderLogger.info("Sound handler reload inhibit removed");
if (reload)
{
LiteLoaderLogger.info("Reloading sound handler");
this.soundHandler.onResourceManagerReload(this.resourceManager);
}
this.inhibited = false;
return true;
}
}
}
catch (Exception ex)
{
LiteLoaderLogger.warning("Error removing sound handler reload inhibit");
}
return false;
}
public boolean isInhibited()
{
return this.inhibited;
}
}