Skip to content

Commit

Permalink
Release 6.11.4
Browse files Browse the repository at this point in the history
- Fixed BungeeCord Load-Balancing.
- Now using PlayerSpawnLocationEvent for spigot JoinTP

Signed-off-by: Fabricio Winter <[email protected]>
  • Loading branch information
Fabricio20 committed Apr 4, 2020
1 parent 862b9f1 commit 97a8bca
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 145 deletions.
13 changes: 11 additions & 2 deletions Bungee/src/main/java/net/notfab/hubbasics/bungee/HubBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Getter;
import net.md_5.bungee.api.plugin.Plugin;
import net.notfab.hubbasics.bungee.commands.LobbyCommand;
import net.notfab.hubbasics.bungee.listeners.JoinListener;
import net.notfab.hubbasics.bungee.managers.ConfigHandler;
import net.notfab.hubbasics.bungee.managers.HBLogger;
import net.notfab.spigot.simpleconfig.SimpleConfigManager;
Expand Down Expand Up @@ -35,13 +36,21 @@ public void onEnable() {
} else {
loggerManager.info("Running in Development mode, Metrics disabled.");
}
getProxy().getPluginManager().registerCommand(this, new LobbyCommand());
JoinListener listener = new JoinListener(this);
listener.setup(this);
if (listener.isEnabled()) {
this.getProxy().getPluginManager().registerListener(this, new JoinListener(this));
}
LobbyCommand command = new LobbyCommand(this);
command.setup(this);
getProxy().getPluginManager().registerCommand(this, command);
}

@Override
public void onDisable() {
this.loggerManager.onDisable();
getProxy().getPluginManager().unregisterListeners(this);
getProxy().getPluginManager().unregisterCommands(this);
}

}
}
7 changes: 7 additions & 0 deletions Bungee/src/main/java/net/notfab/hubbasics/bungee/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.notfab.hubbasics.bungee;

