浏览代码

new table lib, changed command format, custom named scripts

Kajetan Johannes Hammerle 4 年之前
父节点
当前提交
c2f6ec37bd

+ 1 - 1
src/main/java/me/km/Server.java

@@ -125,7 +125,7 @@ public class Server {
                 customEventCaller, scriptBank, databank, plotMap, commands);
 
         MinecraftForge.EVENT_BUS.register(new WorldEvents());
-        scheduler.scheduleTask(() -> scripts.startScript("startscript"));
+        scheduler.scheduleTask(() -> scripts.startScript(null, "startscript"));
     }
 
     public static void onStop() {

+ 1 - 1
src/main/java/me/km/overrides/ModEntityPlayerMP.java

@@ -54,7 +54,7 @@ public class ModEntityPlayerMP extends ServerPlayerEntity {
         scheduler.scheduleTask(() -> {
             server.getPlayerList().sendPacketToAllPlayers(
                     new SPlayerListItemPacket(SPlayerListItemPacket.Action.UPDATE_DISPLAY_NAME, this));
-        });
+        }, 5);
     }
 
     @Override

+ 1 - 1
src/main/java/me/km/permissions/Command.java

@@ -17,7 +17,7 @@ public abstract class Command {
         cs.sendMessage(new StringTextComponent(String.format("[§dScript§r] %s", message)));
     }
 
-    public void sendMessage(ICommandSource cs, String message1, String message2) {
+    public void sendListMessage(ICommandSource cs, String message1, String message2) {
         cs.sendMessage(new StringTextComponent(String.format("§d - %s§r %s", message1, message2)));
     }
 }

+ 18 - 17
src/main/java/me/km/snuviscript/CommandScript.java

