Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed May 23, 2024
1 parent ccec06a commit d1e76de
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -71,6 +75,17 @@ public PlaceholderListener registerPlaceholder(PlaceholderEvaluator evaluator, S
}
};

private static final Collection<PlaceholderPlugin> INSTALLED_PLUGINS = Arrays.stream(values())
.filter(PlaceholderPlugin::isInstalled)
.collect(Collectors.toSet());

/**
* @return A list of all installed placeholder plugins.
*/
public static Collection<PlaceholderPlugin> getInstalledPlugins() {
return INSTALLED_PLUGINS;
}

private final String displayName;
private final boolean installed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean
@Override
protected String @NotNull [] get(@NotNull Event event) {
List<String> 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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d1e76de

Please sign in to comment.