Skip to content

Commit b3638a3

Browse files
committed
functions for extendable objects that can return null are no longer executed when no players are online
1 parent 8ea06b5 commit b3638a3

19 files changed

+125
-114
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = 'net.ioixd'
11-
version = '1.0-SNAPSHOT'
11+
version = '1.1'
1212

1313
compileJava {
1414
options.compilerArgs += ["-h", file("include")]

native/src/lib.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
struct SwitchableLibrary {
2-
library: Library,
2+
library: RwLock<ManuallyDrop<Library>>,
33
enabled: RwLock<bool>,
44
}
55

66
impl SwitchableLibrary {
7-
fn new(library: Library, enabled: bool) -> Self {
7+
fn new(library: ManuallyDrop<Library>, enabled: bool) -> Self {
88
Self {
9-
library: library,
9+
library: RwLock::new(library),
1010
enabled: RwLock::new(enabled),
1111
}
1212
}
13-
fn library(&self) -> &Library {
14-
&self.library
15-
}
1613
fn set_enabled(&self, enabled: bool) {
1714
*self.enabled.write() = enabled;
1815
}
@@ -28,7 +25,7 @@ impl LibraryManager {
2825
loaded_libraries: RwLock::new(HashMap::new()),
2926
}
3027
}
31-
fn push_lib(&self, libname: String, lib: (Library, bool)) {
28+
fn push_lib(&self, libname: String, lib: (ManuallyDrop<Library>, bool)) {
3229
let libs = &mut self.loaded_libraries.write();
3330
libs.insert(libname, SwitchableLibrary::new(lib.0, lib.1));
3431
}
@@ -54,11 +51,11 @@ use jni::{
5451
sys::{jboolean, jint},
5552
JNIEnv,
5653
};
57-
use libloading::Library;
54+
use libloading::{Library, Symbol};
5855
use once_cell::sync::Lazy;
59-
use parking_lot::{RwLock, RwLockReadGuard};
56+
use parking_lot::{Mutex, RwLock, RwLockReadGuard};
6057

61-
use std::{collections::HashMap, error::Error, fmt::Display};
58+
use std::{collections::HashMap, error::Error, fmt::Display, mem::ManuallyDrop};
6259

6360
fn load_library(path: String) -> Result<Library, libloading::Error> {
6461
unsafe {
@@ -108,7 +105,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
108105
{
109106
name = name.replace(".so", "");
110107
}
111-
LIBRARY_MANAGER.push_lib(name, (lib, true));
108+
LIBRARY_MANAGER.push_lib(name, (ManuallyDrop::new(lib), true));
112109
}
113110

114111
#[no_mangle]
@@ -164,7 +161,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
164161
.to_string();
165162

166163
let manager = LIBRARY_MANAGER.libraries();
167-
let lib = manager.get(&libname).unwrap().library();
164+
let lib = manager.get(&libname).unwrap().library.read();
168165
let func: Result<
169166
libloading::Symbol<unsafe extern "C" fn(JNIEnv<'_>, JObject<'_>)>,
170167
libloading::Error,
@@ -178,7 +175,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
178175
return false.into();
179176
}
180177
_ => {
181-
throw_libloading_error(&mut env, &libname, e);
178+
throw_libloading_error(&mut env, &libname, &e);
182179
return false.into();
183180
}
184181
}
@@ -210,7 +207,8 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
210207
&"library".to_string(),
211208
manager.get(&libname),
212209
)
213-
.library();
210+
.library
211+
.read();
214212
let func: Result<
215213
libloading::Symbol<extern "C" fn(JNIEnv<'_>, JObject<'_>)>,
216214
libloading::Error,
@@ -224,7 +222,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
224222
return false.into();
225223
}
226224
_ => {
227-
throw_libloading_error(&mut env, &libname, e);
225+
throw_libloading_error(&mut env, &libname, &e);
228226
return false.into();
229227
}
230228
}
@@ -260,7 +258,8 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
260258
&format!("manager.get({})", &libname),
261259
manager.get(&libname),
262260
)
263-
.library();
261+
.library
262+
.write();
264263
let func: Result<
265264
libloading::Symbol<
266265
extern "C" fn(JNIEnv<'_>, jint, JObject<'_>, JObject<'_>) -> JObject<'a>,
@@ -270,12 +269,17 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
270269
if let Err(e) = &func {
271270
match e {
272271
_ => {
273-
throw_libloading_error(&mut env, &libname, e);
272+
throw_libloading_error(&mut env, &libname, &e);
274273
}
275274
}
276275
}
277276
let func = func.unwrap();
278-
return func(env, address, plugin, ev);
277+
if !plugin.is_null() && !ev.is_null() {
278+
return func(env, address, plugin, ev);
279+
} else {
280+
println!("NULL");
281+
return JObject::null();
282+
}
279283
}
280284

