Add checking for valid color and sending message if its invalid
This commit is contained in:
@@ -14,6 +14,7 @@ import org.bukkit.entity.Player;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.ComponentStyle;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||
import nl.interestingcorner.db.Coordinate;
|
||||
import nl.interestingcorner.db.DatabaseManager;
|
||||
import nl.interestingcorner.db.MinecraftColor;
|
||||
@@ -36,14 +37,12 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
this.sender = sender;
|
||||
|
||||
List<String> argsWithoutFirstCommand = removeFirstArg(args);
|
||||
if (argsWithoutFirstCommand.isEmpty())
|
||||
{
|
||||
if (argsWithoutFirstCommand.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<String> parsedArgs = parseArgs(argsWithoutFirstCommand.toArray(String[]::new));
|
||||
if (parsedArgs.isEmpty())
|
||||
{
|
||||
if (parsedArgs.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -51,17 +50,16 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
sender.sendMessage("0:" + parsedArgs.get(0));
|
||||
sender.sendMessage("1:" + parsedArgs.get(1));
|
||||
sender.sendMessage("2:" + parsedArgs.get(2));
|
||||
|
||||
if (!MinecraftColor.isValidColor(parsedArgs.get(2)))
|
||||
{
|
||||
TextComponent errorMessage = new TextComponent();
|
||||
ComponentStyle messageStyle = new ComponentStyle();
|
||||
messageStyle.setColor(ChatColor.DARK_RED);
|
||||
errorMessage.setStyle(messageStyle);
|
||||
errorMessage.setText("Color " + parsedArgs.get(2) + " is not a valid color!");
|
||||
|
||||
String name = parsedArgs.get(0);
|
||||
String description = parsedArgs.get(0);
|
||||
String color = parsedArgs.get(0);
|
||||
|
||||
if (!MinecraftColor.isValidColor(color)) {
|
||||
sendInvalidColorMessage(color);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (sender instanceof Player player) {
|
||||
Location playerLocation = player.getLocation();
|
||||
if (playerLocation == null) {
|
||||
@@ -73,15 +71,14 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return DatabaseManager.INSTANCE
|
||||
.addCoordinate(parsedArgs.get(0), // name
|
||||
parsedArgs.get(1), // description
|
||||
.addCoordinate(name, // name
|
||||
description, // description
|
||||
new Coordinate.Position(playerLocation.getBlockX(), playerLocation.getBlockY(),
|
||||
playerLocation.getBlockZ()), // position
|
||||
playerWorld.getEnvironment().equals(World.Environment.NETHER), // nether
|
||||
playerWorld.getName(), // world
|
||||
MinecraftColor.fromString(parsedArgs.get(2)) // color
|
||||
MinecraftColor.fromString(color) // color
|
||||
);
|
||||
}
|
||||
|
||||
@@ -142,4 +139,21 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
return res;
|
||||
}
|
||||
|
||||
private void sendInvalidColorMessage(String color)
|
||||
{
|
||||
TextComponent finalMessage = new TextComponent();
|
||||
TextComponent colorMessage = new TextComponent("Color ");
|
||||
colorMessage.setColor(ChatColor.RED);
|
||||
finalMessage.addExtra(colorMessage);
|
||||
|
||||
TextComponent enteredColor = new TextComponent(color);
|
||||
enteredColor.setColor(ChatColor.AQUA);
|
||||
finalMessage.addExtra(enteredColor);
|
||||
|
||||
TextComponent endOfMessage = new TextComponent(" is not a valid color!");
|
||||
colorMessage.setColor(ChatColor.RED);
|
||||
finalMessage.addExtra(endOfMessage);
|
||||
sender.spigot().sendMessage(finalMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user