@@ -42,7 +42,7 @@ public class CommandScript extends Command {
                 case "start": {
                     if(arg.length >= 2) {
                         String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
-                        scripts.startScript(pars);
+                        scripts.startScript(null, pars);
                         return;
                     }
                     break;
@@ -51,7 +51,7 @@ public class CommandScript extends Command {
                     if(arg.length >= 3) {
                         PlayerEntity p = Utils.getPlayerByName(server, arg[1]);
                         if(p == null) {
-                            sendMessage(cs, String.format("Cannot find player '%s'", arg[1]));
+                            sendMessage(cs, String.format("Cannot find player '%s'.", arg[1]));
                             return;
                         }
                         String[] pars = Arrays.copyOfRange(arg, 2, arg.length);
@@ -75,7 +75,7 @@ public class CommandScript extends Command {
                                 sendMessage(cs, String.format("%s = null", arg[2]));
                             }
                         } catch(NumberFormatException ex) {
-                            sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
+                            sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
                         }
                         return;
                     }
@@ -84,15 +84,16 @@ public class CommandScript extends Command {
                 case "see": {
                     Collection<Script> scs = scripts.getScriptManager().getScripts();
                     if(scs.isEmpty()) {
-                        sendMessage(cs, "No scripts are active");
+                        sendMessage(cs, "No scripts are active.");
                         return;
                     }
                     sendMessage(cs, "Active scripts:");
                     scs.forEach(sc -> {
-                        sendMessage(cs, String.format(" - %s (%d)", sc.getName(), sc.getId()));
+                        String id = String.valueOf(sc.getId());
+                        sendListMessage(cs, id, sc.getName());
                         scripts.getPlayerList(sc.getId()).forEach(uuid -> {
                             ServerPlayerEntity p = server.getPlayerList().getPlayerByUUID(uuid);
-                            sendMessage(cs, String.format("  - %s", p == null ? "null" : p.getName().getFormattedText()));
+                            sendListMessage(cs, id, p == null ? "null" : p.getName().getFormattedText());
                         });
                     });
                     return;
@@ -103,9 +104,9 @@ public class CommandScript extends Command {
                             if(arg[1].equals("all")) {
                                 scripts.clearPlayerRegistry();
                                 if(scripts.getScriptManager().removeScriptsSafe()) {
-                                    sendMessage(cs, "All active scripts were terminated");
+                                    sendMessage(cs, "All active scripts were terminated.");
                                 } else {
-                                    sendMessage(cs, "Iterating is not allowed currently");
+                                    sendMessage(cs, "Iterating is not allowed currently.");
                                 }
                                 return;
                             }
@@ -113,14 +114,14 @@ public class CommandScript extends Command {
                             Script qd = scripts.getScriptManager().getScript(id);
                             if(qd != null) {
                                 scripts.getScriptManager().removeScriptSafe(qd);
-                                sendMessage(cs, String.format("Script '%s' was terminated", qd.getName()));
+                                sendMessage(cs, String.format("Script '%s' was terminated.", qd.getName()));
                             } else {
-                                sendMessage(cs, String.format("Script id '%d' is not valid", id));
+                                sendMessage(cs, String.format("Script id '%d' is not valid.", id));
                             }
                         } catch(NumberFormatException ex) {
-                            sendMessage(cs, String.format("'%s' is not a valid id", arg[1]));
+                            sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
                         } catch(Exception ex) {
-                            sendMessage(cs, "Exception on script termination");
+                            sendMessage(cs, "An exception on script termination was thrown.");
                             ex.printStackTrace();
                         }
                         return;
@@ -131,10 +132,10 @@ public class CommandScript extends Command {
         }
 
         sendMessage(cs, "/script ...");
-        sendMessage(cs, "start <scripts...>", "starts a script");
-        sendMessage(cs, "startp <player> <scripts...>", "starts a player script");
-        sendMessage(cs, "variable <id> <name>", "shows the value of a variable");
-        sendMessage(cs, "see", "shows active scripts");
-        sendMessage(cs, "term <id/all>", "terminates a script");
+        sendListMessage(cs, "start <scripts...>", "starts a script");
+        sendListMessage(cs, "startp <player> <scripts...>", "starts a player script");
+        sendListMessage(cs, "variable <id> <name>", "shows the value of a variable");
+        sendListMessage(cs, "see", "shows active scripts");
+        sendListMessage(cs, "term <id/all>", "terminates a script");
     }
 }

+ 3 - 3
src/main/java/me/km/snuviscript/Scripts.java

@@ -22,7 +22,7 @@ public class Scripts {
     private final HashMap<UUID, Integer> playerScript = new HashMap<>();
     private final HashMap<Integer, List<UUID>> registeredPlayers = new HashMap<>();
     private final HashSet<String> scriptCommands = new HashSet<>();
-    
+
     private final Limits entityLimits = new Limits();
 
     public Scripts(ISnuviLogger logger, ISnuviScheduler scheduler) {
@@ -162,9 +162,9 @@ public class Scripts {
         }, names[0], names);
     }
 
-    public Script startScript(String... names) {
+    public Script startScript(String name, String... names) {
         Arrays.setAll(names, i -> "scripts/" + names[i] + ".txt");
         return manager.startScript(true, sc -> {
-        }, sc -> cec.removeScriptData(sc), names[0], names);
+        }, sc -> cec.removeScriptData(sc), name == null ? names[0] : name, names);
     }
 }

+ 1 - 1
src/main/java/me/km/snuviscript/commands/PlayerCommands.java

@@ -80,7 +80,7 @@ public class PlayerCommands {
             }
         });
         sm.registerConsumer("player.speak", (sc, in) -> {
-            sendMessageToGroup(server, scripts, perms, in[0].get(sc), sc, concat(sc, 2, "§7[§r" + in[1].getString(sc) + "§7]§r ", in));
+            sendMessageToGroup(server, scripts, perms, in[0].get(sc), sc, concat(sc, 2, "[" + in[1].getString(sc) + "§r] ", in));
         });
         sm.registerConsumer("player.setcompass", (sc, in) -> {
             ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new SSpawnPositionPacket(((Location) in[1].get(sc)).getBlockPos()));

+ 8 - 1
src/main/java/me/km/snuviscript/commands/ScriptCommands.java

@@ -38,7 +38,14 @@ public class ScriptCommands {
             for(int i = 1; i < in.length; i++) {
                 names[i] = in[i].getString(sc);
             }
-            return scripts.startScript(names);
+            return scripts.startScript(null, names);
+        });
+        sm.registerFunction("script.startnamed", (sc, in) -> {
+            String[] names = new String[in.length -1];
+            for(int i = 0; i < names.length; i++) {
+                names[i] = in[i + 1].getString(sc);
+            }
+            return scripts.startScript(in[0].getString(sc), names);
         });
         sm.registerFunction("script.join", (sc, in) -> scripts.registerPlayer((Script) in[0].get(sc), (PlayerEntity) in[1].get(sc)));
         sm.registerFunction("script.kick", (sc, in) -> scripts.unregisterPlayer(sc, (PlayerEntity) in[0].get(sc)));

+ 14 - 7
src/main/java/me/km/snuviscript/commands/TableCommands.java

@@ -1,25 +1,32 @@
 package me.km.snuviscript.commands;
 
 import me.hammerle.snuviscript.code.ScriptManager;
