Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/paulscode/sound/CommandThread.java
8644 views
1
package paulscode.sound;
2
3
/**
4
* The CommandThread class is designed to move all command processing into a
5
* single thread to be run in the background and avoid conflicts between
6
* threads. Commands are processed in the order that they were queued. The
7
* arguements for each command are stored in a
8
* {@link paulscode.sound.CommandObject CommandObject}. The Command Queue is
9
* located in the {@link paulscode.sound.SoundSystem SoundSystem} class.
10
* Calling kill() stops the thread, and this should be immediatly followed
11
* by a call to interrupt() to wake up the thread so it may end. This class
12
* also checks for temporary sources that are finished playing, and removes
13
* them.
14
*
15
* NOTE: The command thread is created automatically by the sound system, so it
16
* is unlikely that the user would ever need to use this class.
17
*<br><br>
18
*<b><i> SoundSystem License:</b></i><br><b><br>
19
* You are free to use this library for any purpose, commercial or otherwise.
20
* You may modify this library or source code, and distribute it any way you
21
* like, provided the following conditions are met:
22
*<br>
23
* 1) You may not falsely claim to be the author of this library or any
24
* unmodified portion of it.
25
*<br>
26
* 2) You may not copyright this library or a modified version of it and then
27
* sue me for copyright infringement.
28
*<br>
29
* 3) If you modify the source code, you must clearly document the changes
30
* made before redistributing the modified source code, so other users know
31
* it is not the original code.
32
*<br>
33
* 4) You are not required to give me credit for this library in any derived
34
* work, but if you do, you must also mention my website:
35
* http://www.paulscode.com
36
*<br>
37
* 5) I the author will not be responsible for any damages (physical,
38
* financial, or otherwise) caused by the use if this library or any part
39
* of it.
40
*<br>
41
* 6) I the author do not guarantee, warrant, or make any representations,
42
* either expressed or implied, regarding the use of this library or any
43
* part of it.
44
* <br><br>
45
* Author: Paul Lamb
46
* <br>
47
* http://www.paulscode.com
48
* </b>
49
*/
50
public class CommandThread extends SimpleThread
51
{
52
/**
53
* Processes status messages, warnings, and error messages.
54
*/
55
protected SoundSystemLogger logger;
56
57
/**
58
* Handle to the Sound System. This is where the Command Queue is located.
59
*/
60
private SoundSystem soundSystem;
61
62
/**
63
* Name of this class.
64
*/
65
protected String className = "CommandThread";
66
67
/**
68
* Constructor: Takes a handle to the SoundSystem object as a parameter.
69
* @param s Handle to the SoundSystem.
70
*/
71
public CommandThread( SoundSystem s )
72
{
73
// grab a handle to the message logger:
74
logger = SoundSystemConfig.getLogger();
75
76
soundSystem = s;
77
}
78
79
/**
80
* Shuts the thread down and removes references to all instantiated objects.
81
* NOTE: Method alive() will return false when cleanup() has finished.
82
*/
83
@Override
84
protected void cleanup()
85
{
86
kill();
87
88
logger = null;
89
soundSystem = null;
90
91
super.cleanup(); // Important!
92
}
93
94
/**
95
* The main loop for processing commands. The Command Thread starts out
96
* asleep, and it sleeps again after it finishes processing commands, so it
97
* must be interrupted when commands are queued for processing.
98
*/
99
@Override
100
public void run()
101
{
102
long previousTime = System.currentTimeMillis();
103
long currentTime = previousTime;
104
105
if( soundSystem == null )
106
{
107
errorMessage( "SoundSystem was null in method run().", 0 );
108
cleanup();
109
return;
110
}
111
112
// Start out asleep:
113
snooze( 3600000 );
114
115
while( !dying() )
116
{
117
// Perform user-specific source management:
118
soundSystem.ManageSources();
119
120
// Process all queued commands:
121
soundSystem.CommandQueue( null );
122
123
// Remove temporary sources every ten seconds:
124
currentTime = System.currentTimeMillis();
125
if( (!dying()) && ((currentTime - previousTime) > 10000) )
126
{
127
previousTime = currentTime;
128
soundSystem.removeTemporarySources();
129
}
130
131
// Wait for more commands:
132
if( !dying() )
133
snooze( 3600000 );
134
}
135
136
cleanup(); // Important!
137
}
138
139
/**
140
* Prints a message.
141
* @param message Message to print.
142
*/
143
protected void message( String message, int indent )
144
{
145
logger.message( message, indent );
146
}
147
148
/**
149
* Prints an important message.
150
* @param message Message to print.
151
*/
152
protected void importantMessage( String message, int indent )
153
{
154
logger.importantMessage( message, indent );
155
}
156
157
/**
158
* Prints the specified message if error is true.
159
* @param error True or False.
160
* @param message Message to print if error is true.
161
* @return True if error is true.
162
*/
163
protected boolean errorCheck( boolean error, String message )
164
{
165
return logger.errorCheck( error, className, message, 0 );
166
}
167
168
/**
169
* Prints an error message.
170
* @param message Message to print.
171
*/
172
protected void errorMessage( String message, int indent )
173
{
174
logger.errorMessage( className, message, indent );
175
}
176
}
177
178