Version 1.0
This commit is contained in:
parent
b69f525d24
commit
90ce309418
31
.classpath
Normal file
31
.classpath
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
23
.project
Normal file
23
.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>WooVelocity</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
52
pom.xml
Normal file
52
pom.xml
Normal file
@ -0,0 +1,52 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>WooVelocity</groupId>
|
||||
<artifactId>WooVelocity</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>WooVelocity</name>
|
||||
<url>https://git.jonttu.fi/Jonttu/WooVelocity</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<build>
|
||||
<defaultGoal>clean package</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>velocity</id>
|
||||
<url>https://repo.velocitypowered.com/snapshots/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.velocitypowered</groupId>
|
||||
<artifactId>velocity-api</artifactId>
|
||||
<version>1.1.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
5
resources/config.toml
Normal file
5
resources/config.toml
Normal file
@ -0,0 +1,5 @@
|
||||
[config]
|
||||
update_interval = 300
|
||||
url = https://flexplex.fi/
|
||||
key = 'very secret key'
|
||||
progressoffline = true
|
10
resources/velocity-plugin.json
Normal file
10
resources/velocity-plugin.json
Normal file
@ -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"
|
||||
}
|
57
src/main/java/fi/jonttu/woovelocity/WooVelocity.java
Normal file
57
src/main/java/fi/jonttu/woovelocity/WooVelocity.java
Normal file
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
39
src/main/java/fi/jonttu/woovelocity/util/Config.java
Normal file
39
src/main/java/fi/jonttu/woovelocity/util/Config.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
135
src/main/java/fi/jonttu/woovelocity/util/Connector.java
Normal file
135
src/main/java/fi/jonttu/woovelocity/util/Connector.java
Normal file
@ -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<String, JsonElement> 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<String, JsonElement> 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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user