Add parsing of add command
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package nl.interestingcorner.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -7,6 +11,9 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
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 nl.interestingcorner.db.Coordinate;
|
||||
import nl.interestingcorner.db.DatabaseManager;
|
||||
import nl.interestingcorner.db.MinecraftColor;
|
||||
@@ -18,12 +25,43 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
*/
|
||||
private final int MIN_ARGS = 4;
|
||||
|
||||
private CommandSender sender;
|
||||
|
||||
@Override
|
||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||
if (args.length < MIN_ARGS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.sender = sender;
|
||||
|
||||
List<String> argsWithoutFirstCommand = removeFirstArg(args);
|
||||
if (argsWithoutFirstCommand.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
List<String> parsedArgs = parseArgs(argsWithoutFirstCommand.toArray(String[]::new));
|
||||
if (parsedArgs.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
sender.sendMessage("Args after parsing:");
|
||||
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!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sender instanceof Player player) {
|
||||
Location playerLocation = player.getLocation();
|
||||
if (playerLocation == null) {
|
||||
@@ -35,18 +73,73 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return DatabaseManager.INSTANCE
|
||||
.addCoordinate(args[1], // name
|
||||
args[2], // description
|
||||
.addCoordinate(parsedArgs.get(0), // name
|
||||
parsedArgs.get(1), // description
|
||||
new Coordinate.Position(playerLocation.getBlockX(), playerLocation.getBlockY(),
|
||||
playerLocation.getBlockZ()), // position
|
||||
playerWorld.getEnvironment().equals(World.Environment.NETHER), // nether
|
||||
playerWorld.getName(), // world
|
||||
MinecraftColor.fromString(args[3]) // color
|
||||
MinecraftColor.fromString(parsedArgs.get(2)) // color
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private List<String> removeFirstArg(String[] args) {
|
||||
List<String> onlyArgs = new ArrayList<>();
|
||||
|
||||
sender.sendMessage("removeFirstArg: size of args is " + String.valueOf(args.length));
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
onlyArgs.add(args[i]);
|
||||
}
|
||||
|
||||
sender.sendMessage("removeFirstArg: size of onlyArgs is " + String.valueOf(onlyArgs.size()));
|
||||
return onlyArgs;
|
||||
}
|
||||
|
||||
private List<String> parseArgs(String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
StringBuilder currentArg = new StringBuilder();
|
||||
boolean inQuotes = false;
|
||||
|
||||
for (String arg : args) {
|
||||
if (inQuotes) {
|
||||
if (arg.endsWith("\"")) {
|
||||
// end of quoted entry
|
||||
// add final arg to result
|
||||
String replaced = arg.replace("\"", "");
|
||||
currentArg.append(" ").append(replaced);
|
||||
res.add(currentArg.toString());
|
||||
|
||||
// clear current arg
|
||||
currentArg.setLength(0);
|
||||
inQuotes = false;
|
||||
} else {
|
||||
currentArg.append(" ").append(arg);
|
||||
}
|
||||
} else {
|
||||
if (arg.startsWith("\"")) {
|
||||
String replaced = arg.replace("\"", "");
|
||||
|
||||
if (arg.endsWith("\"")) {
|
||||
// if the current arg starts and ends with a ", just add the arg to the result
|
||||
res.add(replaced);
|
||||
} else {
|
||||
// if the current arg doesn't end with a ", it is quoted and we need to handle
|
||||
// the remainder
|
||||
inQuotes = true;
|
||||
currentArg.append(replaced);
|
||||
}
|
||||
} else {
|
||||
res.add(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,12 +10,16 @@ public class CoordinatesCommand implements CommandExecutor{
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
TextComponent textComponent = new TextComponent("Got command " + command.getName() + " label " + label + " and args ");
|
||||
StringBuilder sb = new StringBuilder("Got command " + command.getName() + " label " + label + " and args ");
|
||||
for (String string : args) {
|
||||
textComponent.addExtra(string);
|
||||
sb.append(" ");
|
||||
sb.append(string);
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
sender.sendMessage(textComponent.getText());
|
||||
sender.sendMessage("Args length: " + String.valueOf(args.length));
|
||||
sender.sendMessage(sb.toString());
|
||||
|
||||
|
||||
return switch (args[0]) {
|
||||
case "get" -> new GetCoordinatesCommandHandler().handleCommand(sender, args);
|
||||
|
||||
@@ -41,7 +41,7 @@ public enum MinecraftColor {
|
||||
*/
|
||||
public static MinecraftColor fromString(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
return WHITE;
|
||||
}
|
||||
|
||||
for (MinecraftColor color : MinecraftColor.values()) {
|
||||
@@ -49,6 +49,27 @@ public enum MinecraftColor {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return WHITE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given name is a valid color value.
|
||||
* @param name the name to check
|
||||
* @return true if it's a valid color value, false otherwise
|
||||
*/
|
||||
public static boolean isValidColor(String name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (MinecraftColor color : values()) {
|
||||
if (color.equalsName(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
4
install_and_jar.sh
Executable file
4
install_and_jar.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
mvn install -f "/home/sem/Development/Minecraft/mc-ic-server/ic_plugin/pom.xml"
|
||||
mvn jar:jar -f "/home/sem/Development/Minecraft/mc-ic-server/ic_plugin/pom.xml"
|
||||
Reference in New Issue
Block a user