Implements the group set command

This commit is contained in:
Kristian Knarvik 2023-03-30 21:07:46 +02:00
parent c29fcdc166
commit 888b20bb93
7 changed files with 139 additions and 46 deletions

View File

@ -2,7 +2,7 @@ package net.knarcraft.dropper.arena;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.property.ArenaGameMode;
import net.knarcraft.dropper.util.ArenaStorageHelper;
import net.knarcraft.dropper.util.StringSanitizer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -203,7 +203,7 @@ public class DropperArena {
* @return <p>This arena's sanitized name</p>
*/
public @NotNull String getArenaNameSanitized() {
return ArenaStorageHelper.sanitizeArenaName(this.getArenaName());
return StringSanitizer.sanitizeArenaName(this.getArenaName());
}
/**

View File

@ -3,7 +3,7 @@ package net.knarcraft.dropper.arena;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.container.SerializableUUID;
import net.knarcraft.dropper.property.ArenaGameMode;
import net.knarcraft.dropper.util.ArenaStorageHelper;
import net.knarcraft.dropper.util.StringSanitizer;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -187,7 +187,7 @@ public class DropperArenaGroup implements ConfigurationSerializable {
* @return <p>The sanitized group name</p>
*/
public @NotNull String getGroupNameSanitized() {
return ArenaStorageHelper.sanitizeArenaName(this.getGroupName());
return StringSanitizer.sanitizeArenaName(this.getGroupName());
}
/**

View File

@ -2,11 +2,13 @@ package net.knarcraft.dropper.arena;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.util.ArenaStorageHelper;
import net.knarcraft.dropper.util.StringSanitizer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -35,11 +37,27 @@ public class DropperArenaHandler {
* Sets the group for the given arena
*
* @param arenaId <p>The id of the arena to change</p>
* @param arenaGroup <p>The group to add the arena to</p>
* @param arenaGroup <p>The group to add the arena to, or null to remove the current group</p>
*/
public void setGroup(@NotNull UUID arenaId, @NotNull DropperArenaGroup arenaGroup) {
this.arenaGroups.put(arenaId, arenaGroup);
arenaGroup.addArena(arenaId);
public void setGroup(@NotNull UUID arenaId, @Nullable DropperArenaGroup arenaGroup) {
if (arenaGroup == null) {
// No need to remove something non-existing
if (!this.arenaGroups.containsKey(arenaId)) {
return;
}
// Remove the existing group
DropperArenaGroup oldGroup = this.arenaGroups.remove(arenaId);
oldGroup.removeArena(arenaId);
} else {
// Make sure to remove the arena from the old group's internal tracking
if (this.arenaGroups.containsKey(arenaId)) {
this.arenaGroups.remove(arenaId).removeArena(arenaId);
}
this.arenaGroups.put(arenaId, arenaGroup);
arenaGroup.addArena(arenaId);
}
saveGroups();
}
@ -50,7 +68,7 @@ public class DropperArenaHandler {
* @return <p>The group, or null if not found</p>
*/
public @Nullable DropperArenaGroup getGroup(String groupName) {
String sanitized = ArenaStorageHelper.sanitizeArenaName(groupName);
String sanitized = StringSanitizer.sanitizeArenaName(groupName);
for (DropperArenaGroup arenaGroup : this.arenaGroups.values()) {
if (arenaGroup.getGroupNameSanitized().equals(sanitized)) {
return arenaGroup;
@ -59,19 +77,6 @@ public class DropperArenaHandler {
return null;
}
/**
* Removes the given arena from its group
*
* @param arenaId <p>The id of the arena to ungroup</p>
*/
public void removeFromGroup(@NotNull UUID arenaId) {
if (this.arenaGroups.containsKey(arenaId)) {
this.arenaGroups.get(arenaId).removeArena(arenaId);
this.arenaGroups.remove(arenaId);
saveGroups();
}
}
/**
* Replaces an arena's lookup name
*
@ -113,7 +118,7 @@ public class DropperArenaHandler {
* @return <p>The arena with the given name, or null if not found</p>
*/
public @Nullable DropperArena getArena(@NotNull String arenaName) {
return this.arenas.get(this.arenaNameLookup.get(ArenaStorageHelper.sanitizeArenaName(arenaName)));
return this.arenas.get(this.arenaNameLookup.get(StringSanitizer.sanitizeArenaName(arenaName)));
}
/**
@ -157,7 +162,7 @@ public class DropperArenaHandler {
*/
public void saveGroups() {
try {
ArenaStorageHelper.saveDropperArenaGroups((Set<DropperArenaGroup>) this.arenaGroups.values());
ArenaStorageHelper.saveDropperArenaGroups(new HashSet<>(this.arenaGroups.values()));
} catch (IOException e) {
Dropper.getInstance().getLogger().log(Level.SEVERE, "Unable to save current arena groups! " +
"Data loss can occur!");

View File

@ -2,6 +2,7 @@ package net.knarcraft.dropper.command;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.arena.DropperArena;
import net.knarcraft.dropper.util.StringSanitizer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -26,15 +27,20 @@ public class CreateArenaCommand implements CommandExecutor {
return false;
}
DropperArena existingArena = Dropper.getInstance().getArenaHandler().getArena(arguments[0]);
// Remove known characters that are likely to cause trouble if used in an arena name
String arenaName = StringSanitizer.removeUnwantedCharacters(arguments[0]);
// An arena name is required
if (arenaName.isBlank()) {
return false;
}
DropperArena existingArena = Dropper.getInstance().getArenaHandler().getArena(arenaName);
if (existingArena != null) {
commandSender.sendMessage("There already exists a dropper arena with that name!");
return false;
}
// Remove known characters that are likely to cause trouble if used in an arena name
String arenaName = arguments[0].replaceAll("[§ :=&]", "");
DropperArena arena = new DropperArena(arenaName, player.getLocation());
Dropper.getInstance().getArenaHandler().addArena(arena);
commandSender.sendMessage("The arena was successfully created!");

View File

@ -1,21 +1,76 @@
package net.knarcraft.dropper.command;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.arena.DropperArena;
import net.knarcraft.dropper.arena.DropperArenaGroup;
import net.knarcraft.dropper.arena.DropperArenaHandler;
import net.knarcraft.dropper.util.StringSanitizer;
import net.knarcraft.dropper.util.TabCompleteHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class GroupSetCommand implements CommandExecutor {
import java.util.ArrayList;
import java.util.List;
/**
* The command for setting the group of an arena
*/
public class GroupSetCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
// TODO: Check if the given group is valid
// TODO: Try and get the group from ArenaHandler.getGroup
// TODO: Create a new group if not found
// TODO: Set the group of the arena
// TODO: Announce success
return false;
if (arguments.length < 2) {
return false;
}
DropperArenaHandler arenaHandler = Dropper.getInstance().getArenaHandler();
DropperArena specifiedArena = arenaHandler.getArena(arguments[0]);
if (specifiedArena == null) {
commandSender.sendMessage("Unable to find the specified dropper arena.");
return false;
}
String groupName = StringSanitizer.removeUnwantedCharacters(arguments[1]);
if (groupName.isBlank()) {
return false;
}
DropperArenaGroup arenaGroup;
if (groupName.equalsIgnoreCase("null") || groupName.equalsIgnoreCase("none")) {
arenaGroup = null;
} else {
arenaGroup = arenaHandler.getGroup(groupName);
if (arenaGroup == null) {
arenaGroup = new DropperArenaGroup(groupName);
}
}
arenaHandler.setGroup(specifiedArena.getArenaId(), arenaGroup);
commandSender.sendMessage("The arena's group has been updated");
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (arguments.length == 1) {
return TabCompleteHelper.getArenas();
} else if (arguments.length == 2) {
List<String> possibleValues = new ArrayList<>();
possibleValues.add("none");
possibleValues.add("GroupName");
return possibleValues;
} else {
return new ArrayList<>();
}
}
}

View File

@ -177,16 +177,6 @@ public final class ArenaStorageHelper {
winBlockType.material(), arenaData);
}
/**
* Sanitizes an arena name for usage as a YAML key
*
* @param arenaName <p>The arena name to sanitize</p>
* @return <p>The sanitized arena name</p>
*/
public static @NotNull String sanitizeArenaName(@NotNull String arenaName) {
return arenaName.toLowerCase().trim().replaceAll(" ", "_");
}
/**
* Stores the given arena data to a file
*

View File

@ -0,0 +1,37 @@
package net.knarcraft.dropper.util;
import org.jetbrains.annotations.NotNull;
/**
* A helper-class for sanitizing strings
*/
public final class StringSanitizer {
private StringSanitizer() {
}
/**
* Removes unwanted characters from a string
*
* <p>This basically removes character that have a special meaning in YML, or ones that cannot be used in the
* chat.</p>
*
* @param input <p>The string to remove from</p>
* @return <p>The string with the unwanted characters removed</p>
*/
public static @NotNull String removeUnwantedCharacters(@NotNull String input) {
return input.replaceAll("[§ :=&]", "");
}
/**
* Sanitizes an arena name for usage as a YAML key
*
* @param arenaName <p>The arena name to sanitize</p>
* @return <p>The sanitized arena name</p>
*/
public static @NotNull String sanitizeArenaName(@NotNull String arenaName) {
return arenaName.toLowerCase().trim().replaceAll(" ", "_");
}
}