-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes support for registering relational placeholders with PAPI
- Loading branch information
1 parent
e493e38
commit fef82b9
Showing
9 changed files
with
239 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ain/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.github.apickledwalrus.skriptplaceholders.placeholder; | ||
|
||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class PlaceholderRegistry { | ||
|
||
private final Plugin plugin; | ||
private final Map<PlaceholderPlugin, PlaceholderRegister> registers = new HashMap<>(); | ||
|
||
public PlaceholderRegistry(Plugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
public void registerPlaceholder(PlaceholderPlugin plugin, String placeholder, PlaceholderEvaluator evaluator) { | ||
PlaceholderRegister register = registers.computeIfAbsent(plugin, PlaceholderRegister::new); | ||
register.registerPlaceholder(placeholder, evaluator); | ||
} | ||
|
||
public void unregisterPlaceholder(PlaceholderPlugin plugin, String placeholder, PlaceholderEvaluator evaluator) { | ||
PlaceholderRegister register = registers.get(plugin); | ||
if (register != null) { | ||
register.unregisterPlaceholder(placeholder, evaluator); | ||
} | ||
} | ||
|
||
private final class PlaceholderRegister { | ||
|
||
private final PlaceholderPlugin plugin; | ||
private final Map<String, PlaceholderListener> listeners = new HashMap<>(); | ||
|
||
public PlaceholderRegister(PlaceholderPlugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
public void registerPlaceholder(String placeholder, PlaceholderEvaluator evaluator) { | ||
PlaceholderListener listener = listeners.computeIfAbsent(placeholder, | ||
key -> plugin.registerPlaceholder(PlaceholderRegistry.this.plugin, key) | ||
); | ||
listener.addEvaluator(evaluator); | ||
} | ||
|
||
public void unregisterPlaceholder(String placeholder, PlaceholderEvaluator evaluator) { | ||
PlaceholderListener listener = listeners.get(placeholder); | ||
if (listener == null) { | ||
return; | ||
} | ||
listener.removeEvaluator(evaluator); | ||
if (!listener.hasEvaluators()) { // if this was the last evaluator, unregister the listener | ||
listener.unregisterListener(); | ||
listeners.remove(placeholder); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...n/java/io/github/apickledwalrus/skriptplaceholders/skript/RelationalPlaceholderEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.apickledwalrus.skriptplaceholders.skript; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
/** | ||
* An event to be used by Skript for passing context regarding relational placeholders during execution. | ||
*/ | ||
public class RelationalPlaceholderEvent extends PlaceholderEvent { | ||
|
||
private final Player other; | ||
|
||
public RelationalPlaceholderEvent(String placeholder, Player player, Player other) { | ||
super(placeholder, player); | ||
this.other = other; | ||
} | ||
|
||
@Override | ||
public Player getPlayer() { | ||
//noinspection ConstantConditions - player is passed up as NotNull | ||
return super.getPlayer().getPlayer(); | ||
} | ||
|
||
public Player getOther() { | ||
return other; | ||
} | ||
|
||
} |
Oops, something went wrong.