diff --git a/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java b/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java index cb3b2ae..4ad3f5a 100644 --- a/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java +++ b/src/main/java/io/github/paldiu/simplexcore/command/CommandInfo.java @@ -1,9 +1,12 @@ package io.github.paldiu.simplexcore.command; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) public @interface CommandInfo { String name(); diff --git a/src/main/java/io/github/paldiu/simplexcore/command/SimplexCommand.java b/src/main/java/io/github/paldiu/simplexcore/command/SimplexCommand.java index ef00b23..42e961a 100644 --- a/src/main/java/io/github/paldiu/simplexcore/command/SimplexCommand.java +++ b/src/main/java/io/github/paldiu/simplexcore/command/SimplexCommand.java @@ -2,6 +2,7 @@ package io.github.paldiu.simplexcore.command; import io.github.paldiu.simplexcore.utils.Constants; import io.github.paldiu.simplexcore.utils.Utilities; +import jdk.jfr.BooleanFlag; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -15,7 +16,8 @@ import java.util.List; import java.util.UUID; public abstract class SimplexCommand implements CommandExecutor, TabCompleter { - public boolean checkSender(CommandSender sender) { + @BooleanFlag + public boolean isPlayer(CommandSender sender) { return sender instanceof Player; } @@ -29,12 +31,23 @@ public abstract class SimplexCommand implements CommandExecutor, TabCompleter { return Constants.getServer().getPlayer(uuid); } - public void playerMsg(Player player, String... message) { + @Nullable + public Player getPlayer(CommandSender sender) { + return isPlayer(sender) ? (Player)sender : null; + } + + public void playerMsg(Player player, String... messages) { StringBuilder builder = new StringBuilder(); - Utilities.primitiveFE(message, builder::append); + Utilities.forEach(messages, builder::append); player.sendMessage(builder.toString()); } + public void msg(CommandSender sender, String... messages) { + StringBuilder builder = new StringBuilder(); + Utilities.forEach(messages, builder::append); + sender.sendMessage(builder.toString()); + } + @Nullable @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java index f63bb59..12d8fc7 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java @@ -14,6 +14,11 @@ import org.bukkit.scheduler.BukkitScheduler; import java.util.logging.Logger; public final class Constants { + // Utility class should not be instantiated. + private Constants() { + throw new AssertionError(); + } + private static final SimplexCore plugin = JavaPlugin.getPlugin(SimplexCore.class); private static final Server server = plugin.getServer(); private static final Logger logger = plugin.getLogger(); diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java index 82deefd..28a1c17 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java @@ -11,36 +11,58 @@ import java.util.stream.Collectors; import java.util.stream.Stream; public final class Utilities { - private static Stream feStr(T[] array) { - return Arrays.stream(array); + //Utility class should not be instantiated. + private Utilities() { + throw new AssertionError(); } - private static Set feCol(Stream stream) { - return stream.collect(Collectors.toSet()); - } - - public static void primitiveFE(T[] array, Consumer action) { - feCol(feStr(array)).forEach(action); - } - - public static void smartFE(Collection collection, Consumer action) { - collection.forEach(action); - } - - public static void mapFE(Map map, BiConsumer actions) { - map.forEach(actions); + public static void forEach(T[] array, Consumer action) { + for (T obj : array) { + action.accept(obj); + } } public static String generateBanId(BanType type) { - String charList = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; - int length = charList.length(); + final String charList = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; + final String numList = "0123456789"; + final int length = charList.length(); + final int lng = numList.length(); - StringBuilder sb = new StringBuilder(); - SplittableRandom random = new SplittableRandom(); + final StringBuilder sb = new StringBuilder(); - for (int x = 0; x <= 8; x++) { - sb.append(charList.indexOf(random.nextInt(length - 1))); + sb.append(type.getPrefix()); + + for (int x = 0; x < 4; x++) { + sb.append(charList.charAt(random.nextInt(length - 1))); + sb.append(numList.charAt(numbers.nextInt(lng - 1))); } + + sb.setCharAt(2, capitalize(sb.charAt(2))); return sb.toString(); } + + private static final SplittableRandom random = new SplittableRandom(); + private static final SplittableRandom numbers = new SplittableRandom(); + private static final List list = new ArrayList<>(){{ + add('0'); + add('1'); + add('2'); + add('3'); + add('4'); + add('5'); + add('6'); + add('7'); + add('8'); + add('9'); + add('-'); + }}; + + private static char capitalize(char character) { + if (list.contains(character)) { + return character; + } + + String temp = String.valueOf(character).toUpperCase(); + return temp.charAt(0); + } } diff --git a/src/test/java/io/github/paldiu/simplexcore/utils/UtilitiesTest.java b/src/test/java/io/github/paldiu/simplexcore/utils/UtilitiesTest.java new file mode 100644 index 0000000..8b3639e --- /dev/null +++ b/src/test/java/io/github/paldiu/simplexcore/utils/UtilitiesTest.java @@ -0,0 +1,10 @@ +package io.github.paldiu.simplexcore.utils; + +import junit.framework.TestCase; +import io.github.paldiu.simplexcore.banning.BanType; + +public class UtilitiesTest extends TestCase { + public void testGenerateBanId() { + System.out.println(Utilities.generateBanId(BanType.TEMPORARY)); + } +} \ No newline at end of file