From 22b320274df7173c4d7c26fb81c150421416f4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez=20Rinc=C3=B3n?= Date: Thu, 14 Sep 2017 19:10:36 +0200 Subject: [PATCH] Previous to stages --- .../playerbalancer/PlayerBalancer.java | 1 + .../playerbalancer/section/SectionLoader.java | 5 ++ .../section/SectionManager.java | 61 +++++++++++------ .../utils/{FileInfo.java => BuildInfo.java} | 2 +- .../playerbalancer/utils/FakeServer.java | 65 +++++++++++++++++++ 5 files changed, 112 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/jaimemartz/playerbalancer/section/SectionLoader.java rename src/main/java/com/jaimemartz/playerbalancer/utils/{FileInfo.java => BuildInfo.java} (90%) create mode 100644 src/main/java/com/jaimemartz/playerbalancer/utils/FakeServer.java diff --git a/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java b/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java index 00cf86b..f32c751 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java +++ b/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java @@ -64,6 +64,7 @@ public class PlayerBalancer extends Plugin { try { CommentedConfigurationNode node = loader.load(); settings = node.getValue(TypeToken.of(SettingsHolder.class)); + System.out.println(settings); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/jaimemartz/playerbalancer/section/SectionLoader.java b/src/main/java/com/jaimemartz/playerbalancer/section/SectionLoader.java new file mode 100644 index 0000000..9e99bad --- /dev/null +++ b/src/main/java/com/jaimemartz/playerbalancer/section/SectionLoader.java @@ -0,0 +1,5 @@ +package com.jaimemartz.playerbalancer.section; + +public class SectionLoader { + private final +} diff --git a/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java b/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java index 76c446a..864b731 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java +++ b/src/main/java/com/jaimemartz/playerbalancer/section/SectionManager.java @@ -3,13 +3,14 @@ package com.jaimemartz.playerbalancer.section; import com.google.common.base.Preconditions; import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps; +import com.jaimemartz.playerbalancer.settings.props.shared.SectionProps; +import com.jaimemartz.playerbalancer.utils.FakeServer; import com.jaimemartz.playerbalancer.utils.FixedAdapter; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.scheduler.ScheduledTask; -import java.net.InetSocketAddress; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; @@ -29,7 +30,7 @@ public class SectionManager { } public void load() throws RuntimeException { - plugin.getLogger().info("Loading sections from the config, this may take a while..."); + plugin.getLogger().info("Executing section initialization stages, this may take a while..."); long starting = System.currentTimeMillis(); props.getSectionProps().forEach((name, prop) -> { @@ -119,6 +120,21 @@ public class SectionManager { return getByServer(server.getInfo()); } + private final Stage[] stages = { + new Stage("Creation") { + @Override + public void execute(SectionProps props, ServerSection section) throws RuntimeException { + + } + }, + new Stage("") { + @Override + public void execute(SectionProps props, ServerSection section) throws RuntimeException { + + } + } + }; + public void processSection(String sectionName, ServerSection section) throws RuntimeException { plugin.getLogger().info(String.format("Loading section with name \"%s\"", sectionName)); @@ -143,29 +159,14 @@ public class SectionManager { Set servers = calculateServers(section); section.getServers().addAll(servers); - //TODO move this to other stage - if (section.getProps().getProvider() != null) { - section.setInherited(false); - } else { - section.setInherited(true); - - if (section.getImplicitProvider() != null) { - - } else { - throw new IllegalStateException(String.format("The section \"%s\" does not have a provider", sectionName)); - } - } + //TODO Load sections section.setPosition(calculatePosition(section)); Optional.ofNullable(section.getProps().getServerName()).ifPresent(serverName -> { - int port = (int) Math.floor(Math.random() * (0xFFFF + 1)); //Get a random valid port for our fake server - ServerInfo server = plugin.getProxy().constructServerInfo( - "@" + serverName, - new InetSocketAddress("0.0.0.0", port), - String.format("Server of Section %s", sectionName), - false); + FakeServer server = new FakeServer(section); section.setServer(server); + plugin.getSectionManager().register(server, section); FixedAdapter.getFakeServers().put(server.getName(), server); plugin.getProxy().getServers().put(server.getName(), server); @@ -174,6 +175,7 @@ public class SectionManager { Optional.ofNullable(section.getProps().getCommand()).ifPresent(props -> { SectionCommand command = new SectionCommand(plugin, props, section); section.setCommand(command); + plugin.getProxy().getPluginManager().registerCommand(plugin, command); }); @@ -258,7 +260,7 @@ public class SectionManager { return Optional.ofNullable(res); } - //maybe store this as a variable? + //todo maybe store this as a variable? public ServerSection getPrincipal() { return getByName(props.getPrincipalSectionName()); } @@ -270,4 +272,21 @@ public class SectionManager { public Map getSections() { return sections; } + + private abstract static class Stage { + private final String title; + + public Stage(String title) { + this.title = title; + } + + public String getTitle() { + return title;a + } + + public abstract void execute( + SectionProps props, + ServerSection section + ) throws RuntimeException; + } } diff --git a/src/main/java/com/jaimemartz/playerbalancer/utils/FileInfo.java b/src/main/java/com/jaimemartz/playerbalancer/utils/BuildInfo.java similarity index 90% rename from src/main/java/com/jaimemartz/playerbalancer/utils/FileInfo.java rename to src/main/java/com/jaimemartz/playerbalancer/utils/BuildInfo.java index 54ff9cd..98cec07 100644 --- a/src/main/java/com/jaimemartz/playerbalancer/utils/FileInfo.java +++ b/src/main/java/com/jaimemartz/playerbalancer/utils/BuildInfo.java @@ -1,6 +1,6 @@ package com.jaimemartz.playerbalancer.utils; -public class FileInfo { +public class BuildInfo { public static final String USER_ID = "%%__USER__%%"; public static final String RESOURCE_ID = "%%__RESOURCE__%%"; public static final String NONCE_ID = "%%__NONCE__%%"; diff --git a/src/main/java/com/jaimemartz/playerbalancer/utils/FakeServer.java b/src/main/java/com/jaimemartz/playerbalancer/utils/FakeServer.java new file mode 100644 index 0000000..d3227f1 --- /dev/null +++ b/src/main/java/com/jaimemartz/playerbalancer/utils/FakeServer.java @@ -0,0 +1,65 @@ +package com.jaimemartz.playerbalancer.utils; + +import com.jaimemartz.playerbalancer.section.ServerSection; +import net.md_5.bungee.api.Callback; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ServerPing; +import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class FakeServer implements ServerInfo { + private final ServerSection section; + + public FakeServer(ServerSection section) { + this.section = section; + } + + @Override + public String getName() { + return "@" + section.getProps().getServerName(); + } + + @Override + public InetSocketAddress getAddress() { + return null; + } + + @Override + public Collection getPlayers() { + List res = new ArrayList<>(); + section.getServers().forEach(server -> { + res.addAll(server.getPlayers()); + }); + return res; + } + + @Override + public String getMotd() { + return "Fake server of section " + section.getName(); + } + + @Override + public boolean canAccess(CommandSender sender) { + return true; + } + + @Override + public void sendData(String channel, byte[] data) { + throw new RuntimeException("Not implemented"); + } + + @Override + public boolean sendData(String channel, byte[] data, boolean queue) { + throw new RuntimeException("Not implemented"); + } + + @Override + public void ping(Callback callback) { + throw new RuntimeException("Not implemented"); + } +}