diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java index a6f0859..569e54a 100644 --- a/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java @@ -13,10 +13,7 @@ public class SkriptPlaceholders extends JavaPlugin { @Override public void onEnable() { instance = this; - for (PlaceholderPlugin plugin : PlaceholderPlugin.values()) { - if (plugin.isInstalled()) { - break; - } + if (PlaceholderPlugin.getInstalledPlugins().isEmpty()) { getLogger().severe("No placeholder plugins were found. Do you have any installed? Disabling..."); getServer().getPluginManager().disablePlugin(this); return; diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderPlugin.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderPlugin.java index 458d7ab..82c97b2 100644 --- a/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderPlugin.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderPlugin.java @@ -6,6 +6,10 @@ import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Collectors; + /** * A utility enum for the placeholder plugins. */ @@ -71,6 +75,17 @@ public PlaceholderListener registerPlaceholder(PlaceholderEvaluator evaluator, S } }; + private static final Collection INSTALLED_PLUGINS = Arrays.stream(values()) + .filter(PlaceholderPlugin::isInstalled) + .collect(Collectors.toSet()); + + /** + * @return A list of all installed placeholder plugins. + */ + public static Collection getInstalledPlugins() { + return INSTALLED_PLUGINS; + } + private final String displayName; private final boolean installed; diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprParsePlaceholder.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprParsePlaceholder.java index 6758313..f44b48e 100644 --- a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprParsePlaceholder.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprParsePlaceholder.java @@ -53,13 +53,11 @@ public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean @Override protected String @NotNull [] get(@NotNull Event event) { List values = new ArrayList<>(); - - for (OfflinePlayer player : players != null ? players.getArray(event) : new OfflinePlayer[]{null}) { - for (String placeholder : placeholders.getArray(event)) { - for (PlaceholderPlugin plugin : PlaceholderPlugin.values()) { - if (!plugin.isInstalled()) { - continue; - } + String[] placeholders = this.placeholders.getArray(event); + OfflinePlayer[] players = this.players != null ? this.players.getArray(event) : new OfflinePlayer[]{null}; + for (OfflinePlayer player : players) { + for (String placeholder : placeholders) { + for (PlaceholderPlugin plugin : PlaceholderPlugin.getInstalledPlugins()) { String value = plugin.parsePlaceholder(placeholder, player); if (value != null) { values.add(value); @@ -68,13 +66,12 @@ public boolean init(Expression[] exprs, int matchedPattern, @NotNull Kleenean } } } - return values.toArray(new String[0]); } @Override public boolean isSingle() { - return players != null ? (placeholders.isSingle() && players.isSingle()) : placeholders.isSingle(); + return placeholders.isSingle() && (players == null || players.isSingle()); } @Override diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprPlaceholder.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprPlaceholder.java index 9886164..932811e 100644 --- a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprPlaceholder.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/ExprPlaceholder.java @@ -57,13 +57,18 @@ public boolean init(Expression @NotNull [] exprs, int matchedPattern, @NotNul @Override protected String @NotNull [] get(@NotNull Event event) { + if (!(event instanceof PlaceholderEvent)) + return new String[0]; + PlaceholderEvent placeholderEvent = (PlaceholderEvent) event; switch (part) { case PLACEHOLDER: - return new String[]{((PlaceholderEvent) event).getPlaceholder()}; + return new String[]{placeholderEvent.getPlaceholder()}; case PREFIX: - return new String[]{((PlaceholderEvent) event).getPrefix()}; + String prefix = placeholderEvent.getPrefix(); + return prefix != null ? new String[]{prefix} : new String[0]; case IDENTIFIER: - return new String[]{((PlaceholderEvent) event).getIdentifier()}; + String identifier = placeholderEvent.getIdentifier(); + return identifier != null ? new String[]{identifier} : new String[0]; default: throw new IllegalArgumentException("Unable to handle PlaceholderPart: " + part); }