diff --git a/build.gradle b/build.gradle index 61ed747..d3a6128 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ version = project.mod_version group = project.maven_group repositories { + mavenCentral() } dependencies { diff --git a/gradle.properties b/gradle.properties index 50caf5c..47f4927 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,6 @@ org.gradle.parallel=true loader_version=0.14.11 # Mod Properties - mod_version = 1.0.0 + mod_version = 2.0.0 maven_group = de.zonlykroks archives_base_name = AdvancedScripts \ No newline at end of file diff --git a/src/main/java/de/steamwar/advancedscripts/AdvancedScripts.java b/src/main/java/de/steamwar/advancedscripts/AdvancedScripts.java new file mode 100644 index 0000000..240e73d --- /dev/null +++ b/src/main/java/de/steamwar/advancedscripts/AdvancedScripts.java @@ -0,0 +1,29 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.advancedscripts; + +import net.fabricmc.api.ClientModInitializer; + +public class AdvancedScripts implements ClientModInitializer { + + @Override + public void onInitializeClient() { + } +} diff --git a/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java b/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java new file mode 100644 index 0000000..dc78d4d --- /dev/null +++ b/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java @@ -0,0 +1,131 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.advancedscripts.lexer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class ScriptColorizer { + + private static final Set KEYWORDS = Set.of( + "and", "break", "do", "else", "elseif", "end", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "until", "while" + ); + + private ScriptColorizer() { + throw new IllegalStateException("Utility class██"); + } + + public static List colorize(String line) { + List tokens = colorizeComment(line); + if (tokens != null) return tokens; + return colorizeLine(line); + } + + private static List colorizeComment(String line) { + if (!line.startsWith("--")) return null; + return List.of(new Token(line, TokenTypeColors.COMMENT)); + } + + private static List colorizeLine(String line) { + List tokens = new ArrayList<>(); + + StringBuilder currentToken = new StringBuilder(); + + boolean inString = false; + boolean doubleQuoteString = false; + + for (char c : line.toCharArray()) { + switch (c) { + case '"' -> { + if (inString) { + currentToken.append(c); + if (doubleQuoteString) { + tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING)); + currentToken = new StringBuilder(); + inString = false; + doubleQuoteString = false; + } + } else { + inString = true; + doubleQuoteString = true; + if (currentToken.length() > 0) { + tokens.add(toToken(currentToken.toString())); + currentToken = new StringBuilder(); + } + currentToken.append(c); + } + } + case '\'' -> { + if (inString) { + currentToken.append(c); + if (!doubleQuoteString) { + tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING)); + currentToken = new StringBuilder(); + inString = false; + } + } else { + inString = true; + doubleQuoteString = false; + if (currentToken.length() > 0) { + tokens.add(toToken(currentToken.toString())); + currentToken = new StringBuilder(); + } + currentToken.append(c); + } + } + case '-', ';', '(', ')', ',', ' ', '{', '\t' -> { + if (inString) { + currentToken.append(c); + } else { + if(currentToken.length() > 0) { + tokens.add(toToken(currentToken.toString())); + } + tokens.add(new Token(String.valueOf(c), TokenTypeColors.OTHER)); + currentToken = new StringBuilder(); + } + } + default -> currentToken.append(c); + } + } + + if (currentToken.length() > 0) { + tokens.add(toToken(currentToken.toString())); + } + + return tokens; + } + + private static Token toToken(String text) { + if (text.length() > 0) { + if (KEYWORDS.contains(text)) { + return new Token(text, TokenTypeColors.CONSTANT); + } else if ("true".contentEquals(text) || "false".contentEquals(text)) { + return new Token(text, TokenTypeColors.BOOLEAN); + } else if (text.matches("[0-9]+")) { + return new Token(text, TokenTypeColors.NUMBER); + } else { + return new Token(text, TokenTypeColors.OTHER); + } + } else { + return Token.SPACE; + } + } +} diff --git a/src/main/java/de/steamwar/advancedscripts/lexer/Token.java b/src/main/java/de/steamwar/advancedscripts/lexer/Token.java new file mode 100644 index 0000000..58f764c --- /dev/null +++ b/src/main/java/de/steamwar/advancedscripts/lexer/Token.java @@ -0,0 +1,40 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.advancedscripts.lexer; + +public class Token { + public static final Token SPACE = new Token(" ", 0xFFFFFFFF); + + public final String text; + public final int color; + + public Token(String text, int color) { + this.text = text; + this.color = color; + } + + @Override + public String toString() { + return "Token{" + + "text='" + text + '\'' + + ", color=" + color + + '}'; + } +} diff --git a/src/main/java/de/steamwar/advancedscripts/lexer/TokenTypeColors.java b/src/main/java/de/steamwar/advancedscripts/lexer/TokenTypeColors.java new file mode 100644 index 0000000..6510fb0 --- /dev/null +++ b/src/main/java/de/steamwar/advancedscripts/lexer/TokenTypeColors.java @@ -0,0 +1,41 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.advancedscripts.lexer; + +public class TokenTypeColors { + + private TokenTypeColors() { + throw new IllegalStateException("Utility class"); + } + + public static final int BACKGROUND = 0xFF1E1F22; + public static final int SELECTION = 0xFF23437F; + public static final int OTHER = 0xFFFFFFFF; + + public static final int ERROR = 0xFFAA0000; + + public static final int VARIABLE = 0xFFFFFFFF; + public static final int COMMENT = 0xFF656565; + public static final int CONSTANT = 0x3F6EC6; + + public static final int NUMBER = 0xFF61839F; + public static final int BOOLEAN = 0xFF925F35; + public static final int STRING = 0x6a8759; +} diff --git a/src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayerEntityMixin.java b/src/main/java/de/steamwar/advancedscripts/mixin/ClientPlayerEntityMixin.java similarity index 51% rename from src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayerEntityMixin.java rename to src/main/java/de/steamwar/advancedscripts/mixin/ClientPlayerEntityMixin.java index 4c00782..11b0a25 100644 --- a/src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayerEntityMixin.java +++ b/src/main/java/de/steamwar/advancedscripts/mixin/ClientPlayerEntityMixin.java @@ -1,6 +1,25 @@ -package de.zonlykroks.advancedscripts.mixin; +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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 . + */ -import de.zonlykroks.advancedscripts.screen.ScriptEditScreen; +package de.steamwar.advancedscripts.mixin; + +import de.steamwar.advancedscripts.screen.ScriptEditScreen; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.item.ItemStack; diff --git a/src/main/java/de/zonlykroks/advancedscripts/mixin/KeyboardMixin.java b/src/main/java/de/steamwar/advancedscripts/mixin/KeyboardMixin.java similarity index 61% rename from src/main/java/de/zonlykroks/advancedscripts/mixin/KeyboardMixin.java rename to src/main/java/de/steamwar/advancedscripts/mixin/KeyboardMixin.java index acdec70..5d0d666 100644 --- a/src/main/java/de/zonlykroks/advancedscripts/mixin/KeyboardMixin.java +++ b/src/main/java/de/steamwar/advancedscripts/mixin/KeyboardMixin.java @@ -1,20 +1,23 @@ /* - This file is a part of the SteamWar software. + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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 . + */ - 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.zonlykroks.advancedscripts.mixin; +package de.steamwar.advancedscripts.mixin; import io.netty.buffer.Unpooled; import net.minecraft.client.Keyboard; diff --git a/src/main/java/de/zonlykroks/advancedscripts/screen/ScriptEditScreen.java b/src/main/java/de/steamwar/advancedscripts/screen/ScriptEditScreen.java similarity index 90% rename from src/main/java/de/zonlykroks/advancedscripts/screen/ScriptEditScreen.java rename to src/main/java/de/steamwar/advancedscripts/screen/ScriptEditScreen.java index b4daf11..07afe06 100644 --- a/src/main/java/de/zonlykroks/advancedscripts/screen/ScriptEditScreen.java +++ b/src/main/java/de/steamwar/advancedscripts/screen/ScriptEditScreen.java @@ -1,9 +1,28 @@ -package de.zonlykroks.advancedscripts.screen; +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.advancedscripts.screen; import com.mojang.blaze3d.systems.RenderSystem; -import de.zonlykroks.advancedscripts.lexer.ScriptColorizer; -import de.zonlykroks.advancedscripts.lexer.Token; -import de.zonlykroks.advancedscripts.lexer.TokenTypeColors; +import de.steamwar.advancedscripts.lexer.TokenTypeColors; +import de.steamwar.advancedscripts.lexer.ScriptColorizer; +import de.steamwar.advancedscripts.lexer.Token; import net.minecraft.client.font.TextHandler; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.Screen; @@ -75,7 +94,10 @@ public class ScriptEditScreen extends Screen { @Override protected void init() { this.addDrawableChild( - new Button(DONE, this.width / 2 - 98 / 2, height - 55, () -> { + new Button(CANCEL, this.width / 2 - 100, height - 55, () -> this.client.setScreen(null)) + ); + this.addDrawableChild( + new Button(DONE, this.width / 2 + 2, height - 55, () -> { this.client.setScreen(null); finalizeBook(); }) @@ -89,6 +111,7 @@ public class ScriptEditScreen extends Screen { } public static final Text DONE = Text.translatable("gui.done"); + public static final Text CANCEL = Text.translatable("gui.cancel"); public static final Text BOOK = Text.translatable("item.minecraft.book"); private static class Button extends PressableWidget { @@ -137,6 +160,7 @@ public class ScriptEditScreen extends Screen { @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + setFocused(null); this.renderBackground(matrices); RenderSystem.setShader(GameRenderer::getPositionTexProgram); RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); @@ -169,11 +193,11 @@ public class ScriptEditScreen extends Screen { if (lineTooLong(s)) { fill(matrices, 25 + lineNumberLength + 2, 25 + lineNumber.getValue() * 9, 25 + lineNumberLength + 3, 25 + lineNumber.getValue() * 9 + height, TokenTypeColors.ERROR); } - this.textRenderer.draw(matrices, lineNumberText + "", 25 + lineNumberLength - textRenderer.getWidth(lineNumberText + ""), 25 + lineNumber.getValue() * 9, 0xFFFFFF); + this.textRenderer.draw(matrices, String.valueOf(lineNumberText), 25f + lineNumberLength - textRenderer.getWidth(String.valueOf(lineNumberText)), 25f + lineNumber.getValue() * 9f, 0xFFFFFF); lineNumberText++; // Line text - List tokens = ScriptColorizer.colorize(lineNumber.getValue(), s); + List tokens = ScriptColorizer.colorize(s); AtomicInteger x = new AtomicInteger(25 + lineNumberLength + 5); AtomicInteger currentXIndex = new AtomicInteger(0); for (Token token : tokens) { @@ -252,6 +276,7 @@ public class ScriptEditScreen extends Screen { } private void key(int keyCode) { + setFocused(null); if (Screen.isSelectAll(keyCode)) { this.cursorX = 0; this.cursorY = 0; @@ -282,46 +307,49 @@ public class ScriptEditScreen extends Screen { int previousCursorX = cursorX; int previousCursorY = cursorY; switch (keyCode) { - case 257: - case 335: + case 258 -> { + insert(" "); + return; + } + case 257, 335 -> { selection(true); newLine(); valid = false; - break; - case 259: + } + case 259 -> { if (selection(true) == null) backspace(selectionType); valid = false; - break; - case 261: + } + case 261 -> { if (selection(true) == null) delete(selectionType); valid = false; - break; - case 262: + } + case 262 -> { moveCursor(1, selectionType); valid = Screen.hasShiftDown(); - break; - case 263: + } + case 263 -> { moveCursor(-1, selectionType); valid = Screen.hasShiftDown(); - break; - case 264: + } + case 264 -> { moveDown(); valid = Screen.hasShiftDown(); - break; - case 265: + } + case 265 -> { moveUp(); valid = Screen.hasShiftDown(); - break; - case 268: + } + case 268 -> { cursorX = 0; valid = Screen.hasShiftDown(); - break; - case 269: + } + case 269 -> { cursorX = lines.get(cursorY).length(); valid = Screen.hasShiftDown(); - break; - default: - break; + } + default -> { + } } if (valid) { if (Screen.hasShiftDown() && savedCursorX == -1 && savedCursorY == -1) { @@ -337,6 +365,7 @@ public class ScriptEditScreen extends Screen { @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + setFocused(null); if (super.keyPressed(keyCode, scanCode, modifiers)) { return true; } @@ -348,17 +377,19 @@ public class ScriptEditScreen extends Screen { @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + setFocused(null); this.keyCode = -1; return super.keyReleased(keyCode, scanCode, modifiers); } @Override public boolean charTyped(char chr, int modifiers) { + setFocused(null); if (super.charTyped(chr, modifiers)) { return true; } selection(true); - boolean valid = insert(chr + ""); + boolean valid = insert(String.valueOf(chr)); savedCursorY = -1; savedCursorX = -1; autoScroll(); diff --git a/src/main/java/de/zonlykroks/advancedscripts/AdvancedScripts.java b/src/main/java/de/zonlykroks/advancedscripts/AdvancedScripts.java deleted file mode 100644 index 6882751..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/AdvancedScripts.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - 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.zonlykroks.advancedscripts; - -import net.fabricmc.api.ClientModInitializer; - -public class AdvancedScripts implements ClientModInitializer { - - @Override - public void onInitializeClient() { - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/Command.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/Command.java deleted file mode 100644 index 5930bc9..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/Command.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.List; - -public class Command { - public final boolean repeatable; - public final List> arguments; - - public Command(boolean repeatable, List> arguments) { - this.repeatable = repeatable; - this.arguments = arguments; - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/Commands.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/Commands.java deleted file mode 100644 index 3c2debc..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/Commands.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.HashMap; -import java.util.Map; - -public class Commands { - - private Commands() { - throw new IllegalStateException("Utility class"); - } - - public static Map COMMANDS = new HashMap<>(); -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java deleted file mode 100644 index feaea4a..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/ExpressionColorizer.java +++ /dev/null @@ -1,134 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ExpressionColorizer { - - private ExpressionColorizer() { - throw new IllegalStateException("Utility class"); - } - - public static List colorize(String expression) { - List parts = tokenize(expression); - List tokens = new ArrayList<>(); - for (int i = 0; i < parts.size(); i++) { - String part = parts.get(i); - if ("{".equals(part) || "}".equals(part)) { - tokens.add(new Token(part, TokenTypeColors.OTHER)); - continue; - } - if ("true".equalsIgnoreCase(part) || "false".equalsIgnoreCase(part)) { - tokens.add(new Token(part, TokenTypeColors.BOOLEAN)); - continue; - } - try { - Double.parseDouble(part); - tokens.add(new Token(part, TokenTypeColors.NUMBER)); - continue; - } catch (NumberFormatException ignored) { - } - try { - Long.parseLong(part); - tokens.add(new Token(part, TokenTypeColors.NUMBER)); - continue; - } catch (NumberFormatException ignored) { - } - if (part.contains(".")) { - String[] split = part.split("\\."); - if (split.length == 1) { - tokens.add(new Token(part, TokenTypeColors.VARIABLE)); - continue; - } - if (VariablePrefixes.RPEFIXES.contains(split[0])) { - tokens.add(new Token(split[0], TokenTypeColors.OTHER)); - tokens.add(new Token(".", TokenTypeColors.OTHER)); - split = Arrays.copyOfRange(split, 1, split.length); - } - tokens.add(new Token(split[0], TokenTypeColors.VARIABLE)); - for (int j = 1; j < split.length; j++) { - String s = split[j]; - tokens.add(new Token(".", TokenTypeColors.OTHER)); - if (VariableSuffixes.SUFFIXES.contains(s)) { - tokens.add(new Token(s, TokenTypeColors.OTHER)); - } else { - tokens.add(new Token(s, TokenTypeColors.ERROR)); - } - } - continue; - } - if (Operators.OPERATORS.contains(part)) { - String previous = get(parts, i, -1); - String next = get(parts, i, 1); - if (previous == null || next == null) { - tokens.add(new Token(part, TokenTypeColors.ERROR)); - continue; - } - if (Operators.OPERATORS.contains(previous) || Operators.OPERATORS.contains(next)) { - tokens.add(new Token(part, TokenTypeColors.ERROR)); - continue; - } - if ("{".equals(previous) || "}".equals(next)) { - tokens.add(new Token(part, TokenTypeColors.ERROR)); - continue; - } - tokens.add(new Token(part, TokenTypeColors.OTHER)); - continue; - } - if (part.matches("[+\\-*/%^&|<>=!]+")) { - tokens.add(new Token(part, TokenTypeColors.ERROR)); - continue; - } - tokens.add(new Token(part, TokenTypeColors.VARIABLE)); - } - return tokens; - } - - private static String get(List parts, int index, int direction) { - for (int i = index + direction; i >= 0 && i < parts.size(); i += direction) { - String part = parts.get(i); - if (!part.isBlank()) return part; - } - return null; - } - - private static List tokenize(String s) { - List tokens = new ArrayList<>(); - StringBuilder token = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (c == '{' || c == '}' || c == ' ') { - if (token.length() > 0) { - tokens.add(token.toString()); - token = new StringBuilder(); - } - tokens.add(c + ""); - continue; - } - StringBuilder op = new StringBuilder(); - for (int j = i; j < s.length(); j++) { - char k = s.charAt(j); - if (k == '+' || k == '-' || k == '*' || k == '/' || k == '%' || k == '^' || k == '&' || k == '|' || k == '>' || k == '<' || k == '=' || k == '!') { - op.append(k); - } else { - break; - } - } - if (op.length() > 0) { - if (token.length() > 0) { - tokens.add(token.toString()); - token = new StringBuilder(); - } - tokens.add(op.toString()); - i += op.length() - 1; - continue; - } - token.append(c); - } - if (token.length() > 0) { - tokens.add(token.toString()); - } - return tokens; - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/Headers.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/Headers.java deleted file mode 100644 index 2e3c4e7..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/Headers.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.HashSet; -import java.util.Set; - -public class Headers { - - private Headers() { - throw new IllegalStateException("Utility class"); - } - - public static final Set HEADERS = new HashSet<>(); -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/Operators.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/Operators.java deleted file mode 100644 index 70586f4..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/Operators.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.HashSet; -import java.util.Set; - -public class Operators { - - private Operators() { - throw new IllegalStateException("Utility class"); - } - - public static final Set OPERATORS = new HashSet<>(); -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java deleted file mode 100644 index 6086486..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptColorizer.java +++ /dev/null @@ -1,278 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.ArrayList; -import java.util.List; - -public class ScriptColorizer { - - private ScriptColorizer() { - throw new IllegalStateException("Utility class"); - } - - public static List colorize(int lineNumber, String line) { - if (lineNumber == 0) { - List tokens = colorizeHeader(line); - if (tokens != null) return tokens; - } - - List tokens; - tokens = colorizeComment(line); - if (tokens != null) return tokens; - tokens = colorizeJumpPoint(line); - if (tokens != null) return tokens; - return colorizeLine(line); - } - - private static List colorizeHeader(String line) { - if (!line.startsWith("#!")) return null; - List tokens = new ArrayList<>(); - tokens.add(new Token("#!", TokenTypeColors.COMMENT)); - String s = line.substring(2); - - for (String pattern : Headers.HEADERS) { - if (s.matches(pattern)) { - tokens.add(new Token(s, TokenTypeColors.OTHER)); - return tokens; - } - } - tokens.add(new Token(s, TokenTypeColors.ERROR)); - return tokens; - } - - private static List colorizeComment(String line) { - if (!line.startsWith("#")) return null; - return List.of(new Token(line, TokenTypeColors.COMMENT)); - } - - private static List colorizeJumpPoint(String line) { - if (!line.startsWith(".")) return null; - return List.of(new Token(line, TokenTypeColors.JUMP_POINT)); - } - - private static List colorizeLine(String line) { - List tokens = new ArrayList<>(); - - String command = line; - if (line.indexOf(' ') != -1) { - command = line.substring(0, line.indexOf(' ')); - } - boolean repeatable = false; - List> argumentTypes = null; - if (Commands.COMMANDS.containsKey(command)) { - Command c = Commands.COMMANDS.get(command); - repeatable = c.repeatable; - argumentTypes = c.arguments; - tokens.add(new Token(command, TokenTypeColors.LITERAL)); - } else { - repeatable = true; - argumentTypes = new ArrayList<>(); - argumentTypes.add(List.of(TokenType.any)); - tokens.add(new Token(command, TokenTypeColors.OTHER)); - } - if (command.equals(line)) return tokens; - tokens.add(Token.SPACE); - - String args = line.substring(command.length() + 1); - tokens.addAll(colorizeArgs(args, repeatable, argumentTypes)); - return tokens; - } - - private static List colorizeArgs(String args, boolean repeatable, List> argumentTypes) { - List tokens = new ArrayList<>(); - - for (List tokenTypes : argumentTypes) { - List temp = new ArrayList<>(); - int index = 0; - int argIndex = 0; - try { - while (argIndex < args.length()) { - if (args.charAt(argIndex) == ' ') { - argIndex++; - temp.add(Token.SPACE); - continue; - } - List current = parse(tokenTypes.get(index), args.substring(argIndex)); - if (current.isEmpty()) { - break; - } - temp.addAll(current); - argIndex += current.stream().mapToInt(t -> t.text.length()).sum(); - index++; - if (repeatable && index == tokenTypes.size()) { - index--; - } - if (index == tokenTypes.size()) { - break; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - if (argIndex != args.length()) { - continue; - } - if (index != tokenTypes.size() - (repeatable ? 1 : 0)) { - continue; - } - - if (!temp.isEmpty()) { - tokens.addAll(temp); - break; - } - } - - if (tokens.isEmpty()) { - tokens.add(new Token(args, TokenTypeColors.OTHER)); - } - return tokens; - } - - private static List parse(TokenType type, String current) { - return switch (type) { - case any -> parseAny(current); - case expression -> parseExpression(current); - case jump_point -> parseJumpPoint(current); - case variable -> parseVariable(current); - case text_type -> parseText(current); - case number_type -> parseNumber(current); - case floating_number_type -> parseFloatingNumber(current); - case boolean_type -> parseBoolean(current); - }; - } - - private static List parseAny(String current) { - List tokens = parseExpression(current); - if (!tokens.isEmpty()) return tokens; - tokens = parseFloatingNumber(current); - if (!tokens.isEmpty()) return tokens; - tokens = parseNumber(current); - if (!tokens.isEmpty()) return tokens; - tokens = parseBoolean(current); - if (!tokens.isEmpty()) return tokens; - return parseText(current); - } - - private static List parseExpression(String current) { - if (!current.startsWith("{")) return new ArrayList<>(); - int depth = 0; - int index = 0; - do { - if (current.charAt(index) == '{') { - depth++; - } else if (current.charAt(index) == '}') { - depth--; - } - index++; - } while (depth != 0 && index < current.length()); - if (depth != 0) return List.of(new Token(current, TokenTypeColors.ERROR)); - return ExpressionColorizer.colorize(current.substring(0, index)); - } - - private static List parseJumpPoint(String current) { - int index = current.indexOf(' '); - if (index == -1) { - return List.of(new Token(current, TokenTypeColors.JUMP_POINT)); - } else { - return List.of(new Token(current.substring(0, index), TokenTypeColors.JUMP_POINT)); - } - } - - private static List parseVariable(String current) { - int index = current.indexOf(' '); - if (index == -1) { - return List.of(new Token(current, TokenTypeColors.VARIABLE)); - } else { - return List.of(new Token(current.substring(0, index), TokenTypeColors.VARIABLE)); - } - } - - private static List parseText(String current) { - int index = current.indexOf(' '); - if (index != -1) { - current = current.substring(0, index); - } - List tokens = new ArrayList<>(); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < current.length(); i++) { - char c = current.charAt(i); - if (c == '&' && i + 1 < current.length()) { - char color = current.charAt(i + 1); - if (color >= '0' && color <= '9' || color >= 'a' && color <= 'f' || color >= 'A' && color <= 'F') { - if (sb.length() > 0) { - tokens.add(new Token(sb.toString(), TokenTypeColors.TEXT)); - sb = new StringBuilder(); - } - i++; - switch (color) { - case '0' -> tokens.add(new Token("&0", 0xFF000000)); - case '1' -> tokens.add(new Token("&1", 0xFF0000AA)); - case '2' -> tokens.add(new Token("&2", 0xFF00AA00)); - case '3' -> tokens.add(new Token("&3", 0xFF00AAAA)); - case '4' -> tokens.add(new Token("&4", 0xFFAA0000)); - case '5' -> tokens.add(new Token("&5", 0xFFAA00AA)); - case '6' -> tokens.add(new Token("&6", 0xFFFFAA00)); - case '7' -> tokens.add(new Token("&7", 0xFFAAAAAA)); - case '8' -> tokens.add(new Token("&8", 0xFF555555)); - case '9' -> tokens.add(new Token("&9", 0xFF5555FF)); - case 'a', 'A' -> tokens.add(new Token("&a", 0xFF55FF55)); - case 'b', 'B' -> tokens.add(new Token("&b", 0xFF55FFFF)); - case 'c', 'C' -> tokens.add(new Token("&c", 0xFFFF5555)); - case 'd', 'D' -> tokens.add(new Token("&d", 0xFFFF55FF)); - case 'e', 'E' -> tokens.add(new Token("&e", 0xFFFFFF55)); - case 'f', 'F' -> tokens.add(new Token("&f", 0xFFFFFFFF)); - default -> tokens.add(new Token("&" + color, TokenTypeColors.TEXT)); - } - } else { - sb.append(c); - } - } else { - sb.append(c); - } - } - if (sb.length() > 0) { - tokens.add(new Token(sb.toString(), TokenTypeColors.TEXT)); - } - return tokens; - } - - private static List parseNumber(String current) { - int index = current.indexOf(' '); - String number = current; - if (index != -1) { - number = current.substring(0, index); - } - try { - Long.parseLong(number); - return List.of(new Token(number, TokenTypeColors.NUMBER)); - } catch (NumberFormatException e) { - return new ArrayList<>(); - } - } - - private static List parseFloatingNumber(String current) { - int index = current.indexOf(' '); - String number = current; - if (index != -1) { - number = current.substring(0, index); - } - try { - Double.parseDouble(number); - return List.of(new Token(number, TokenTypeColors.NUMBER)); - } catch (NumberFormatException e) { - return new ArrayList<>(); - } - } - - private static List parseBoolean(String current) { - int index = current.indexOf(' '); - String bool = current; - if (index != -1) { - bool = current.substring(0, index); - } - if ("true".equalsIgnoreCase(bool) || "false".equalsIgnoreCase(bool)) { - return List.of(new Token(bool, TokenTypeColors.BOOLEAN)); - } else { - return new ArrayList<>(); - } - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptSyntaxPacketParser.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptSyntaxPacketParser.java deleted file mode 100644 index abf09f6..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/ScriptSyntaxPacketParser.java +++ /dev/null @@ -1,78 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -public class ScriptSyntaxPacketParser { - - private ScriptSyntaxPacketParser() { - throw new IllegalStateException("Utility class"); - } - - private static final TokenType[] TOKEN_TYPES = TokenType.values(); - - private static void reset() { - Operators.OPERATORS.clear(); - Headers.HEADERS.clear(); - VariablePrefixes.RPEFIXES.clear(); - VariableSuffixes.SUFFIXES.clear(); - Commands.COMMANDS.clear(); - } - - public static synchronized void parse(String scriptSyntax) { - reset(); - - JsonObject jsonObject = JsonParser.parseString(scriptSyntax).getAsJsonObject(); - for (String key : jsonObject.keySet()) { - JsonArray jsonElements = jsonObject.get(key).getAsJsonArray(); - if (key.startsWith("@")) { - parseSpecial(key, jsonElements); - } else { - parseCommand(key, jsonElements); - } - } - } - - private static void parseCommand(String key, JsonArray value) { - boolean repeating = value.get(0).getAsBoolean(); - List> validArgumentTypes = new ArrayList<>(); - for (int i = 1; i < value.size(); i++) { - JsonArray parameters = value.get(i).getAsJsonArray(); - List parameterTypes = new ArrayList<>(); - for (JsonElement parameter : parameters) { - parameterTypes.add(TOKEN_TYPES[parameter.getAsInt()]); - } - validArgumentTypes.add(parameterTypes); - } - Commands.COMMANDS.put(key, new Command(repeating, validArgumentTypes)); - } - - private static void parseSpecial(String key, JsonArray value) { - Set set; - switch (key) { - case "@operators": - set = Operators.OPERATORS; - break; - case "@headers": - set = Headers.HEADERS; - break; - case "@prefixes": - set = VariablePrefixes.RPEFIXES; - break; - case "@suffixes": - set = VariableSuffixes.SUFFIXES; - break; - default: - return; - } - for (JsonElement element : value) { - set.add(element.getAsString()); - } - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/Token.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/Token.java deleted file mode 100644 index 6e7d022..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/Token.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -public class Token { - public static final Token SPACE = new Token(" ", 0xFFFFFFFF); - - public final String text; - public final int color; - - public Token(String text, int color) { - this.text = text; - this.color = color; - } -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenType.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenType.java deleted file mode 100644 index 075646c..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenType.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -public enum TokenType { // This is copied from the BauSystem2.0 sources. - any, // This does not include jump_point and variable - expression, - jump_point, - variable, - - text_type, - number_type, - floating_number_type, - boolean_type, -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenTypeColors.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenTypeColors.java deleted file mode 100644 index 71a5643..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/TokenTypeColors.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -public class TokenTypeColors { - - private TokenTypeColors() { - throw new IllegalStateException("Utility class"); - } - - public static final int BACKGROUND = 0xFF1E1F22; - public static final int SELECTION = 0xFF23437F; - public static final int OTHER = 0xFFFFFFFF; - - public static final int ERROR = 0xFFAA0000; - - public static final int VARIABLE = 0xFFFFFFFF; - public static final int LITERAL = 0xFF925F35; - public static final int COMMENT = 0xFF656565; - public static final int JUMP_POINT = 0xFFFFa500; - - public static final int NUMBER = 0xFF61839F; - public static final int BOOLEAN = 0xFF925F35; - public static final int TEXT = 0xFF6F855D; -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/VariablePrefixes.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/VariablePrefixes.java deleted file mode 100644 index 631fd15..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/VariablePrefixes.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.HashSet; -import java.util.Set; - -public class VariablePrefixes { - - private VariablePrefixes() { - throw new IllegalStateException("Utility class"); - } - - public static final Set RPEFIXES = new HashSet<>(); -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/lexer/VariableSuffixes.java b/src/main/java/de/zonlykroks/advancedscripts/lexer/VariableSuffixes.java deleted file mode 100644 index d4d67f1..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/lexer/VariableSuffixes.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.zonlykroks.advancedscripts.lexer; - -import java.util.HashSet; -import java.util.Set; - -public class VariableSuffixes { - - private VariableSuffixes() { - throw new IllegalStateException("Utility class"); - } - - public static final Set SUFFIXES = new HashSet<>(); -} diff --git a/src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayNetworkHandlerMixin.java deleted file mode 100644 index 3b9c457..0000000 --- a/src/main/java/de/zonlykroks/advancedscripts/mixin/ClientPlayNetworkHandlerMixin.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.zonlykroks.advancedscripts.mixin; - -import de.zonlykroks.advancedscripts.lexer.ScriptSyntaxPacketParser; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.util.Identifier; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin { - - private static final Identifier CHANNEL = new Identifier("sw:script_syntax"); - - @Inject(method = "onCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/CustomPayloadS2CPacket;getData()Lnet/minecraft/network/PacketByteBuf;"), cancellable = true) - public void onCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) { - if (CHANNEL.equals(packet.getChannel())) { - PacketByteBuf buf = packet.getData(); - int readableBytes = buf.readableBytes(); - StringBuilder st = new StringBuilder(); - for (int i = 0; i < readableBytes; i++) { - st.append((char) buf.readByte()); - } - ScriptSyntaxPacketParser.parse(st.toString()); - ci.cancel(); - } - } -} diff --git a/src/main/resources/advanced-scripts-icon.png b/src/main/resources/advanced-scripts-icon.png new file mode 100644 index 0000000..ca9bdfd Binary files /dev/null and b/src/main/resources/advanced-scripts-icon.png differ diff --git a/src/main/resources/advancedscripts.mixins.json b/src/main/resources/advancedscripts.mixins.json index 4f89400..a455d4c 100644 --- a/src/main/resources/advancedscripts.mixins.json +++ b/src/main/resources/advancedscripts.mixins.json @@ -1,14 +1,13 @@ { "required": true, "minVersion": "0.8", - "package": "de.zonlykroks.advancedscripts.mixin", + "package": "de.steamwar.advancedscripts.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ ], "client": [ "KeyboardMixin", - "ClientPlayerEntityMixin", - "ClientPlayNetworkHandlerMixin" + "ClientPlayerEntityMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 025699f..097be61 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -6,13 +6,15 @@ "name": "Advanced Scripts", "description": "An addition to the Steamwar script system!", "authors": [ - "zOnlyKroks" + "zOnlyKroks", + "Chaoscaot" ], + "icon": "advanced-scripts-icon.png", "license": "AGPL-3.0", "environment": "client", "entrypoints": { "client": [ - "de.zonlykroks.advancedscripts.AdvancedScripts" + "de.steamwar.advancedscripts.AdvancedScripts" ] }, "mixins": [ @@ -20,7 +22,7 @@ ], "depends": { "fabricloader": ">=0.14.11", - "minecraft": ">=1.19.2", + "minecraft": ">=1.19.3", "java": ">=17" }, "accessWidener" : "advancedscripts.accesswidener" diff --git a/steamwarci.yml b/steamwarci.yml index ddd1f90..2498190 100644 --- a/steamwarci.yml +++ b/steamwarci.yml @@ -2,4 +2,4 @@ build: - "./gradlew remapJar" artifacts: - "/binarys/AdvancedScripts.jar": "build/libs/AdvancedScripts-1.0.0.jar" \ No newline at end of file + "/binarys/AdvancedScripts.jar": "build/libs/AdvancedScripts-2.0.0.jar" \ No newline at end of file