Skip to content

Commit 8509b2c

Browse files
WunderoWundero
Wundero
authored and
Wundero
committed
4.1 fixes
Increment version number Handle listeners properly. Should no longer have issues with stack dumps. Enable and disable commands.
1 parent e79dda3 commit 8509b2c

File tree

8 files changed

+114
-53
lines changed

8 files changed

+114
-53
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'me.rojo8399.placeholderapi'
7-
version = '4.0'
7+
version = '4.1'
88
description = 'An API for all of your placeholders.'
99

1010
compileJava.options.encoding = 'UTF-8'

src/main/java/me/rojo8399/placeholderapi/ExpansionBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ public interface ExpansionBuilder<S, O, V, B extends ExpansionBuilder<S, O, V, B
133133
*/
134134
boolean buildAndRegister() throws Exception;
135135

136+
/**
137+
* Register listeners via the placeholder. This will attempt to use the
138+
* provided plugin object for registration.
139+
*
140+
* This listener will be unregistered and then registered again on reload.
141+
*/
142+
B listen(Object listeners);
143+
136144
/**
137145
* Adds to the list of supported tokens for the expansion.
138146
*

src/main/java/me/rojo8399/placeholderapi/impl/PlaceholderAPIPlugin.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
3232
import java.util.HashMap;
3333
import java.util.HashSet;
3434
import java.util.List;
35+
import java.util.Optional;
3536
import java.util.Set;
3637
import java.util.stream.Collectors;
3738

@@ -85,7 +86,7 @@ public class PlaceholderAPIPlugin {
8586

8687
public static final String PLUGIN_ID = "placeholderapi";
8788
public static final String PLUGIN_NAME = "PlaceholderAPI";
88-
public static final String PLUGIN_VERSION = "4.0";
89+
public static final String PLUGIN_VERSION = "4.1";
8990
private static PlaceholderAPIPlugin instance;
9091

9192
@Inject
@@ -227,6 +228,55 @@ public void onGameInitializationEvent(GameInitializationEvent event) {
227228
CommandSpec reloadCommand = CommandSpec.builder()
228229
.arguments(GenericArguments.optional(GenericArguments.string(Text.of("id"))))
229230
.permission("placeholderapi.admin").executor(new RefreshCommand()).build();
231+
CommandSpec enable = CommandSpec.builder().executor((src, params) -> {
232+
String id = params.<String>getOne("id").orElse(null);
233+
if (!Store.get().has(id)) {
234+
throw new CommandException(Messages.get().placeholder.invalidPlaceholder.t());
235+
}
236+
Boolean rel = params.<Boolean>getOne("relational").orElse(null);
237+
if (rel != null) {
238+
Optional<Expansion<?, ?, ?>> e = Store.get().get(id, rel);
239+
if (!e.isPresent()) {
240+
throw new CommandException(Messages.get().placeholder.invalidPlaceholder.t());
241+
}
242+
e.get().enable();
243+
src.sendMessage(Messages.get().placeholder.placeholderEnabled.t());
244+
return CommandResult.success();
245+
} else {
246+
Expansion<?, ?, ?> e = Store.get().get(id, false).orElse(Store.get().get(id, false)
247+
.orElseThrow(() -> new CommandException(Messages.get().placeholder.invalidPlaceholder.t())));
248+
e.enable();
249+
src.sendMessage(Messages.get().placeholder.placeholderEnabled.t());
250+
return CommandResult.success();
251+
}
252+
}).arguments(GenericArguments.string(Text.of("id")),
253+
GenericArguments.optional(GenericArguments.bool(Text.of("relational"))))
254+
.permission("placeholderapi.admin").build();
255+
256+
CommandSpec disable = CommandSpec.builder().executor((src, params) -> {
257+
String id = params.<String>getOne("id").orElse(null);
258+
if (!Store.get().has(id)) {
259+
throw new CommandException(Messages.get().placeholder.invalidPlaceholder.t());
260+
}
261+
Boolean rel = params.<Boolean>getOne("relational").orElse(null);
262+
if (rel != null) {
263+
Optional<Expansion<?, ?, ?>> e = Store.get().get(id, rel);
264+
if (!e.isPresent()) {
265+
throw new CommandException(Messages.get().placeholder.invalidPlaceholder.t());
266+
}
267+
e.get().disable();
268+
src.sendMessage(Messages.get().placeholder.placeholderDisabled.t());
269+
return CommandResult.success();
270+
} else {
271+
Expansion<?, ?, ?> e = Store.get().get(id, false).orElse(Store.get().get(id, false)
272+
.orElseThrow(() -> new CommandException(Messages.get().placeholder.invalidPlaceholder.t())));
273+
e.disable();
274+
src.sendMessage(Messages.get().placeholder.placeholderDisabled.t());
275+
return CommandResult.success();
276+
}
277+
}).arguments(GenericArguments.string(Text.of("id")),
278+
GenericArguments.optional(GenericArguments.bool(Text.of("relational"))))
279+
.permission("placeholderapi.admin").build();
230280
// placeholderapi
231281
CommandSpec baseCmd = CommandSpec.builder().executor(new CommandExecutor() {
232282
// send plugin name + version
@@ -237,25 +287,29 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
237287
return CommandResult.success();
238288
}
239289
}).child(parseCmd, "parse", "p").child(listCmd, "list", "l").child(infoCmd, "info", "i")
240-
.child(reloadCommand, "reload", "r").build();
290+
.child(reloadCommand, "reload", "r").child(enable, "enable").child(disable, "disable").build();
241291
game.getCommandManager().register(plugin, baseCmd, "placeholderapi", "papi");
242292

243293
}
244294

245295
private Set<Object> alreadyRegistered = new HashSet<>();
246296

247-
public void registerListeners(Object object) {
297+
public void registerListeners(Object object, Object plugin) {
248298
if (alreadyRegistered.contains(object)) {
249299
return;
250300
}
251-
Sponge.getEventManager().registerListeners(this, object);
301+
Sponge.getEventManager().registerListeners(plugin, object);
252302
alreadyRegistered.add(object);
253303
}
254304

305+
public void registerListeners(Object object) {
306+
registerListeners(object, this);
307+
}
308+
255309
public void unregisterListeners(Object object) {
256-
Sponge.getEventManager().unregisterListeners(object);
257310
if (alreadyRegistered.contains(object)) {
258311
alreadyRegistered.remove(object);
312+
Sponge.getEventManager().unregisterListeners(object);
259313
}
260314
}
261315

src/main/java/me/rojo8399/placeholderapi/impl/configs/Messages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public static class Placeholders {
174174
public Message reloadSuccess = of("&aPlaceholder reloaded successfully!");
175175
@Setting("reload-failed")
176176
public Message reloadFailed = of("&cPlaceholder failed to reload!");
177+
@Setting("placeholder-enabled")
178+
public Message placeholderEnabled = of("&aPlaceholder enabled!");
179+
@Setting("placeholder-disabled")
180+
public Message placeholderDisabled = of("&aPlaceholder disabled!");
177181
}
178182

179183
@Setting

src/main/java/me/rojo8399/placeholderapi/impl/placeholder/Expansion.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public abstract class Expansion<S, O, V> {
6464
private Class<? extends V> valueClass;
6565
private boolean relational = false;
6666
private boolean enabled = true;
67+
private Runnable reloadListeners;
6768

6869
/**
6970
* Parse the placeholder for the provided arguments.
@@ -87,10 +88,24 @@ public abstract class Expansion<S, O, V> {
8788
*/
8889
final boolean refresh() {
8990
populateConfig();
90-
unregisterListeners();
91-
registerListeners();
91+
reloadListeners();
9292
return reload();
9393
}
94+
95+
final void reloadListeners() {
96+
if (reloadListeners != null) {
97+
reloadListeners.run();
98+
}
99+
}
100+
101+
/**
102+
* Set the function to call to reload listeners. Will be called upon reload.
103+
* @param run
104+
* The code to execute.
105+
*/
106+
final void setReloadListeners(Runnable run) {
107+
this.reloadListeners = run;
108+
}
94109

