From 90ce309418edf31ab32b4936ff55a3328859b1a5 Mon Sep 17 00:00:00 2001
From: Jonttuuu <50843099+Jonttuuu@users.noreply.github.com>
Date: Sat, 31 Jul 2021 02:39:12 +0300
Subject: [PATCH] Version 1.0
---
.classpath | 31 ++++
.project | 23 +++
pom.xml | 52 +++++++
resources/config.toml | 5 +
resources/velocity-plugin.json | 10 ++
.../fi/jonttu/woovelocity/WooVelocity.java | 57 ++++++++
.../fi/jonttu/woovelocity/util/Config.java | 39 +++++
.../fi/jonttu/woovelocity/util/Connector.java | 135 ++++++++++++++++++
8 files changed, 352 insertions(+)
create mode 100644 .classpath
create mode 100644 .project
create mode 100644 pom.xml
create mode 100644 resources/config.toml
create mode 100644 resources/velocity-plugin.json
create mode 100644 src/main/java/fi/jonttu/woovelocity/WooVelocity.java
create mode 100644 src/main/java/fi/jonttu/woovelocity/util/Config.java
create mode 100644 src/main/java/fi/jonttu/woovelocity/util/Connector.java
diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..7fb1181
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.project b/.project
new file mode 100644
index 0000000..a66e20c
--- /dev/null
+++ b/.project
@@ -0,0 +1,23 @@
+
+
+ WooVelocity
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.m2e.core.maven2Nature
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..f02d69f
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,52 @@
+
+ 4.0.0
+ WooVelocity
+ WooVelocity
+ 0.0.1-SNAPSHOT
+ WooVelocity
+ https://git.jonttu.fi/Jonttu/WooVelocity
+
+ UTF-8
+
+
+ clean package
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 1.6
+
+
+ 1.8
+
+
+
+ package
+
+ shade
+
+
+
+
+
+
+
+ resources
+
+
+
+
+
+ velocity
+ https://repo.velocitypowered.com/snapshots/
+
+
+
+
+ com.velocitypowered
+ velocity-api
+ 1.1.1-SNAPSHOT
+ provided
+
+
+
\ No newline at end of file
diff --git a/resources/config.toml b/resources/config.toml
new file mode 100644
index 0000000..a043d24
--- /dev/null
+++ b/resources/config.toml
@@ -0,0 +1,5 @@
+[config]
+update_interval = 300
+url = https://flexplex.fi/
+key = 'very secret key'
+progressoffline = true
\ No newline at end of file
diff --git a/resources/velocity-plugin.json b/resources/velocity-plugin.json
new file mode 100644
index 0000000..436c1af
--- /dev/null
+++ b/resources/velocity-plugin.json
@@ -0,0 +1,10 @@
+{
+ "id":"woovelocity",
+ "name":"WooVelocity",
+ "version":"1.0",
+ "description":"WooMinecraft Velocity plugin!",
+ "url":"https://jonttu.fi",
+ "authors":["Jonttu1237"],
+ "dependencies":[],
+ "main":"fi.jonttu.woovelocity.WooVelocity"
+}
\ No newline at end of file
diff --git a/src/main/java/fi/jonttu/woovelocity/WooVelocity.java b/src/main/java/fi/jonttu/woovelocity/WooVelocity.java
new file mode 100644
index 0000000..c80859e
--- /dev/null
+++ b/src/main/java/fi/jonttu/woovelocity/WooVelocity.java
@@ -0,0 +1,57 @@
+package fi.jonttu.woovelocity;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+
+import com.google.inject.Inject;
+import com.moandjiezana.toml.Toml;
+import com.velocitypowered.api.event.Subscribe;
+import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
+import com.velocitypowered.api.proxy.ProxyServer;
+
+import fi.jonttu.woovelocity.util.Config;
+import fi.jonttu.woovelocity.util.Connector;
+
+public class WooVelocity {
+
+ private static WooVelocity instance;
+
+ public static WooVelocity getInstance() {
+ return instance;
+ }
+
+ private final ProxyServer server;
+ private final Logger logger;
+ private Toml config;
+
+ public ProxyServer getServer() {
+ return this.server;
+ }
+
+ public Logger getLogger() {
+ return this.logger;
+ }
+
+ public Toml getConfig() {
+ return this.config;
+ }
+
+ @Inject
+ public WooVelocity(ProxyServer server, Logger logger) {
+ instance = this;
+ this.server = server;
+ this.logger = logger;
+ }
+
+ @Subscribe
+ public void proxyInitialize(ProxyInitializeEvent event) {
+
+ this.config = Config.loadConfig();
+
+ this.server.getScheduler().buildTask(this, new Runnable() {
public void run() {
Connector.update();
}
})
+ .repeat(config.getLong("config.update_interval"), TimeUnit.SECONDS)
+ .schedule();
+
+ }
+
+}
diff --git a/src/main/java/fi/jonttu/woovelocity/util/Config.java b/src/main/java/fi/jonttu/woovelocity/util/Config.java
new file mode 100644
index 0000000..82e84b5
--- /dev/null
+++ b/src/main/java/fi/jonttu/woovelocity/util/Config.java
@@ -0,0 +1,39 @@
+package fi.jonttu.woovelocity.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+
+import com.moandjiezana.toml.Toml;
+
+import fi.jonttu.woovelocity.WooVelocity;
+
+public final class Config {
+
+ public final static Toml loadConfig() {
+ final File file = new File("plugins/WooVelocity/config.toml");
+ if (! file.getParentFile().exists()) {
+ file.getParentFile().mkdirs();
+ }
+
+ if (! file.exists()) {
+ try {
+ final InputStream input = WooVelocity.getInstance().getClass().getResourceAsStream("/" + file.getName());
+ if (input != null) {
+ Files.copy(input, file.toPath());
+ } else {
+ file.createNewFile();
+ }
+ } catch (final IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ final Toml toml = new Toml();
+ toml.read(file);
+ return toml;
+ }
+
+}
diff --git a/src/main/java/fi/jonttu/woovelocity/util/Connector.java b/src/main/java/fi/jonttu/woovelocity/util/Connector.java
new file mode 100644
index 0000000..e0aea6f
--- /dev/null
+++ b/src/main/java/fi/jonttu/woovelocity/util/Connector.java
@@ -0,0 +1,135 @@
+package fi.jonttu.woovelocity.util;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+import fi.jonttu.woovelocity.WooVelocity;
+
+public final class Connector {
+
+ public static boolean update() {
+ try {
+ final BufferedReader bReader = new BufferedReader(
+ new InputStreamReader(
+ new URL(
+ WooVelocity.getInstance().getConfig().getString("config.url")
+ + "?wmc_key="
+ + WooVelocity.getInstance().getConfig().getString("config.key")
+ ).openConnection().getInputStream()
+ )
+ );
+
+ String data = "";
+ String line = null;
+ while ((line = bReader.readLine()) != null) {
+ if (data.equals("")) {
+ data = line;
+ } else {
+ data = data + "\n" + line;
+ }
+ }
+
+ final JsonObject o = JsonParser.parseString(data).getAsJsonObject();
+ if (! o.get("success").getAsBoolean()) {
+ return false;
+ }
+ final JsonArray processedData = new JsonArray();
+ if (! (o.get("data") instanceof JsonObject)) {
+ return true;
+ }
+
+ for (final Map.Entry entry : o.get("data").getAsJsonObject().entrySet()) {
+ WooVelocity.getInstance().getLogger().info("Got new unprocessed orders");
+ final String username = entry.getKey();
+ String orderId = null;
+ if (allowUserProcessing(username)) {
+
+ for (final Map.Entry e : o.get("data").getAsJsonObject().get(username).getAsJsonObject().entrySet()) {
+ orderId = e.getKey();
+ }
+
+ processedData.add(Integer.parseInt(orderId));
+
+ for (int i = 0; i < o.get("data").getAsJsonObject().get(orderId).getAsJsonArray().size(); i++) {
+ String command = o.get("data").getAsJsonObject().get(username).getAsJsonObject().get(orderId).getAsJsonArray().get(i).getAsString();
+
+ // This fixes command formats that come from wordpress
+ command = command.replaceAll(""", "\"");
+
+ WooVelocity.getInstance().getServer().getCommandManager().executeAsync(
+ WooVelocity.getInstance().getServer().getConsoleCommandSource(),
+ command
+ );
+
+ WooVelocity.getInstance().getLogger().info("Executed command '" + command + "'");
+ }
+
+ }
+ }
+
+ if (1 > processedData.size()) {
+ return false;
+ }
+
+ if (! sendProcessedOrders(processedData.toString())) {
+ return false;
+ }
+
+ bReader.close();
+
+ return true;
+ } catch (final Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ // Check if processing server update and sending command is allowed at a moment
+ private static boolean allowUserProcessing(final String name) {
+ if (WooVelocity.getInstance().getConfig().getBoolean("config.progressoffline")) {
+ return true;
+ }
+ if (WooVelocity.getInstance().getServer().getPlayer(name).isPresent()) {
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean sendProcessedOrders(final String json) {
+ String data = "";
+
+ try {
+ final BufferedReader bReader = new BufferedReader(
+ new InputStreamReader(
+ new URL(
+ WooVelocity.getInstance().getConfig().getString("config.url")
+ + "&processedOrders="
+ + json
+ ).openConnection().getInputStream()
+ )
+ );
+ String line = null;
+ while ((line = bReader.readLine()) != null) {
+ if (data.equals("")) {
+ data = line;
+ } else {
+ data = data + "\n" + line;
+ }
+ }
+ bReader.close();
+ } catch (final Exception e) {
+ e.printStackTrace();
+ }
+
+ final JsonObject o = JsonParser.parseString(data).getAsJsonObject();
+ return o.get("success").getAsBoolean();
+ }
+
+}