Skip to content

Commit

Permalink
Better annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed May 23, 2024
1 parent 2228ad2 commit 8b0f4b0
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 93 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ repositories {
}

dependencies {
implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:2.2.600'
implementation 'org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT'
implementation ('com.github.SkriptLang:Skript:2.7.3') {
transitive = false
}
implementation ('me.clip:placeholderapi:2.11.3')
implementation ('me.clip:placeholderapi:2.11.6')
implementation('be.maximvdw:MVdWPlaceholderAPI:3.0.1-SNAPSHOT') {
transitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public void onEnable() {
try {
Skript.registerAddon(this).loadClasses("io.github.apickledwalrus.skriptplaceholders.skript.elements");
} catch (IOException e) {
e.printStackTrace();
getLogger().severe("A severe error occurred while trying to load the addon. Disabling...");
getLogger().severe(e.toString());
getServer().getPluginManager().disablePlugin(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;

/**
* A placeholder listener for placeholders created using {@link PlaceholderPlugin#MVDW_PLACEHOLDER_API}.
*/
public class MVdWPlaceholderAPIListener implements PlaceholderListener {

private final Plugin plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A placeholder listener for placeholders created using {@link PlaceholderPlugin#PLACEHOLDER_API}.
*/
public class PlaceholderAPIListener extends PlaceholderExpansion implements PlaceholderListener {

private final Plugin plugin;
Expand All @@ -19,20 +22,17 @@ public PlaceholderAPIListener(Plugin plugin, PlaceholderEvaluator evaluator, Str
}

@Override
@NonNull
public String getIdentifier() {
public @NotNull String getIdentifier() {
return prefix;
}

@Override
@NonNull
public String getAuthor() {
public @NotNull String getAuthor() {
return plugin.getDescription().getAuthors().toString();
}

@Override
@NonNull
public String getVersion() {
public @NotNull String getVersion() {
return plugin.getDescription().getVersion();
}

Expand All @@ -42,8 +42,7 @@ public boolean persist() {
}

@Override
@Nullable
public String onRequest(@Nullable OfflinePlayer player, @NonNull String identifier) {
public @Nullable String onRequest(@Nullable OfflinePlayer player, @NotNull String identifier) {
return evaluator.evaluate(this.prefix + "_" + identifier, player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public interface PlaceholderEvaluator {
* Can be null for placeholders that do not require such context.
* @return The value of the placeholder.
*/
@Nullable
String evaluate(String placeholder, @Nullable OfflinePlayer player);
@Nullable String evaluate(String placeholder, @Nullable OfflinePlayer player);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.apickledwalrus.skriptplaceholders.SkriptPlaceholders;
import org.apache.commons.lang.StringUtils;
import org.bukkit.OfflinePlayer;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

/**
* A utility enum for the placeholder plugins.
Expand All @@ -15,8 +15,7 @@ public enum PlaceholderPlugin {
private final char[] illegalCharacters = new char[]{'%', '{', '}', '_'};

@Override
@Nullable
public String isValidPrefix(String prefix) {
public @Nullable String isValidPrefix(String prefix) {
if (StringUtils.isBlank(prefix)) {
return "A prefix cannot be blank";
}
Expand All @@ -38,8 +37,7 @@ public PlaceholderListener registerPlaceholder(PlaceholderEvaluator evaluator, S
}

@Override
@Nullable
public String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player) {
public @Nullable String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player) {
if (placeholder.indexOf('%') == -1) // Try to add percentage signs manually
placeholder = "%" + placeholder + "%";
String value = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, placeholder);
Expand All @@ -50,8 +48,7 @@ public String parsePlaceholder(String placeholder, @Nullable OfflinePlayer playe
},
MVDW_PLACEHOLDER_API("MVdWPlaceholderAPI", Skript.classExists("be.maximvdw.placeholderapi.PlaceholderAPI")) {
@Override
@Nullable
public String isValidPrefix(String prefix) {
public @Nullable String isValidPrefix(String prefix) {
return StringUtils.isBlank(prefix) ? "A placeholder cannot be blank" : null;
}

Expand All @@ -63,8 +60,7 @@ public PlaceholderListener registerPlaceholder(PlaceholderEvaluator evaluator, S
}

@Override
@Nullable
public String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player) {
public @Nullable String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player) {
if (placeholder.charAt(0) == '{' && placeholder.charAt(placeholder.length() - 1) == '}') {
String value = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, placeholder);
if (value.isEmpty() || value.equalsIgnoreCase(placeholder))
Expand Down Expand Up @@ -101,8 +97,7 @@ public final boolean isInstalled() {
* @param prefix The prefix to validate.
* @return Null, or an error message detailing why the prefix is invalid.
*/
@Nullable
public abstract String isValidPrefix(String prefix);
public abstract @Nullable String isValidPrefix(String prefix);

/**
* Registers a new placeholder with this plugin.
Expand All @@ -116,7 +111,6 @@ public final boolean isInstalled() {
* @param player The player to obtain the placeholder from. For some implementations, a player is not required.
* @return The value of the placeholder for the given player (if provided).
*/
@Nullable
public abstract String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player);
public abstract @Nullable String parsePlaceholder(String placeholder, @Nullable OfflinePlayer player);

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* An event to be used by Skript for passing context during execution.
*/
public class PlaceholderEvent extends Event {

@Nullable
private final OfflinePlayer player;
private final @Nullable OfflinePlayer player;
private final String placeholder;
@Nullable
private final String prefix;
@Nullable
private final String identifier;
@Nullable
private String result;
private final @Nullable String prefix;
private final @Nullable String identifier;
private @Nullable String result;

public PlaceholderEvent(String placeholder, @Nullable OfflinePlayer player) {
// Declare the event as sync or async.
Expand All @@ -43,18 +39,15 @@ public String getPlaceholder() {
return this.placeholder;
}

@Nullable
public String getPrefix() {
public @Nullable String getPrefix() {
return prefix;
}

@Nullable
public String getIdentifier() {
public @Nullable String getIdentifier() {
return identifier;
}

@Nullable
public OfflinePlayer getPlayer() {
public @Nullable OfflinePlayer getPlayer() {
return this.player;
}

Expand All @@ -72,8 +65,7 @@ public String getResult() {
private static final HandlerList handlerList = new HandlerList();

@Override
@NonNull
public HandlerList getHandlers() {
public @NotNull HandlerList getHandlers() {
return handlerList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderPlugin;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,21 +38,20 @@ public class ExprParsePlaceholder extends SimpleExpression<String> {
);
}

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<String> placeholders;
@Nullable
private Expression<OfflinePlayer> players;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
placeholders = (Expression<String>) exprs[0];
players = (Expression<OfflinePlayer>) exprs[1];
return true;
}

@Override
protected String[] get(Event event) {
protected String @NotNull [] get(@NotNull Event event) {
List<String> values = new ArrayList<>();

for (OfflinePlayer player : players != null ? players.getArray(event) : new OfflinePlayer[]{null}) {
Expand All @@ -78,12 +78,12 @@ public boolean isSingle() {
}

@Override
public Class<? extends String> getReturnType() {
public @NotNull Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "the value of placeholder(s) " + placeholders.toString(event, debug)
+ (players != null ? " from " + players.toString(event, debug) : "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.util.Kleenean;
import io.github.apickledwalrus.skriptplaceholders.skript.PlaceholderEvent;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("Placeholder")
@Description("An expression to obtain the placeholder (or part of it) in a placeholder request event.")
Expand Down Expand Up @@ -43,21 +43,20 @@ private enum PlaceholderPart {
IDENTIFIER
}

@SuppressWarnings("NotNullFieldNotInitialized")
private PlaceholderPart part;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlaceholderEvent.class)) {
Skript.error("The placeholder can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR);
Skript.error("The placeholder can only be used in a placeholder request event");
return false;
}
this.part = PlaceholderPart.values()[parseResult.mark];
return true;
}

@Override
protected String[] get(Event event) {
protected String @NotNull [] get(@NotNull Event event) {
switch (part) {
case PLACEHOLDER:
return new String[]{((PlaceholderEvent) event).getPlaceholder()};
Expand All @@ -76,12 +75,12 @@ public boolean isSingle() {
}

@Override
public Class<? extends String> getReturnType() {
public @NotNull Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
public @NotNull String toString(@Nullable Event event, boolean debug) {
switch (part) {
case PLACEHOLDER:
return "the placeholder";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import ch.njol.util.coll.CollectionUtils;
import io.github.apickledwalrus.skriptplaceholders.skript.PlaceholderEvent;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("Placeholder Result")
@Description("The value of a placeholder in a placeholder event. Can be set, reset, or deleted.")
Expand All @@ -39,7 +40,7 @@ public class ExprPlaceholderResult extends SimpleExpression<String> {
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlaceholderEvent.class)) {
Skript.error("The placeholder result can only be used in a placeholder request event");
return false;
Expand All @@ -48,25 +49,25 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
protected String[] get(Event event) {
protected String @NotNull [] get(@NotNull Event event) {
return new String[]{((PlaceholderEvent) event).getResult()};
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
public Class<?> @NotNull [] acceptChange(ChangeMode mode) {
switch (mode) {
case SET:
case DELETE:
case RESET:
return CollectionUtils.array(Object.class);
default:
//noinspection ConstantConditions - Skript annotated this wrong
return null;
}
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
public void change(@NotNull Event event, Object @NotNull [] delta, ChangeMode mode) {
PlaceholderEvent placeholderEvent = ((PlaceholderEvent) event);
switch (mode) {
case SET:
Expand All @@ -91,12 +92,12 @@ public boolean isSingle() {
}

@Override
public Class<? extends String> getReturnType() {
public @NotNull Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "the placeholder result";
}

Expand Down
Loading

0 comments on commit 8b0f4b0

Please sign in to comment.