281285
type BiConsumer = unsafe extern "system" fn(t: JObject, u: JObject);
@@ -395,6 +399,4 @@ pub fn throw_with_error(env: &mut JNIEnv, exception_name: &str, exception_messag
395399
if let Err(_) = er {
396400
env.exception_describe().unwrap();
397401
}
398-
// TEMPORARY: just describe the error
399-
env.exception_describe().unwrap();
400402
}

src/main/java/net/ioixd/blackbox/extendables/ExtendableBiomeProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public org.bukkit.block.Biome getBiome(org.bukkit.generator.WorldInfo arg0, int
2626
result = Misc.tryExecute(this.inLibName, this.name, "BiomeProvider", "getBiome",
2727
address, plugin, new Object[] {
2828
arg0, arg1, arg2, arg3
29-
}, true);
29+
}, true, true);
3030
} catch (Exception ex) {
3131
ex.printStackTrace();
3232
}
@@ -39,7 +39,7 @@ public java.util.List<org.bukkit.block.Biome> getBiomes(org.bukkit.generator.Wor
3939
result = Misc.tryExecute(this.inLibName, this.name, "BiomeProvider", "getBiomes",
4040
address, plugin, new Object[] {
4141
arg0
42-
}, true);
42+
}, true, true);
4343
} catch (Exception ex) {
4444
ex.printStackTrace();
4545
}

src/main/java/net/ioixd/blackbox/extendables/ExtendableBlockPopulator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void populate(WorldInfo worldInfo, Random random, int chunkX, int chunkZ,
2020
result = Misc.tryExecute(this.inLibName, this.name, "BlockPopulator", "populate",
2121
address, plugin, new Object[] {
2222
worldInfo, random, chunkX, chunkZ, limitedRegion
23-
}, false);
23+
}, false, false);
2424
} catch (Exception ex) {
2525
ex.printStackTrace();
2626
}

