Skip to content

Commit

Permalink
feat: console tab
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed May 25, 2024
1 parent 274046a commit 4d47ed7
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 62 deletions.
5 changes: 5 additions & 0 deletions Allay-Server/src/main/java/org/allaymc/server/Allay.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.allaymc.server.entity.registry.AllayEntityTypeRegistry;
import org.allaymc.server.entity.type.AllayEntityType;
import org.allaymc.server.eventbus.AllayEventBus;
import org.allaymc.server.gui.Dashboard;
import org.allaymc.server.i18n.AllayI18N;
import org.allaymc.server.i18n.AllayI18nLoader;
import org.allaymc.server.item.attribute.AllayVanillaItemAttributeRegistry;
Expand All @@ -69,11 +70,15 @@
public final class Allay {

public static final DynamicURLClassLoader EXTRA_RESOURCE_CLASS_LOADER = new DynamicURLClassLoader(Allay.class.getClassLoader());
public static Dashboard DASHBOARD;

public static void main(String[] args) {
long startTime = System.currentTimeMillis();
System.setProperty("joml.format", "false"); // set JOML vectors are output without a scientific notation
System.setProperty("log4j2.contextSelector", AsyncLoggerContextSelector.class.getName()); // enable async logging
if (Server.SETTINGS.genericSettings().enableGui()) {
DASHBOARD = Dashboard.getInstance();
}
log.info("Starting Allay...");
try {
initAllayAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ public final class AllayServer implements Server {
})
.build();

private Dashboard dashboard;

private AllayServer() {}

public static AllayServer getInstance() {
Expand Down Expand Up @@ -193,9 +191,7 @@ public void run() {
networkServer.start();
startTime = System.currentTimeMillis();
sendTr(TrKeys.A_NETWORK_SERVER_STARTED, SETTINGS.networkSettings().ip(), String.valueOf(SETTINGS.networkSettings().port()), String.valueOf(startTime - timeMillis));
if (SETTINGS.genericSettings().enableGui()) {
dashboard = Dashboard.getInstance();
}
if (SETTINGS.genericSettings().enableGui()) Allay.DASHBOARD.serverStarted();
gameLoop.startLoop();
}

Expand Down
83 changes: 83 additions & 0 deletions Allay-Server/src/main/java/org/allaymc/server/gui/ANSIColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.allaymc.server.gui;

import lombok.Getter;

import java.awt.*;
import java.util.regex.Pattern;

/**
* Allay Project 2024/5/25
*
* @author daoge_cmd
*/
public enum ANSIColor {
// Normal colors
BLACK("(0;)?30(0;)?m", Color.getHSBColor(0.000f, 0.000f, 0.000f)),
RED("(0;)?31(0;)?m", Color.getHSBColor(0.000f, 1.000f, 0.502f)),
GREEN("(0;)?32(0;)?m", Color.getHSBColor(0.333f, 1.000f, 0.502f)),
YELLOW("(0;)?33(0;)?m", Color.getHSBColor(0.167f, 1.000f, 0.502f)),
BLUE("(0;)?34(0;)?m", Color.getHSBColor(0.667f, 1.000f, 0.502f)),
MAGENTA("(0;)?35(0;)?m", Color.getHSBColor(0.833f, 1.000f, 0.502f)),
CYAN("(0;)?36(0;)?m", Color.getHSBColor(0.500f, 1.000f, 0.502f)),
WHITE("(0;)?37(0;)?m", Color.getHSBColor(0.000f, 0.000f, 0.753f)),

// Bold colors
B_BLACK("(1;30|30;1)m", Color.getHSBColor(0.000f, 0.000f, 0.502f)),
B_RED("(1;31|31;1)m", Color.getHSBColor(0.000f, 1.000f, 1.000f)),
B_GREEN("(1;32|32;1)m", Color.getHSBColor(0.333f, 1.000f, 1.000f)),
B_YELLOW("(1;33|33;1)m", Color.getHSBColor(0.167f, 1.000f, 1.000f)),
B_BLUE("(1;34|34;1)m", Color.getHSBColor(0.667f, 1.000f, 1.000f)),
B_MAGENTA("(1;35|35;1)m", Color.getHSBColor(0.833f, 1.000f, 1.000f)),
B_CYAN("(1;36|36;1)m", Color.getHSBColor(0.500f, 1.000f, 1.000f)),
B_WHITE("(1;37|37;1)m", Color.getHSBColor(0.000f, 0.000f, 1.000f)),

RESET("0m", Color.getHSBColor(0.000f, 0.000f, 1.000f));

private static final ANSIColor[] VALUES = values();
private static final String PREFIX = Pattern.quote("\u001B[");

private final String ANSICode;

@Getter
private final Color color;

ANSIColor(String ANSICode, Color color) {
this.ANSICode = ANSICode;
this.color = color;
}

public static ANSIColor fromANSI(String code) {
for (ANSIColor value : VALUES) {
if (code.matches(PREFIX + value.ANSICode)) {
return value;
}
}

return B_WHITE;
}
}
128 changes: 128 additions & 0 deletions Allay-Server/src/main/java/org/allaymc/server/gui/ConsolePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.allaymc.server.gui;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.Serial;

/**
* Allay Project 2024/5/25
*
* @author daoge_cmd
*
* This class was based on this <a href="https://stackoverflow.com/a/6899478/5299903">code</a>
*/
@Slf4j
public class ConsolePanel extends JTextPane {

@Serial
private static final long serialVersionUID = 1L;

private static Color colorCurrent = ANSIColor.RESET.getColor();
private String remaining = "";

/**
* Append the given string in the given color to the text pane
* @param c The color
* @param s The text
*/
private void append(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();

try {
getDocument().insertString(len, s, aset);
} catch (BadLocationException e) {
log.error("Error while appending text to console", e);
}
}

/**
* Extract the ANSI color codes from the string and add each part to the text pane
*
* @param s The text to parse
*/
public void appendANSI(String s) { // convert ANSI color codes first
int aPos = 0; // current char position in addString
int aIndex; // index of next Escape sequence
int mIndex; // index of "m" terminating Escape sequence
String tmpString;
boolean stillSearching = true; // true until no more Escape sequences
String addString = remaining + s;
remaining = "";

if (!addString.isEmpty()) {
aIndex = addString.indexOf("\u001B"); // find first escape
if (aIndex == -1) { // no escape/color change in this string, so just send it with current color
append(colorCurrent, addString);
return;
}
// otherwise There is an escape character in the string, so we must process it

if (aIndex > 0) { // Escape is not first char, so send text up to first escape
tmpString = addString.substring(0, aIndex);
append(colorCurrent, tmpString);
aPos = aIndex; // aPos is now at the beginning of the first escape sequence
}


// while there's text in the input buffer
while (stillSearching) {
mIndex = addString.indexOf("m", aPos); // find the end of the escape sequence
if (mIndex < 0) { // the buffer ends halfway through the ansi string!
remaining = addString.substring(aPos);
stillSearching = false;
continue;
} else {
tmpString = addString.substring(aPos, mIndex+1);
colorCurrent = ANSIColor.fromANSI(tmpString).getColor();
}
aPos = mIndex + 1;
// now we have the color, send text that is in that color (up to next escape)

aIndex = addString.indexOf("\u001B", aPos);

if (aIndex == -1) { // if that was the last sequence of the input, send remaining text
tmpString = addString.substring(aPos);
append(colorCurrent, tmpString);
stillSearching = false;
continue; // jump out of loop early, as the whole string has been sent now
}

// there is another escape sequence, so send part of the string and prepare for the next
tmpString = addString.substring(aPos, aIndex);
aPos = aIndex;
append(colorCurrent, tmpString);

}
}
}
}
70 changes: 68 additions & 2 deletions Allay-Server/src/main/java/org/allaymc/server/gui/Dashboard.form
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@
<properties/>
<border type="none"/>
<children>
<grid id="b9d5d" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Console"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="a4de8">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="d81e5" class="org.allaymc.server.gui.ConsolePanel" binding="consolePane" custom-create="true">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
<component id="5821d" class="javax.swing.JTextField" binding="cmdInput">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
</component>
</children>
</grid>
<grid id="a3704" binding="perfTab" layout-manager="FormLayout">
<rowspec value="center:d:grow"/>
<colspec value="fill:d:grow"/>
Expand All @@ -26,7 +59,7 @@
<properties/>
<border type="none"/>
<children>
<tabbedpane id="6a40b" binding="tabbedPane1" default-binding="true">
<tabbedpane id="6a40b" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
Expand All @@ -50,6 +83,39 @@
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="555d" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Chunk"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1a2ca" class="org.allaymc.server.gui.GraphPanel" binding="chunkGraph" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="a7817" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Entity"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6c4ed" class="org.allaymc.server.gui.GraphPanel" binding="entityGraph" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
Expand Down Expand Up @@ -92,7 +158,7 @@
</scrollpane>
</children>
</grid>
<grid id="32e90" binding="pluginTab" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="32e90" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Plugins"/>
Expand Down
Loading

0 comments on commit 4d47ed7

Please sign in to comment.