Adds tab completions for arena properties

This commit is contained in:
Kristian Knarvik 2023-04-26 14:41:54 +02:00
parent 4a3329459e
commit 8d1b841619
6 changed files with 232 additions and 20 deletions

View File

@ -0,0 +1,43 @@
package net.knarcraft.minigames.arena;
/**
* The type of one editable property
*/
public enum EditablePropertyType {
/**
* The property is a location
*/
LOCATION,
/**
* The property is an arena name
*/
ARENA_NAME,
/**
* The property is a horizontal velocity
*/
HORIZONTAL_VELOCITY,
/**
* The property is a vertical velocity (fly speed)
*/
VERTICAL_VELOCITY,
/**
* The property is a material that specifies a block
*/
BLOCK_TYPE,
/**
* The property clears a checkpoint
*/
CHECKPOINT_CLEAR,
/**
* The property is a comma-separated list of materials
*/
MATERIAL_LIST
}

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.arena.dropper; package net.knarcraft.minigames.arena.dropper;
import net.knarcraft.minigames.arena.EditablePropertyType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -13,45 +14,62 @@ public enum DropperArenaEditableProperty {
/** /**
* The name of the arena * The name of the arena
*/ */
NAME("name", DropperArena::getArenaName), NAME("name", DropperArena::getArenaName, EditablePropertyType.ARENA_NAME),
/** /**
* The arena's spawn location * The arena's spawn location
*/ */
SPAWN_LOCATION("spawnLocation", (arena) -> String.valueOf(arena.getSpawnLocation())), SPAWN_LOCATION("spawnLocation", (arena) -> String.valueOf(arena.getSpawnLocation()),
EditablePropertyType.LOCATION),
/** /**
* The arena's exit location * The arena's exit location
*/ */
EXIT_LOCATION("exitLocation", (arena) -> String.valueOf(arena.getExitLocation())), EXIT_LOCATION("exitLocation", (arena) -> String.valueOf(arena.getExitLocation()),
EditablePropertyType.LOCATION),
/** /**
* The arena's vertical velocity * The arena's vertical velocity
*/ */
VERTICAL_VELOCITY("verticalVelocity", (arena) -> String.valueOf(arena.getPlayerVerticalVelocity())), VERTICAL_VELOCITY("verticalVelocity", (arena) -> String.valueOf(arena.getPlayerVerticalVelocity()),
EditablePropertyType.VERTICAL_VELOCITY),
/** /**
* The arena's horizontal velocity * The arena's horizontal velocity
*/ */
HORIZONTAL_VELOCITY("horizontalVelocity", (arena) -> String.valueOf(arena.getPlayerHorizontalVelocity())), HORIZONTAL_VELOCITY("horizontalVelocity", (arena) -> String.valueOf(arena.getPlayerHorizontalVelocity()),
EditablePropertyType.HORIZONTAL_VELOCITY),
/** /**
* The arena's win block type * The arena's win block type
*/ */
WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString()), WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString(),
EditablePropertyType.BLOCK_TYPE),
; ;
private final @NotNull String argumentString; private final @NotNull String argumentString;
private final Function<DropperArena, String> currentValueProvider; private final Function<DropperArena, String> currentValueProvider;
private final EditablePropertyType propertyType;
/** /**
* Instantiates a new arena editable property * Instantiates a new arena editable property
* *
* @param argumentString <p>The argument string used to specify this property</p> * @param argumentString <p>The argument string used to specify this property</p>
*/ */
DropperArenaEditableProperty(@NotNull String argumentString, Function<DropperArena, String> currentValueProvider) { DropperArenaEditableProperty(@NotNull String argumentString, Function<DropperArena, String> currentValueProvider,
EditablePropertyType propertyType) {
this.argumentString = argumentString; this.argumentString = argumentString;
this.currentValueProvider = currentValueProvider; this.currentValueProvider = currentValueProvider;
this.propertyType = propertyType;
}
/**
* Gets the type of property this editable property represents
*
* @return <p>The type of this property</p>
*/
public EditablePropertyType getPropertyType() {
return this.propertyType;
} }
/** /**

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.arena.parkour; package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.minigames.arena.EditablePropertyType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -13,22 +14,25 @@ public enum ParkourArenaEditableProperty {
/** /**
* The name of the arena * The name of the arena
*/ */
NAME("name", ParkourArena::getArenaName), NAME("name", ParkourArena::getArenaName, EditablePropertyType.ARENA_NAME),
/** /**
* The arena's spawn location * The arena's spawn location
*/ */
SPAWN_LOCATION("spawnLocation", (arena) -> String.valueOf(arena.getSpawnLocation())), SPAWN_LOCATION("spawnLocation", (arena) -> String.valueOf(arena.getSpawnLocation()),
EditablePropertyType.LOCATION),
/** /**
* The arena's exit location * The arena's exit location
*/ */
EXIT_LOCATION("exitLocation", (arena) -> String.valueOf(arena.getExitLocation())), EXIT_LOCATION("exitLocation", (arena) -> String.valueOf(arena.getExitLocation()),
EditablePropertyType.LOCATION),
/** /**
* The arena's win block type * The arena's win block type
*/ */
WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString()), WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString(),
EditablePropertyType.BLOCK_TYPE),
/** /**
* The arena's win location (overrides the win block type) * The arena's win location (overrides the win block type)
@ -39,35 +43,50 @@ public enum ParkourArenaEditableProperty {
} else { } else {
return "null"; return "null";
} }
}), }, EditablePropertyType.LOCATION),
/** /**
* The arena's check points. Specifically used for adding. * The arena's check points. Specifically used for adding.
*/ */
CHECKPOINT_ADD("checkpointAdd", (arena) -> String.valueOf(arena.getCheckpoints())), CHECKPOINT_ADD("checkpointAdd", (arena) -> String.valueOf(arena.getCheckpoints()),
EditablePropertyType.LOCATION),
/** /**
* The arena's check points. Specifically used for clearing. * The arena's check points. Specifically used for clearing.
*/ */
CHECKPOINT_CLEAR("checkpointClear", (arena) -> String.valueOf(arena.getCheckpoints())), CHECKPOINT_CLEAR("checkpointClear", (arena) -> String.valueOf(arena.getCheckpoints()),
EditablePropertyType.CHECKPOINT_CLEAR),
/** /**
* The blocks constituting the arena's lethal blocks * The blocks constituting the arena's lethal blocks
*/ */
KILL_PLANE_BLOCKS("killPlaneBlocks", (arena) -> String.valueOf(arena.getKillPlaneBlockNames())), KILL_PLANE_BLOCKS("killPlaneBlocks", (arena) -> String.valueOf(arena.getKillPlaneBlockNames()),
EditablePropertyType.MATERIAL_LIST),
; ;
private final @NotNull String argumentString; private final @NotNull String argumentString;
private final Function<ParkourArena, String> currentValueProvider; private final Function<ParkourArena, String> currentValueProvider;
private final EditablePropertyType propertyType;
/** /**
* Instantiates a new arena editable property * Instantiates a new arena editable property
* *
* @param argumentString <p>The argument string used to specify this property</p> * @param argumentString <p>The argument string used to specify this property</p>
*/ */
ParkourArenaEditableProperty(@NotNull String argumentString, Function<ParkourArena, String> currentValueProvider) { ParkourArenaEditableProperty(@NotNull String argumentString, Function<ParkourArena, String> currentValueProvider,
EditablePropertyType propertyType) {
this.argumentString = argumentString; this.argumentString = argumentString;
this.currentValueProvider = currentValueProvider; this.currentValueProvider = currentValueProvider;
this.propertyType = propertyType;
}
/**
* Gets the type of property this editable property represents
*
* @return <p>The type of this property</p>
*/
public EditablePropertyType getPropertyType() {
return this.propertyType;
} }
/** /**

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -25,8 +26,12 @@ public class EditDropperArenaTabCompleter implements TabCompleter {
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
return filterMatchingContains(TabCompleteHelper.getDropperArenaProperties(), arguments[1]); return filterMatchingContains(TabCompleteHelper.getDropperArenaProperties(), arguments[1]);
} else if (arguments.length == 3) { } else if (arguments.length == 3) {
//TODO: Tab-complete possible values for the given property DropperArenaEditableProperty property = DropperArenaEditableProperty.getFromArgumentString(arguments[1]);
return null; if (property == null) {
return new ArrayList<>();
}
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(property.getPropertyType()),
arguments[2]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -25,8 +26,12 @@ public class EditParkourArenaTabCompleter implements TabCompleter {
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
return filterMatchingContains(TabCompleteHelper.getParkourArenaProperties(), arguments[1]); return filterMatchingContains(TabCompleteHelper.getParkourArenaProperties(), arguments[1]);
} else if (arguments.length == 3) { } else if (arguments.length == 3) {
//TODO: Tab-complete possible values for the given property ParkourArenaEditableProperty property = ParkourArenaEditableProperty.getFromArgumentString(arguments[1]);
return null; if (property == null) {
return new ArrayList<>();
}
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(property.getPropertyType()),
arguments[2]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -3,18 +3,24 @@ package net.knarcraft.minigames.util;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaHandler; import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.arena.EditablePropertyType;
import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty; import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty;
import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty; import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* A helper-class for common tab-completions * A helper-class for common tab-completions
*/ */
public final class TabCompleteHelper { public final class TabCompleteHelper {
private static Map<EditablePropertyType, List<String>> tabCompleteSuggestions;
private TabCompleteHelper() { private TabCompleteHelper() {
} }
@ -93,4 +99,120 @@ public final class TabCompleteHelper {
return configValues; return configValues;
} }
/**
* Gets tab-complete suggestions for the given property type
*
* @param propertyType <p>The property type to get suggestions for</p>
* @return <p>The suggestions produced</p>
*/
public static List<String> getTabCompleteSuggestions(EditablePropertyType propertyType) {
if (tabCompleteSuggestions == null) {
tabCompleteSuggestions = new HashMap<>();
tabCompleteSuggestions.put(EditablePropertyType.LOCATION, getLocationSuggestions());
tabCompleteSuggestions.put(EditablePropertyType.ARENA_NAME, getNameSuggestions());
tabCompleteSuggestions.put(EditablePropertyType.HORIZONTAL_VELOCITY, getHorizontalVelocitySuggestions());
tabCompleteSuggestions.put(EditablePropertyType.VERTICAL_VELOCITY, getVerticalVelocitySuggestions());
tabCompleteSuggestions.put(EditablePropertyType.BLOCK_TYPE, getBlockTypeSuggestions());
tabCompleteSuggestions.put(EditablePropertyType.CHECKPOINT_CLEAR, getCheckpointClearSuggestions());
tabCompleteSuggestions.put(EditablePropertyType.MATERIAL_LIST, getMaterialListSuggestions());
}
return tabCompleteSuggestions.get(propertyType);
}
/**
* Gets suggestions for a list of materials
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getMaterialListSuggestions() {
List<String> suggestions = new ArrayList<>();
suggestions.add("LAVA,MAGMA_BLOCK");
suggestions.add("WATER,MAGMA_BLOCK,LAVA,+BUTTONS,+CORALS");
return suggestions;
}
/**
* Gets suggestions for checkpointClear
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getCheckpointClearSuggestions() {
List<String> suggestions = new ArrayList<>();
suggestions.add("true");
return suggestions;
}
/**
* Gets suggestions for a block material
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getBlockTypeSuggestions() {
List<String> materials = new ArrayList<>();
for (Material material : Material.values()) {
if (material.isBlock()) {
materials.add(material.name());
}
}
return materials;
}
/**
* Gets suggestions for a vertical velocity
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getVerticalVelocitySuggestions() {
List<String> velocities = new ArrayList<>();
velocities.add("0.01");
velocities.add("0.5");
velocities.add("1");
velocities.add("3");
velocities.add("10");
velocities.add("30");
velocities.add("75");
return velocities;
}
/**
* Gets suggestions for a horizontal velocity
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getHorizontalVelocitySuggestions() {
List<String> velocities = new ArrayList<>();
velocities.add("0.01");
velocities.add("0.1");
velocities.add("0.5");
velocities.add("1");
return velocities;
}
/**
* Gets suggestions for an arena name
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getNameSuggestions() {
List<String> locations = new ArrayList<>();
locations.add("DropperArena1");
locations.add("DropperArena2");
locations.add("ParkourArena1");
locations.add("ParkourArena2");
return locations;
}
/**
* Gets suggestions for a location
*
* @return <p>A list of suggestions</p>
*/
private static List<String> getLocationSuggestions() {
List<String> locations = new ArrayList<>();
locations.add("here");
locations.add("x,y,z");
return locations;
}
} }