Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Velocity Proxy and bridge that connects server to the API #20

Open
wants to merge 18 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -10,9 +10,11 @@
<packaging>pom</packaging>

<modules>
<module>zander-proxy</module>
<module>zander-waterfall</module>
<module>zander-velocity</module>
<module>zander-hub</module>
<module>zander-auth</module>
<module>zander-bridge</module>
</modules>

<properties>
4 changes: 2 additions & 2 deletions zander-auth/pom.xml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>zander-auth</artifactId>
<version>1.0</version>
<version>1.3.0</version>

<repositories>
<!-- PaperMC/WaterFall -->
@@ -48,7 +48,7 @@
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
<version>[2.9.0,)</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
67 changes: 67 additions & 0 deletions zander-bridge/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>zander</artifactId>
<groupId>org.modularsoft</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zander-bridge</artifactId>
<version>1.3.0</version>

<repositories>
<!-- PaperMC/WaterFall -->
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
<!-- PaperMC -->
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.ModularEnigma</groupId>
<artifactId>Requests</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.modularsoft.zander.bridge;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.plugin.java.JavaPlugin;
import org.modularsoft.zander.bridge.util.api.Bridge;

public class ZanderBridgeMain extends JavaPlugin {
public static ZanderBridgeMain plugin;

@Override
public void onEnable() {
plugin = this;

// Init Message
TextComponent enabledMessage = Component.empty()
.color(NamedTextColor.GREEN)
.append(Component.text("\n\nZander Bridge has been enabled.\n"))
.append(Component.text("Running Version " + plugin.getDescription().getVersion() + "\n"))
.append(Component.text("GitHub Repository: https://github.com/ModularSoftAU/zander\n"))
.append(Component.text("Created by Modular Software\n\n", NamedTextColor.DARK_PURPLE));
getServer().sendMessage(enabledMessage);

// Create an instance of Bridge and start the task
Bridge bridge = new Bridge(this);
bridge.startBridgeTask();

saveDefaultConfig();
}

@Override
public void onDisable() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.modularsoft.zander.bridge.model;

import com.google.gson.Gson;
import lombok.Builder;
import lombok.Getter;

@Builder
public class BridgeProcess {

@Getter Integer bridgeId;

@Override
public String toString() {
return new Gson().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.modularsoft.zander.bridge.util.api;

import com.jayway.jsonpath.JsonPath;
import io.github.ModularEnigma.Request;
import io.github.ModularEnigma.Response;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.modularsoft.zander.bridge.ZanderBridgeMain;
import org.modularsoft.zander.bridge.model.BridgeProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Bridge {
// Initialize the logger
private static final Logger logger = LoggerFactory.getLogger(Bridge.class);

private final ZanderBridgeMain plugin;

// Constructor to get the instance of the plugin
public Bridge(ZanderBridgeMain plugin) {
this.plugin = plugin;
}

public void startBridgeTask() {
String BaseAPIURL = plugin.getConfig().getString("BaseAPIURL");
String APIKey = plugin.getConfig().getString("APIKey");
String TargetServerName = plugin.getConfig().getString("TargetServerName");

// Create a ScheduledExecutorService with a single thread
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

// Schedule the task to run every 60 seconds
scheduler.scheduleAtFixedRate(() -> {
try {
Request req = Request.builder()
.setURL(BaseAPIURL + "/bridge/get?targetServer=" + TargetServerName)
.setMethod(Request.Method.GET)
.addHeader("x-access-token", APIKey)
.build();

Response res = req.execute();
String json = res.getBody();

// Extract the list of data objects
List<Object> dataList = JsonPath.read(json, "$.data");

// Check if dataList is null or empty
if (dataList == null || dataList.isEmpty()) {
logger.info("No actions found for the bridge.");
return; // Exit the method early if there are no actions
}

// Loop through each entry in the data list
for (Object dataEntry : dataList) {
int bridgeId = JsonPath.read(dataEntry, "$.bridgeId");
String command = JsonPath.read(dataEntry, "$.command");

// Execute the command synchronously on the main server thread
Bukkit.getScheduler().runTask(plugin, () -> {
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
boolean executedSuccessfully = Bukkit.getServer().dispatchCommand(console, command);

if (executedSuccessfully) {
logger.info("Command executed successfully: {}", command);

// Mark the command as processed
try {
BridgeProcess bridgeProcess = BridgeProcess.builder()
.bridgeId(bridgeId)
.build();

Request bridgeProcessReq = Request.builder()
.setURL(BaseAPIURL + "/bridge/command/process")
.setMethod(Request.Method.POST)
.addHeader("x-access-token", APIKey)
.setRequestBody(bridgeProcess.toString())
.build();

Response bridgeProcessRes = bridgeProcessReq.execute();
logger.info("Response (" + bridgeProcessRes.getStatusCode() + "): " + bridgeProcessRes.getBody());
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to process command: {}", bridgeId);
}
} else {
logger.warn("Command execution failed: {}", command);
}
});
}

} catch (Exception e) {
// Handle exceptions here
e.printStackTrace();
logger.error("Fetching Bridge actions Failed.");
}
}, 0, 60, TimeUnit.SECONDS);
}
}
3 changes: 3 additions & 0 deletions zander-bridge/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BaseAPIURL: "http://localhost:8080/api"
APIKey: "KEY"
TargetServerName: "server"
5 changes: 5 additions & 0 deletions zander-bridge/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
main: org.modularsoft.zander.bridge.ZanderBridgeMain
name: zander-bridge
version: ${project.version}
author: ModularSoft
api-version: 1.19
2 changes: 1 addition & 1 deletion zander-hub/pom.xml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>zander-hub</artifactId>
<version>1.0</version>
<version>1.3.0</version>

<repositories>
<!-- PaperMC/WaterFall -->
90 changes: 90 additions & 0 deletions zander-velocity/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.modularsoft</groupId>
<artifactId>zander-velocity</artifactId>
<name>zander-velocity</name>
<version>1.3.0</version>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.MF</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>dev.dejvokep.boostedyaml</pattern>
<shadedPattern>org.modularsoft.zander.velocity.libs</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>maven2</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.4.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
</plugin>
</plugins>
</reporting>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<java.version>17</java.version>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Loading