public interface Module {

void setup(HubBasics hubBasics);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,53 @@
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.notfab.hubbasics.bungee.HubBasics;
import net.notfab.hubbasics.bungee.Module;
import net.notfab.hubbasics.bungee.managers.HBLogger;
import net.notfab.hubbasics.bungee.utils.Messages;
import net.notfab.spigot.simpleconfig.SimpleConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class LobbyCommand extends Command {
public class LobbyCommand extends Command implements Module {

private net.notfab.hubbasics.bungee.HubBasics HubBasics = net.notfab.hubbasics.bungee.HubBasics.getInstance();
private static final HBLogger logger = HBLogger.getLogger("HubBasics");
private HubBasics hubBasics;

public LobbyCommand() {
private List<String> servers = new ArrayList<>();

public LobbyCommand(HubBasics hubBasics) {
super("lobby", null, "hub");
this.hubBasics = hubBasics;
}

@Override
public void setup(HubBasics hubBasics) {
SimpleConfig config = hubBasics.getConfigManager().getNewConfig("config.yml");
boolean isList = config.get("Lobby") instanceof List;
if (isList) {
for (String server : config.getStringList("Lobby")) {
if (hubBasics.getProxy().getServerInfo(server) == null) {
logger.warn("Unknown Server: " + server);
continue;
}
this.servers.add(server);
}
} else {
String server = config.getString("Lobby");
if (hubBasics.getProxy().getServerInfo(server) == null) {
logger.warn("Unknown Server: " + server);
return;
}
this.servers.add(server);
}
}

@Override
public void execute(CommandSender commandSender, String[] strings) {
ServerInfo serverInfo = getLobby();
ServerInfo serverInfo = this.getLobby();
if (serverInfo != null && commandSender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) commandSender;
if (!player.getServer().getInfo().getName().equals(serverInfo.getName())) {
Expand All @@ -34,31 +64,26 @@ public void execute(CommandSender commandSender, String[] strings) {
out.writeUTF("Lobby");
player.sendData("BungeeCord", out.toByteArray());
}
} else if (commandSender instanceof ProxiedPlayer) {
} else if (serverInfo == null) {
commandSender.sendMessage(new TextComponent(Messages.get(commandSender, "LOBBY_NOT_DEFINED")));
} else {
commandSender.sendMessage(new TextComponent(Messages.get(commandSender, "COMMAND_PLAYER")));
}
}

private ServerInfo getLobby() {
SimpleConfig config = HubBasics.getConfigManager().getNewConfig("config.yml");
boolean isList = config.get("Lobby") instanceof List;
if (isList) {
int lowest = Integer.MAX_VALUE - 1;
AtomicReference<ServerInfo> serverInfo = new AtomicReference<>();
List<String> lobbies = config.getStringList("Lobby");
lobbies.forEach(lobby -> {
ServerInfo info = HubBasics.getProxy().getServerInfo(lobby);
if (info.getPlayers().size() < lowest) {
serverInfo.set(info);
}
});
return serverInfo.get();
} else {
String name = config.getString("Lobby");
return HubBasics.getProxy().getServerInfo(name);
if (this.servers.size() == 1) {
return hubBasics.getProxy().getServerInfo(this.servers.get(0));
}
int lowest = Integer.MAX_VALUE - 1;
AtomicReference<ServerInfo> serverInfo = new AtomicReference<>();
this.servers.forEach(lobby -> {
ServerInfo info = hubBasics.getProxy().getServerInfo(lobby);
if (info.getPlayers().size() < lowest) {
serverInfo.set(info);
}
});
return serverInfo.get();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package net.notfab.hubbasics.bungee.listeners;

import lombok.Getter;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.notfab.hubbasics.bungee.HubBasics;
import net.notfab.hubbasics.bungee.Module;
import net.notfab.spigot.simpleconfig.SimpleConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

public class JoinListener implements Listener, Module {

private static final Logger logger = Logger.getLogger("HubBasics");
private HubBasics hubBasics;

@Getter
private boolean enabled = false;
private List<String> servers = new ArrayList<>();

public JoinListener(HubBasics hubBasics) {
this.hubBasics = hubBasics;
}

@Override
public void setup(HubBasics hubBasics) {
SimpleConfig config = hubBasics.getConfigManager().getNewConfig("config.yml");
this.enabled = config.getBoolean("LobbyOnJoin");
// List of servers
boolean isList = config.get("Lobby") instanceof List;
if (isList) {
for (String server : config.getStringList("Lobby")) {
if (hubBasics.getProxy().getServerInfo(server) == null) {
logger.warning("Unknown Server: " + server);
continue;
}
this.servers.add(server);
}
} else {
String server = config.getString("Lobby");
if (hubBasics.getProxy().getServerInfo(server) == null) {
logger.warning("Unknown Server: " + server);
return;
}
this.servers.add(server);
}
}

@EventHandler
public void onJoin(ServerConnectEvent event) {
if (event.getReason() == ServerConnectEvent.Reason.JOIN_PROXY
|| event.getReason() == ServerConnectEvent.Reason.LOBBY_FALLBACK) {
ServerInfo serverInfo = this.getLobby();
if (serverInfo != null) {
event.setTarget(serverInfo);
} else {
logger.warning("No available lobby servers!");
}
}
}

private ServerInfo getLobby() {
if (this.servers.size() == 1) {
return hubBasics.getProxy().getServerInfo(this.servers.get(0));
}
int lowest = Integer.MAX_VALUE - 1;
AtomicReference<ServerInfo> serverInfo = new AtomicReference<>();
this.servers.forEach(lobby -> {
ServerInfo info = hubBasics.getProxy().getServerInfo(lobby);
if (info.getPlayers().size() < lowest) {
serverInfo.set(info);
}
});
return serverInfo.get();
}
}
10 changes: 9 additions & 1 deletion Bungee/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
# Lobby Server
# Either a string with the name of a server (that is configured on BungeeCord)
# or a list of strings for HubBasics to balance between.
Lobby: Lobby
#
# Example:
#
#Lobby:
# - hub1
# - hub2
Lobby: Lobby
# Sends players to the lobby when they join (or fallback).
LobbyOnJoin: false
2 changes: 1 addition & 1 deletion Spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ task runSpigot(type: JavaExec, dependsOn: copyFiles) {
jarFile.getAbsolutePath()
]
debug = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -55,16 +55,16 @@ public void onDisable() {
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
public void onJoin(PlayerSpawnLocationEvent event) {
if (globalTP) {
HLocation location = HubBasics.getLocationManager().get(globalTarget);
location.teleport(event.getPlayer());
event.setSpawnLocation(location.toBukkitLocation());
} else {
if (!worlds.contains(event.getPlayer().getWorld().getName())) {
return;
}
HLocation location = HubBasics.getLocationManager().get(worldLocations.get(event.getPlayer().getWorld().getName()));
location.teleport(event.getPlayer());
event.setSpawnLocation(location.toBukkitLocation());
}
}

Expand All @@ -85,4 +85,4 @@ public void onWorld(PlayerChangedWorldEvent event) {
}
}

}
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
allprojects {
group 'net.notfab.hubbasics'
version = '6.11.3'
version = '6.11.4'
wrapper {
gradleVersion = '6.0.1'
gradleVersion = '6.3'
}
ext.getGitHash = { ->
def stdout = new ByteArrayOutputStream()
Expand All @@ -12,4 +12,4 @@ allprojects {
}
return stdout.toString().trim()
}
}
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 97a8bca

Please sign in to comment.