From a1f76bcfc2b58064061f85be669ae7651763f043 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 22 Apr 2024 12:41:55 +0200 Subject: [PATCH] Fixes #36 --- .../minigames/arena/dropper/DropperArena.java | 4 +-- .../minigames/arena/parkour/ParkourArena.java | 19 +++++++---- .../parkour/EditParkourArenaCommand.java | 6 +++- .../minigames/util/InputValidationHelper.java | 18 ++++++++++- .../minigames/util/TabCompleteHelper.java | 32 ++++++++++++++----- 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java index 88e5d68..2e659b8 100644 --- a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java +++ b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java @@ -263,7 +263,7 @@ public class DropperArena implements Arena { * @param newLocation

The new spawn location

* @return

True if successfully updated

*/ - public boolean setSpawnLocation(@NotNull Location newLocation) { + public boolean setSpawnLocation(@Nullable Location newLocation) { if (isInvalid(newLocation)) { return false; } else { @@ -279,7 +279,7 @@ public class DropperArena implements Arena { * @param newLocation

The new exit location

* @return

True if successfully updated

*/ - public boolean setExitLocation(@NotNull Location newLocation) { + public boolean setExitLocation(@Nullable Location newLocation) { if (isInvalid(newLocation)) { return false; } else { diff --git a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java index 3b4bbaa..b97d712 100644 --- a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java +++ b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java @@ -356,7 +356,7 @@ public class ParkourArena implements Arena { * @param newLocation

The new spawn location

* @return

True if successfully updated

*/ - public boolean setSpawnLocation(@NotNull Location newLocation) { + public boolean setSpawnLocation(@Nullable Location newLocation) { if (isInvalid(newLocation)) { return false; } else { @@ -372,7 +372,7 @@ public class ParkourArena implements Arena { * @param newLocation

The new exit location

* @return

True if successfully updated

*/ - public boolean setExitLocation(@NotNull Location newLocation) { + public boolean setExitLocation(@Nullable Location newLocation) { if (isInvalid(newLocation)) { return false; } else { @@ -425,13 +425,18 @@ public class ParkourArena implements Arena { * @param newLocation

The location players have to reach

* @return

True if successfully changed

*/ - public boolean setWinLocation(@NotNull Location newLocation) { - if (isInvalid(newLocation)) { - return false; - } else { + public boolean setWinLocation(@Nullable Location newLocation) { + if (newLocation == null) { + // Un-set the win location, and fall back to the win block type + this.winLocation = null; + this.saveArena(); + return true; + } else if (!isInvalid(newLocation)) { this.winLocation = newLocation.clone(); this.saveArena(); return true; + } else { + return false; } } @@ -485,7 +490,7 @@ public class ParkourArena implements Arena { * @param checkpoint

The checkpoint to add

* @return

True if successfully added

*/ - public boolean addCheckpoint(@NotNull Location checkpoint) { + public boolean addCheckpoint(@Nullable Location checkpoint) { if (isInvalid(checkpoint)) { return false; } diff --git a/src/main/java/net/knarcraft/minigames/command/parkour/EditParkourArenaCommand.java b/src/main/java/net/knarcraft/minigames/command/parkour/EditParkourArenaCommand.java index 7762c06..95524f0 100644 --- a/src/main/java/net/knarcraft/minigames/command/parkour/EditParkourArenaCommand.java +++ b/src/main/java/net/knarcraft/minigames/command/parkour/EditParkourArenaCommand.java @@ -5,6 +5,7 @@ import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty; import net.knarcraft.minigames.config.MiniGameMessage; +import net.knarcraft.minigames.util.InputValidationHelper; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.command.Command; @@ -12,6 +13,7 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashSet; import java.util.List; @@ -109,7 +111,7 @@ public class EditParkourArenaCommand implements CommandExecutor { * @param locationString

The location string to parse

* @return

The parsed location, or the player's location if not parse-able

*/ - private @NotNull Location parseLocation(Player player, String locationString) { + private @Nullable Location parseLocation(Player player, String locationString) { if ((locationString.trim() + ",").matches("([0-9]+.?[0-9]*,){3}")) { String[] parts = locationString.split(","); Location newLocation = player.getLocation().clone(); @@ -117,6 +119,8 @@ public class EditParkourArenaCommand implements CommandExecutor { newLocation.setY(Double.parseDouble(parts[1].trim())); newLocation.setZ(Double.parseDouble(parts[2].trim())); return newLocation; + } else if (InputValidationHelper.isEmptyValue(locationString)) { + return null; } else { return player.getLocation().clone(); } diff --git a/src/main/java/net/knarcraft/minigames/util/InputValidationHelper.java b/src/main/java/net/knarcraft/minigames/util/InputValidationHelper.java index 9e68afd..ee841ca 100644 --- a/src/main/java/net/knarcraft/minigames/util/InputValidationHelper.java +++ b/src/main/java/net/knarcraft/minigames/util/InputValidationHelper.java @@ -2,6 +2,8 @@ package net.knarcraft.minigames.util; import org.bukkit.Location; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * A helper class for validating whether given input is valid @@ -18,9 +20,23 @@ public final class InputValidationHelper { * @param location

The location to validate

* @return

False if the location is valid

*/ - public static boolean isInvalid(Location location) { + public static boolean isInvalid(@Nullable Location location) { + if (location == null) { + return true; + } World world = location.getWorld(); return world == null || !world.getWorldBorder().isInside(location); } + /** + * Checks whether the given value can be considered "empty" + * + * @param value

The value to check

+ * @return

True if the value can be considered as empty

+ */ + public static boolean isEmptyValue(@NotNull String value) { + return value.equalsIgnoreCase("null") || value.equalsIgnoreCase("0") || + value.equalsIgnoreCase("clear") || value.equalsIgnoreCase("none"); + } + } diff --git a/src/main/java/net/knarcraft/minigames/util/TabCompleteHelper.java b/src/main/java/net/knarcraft/minigames/util/TabCompleteHelper.java index 213b515..9d46c0b 100644 --- a/src/main/java/net/knarcraft/minigames/util/TabCompleteHelper.java +++ b/src/main/java/net/knarcraft/minigames/util/TabCompleteHelper.java @@ -36,7 +36,8 @@ public final class TabCompleteHelper { * * @return

All arena names

*/ - public static @NotNull List getDropperArenas() { + @NotNull + public static List getDropperArenas() { return getArenas(MiniGames.getInstance().getDropperArenaHandler()); } @@ -45,7 +46,8 @@ public final class TabCompleteHelper { * * @return

All arena names

*/ - public static @NotNull List getParkourArenas() { + @NotNull + public static List getParkourArenas() { return getArenas(MiniGames.getInstance().getParkourArenaHandler()); } @@ -54,7 +56,8 @@ public final class TabCompleteHelper { * * @return

All arena names

*/ - private static @NotNull List getArenas(ArenaHandler arenaHandler) { + @NotNull + private static List getArenas(@NotNull ArenaHandler arenaHandler) { List arenaNames = new ArrayList<>(); for (Arena arena : arenaHandler.getArenas().values()) { arenaNames.add(arena.getArenaName()); @@ -67,7 +70,8 @@ public final class TabCompleteHelper { * * @return

All arena properties

*/ - public static @NotNull List getDropperArenaProperties() { + @NotNull + public static List getDropperArenaProperties() { List arenaProperties = new ArrayList<>(); for (DropperArenaEditableProperty property : DropperArenaEditableProperty.values()) { arenaProperties.add(property.getArgumentString()); @@ -80,7 +84,8 @@ public final class TabCompleteHelper { * * @return

All arena properties

*/ - public static @NotNull List getParkourArenaProperties() { + @NotNull + public static List getParkourArenaProperties() { List arenaProperties = new ArrayList<>(); for (ParkourArenaEditableProperty property : ParkourArenaEditableProperty.values()) { arenaProperties.add(property.getArgumentString()); @@ -94,7 +99,8 @@ public final class TabCompleteHelper { * @param propertyType

The property type to get suggestions for

* @return

The suggestions produced

*/ - public static List getTabCompleteSuggestions(EditablePropertyType propertyType) { + @NotNull + public static List getTabCompleteSuggestions(@NotNull EditablePropertyType propertyType) { if (tabCompleteSuggestions == null) { tabCompleteSuggestions = new HashMap<>(); tabCompleteSuggestions.put(EditablePropertyType.LOCATION, getLocationSuggestions()); @@ -115,6 +121,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getDoubleSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("0"); @@ -133,6 +140,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getMaterialListSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("LAVA,MAGMA_BLOCK"); @@ -146,6 +154,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getCheckpointClearSuggestions() { List suggestions = new ArrayList<>(); suggestions.add("true"); @@ -157,6 +166,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getBlockTypeSuggestions() { List materials = new ArrayList<>(); for (Material material : Material.values()) { @@ -172,6 +182,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getVerticalVelocitySuggestions() { List velocities = new ArrayList<>(); velocities.add("0.01"); @@ -189,6 +200,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getHorizontalVelocitySuggestions() { List velocities = new ArrayList<>(); velocities.add("0.01"); @@ -203,6 +215,7 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getNameSuggestions() { List locations = new ArrayList<>(); locations.add("DropperArena1"); @@ -217,10 +230,12 @@ public final class TabCompleteHelper { * * @return

A list of suggestions

*/ + @NotNull private static List getLocationSuggestions() { List locations = new ArrayList<>(); locations.add("here"); locations.add("x,y,z"); + locations.add("null"); return locations; } @@ -230,7 +245,8 @@ public final class TabCompleteHelper { * @param typedNode

The full permission node typed by the player

* @return

All known valid auto-complete options

*/ - public static List tabCompletePermission(String typedNode) { + @NotNull + public static List tabCompletePermission(@NotNull String typedNode) { if (plugins == null) { loadAvailablePermissions(); } @@ -268,7 +284,7 @@ public final class TabCompleteHelper { * * @param permissionName

The permission to load

*/ - private static void loadPermission(String permissionName) { + private static void loadPermission(@NotNull String permissionName) { String[] permissionParts = permissionName.split("\\."); if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) { plugins.add(permissionParts[0]);