Updated a bunch of framework, removed useless functions and fixed possible N(2o) complexity issue.

This commit is contained in:
Paldiu 2021-02-16 10:30:42 -06:00
parent e27fbaf9de
commit eb05f43058
5 changed files with 78 additions and 25 deletions

View File

@ -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();

View File

@ -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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {

View File

@ -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();

View File

@ -11,36 +11,58 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class Utilities {
private static <T> Stream<T> feStr(T[] array) {
return Arrays.stream(array);
//Utility class should not be instantiated.
private Utilities() {
throw new AssertionError();
}
private static <T> Set<T> feCol(Stream<T> stream) {
return stream.collect(Collectors.toSet());
}
public static <T> void primitiveFE(T[] array, Consumer<? super T> action) {
feCol(feStr(array)).forEach(action);
}
public static <T> void smartFE(Collection<T> collection, Consumer<? super T> action) {
collection.forEach(action);
}
public static <K, V> void mapFE(Map<K, V> map, BiConsumer<K, V> actions) {
map.forEach(actions);
public static <T> void forEach(T[] array, Consumer<? super T> 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<Character> 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);
}
}

View File

@ -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));
}
}