From 96eb3e52ae7cbed412e813b3489fdf5542e38924 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 27 Dec 2020 23:03:15 +0100 Subject: [PATCH] Add ArgumentMap for easy argument use Add Argument.of Add Argument.ofIgnoreCase --- .../src/de/steamwar/command/Argument.java | 12 +++- .../src/de/steamwar/command/ArgumentMap.java | 56 +++++++++++++++++++ .../src/de/steamwar/command/Executor.java | 2 +- .../src/de/steamwar/command/SWCommand.java | 2 +- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index c7d3edb..8d3876e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -31,8 +31,8 @@ public class Argument { public static final Argument INT = new Argument<>(ArgumentType.INT, integer -> true); public static final Argument LONG = new Argument<>(ArgumentType.LONG, l -> true); - public static final Argument FLOAT = new Argument<>(ArgumentType.FLOAT, integer -> true); - public static final Argument DOUBLE = new Argument<>(ArgumentType.DOUBLE, integer -> true); + public static final Argument FLOAT = new Argument<>(ArgumentType.FLOAT, f -> true); + public static final Argument DOUBLE = new Argument<>(ArgumentType.DOUBLE, d -> true); public static final Argument STRING = new Argument<>(ArgumentType.STRING, string -> true); public static final Argument PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer); @@ -50,6 +50,14 @@ public class Argument { this.valueMapper = valueMapper; } + public static Argument of(String argument) { + return new Argument<>(ArgumentType.STRING, string -> string.equals(argument)); + } + + public static Argument ofIgnoreCase(String argument) { + return new Argument<>(ArgumentType.STRING, string -> string.equalsIgnoreCase(argument)); + } + public Optional valueSupplier(String s) { try { T argumentMapped = argumentType.mapper.apply(s); diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java new file mode 100644 index 0000000..f4266a6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java @@ -0,0 +1,56 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.command; + +import java.util.Arrays; + +public class ArgumentMap { + + private Object[] objects; + + public ArgumentMap(Object[] objects) { + this.objects = objects; + } + + public int length() { + return objects.length; + } + + public T get(int index) { + return (T)objects[index]; + } + + @Override + public String toString() { + StringBuilder st = new StringBuilder(); + st.append("ArgumentMap{"); + boolean b = false; + for (Object o : objects) { + if (b) st.append(", "); + b = true; + + st.append(o.getClass().getSimpleName()).append("=").append(o); + } + st.append("}"); + return st.toString(); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/command/Executor.java b/SpigotCore_Main/src/de/steamwar/command/Executor.java index 0b42f79..f752951 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Executor.java +++ b/SpigotCore_Main/src/de/steamwar/command/Executor.java @@ -25,5 +25,5 @@ import org.bukkit.entity.Player; @FunctionalInterface public interface Executor { - boolean execute(Player player, Object[] args); + boolean execute(Player player, ArgumentMap argumentMap); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ed7b939..13bd061 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -56,7 +56,7 @@ public class SWCommand { if (!optional.isPresent()) throw new IllegalStateException(); objects[i] = optional.get(); } - return Optional.of(executor.execute(player, objects)); + return Optional.of(executor.execute(player, new ArgumentMap(objects))); } }