src/main/java/net/ioixd/blackbox/extendables/ExtendableBukkitRunnable.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public synchronized BukkitTask runTask(Plugin arg0) throws IllegalArgumentExcept
1919
result = Misc.tryExecute(this.inLibName, this.name, "BukkitTask", "runTask",
2020
address, plugin, new Object[] {
2121
arg0
22-
}, false);
22+
}, false, true);
2323
} catch (Exception ex) {
2424
ex.printStackTrace();
2525
}
@@ -38,7 +38,7 @@ public synchronized BukkitTask runTaskAsynchronously(Plugin arg0)
3838
result = Misc.tryExecute(this.inLibName, this.name, "BukkitTask", "runTaskAsynchronously",
3939
address, plugin, new Object[] {
4040
arg0
41-
}, false);
41+
}, false, true);
4242
} catch (Exception ex) {
4343
ex.printStackTrace();
4444
}
@@ -57,7 +57,7 @@ public synchronized BukkitTask runTaskLater(Plugin arg0, long arg1)
5757
result = Misc.tryExecute(this.inLibName, this.name, "BukkitTask", "runTaskLater",
5858
address, plugin, new Object[] {
5959
arg0, arg1
60-
}, false);
60+
}, false, true);
6161
} catch (Exception ex) {
6262
ex.printStackTrace();
6363
}
@@ -77,7 +77,7 @@ public synchronized BukkitTask runTaskLaterAsynchronously(Plugin arg0, long arg1
7777
"runTaskLaterAsynchronously",
7878
address, plugin, new Object[] {
7979
arg0, arg1
80-
}, false);
80+
}, false, true);
8181
} catch (Exception ex) {
8282
ex.printStackTrace();
8383
}
@@ -96,7 +96,7 @@ public synchronized BukkitTask runTaskTimer(Plugin arg0, long arg1, long arg2)
9696
result = Misc.tryExecute(this.inLibName, this.name, "BukkitTask", "runTaskTimer",
9797
address, plugin, new Object[] {
9898
arg0, arg1, arg2
99-
}, false);
99+
}, false, true);
100100
} catch (Exception ex) {
101101
throw new RuntimeException(ex);
102102
}
@@ -116,7 +116,7 @@ public synchronized BukkitTask runTaskTimerAsynchronously(Plugin arg0, long arg1
116116
"runTaskTimerAsynchronously",
117117
address, plugin, new Object[] {
118118
arg0, arg1, arg2
119-
}, false);
119+
}, false, true);
120120
} catch (Exception ex) {
121121
ex.printStackTrace();
122122
}
@@ -139,7 +139,7 @@ public ExtendableBukkitRunnable(int address, Plugin plugin, String name, String
139139
public void run() {
140140
try {
141141
Misc.tryExecute(this.inLibName, this.name, "BukkitRunnable", "run",
142-
address, plugin, new Object[] {}, true);
142+
address, plugin, new Object[] {}, true, false);
143143
} catch (Exception ex) {
144144
ex.printStackTrace();
145145
}

src/main/java/net/ioixd/blackbox/extendables/ExtendableChunkGenerator.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public boolean canSpawn(World arg0, int arg1, int arg2) {
2626
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "canSpawn",
2727
address, plugin, new Object[] {
2828
arg0, arg1, arg2
29-
}, false);
29+
}, false, true);
3030
} catch (Exception ex) {
3131
ex.printStackTrace();
3232
}
@@ -44,7 +44,7 @@ public void generateBedrock(WorldInfo arg0, Random arg1, int arg2, int arg3, Chu
4444
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "generateBedrock",
4545
address, plugin, new Object[] {
4646
arg0, arg1, arg2, arg3, arg4
47-
}, false);
47+
}, false, false);
4848
} catch (Exception ex) {
4949
ex.printStackTrace();
5050
}
@@ -60,7 +60,7 @@ public void generateCaves(WorldInfo arg0, Random arg1, int arg2, int arg3, Chunk
6060
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "generateCaves",
6161
address, plugin, new Object[] {
6262
arg0, arg1, arg2, arg3, arg4
63-
}, false);
63+
}, false, false);
6464
} catch (Exception ex) {
6565
ex.printStackTrace();
6666
}
@@ -76,7 +76,7 @@ public ChunkData generateChunkData(World arg1, Random arg2, int arg3, int arg4,
7676
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "generateChunkData",
7777
address, plugin, new Object[] {
7878
arg1, arg2, arg3, arg4, arg5
79-
}, false);
79+
}, false, true);
8080
} catch (Exception ex) {
8181
ex.printStackTrace();
8282
}
@@ -94,7 +94,7 @@ public void generateNoise(WorldInfo arg1, Random arg2, int arg3, int arg4, Chunk
9494
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "generateNoise",
9595
address, plugin, new Object[] {
9696
arg1, arg2, arg3, arg4, arg5
97-
}, false);
97+
}, false, false);
9898
} catch (Exception ex) {
9999
ex.printStackTrace();
100100
}
@@ -110,7 +110,7 @@ public void generateSurface(WorldInfo arg1, Random arg2, int arg3, int arg4, Chu
110110
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "generateSurface",
111111
address, plugin, new Object[] {
112112
arg1, arg2, arg3, arg4, arg5
113-
}, false);
113+
}, false, false);
114114
} catch (Exception ex) {
115115
ex.printStackTrace();
116116
}
@@ -126,7 +126,7 @@ public int getBaseHeight(WorldInfo arg1, Random arg2, int arg3, int arg4, Height
126126
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "getBaseHeight",
127127
address, plugin, new Object[] {
128128
arg1, arg2, arg3, arg4, arg5
129-
}, false);
129+
}, false, false);
130130
} catch (Exception ex) {
131131
ex.printStackTrace();
132132
}
@@ -145,7 +145,7 @@ public BiomeProvider getDefaultBiomeProvider(WorldInfo arg1) {
145145
"getDefaultBiomeProvider",
146146
address, plugin, new Object[] {
147147
arg1
148-
}, false);
148+
}, false, false);
149149
} catch (Exception ex) {
150150
ex.printStackTrace();
151151
}
@@ -163,7 +163,7 @@ public List<BlockPopulator> getDefaultPopulators(World arg1) {
163163
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "getDefaultPopulators",
164164
address, plugin, new Object[] {
165165
arg1
166-
}, false);
166+
}, false, false);
167167
} catch (Exception ex) {
168168
ex.printStackTrace();
169169
}
@@ -182,7 +182,7 @@ public Location getFixedSpawnLocation(World arg1, Random arg2) {
182182
"getFixedSpawnLocation",
183183
address, plugin, new Object[] {
184184
arg1, arg2
185-
}, false);
185+
}, false, true);
186186
} catch (Exception ex) {
187187
ex.printStackTrace();
188188
}
@@ -198,7 +198,7 @@ public boolean isParallelCapable() {
198198
Object result = null;
199199
try {
200200
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "isParallelCapable",
201-
address, plugin, new Object[] {}, false);
201+
address, plugin, new Object[] {}, false, true);
202202
} catch (Exception ex) {
203203
ex.printStackTrace();
204204
}
@@ -215,7 +215,7 @@ public boolean shouldGenerateBedrock() {
215215
try {
216216
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator",
217217
"shouldGenerateBedrock",
218-
address, plugin, new Object[] {}, false);
218+
address, plugin, new Object[] {}, false, true);
219219
} catch (Exception ex) {
220220
ex.printStackTrace();
221221
}
@@ -233,7 +233,7 @@ public boolean shouldGenerateCaves(WorldInfo arg1, Random arg2, int arg3, int ar
233233
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "shouldGenerateCaves",
234234
address, plugin, new Object[] {
235235
arg1, arg2, arg3, arg4
236-
}, false);
236+
}, false, true);
237237
} catch (Exception ex) {
238238
ex.printStackTrace();
239239
}
@@ -252,7 +252,7 @@ public boolean shouldGenerateDecorations(WorldInfo arg0, Random arg1, int arg2,
252252
"shouldGenerateDecorations",
253253
address, plugin, new Object[] {
254254
arg0, arg1, arg2, arg3
255-
}, false);
255+
}, false, true);
256256
} catch (Exception ex) {
257257
ex.printStackTrace();
258258
}
@@ -270,7 +270,7 @@ public boolean shouldGenerateMobs(WorldInfo arg0, Random arg1, int arg2, int arg
270270
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "shouldGenerateMobs",
271271
address, plugin, new Object[] {
272272
arg0, arg1, arg2, arg3
273-
}, false);
273+
}, false, true);
274274
} catch (Exception ex) {
275275
ex.printStackTrace();
276276
}
@@ -288,7 +288,7 @@ public boolean shouldGenerateNoise(WorldInfo arg0, Random arg1, int arg2, int ar
288288
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "shouldGenerateNoise",
289289
address, plugin, new Object[] {
290290
arg0, arg1, arg2, arg3
291-
}, false);
291+
}, false, true);
292292
} catch (Exception ex) {
293293
ex.printStackTrace();
294294
}
@@ -307,7 +307,7 @@ public boolean shouldGenerateStructures(WorldInfo arg0, Random arg1, int arg2, i
307307
"shouldGenerateStructures",
308308
address, plugin, new Object[] {
309309
arg0, arg1, arg2, arg3
310-
}, false);
310+
}, false, true);
311311
} catch (Exception ex) {
312312
ex.printStackTrace();
313313
}
@@ -325,7 +325,7 @@ public boolean shouldGenerateSurface(WorldInfo arg0, Random arg1, int arg2, int
325325
result = Misc.tryExecute(this.inLibName, this.name, "ChunkGenerator", "runTaskTimer",
326326
address, plugin, new Object[] {
327327
arg0, arg1, arg2, arg3
328-
}, false);
328+
}, false, true);
329329
} catch (Exception ex) {
330330
ex.printStackTrace();
331331
}

src/main/java/net/ioixd/blackbox/extendables/ExtendableCommandExecutor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public boolean onCommand(org.bukkit.command.CommandSender arg0, org.bukkit.comma
2525
result = Misc.tryExecute(this.inLibName, this.name, "CommandExecutor", "onCommand",
2626
address, plugin, new Object[] {
2727
arg0, arg1, arg2, arg3
28-
}, true);
28+
}, true, true);
2929
} catch (Exception ex) {
3030
ex.printStackTrace();
3131
}

0 commit comments

Comments
 (0)