-import me.km.utils.TableUtils;
+import me.km.utils.Table;
 
 public class TableCommands {
     public static void registerFunctions(ScriptManager sm) {
+        sm.registerFunction("table.new", (sc, in) -> {
+            int[] widths = new int[in.length - 1];
+            for(int i = 0; i < widths.length; i++) {
+                widths[i] = in[i + 1].getInt(sc);
+            }
+            return new Table(in[0].getString(sc), widths);
+        });
         sm.registerFunction("table.getstart", (sc, in) -> {
-            return TableUtils.getTableStart(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc));
+            return ((Table) in[0].get(sc)).getStart();
         });
         sm.registerFunction("table.getmiddle", (sc, in) -> {
-            return TableUtils.getTableMiddle(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc));
+            return ((Table) in[0].get(sc)).getMiddle();
         });
         sm.registerFunction("table.getend", (sc, in) -> {
-            return TableUtils.getTableEnd(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc));
+            return ((Table) in[0].get(sc)).getEnd();
         });
         sm.registerFunction("table.get", (sc, in) -> {
-            String[] columns = new String[in.length - 2];
+            String[] columns = new String[in.length - 1];
             for(int i = 0; i < columns.length; i++) {
-                columns[i] = in[i + 2].getString(sc);
+                columns[i] = in[i + 1].getString(sc);
             }
-            return TableUtils.getTable(in[0].getInt(sc), in[1].getString(sc), columns);
+            return ((Table) in[0].get(sc)).get(columns);
         });
     }
 }

+ 44 - 38
src/main/java/me/km/utils/TableUtils.java → src/main/java/me/km/utils/Table.java

@@ -1,55 +1,50 @@
 package me.km.utils;
 
-public class TableUtils {
-    public static String getTable(int width, String color, String... args) {
-        width = 9 * width - 1; // ([ ] + [│]) - [─] = 1
-        StringBuilder sb = new StringBuilder(color).append("│ ");
-        for(String s : args) {
-            sb.append("§r");
-            sb.append(getStringWithMaxLength(s, width));
-            sb.append(color).append("│ ");
-        }
-        sb.deleteCharAt(sb.length() - 1);
-        return sb.toString();
+public class Table {
+    private final String color;
+    private final int[] widths;
+
+    public Table(String color, int... widths) {
+        this.color = color;
+        this.widths = widths;
     }
 
-    public static String getTableMiddle(int width, int columns, String color) {
-        StringBuilder sb = new StringBuilder(color).append("├");
-        for(int i = 0; i < columns; i++) {
-            for(int j = 0; j < width; j++) {
-                sb.append("─");
-            }
-            sb.append("┼");
-        }
-        sb.setCharAt(sb.length() - 1, '┤');
-        return sb.toString();
+    public String getStart() {
+        return getLine('┌', '┬', '┐');
     }
 
-    public static String getTableStart(int width, int columns, String color) {
-        StringBuilder sb = new StringBuilder(color).append("┌");
-        for(int i = 0; i < columns; i++) {
+    public String getMiddle() {
+        return getLine('├', '┼', '┤');
+    }
+
+    public String getEnd() {
+        return getLine('└', '┴', '┘');
+    }
+
+    private String getLine(char start, char middle, char end) {
+        StringBuilder sb = new StringBuilder(color).append(start);
+        for(int width : widths) {
             for(int j = 0; j < width; j++) {
-                sb.append("─");
+                sb.append('─');
             }
-            sb.append("┬");
+            sb.append(middle);
         }
-        sb.setCharAt(sb.length() - 1, '┐');
+        sb.setCharAt(sb.length() - 1, end);
         return sb.toString();
     }
 
-    public static String getTableEnd(int width, int columns, String color) {
-        StringBuilder sb = new StringBuilder(color).append("└");
-        for(int i = 0; i < columns; i++) {
-            for(int j = 0; j < width; j++) {
-                sb.append("─");
-            }
-            sb.append("┴");
+    public String get(String... args) {
+        StringBuilder sb = new StringBuilder(color).append("│ ");
+        for(int i = 0; i < widths.length; i++) {
+            sb.append("§r");
+            sb.append(shorten(args[i], 9 * widths[i] - 1));
+            sb.append(color).append("│ ");
         }
-        sb.setCharAt(sb.length() - 1, '┘');
+        sb.deleteCharAt(sb.length() - 1);
         return sb.toString();
     }
 
-    private static int getCharWidth(char c) {
+    private int getCharWidth(char c) {
         switch(c) {
             case 'f':
             case 'k':
@@ -75,16 +70,27 @@ public class TableUtils {
         return 6;
     }
 
-    private static String getStringWithMaxLength(String s, int max) {
+    private String shorten(String s, int max) {
         int sum = 0;
+        int fat = 0;
         StringBuilder sb = new StringBuilder();
         for(int i = 0; i < s.length(); i++) {
             char c = s.charAt(i);
             if(c == '§') {
+                sb.append('§');
                 i++;
+                if(i < s.length()) {
+                    char cc = Character.toLowerCase(s.charAt(i));
+                    if(cc == 'l') {
+                        fat = 1;
+                    } else if(cc != 'm' && cc != 'n' & cc != 'o') {
+                        fat = 0;
+                    }
+                    sb.append(cc);
+                }
                 continue;
             }
-            int width = getCharWidth(c);
+            int width = getCharWidth(c) + fat;
             if(sum + width > max) {
                 break;
             }