Skip to content

Commit d9d1092

Browse files
author
Tobias de Bruijn
committed
Initial release
1 parent a95f2ff commit d9d1092

29 files changed

+789
-92
lines changed

build.gradle

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ plugins {
88
allprojects {
99

1010
version = pluginVersion
11-
group = pluginGroup
1211

1312
sourceCompatibility = 1.11
1413
targetCompatibility = 1.11
@@ -22,19 +21,17 @@ allprojects {
2221
processResources {
2322
from(sourceSets.main.resources.srcDirs) {
2423
filter ReplaceTokens, tokens: [version: version]
25-
filter ReplaceTokens, tokens: [name: pluginName]
26-
filter ReplaceTokens, tokens: [mainClassName: mainClassName]
27-
filter ReplaceTokens, tokens: [groupId: pluginGroup]
28-
filter ReplaceTokens, tokens: [author: pluginAuthor]
29-
filter ReplaceTokens, tokens: [apiVersion: pluginApiVersion]
3024
}
3125
}
3226
}
3327

3428
dependencies {
35-
3629
//Spigot API
37-
compileOnly 'org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT'
30+
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
31+
32+
compileOnly group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8.1'
33+
compileOnly group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.8.1'
34+
3835
}
3936

4037
task testJar(type: Jar) {

gradle.properties

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
pluginVersion = 1.0
2-
pluginGroup = nl.thedutchmc.basebukkitplugin
3-
mainClassName = BaseBukkitPlugin
4-
pluginName = BaseBukkitPlugin
5-
pluginAuthor = TheDutchMC
6-
pluginApiVersion = 1.16
1+
pluginVersion = 0.1

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* in the user guide at https://docs.gradle.org/5.1.1/userguide/multi_project_builds.html
88
*/
99

10-
rootProject.name = pluginName
10+
rootProject.name = 'rConsole'

src/main/java/nl/thedutchmc/BaseBukkitPlugin/BaseBukkitPlugin.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/nl/thedutchmc/BaseBukkitPlugin/ConfigurationHandler.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package nl.thedutchmc.rconsole;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.net.Socket;
6+
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
9+
import com.google.gson.Gson;
10+
11+
import nl.thedutchmc.rconsole.config.Configuration;
12+
import nl.thedutchmc.rconsole.gson.out.ServerShutdownPacket;
13+
import nl.thedutchmc.rconsole.tcp.TcpClient;
14+
import nl.thedutchmc.rconsole.tcp.TcpServer;
15+
16+
public class RConsole extends JavaPlugin {
17+
18+
private static RConsole INSTANCE;
19+
private static volatile boolean DEBUG = false;
20+
private static volatile boolean IS_RUNNING = true;
21+
22+
private TcpServer tcpServer;
23+
24+
@Override
25+
public void onEnable() {
26+
RConsole.INSTANCE = this;
27+
28+
Configuration config = new Configuration(this);
29+
RConsole.DEBUG = config.getConfig().isDebugMode();
30+
31+
this.tcpServer = new TcpServer(config.getConfig().getListenPort(), config.getConfig().getTokens(), this);
32+
new Thread(tcpServer, "rConsole-TCPServer-Thread").start();
33+
}
34+
35+
@Override
36+
public void onDisable() {
37+
38+
String packetSerialized = new Gson().toJson(new ServerShutdownPacket());
39+
for(TcpClient client : this.tcpServer.getSignedInClients()) {
40+
RConsole.logDebug(String.format("Disconnecting client '%s'", client.getName()));
41+
Socket socket = client.getSocket();
42+
43+
if(socket.isClosed()) {
44+
continue;
45+
}
46+
47+
try {
48+
PrintWriter pw = new PrintWriter(socket.getOutputStream());
49+
pw.println(packetSerialized);
50+
pw.flush();
51+
pw.close();
52+
} catch(IOException e) {
53+
}
54+
}
55+
}
56+
57+
public static void logInfo(Object log) {
58+
RConsole.INSTANCE.getLogger().info(log.toString());
59+
}
60+
61+
public static void logWarn(Object log) {
62+
RConsole.INSTANCE.getLogger().warning(log.toString());
63+
}
64+
65+
public static void logDebug(Object log) {
66+
if(!RConsole.DEBUG) return;
67+
68+
RConsole.INSTANCE.getLogger().info("[DEBUG] " + log.toString());
69+
}
70+
71+
public static boolean getIsRunning() {
72+
return RConsole.IS_RUNNING;
73+
}
74+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package nl.thedutchmc.rconsole;
2+
3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
5+
6+
public class Util {
7+
8+
public static String getStackTrace(Throwable e) {
9+
StringWriter sw = new StringWriter();
10+
PrintWriter pw = new PrintWriter(sw, true);
11+
12+
e.printStackTrace(pw);
13+
return sw.getBuffer().toString();
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package nl.thedutchmc.rconsole.annotations;
2+
3+
import java.lang.annotation.Documented;
4+
5+
/**
6+
* The returned value can be null, so you should do a null-check probably
7+
*/
8+
@Documented
9+
public @interface Nullable {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package nl.thedutchmc.rconsole.config;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import org.yaml.snakeyaml.Yaml;
7+
8+
import com.google.gson.Gson;
9+
10+
import nl.thedutchmc.rconsole.RConsole;
11+
import nl.thedutchmc.rconsole.config.gson.ConfigObject;
12+
13+
public class Configuration {
14+
15+
private ConfigObject configObj;
16+
17+
public Configuration(RConsole plugin) {
18+
19+
loadConfig(plugin);
20+
}
21+
22+
private void loadConfig(RConsole plugin) {
23+
File configFile = new File(plugin.getDataFolder(), "config.yml");
24+
25+
if(!configFile.exists()) {
26+
configFile.getParentFile().mkdirs();
27+
plugin.saveResource("config.yml", false);
28+
}
29+
30+
final Yaml yaml = new Yaml();
31+
final Gson gson = new Gson();
32+
33+
Object loadedYaml;
34+
try {
35+
loadedYaml = yaml.load(new FileReader(configFile));
36+
} catch(FileNotFoundException e) {
37+
return;
38+
}
39+
40+
String json = gson.toJson(loadedYaml, java.util.LinkedHashMap.class);
41+
this.configObj = gson.fromJson(json, ConfigObject.class);
42+
}
43+
44+
public ConfigObject getConfig() {
45+
return this.configObj;
46+
}
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package nl.thedutchmc.rconsole.config.gson;
2+
3+
public class ConfigObject {
4+
private boolean debugMode;
5+
private int listenPort;
6+
private TokenObject[] tokens;
7+
/**
8+
* @return the debugMode
9+
*/
10+
public boolean isDebugMode() {
11+
return debugMode;
12+
}
13+
/**
14+
* @return the listenPort
15+
*/
16+
public int getListenPort() {
17+
return listenPort;
18+
}
19+
/**
20+
* @return the tokens
21+
*/
22+
public TokenObject[] getTokens() {
23+
return tokens;
24+
}
25+
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package nl.thedutchmc.rconsole.config.gson;
2+
3+
import nl.thedutchmc.rconsole.gson.Scope;
4+
5+
public class TokenObject {
6+
private String name, token;
7+
private Scope[] scopes;
8+
9+
/**
10+
* @return the name
11+
*/
12+
public String getName() {
13+
return name;
14+
}
15+
/**
16+
* @return the token
17+
*/
18+
public String getToken() {
19+
return token;
20+
}
21+
/**
22+
* @return the scopes
23+
*/
24+
public Scope[] getScopes() {
25+
return scopes;
26+
}
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package nl.thedutchmc.rconsole.features;
2+
3+
public enum Feature {
4+
READ_CONSOLE
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package nl.thedutchmc.rconsole.features.readconsole;
2+
3+
import org.apache.logging.log4j.core.LogEvent;
4+
import org.apache.logging.log4j.core.appender.AbstractAppender;
5+
import org.apache.logging.log4j.core.layout.PatternLayout;
6+
7+
public class ConsoleAppender extends AbstractAppender {
8+
9+
private ReadConsole readConsole;
10+
11+
protected ConsoleAppender(ReadConsole readConsole) {
12+
super("rconsole-appender", null, PatternLayout.createDefaultLayout());
13+
this.readConsole = readConsole;
14+
super.start();
15+
}
16+
17+
@Override
18+
public void append(LogEvent event) {
19+
LogEvent eventImmutable = event.toImmutable();
20+
21+
this.readConsole.send(
22+
eventImmutable.getMessage().getFormattedMessage(),
23+
eventImmutable.getTimeMillis() / 1000,
24+
eventImmutable.getLevel().name(),
25+
eventImmutable.getThreadName());
26+
}
27+
}

0 commit comments

Comments
 (0)