geforkt von SteamWar/PersistentBungeeCore
Start of persistent system
Dieser Commit ist enthalten in:
Ursprung
4ec9e7ba51
Commit
b4f2a3b839
2
.gitignore
vendored
Normale Datei
2
.gitignore
vendored
Normale Datei
@ -0,0 +1,2 @@
|
||||
.idea
|
||||
target
|
55
pom.xml
Normale Datei
55
pom.xml
Normale Datei
@ -0,0 +1,55 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.steamwar</groupId>
|
||||
<artifactId>PersistentBungeeCore</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>PersistentBungeeCore</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>bungeecord-repo</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src</directory>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-api</artifactId>
|
||||
<version>1.12-SNAPSHOT</version>
|
||||
<type>jar</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
22
src/de/steamwar/bungeecore/Bauserver.java
Normale Datei
22
src/de/steamwar/bungeecore/Bauserver.java
Normale Datei
@ -0,0 +1,22 @@
|
||||
package de.steamwar.bungeecore;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Bauserver extends Subserver {
|
||||
|
||||
private final int id;
|
||||
private final UUID owner;
|
||||
|
||||
public Bauserver(Servertype type, String serverName, int id, UUID owner, int port, String... command) {
|
||||
super(type, serverName, port, command);
|
||||
this.id = id;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return id;
|
||||
}
|
||||
public UUID getOwner(){
|
||||
return owner;
|
||||
}
|
||||
}
|
44
src/de/steamwar/bungeecore/Persistent.java
Normale Datei
44
src/de/steamwar/bungeecore/Persistent.java
Normale Datei
@ -0,0 +1,44 @@
|
||||
package de.steamwar.bungeecore;
|
||||
|
||||
import net.md_5.bungee.api.event.ProxyReloadEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
public class Persistent extends Plugin implements Listener {
|
||||
|
||||
private static Persistent instance;
|
||||
private static String chatPrefix = "";
|
||||
private static String lobbyServer = "";
|
||||
|
||||
@Override
|
||||
public void onEnable(){
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
Subserver.shutdown();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onGReload(ProxyReloadEvent e){
|
||||
|
||||
}
|
||||
|
||||
public static void setLobbyServer(String lobbyServer) {
|
||||
Persistent.lobbyServer = lobbyServer;
|
||||
}
|
||||
public static void setChatPrefix(String prefix){
|
||||
chatPrefix = prefix;
|
||||
}
|
||||
public static String getLobbyServer() {
|
||||
return lobbyServer;
|
||||
}
|
||||
public static String getPrefix(){
|
||||
return chatPrefix;
|
||||
}
|
||||
public static Persistent getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
6
src/de/steamwar/bungeecore/Servertype.java
Normale Datei
6
src/de/steamwar/bungeecore/Servertype.java
Normale Datei
@ -0,0 +1,6 @@
|
||||
package de.steamwar.bungeecore;
|
||||
|
||||
public enum Servertype {
|
||||
BAUSERVER,
|
||||
ARENA
|
||||
}
|
171
src/de/steamwar/bungeecore/Subserver.java
Normale Datei
171
src/de/steamwar/bungeecore/Subserver.java
Normale Datei
@ -0,0 +1,171 @@
|
||||
package de.steamwar.bungeecore;
|
||||
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.event.ServerSwitchEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Subserver implements Runnable, Listener {
|
||||
|
||||
private static final List<Subserver> serverList = new LinkedList<>();
|
||||
private static final Logger logger = ProxyServer.getInstance().getLogger();
|
||||
|
||||
public static Subserver getSubserver(ProxiedPlayer p){
|
||||
for(Subserver s : serverList){
|
||||
if(s.onServer(p))
|
||||
return s;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private final String serverName;
|
||||
private Process p;
|
||||
private PrintWriter writer;
|
||||
private ServerInfo server;
|
||||
private Servertype type;
|
||||
private boolean started;
|
||||
|
||||
private final List<ProxiedPlayer> cachedPlayers = new ArrayList<>();
|
||||
private final List<ProxiedPlayer> players = new ArrayList<>();
|
||||
|
||||
public Subserver(Servertype type, String serverName, int port, String... command){
|
||||
this.started = false;
|
||||
this.serverName = serverName;
|
||||
this.type = type;
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(command);
|
||||
InetSocketAddress address = new InetSocketAddress("127.0.0.1", port);
|
||||
|
||||
try{
|
||||
this.p = pb.start();
|
||||
}catch(IOException e){
|
||||
logger.log(Level.SEVERE, "Server could not be started", e);
|
||||
return;
|
||||
}
|
||||
|
||||
this.server = ProxyServer.getInstance().constructServerInfo(
|
||||
serverName, address, "SteamWar.de - Subserver", false);
|
||||
this.writer = new PrintWriter(p.getOutputStream(), true);
|
||||
|
||||
ProxyServer.getInstance().getScheduler().runAsync(Persistent.getInstance(), this);
|
||||
}
|
||||
|
||||
public ServerInfo getServer(){
|
||||
return server;
|
||||
}
|
||||
|
||||
public void sendPlayer(ProxiedPlayer p){
|
||||
if(!started){
|
||||
p.sendMessage(Persistent.getPrefix() + "§7Der Server wird gestartet, einen Moment bitte...");
|
||||
cachedPlayers.add(p);
|
||||
}else{
|
||||
p.connect(server);
|
||||
players.add(p);
|
||||
}
|
||||
}
|
||||
|
||||
static void shutdown(){
|
||||
try {
|
||||
while (!serverList.isEmpty()) {
|
||||
Subserver server = serverList.get(0);
|
||||
server.writer.println("stop");
|
||||
server.p.waitFor();
|
||||
}
|
||||
}catch(InterruptedException e){
|
||||
logger.log(Level.SEVERE, "Subserver shutdown interrupted!", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean onServer(ProxiedPlayer p){
|
||||
return cachedPlayers.contains(p) || players.contains(p);
|
||||
}
|
||||
|
||||
private void fatalError(){
|
||||
for(ProxiedPlayer cached : cachedPlayers){
|
||||
cached.sendMessage(Persistent.getPrefix() + "§cUnerwarteter Fehler beim Serverstart.");
|
||||
}
|
||||
for(ProxiedPlayer player : server.getPlayers()){
|
||||
player.connect(ProxyServer.getInstance().getServerInfo(Persistent.getLobbyServer()));
|
||||
player.sendMessage(Persistent.getPrefix() + "§cUnerwarteter Fehler im Server.");
|
||||
}
|
||||
}
|
||||
|
||||
public void run(){
|
||||
ProxyServer.getInstance().getServers().put(serverName, server);
|
||||
ProxyServer.getInstance().getPluginManager().registerListener(Persistent.getInstance(), this);
|
||||
serverList.add(this);
|
||||
|
||||
try(BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))){
|
||||
String line = "";
|
||||
while (!started && (line = reader.readLine()) != null) {
|
||||
started = line.contains(" INFO]: Done ("); //Problematisch
|
||||
}
|
||||
|
||||
if(line == null){
|
||||
logger.log(Level.SEVERE, "Subserver {0} stopped to early!", serverName);
|
||||
fatalError();
|
||||
return;
|
||||
}
|
||||
|
||||
Thread.sleep(800);
|
||||
|
||||
for(ProxiedPlayer cachedPlayer : cachedPlayers){
|
||||
Thread.sleep(200);
|
||||
sendPlayer(cachedPlayer);
|
||||
}
|
||||
cachedPlayers.clear();
|
||||
|
||||
p.waitFor();
|
||||
} catch(IOException e) {
|
||||
logger.log(Level.SEVERE, "Server " + serverName + " was interrupted!", e);
|
||||
fatalError();
|
||||
} catch(InterruptedException e) {
|
||||
logger.log(Level.SEVERE, "Server " + serverName + " was interrupted!", e);
|
||||
fatalError();
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
serverList.remove(this);
|
||||
ProxyServer.getInstance().getServers().remove(serverName);
|
||||
ProxyServer.getInstance().getPluginManager().unregisterListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onServerSwitchEvent(ServerSwitchEvent e){
|
||||
ProxiedPlayer player = e.getPlayer();
|
||||
if(player.getServer().getInfo().equals(server))
|
||||
return;
|
||||
|
||||
Iterator<ProxiedPlayer> it = cachedPlayers.iterator();
|
||||
while(it.hasNext()){
|
||||
if(it.next().equals(player)){
|
||||
it.remove();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
it = players.iterator();
|
||||
while(it.hasNext()){
|
||||
if(it.next().equals(player)){
|
||||
it.remove();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
4
src/plugin.yml
Normale Datei
4
src/plugin.yml
Normale Datei
@ -0,0 +1,4 @@
|
||||
name: PersistentBungeeCore
|
||||
main: de.steamwar.bungeecore.Persistent
|
||||
version: 1.0
|
||||
author: Lixfel
|
In neuem Issue referenzieren
Einen Benutzer sperren