95110
/**
96111
* Attempt to cast an object to the Observer type. Returns null if it fails.
@@ -433,18 +448,6 @@ public final void disable() {
433448
this.enabled = false;
434449
}
435450

436-
/**
437-
* Unregister listeners. By default this does nothing.
438-
*/
439-
public void unregisterListeners() {
440-
}
441-
442-
/**
443-
* Register listeners. By default this does nothing.
444-
*/
445-
public void registerListeners() {
446-
}
447-
448451
private final void checkClasses() {
449452
Class<? extends V> v = getValueClass();
450453
if (this.valueClass == null || !v.isAssignableFrom(this.valueClass)) {

src/main/java/me/rojo8399/placeholderapi/impl/placeholder/ExpansionBuilderImpl.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ of this software and associated documentation files (the "Software"), to deal
4545

4646
import me.rojo8399.placeholderapi.ExpansionBuilder;
4747
import me.rojo8399.placeholderapi.Placeholder;
48+
import me.rojo8399.placeholderapi.impl.PlaceholderAPIPlugin;
4849

4950
/**
5051
* @author Wundero
@@ -150,7 +151,7 @@ public static <S, O, V> ExpansionBuilderImpl<S, O, V> unverified() {
150151
private ExpansionFunction<S, O, V> func;
151152
private Predicate<Expansion<S, O, V>> reload = (func) -> true;
152153
private boolean relational = false;
153-
private Object plugin, config;
154+
private Object plugin, config, listeners;
154155

155156
/**
156157
* @return The description of the expansion.
@@ -236,6 +237,14 @@ public ExpansionBuilderImpl<S, O, V> plugin(Object plugin) {
236237
return this;
237238
}
238239

240+
@Override
241+
public ExpansionBuilderImpl<S, O, V> listen(Object o) {
242+
if (o != null) {
243+
this.listeners = o;
244+
}
245+
return this;
246+
}
247+
239248
/**
240249
* Add a function to call upon reload of the placeholder.
241250
*
@@ -460,26 +469,16 @@ public V parse(S source, O observer, Optional<String> token) throws Exception {
460469
public boolean reload() {
461470
return reload.test(this);
462471
}
463-
464-
@Override
465-
public void registerListeners() {
466-
super.registerListeners();
467-
if (regList != null) {
468-
regList.run();
469-
}
470-
}
471-
472-
@Override
473-
public void unregisterListeners() {
474-
super.unregisterListeners();
475-
if (unregList != null) {
476-
unregList.run();
477-
}
478-
}
479472
};
480473
if (config != null) {
481474
exp.setConfig(config);
482475
}
476+
if (listeners != null) {
477+
exp.setReloadListeners(() -> {
478+
PlaceholderAPIPlugin.getInstance().unregisterListeners(listeners);
479+
PlaceholderAPIPlugin.getInstance().registerListeners(listeners, plugin);
480+
});
481+
}
483482
return exp;
484483
}
485484

@@ -534,8 +533,6 @@ private static <S, O, V> ExpansionBuilderImpl<S, O, V> from(ExpansionBuilderImpl
534533
if (n.config == null) {
535534
n.config = exp.getConfiguration();
536535
}
537-
n.regList = exp::registerListeners;
538-
n.unregList = exp::unregisterListeners;
539536
n.func = exp::parse;
540537
return n;
541538
}
@@ -600,8 +597,6 @@ public ExpansionBuilderImpl<S, O, V> from(Expansion<S, O, V> exp) {
600597
this.plugin = exp.getPlugin();
601598
this.config = exp.getConfiguration();
602599
this.relational = exp.relational();
603-
this.regList = exp::registerListeners;
604-
this.unregList = exp::unregisterListeners;
605600
this.func = exp::parse;
606601
return this;
607602
}

src/main/java/me/rojo8399/placeholderapi/impl/placeholder/Store.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ public boolean register(Expansion<?, ?, ?> expansion) {
105105
return false;
106106
}
107107
expansion.populateConfig();
108-
expansion.registerListeners();
108+
expansion.reloadListeners();
109109
getMap(expansion.relational()).put(id, expansion);
110110
return true;
111111
}
112112

113113
public boolean has(String id, boolean relational) {
114+
if(id==null) {
115+
return false;
116+
}
114117
return getMap(relational).containsKey(fix(id));
115118
}
116119

@@ -329,7 +332,11 @@ private int verify(Object object, Method m) {
329332
}
330333
}
331334
if (l) {
332-
Sponge.getEventManager().registerListeners(plugin, o);
335+
final Object o2 = o;
336+
pl.setReloadListeners(() -> {
337+
PlaceholderAPIPlugin.getInstance().unregisterListeners(o2);
338+
PlaceholderAPIPlugin.getInstance().registerListeners(o2, plugin);
339+
});
333340
}
334341
pl.setId(p.id());
335342
pl.setRelational(r);

src/main/java/me/rojo8399/placeholderapi/impl/placeholder/gen/InternalExpansion.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ public Object getHandle() {
4848
return handle;
4949
}
5050

51-
@Override
52-
public void unregisterListeners() {
53-
PlaceholderAPIPlugin.getInstance().unregisterListeners(handle);
54-
}
55-
56-
@Override
57-
public void registerListeners() {
58-
PlaceholderAPIPlugin.getInstance().registerListeners(handle);
59-
}
60-
6151
@SuppressWarnings("unchecked")
6252
@Override
6353
public <T> T getConfiguration() {

0 commit comments

Comments
 (0)