Initial commit
This commit is contained in:
89
src/eu/ruekov/ruakij/forceServer/ConfigHelper.java
Executable file
89
src/eu/ruekov/ruakij/forceServer/ConfigHelper.java
Executable file
@@ -0,0 +1,89 @@
|
||||
package eu.ruekov.ruakij.forceServer;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.Files;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ConfigHelper {
|
||||
public static Configuration loadOrRecreateConfig(Plugin plugin, String resource) throws IOException {
|
||||
return loadOrRecreateConfig(plugin, resource, null);
|
||||
}
|
||||
public static Configuration loadOrRecreateConfig(Plugin plugin, String resource, Logger log) throws IOException {
|
||||
|
||||
for(int i=0; i<2; i++) {
|
||||
try {
|
||||
// Copy default config if not existing
|
||||
ConfigHelper.loadAndSaveResource(plugin, "config.yml");
|
||||
} catch (IOException e) {
|
||||
|
||||
if(log!=null)log.severe("Could not load plugin-resource!");
|
||||
throw e;
|
||||
}
|
||||
|
||||
try {
|
||||
// Read config
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(plugin.getDataFolder(), resource));
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
if(i == 0) {
|
||||
if(log!=null)log.severe("Could not load configuration-file!");
|
||||
e.printStackTrace();
|
||||
|
||||
if(log!=null)log.info("Backing-up config and creating a new one");
|
||||
backupAndDisableConfig(resource);
|
||||
}
|
||||
else
|
||||
throw e;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// We should not end up here..
|
||||
throw new IOException("Could not create/load configuration-file!");
|
||||
}
|
||||
|
||||
public static void backupAndDisableConfig(String resource) throws IOException {
|
||||
|
||||
// Rename the old config to a new-one, so settings are not lost
|
||||
Random rand = new Random();
|
||||
String newResource = resource + rand.nextInt();
|
||||
|
||||
Files.move(new File(resource), new File(newResource));
|
||||
}
|
||||
|
||||
public static File loadAndSaveResource(Plugin plugin, String resource) throws IOException {
|
||||
File folder = plugin.getDataFolder();
|
||||
if (!folder.exists())
|
||||
folder.mkdir();
|
||||
File resourceFile = new File(folder, resource);
|
||||
if (!resourceFile.exists()) {
|
||||
resourceFile.createNewFile();
|
||||
try (InputStream in = plugin.getResourceAsStream(resource);
|
||||
OutputStream out = new FileOutputStream(resourceFile)) {
|
||||
ByteStreams.copy(in, out);
|
||||
}
|
||||
}
|
||||
return resourceFile;
|
||||
}
|
||||
|
||||
public static boolean validateEntry(String entry, String[] allowed) {
|
||||
return validateEntry(entry, allowed, false);
|
||||
}
|
||||
public static boolean validateEntry(String entry, String[] allowed, boolean caseSensetive) {
|
||||
|
||||
for(String allow : allowed) {
|
||||
|
||||
if(entry.equalsIgnoreCase(allow))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
85
src/eu/ruekov/ruakij/forceServer/Main.java
Executable file
85
src/eu/ruekov/ruakij/forceServer/Main.java
Executable file
@@ -0,0 +1,85 @@
|
||||
package eu.ruekov.ruakij.forceServer;
|
||||
|
||||
import eu.ruekov.ruakij.forceServer.command.forceServer;
|
||||
import eu.ruekov.ruakij.forceServer.listener.PostLogin;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Main extends Plugin {
|
||||
|
||||
public static Main plugin;
|
||||
public static PluginDescription pInfo;
|
||||
|
||||
public static Logger log;
|
||||
|
||||
public static Configuration config;
|
||||
|
||||
public static HashMap<String, String> forceServerByUUID;
|
||||
public static HashMap<String, String> forceServerByName;
|
||||
public static HashMap<String, String> forceServerByPermission;
|
||||
|
||||
public void onEnable() {
|
||||
log = getLogger(); // Get logger
|
||||
plugin = this; // Get plugin
|
||||
pInfo = this.getDescription(); // Get plugin-info
|
||||
|
||||
log.info("Loading config..");
|
||||
try {
|
||||
loadConfig();
|
||||
} catch (IOException ex) {
|
||||
log.severe("Loading config failed!");
|
||||
ex.printStackTrace();
|
||||
|
||||
// Disable plugin
|
||||
onDisable();
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("Registering commands..");
|
||||
this.getProxy().getPluginManager().registerCommand(this, new forceServer("forceserver"));
|
||||
|
||||
log.info("Registering events..");
|
||||
this.getProxy().getPluginManager().registerListener(this, new PostLogin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
log.info("Version: " + pInfo.getVersion() + " is now disabled!");
|
||||
}
|
||||
|
||||
public void loadConfig() throws IOException {
|
||||
config = ConfigHelper.loadOrRecreateConfig(this, "config.yml");
|
||||
|
||||
// Load UUID-List into memory
|
||||
forceServerByUUID = new HashMap<>();
|
||||
Configuration byUuidSection = config.getSection("forceServer.byUUID");
|
||||
for(String uuid : byUuidSection.getKeys()) {
|
||||
String targetServer = byUuidSection.getString(uuid);
|
||||
forceServerByUUID.put(uuid, targetServer);
|
||||
}
|
||||
log.info(forceServerByUUID.size() +" UUID's");
|
||||
|
||||
forceServerByName = new HashMap<>();
|
||||
Configuration byNameSection = config.getSection("forceServer.byName");
|
||||
for(String uuid : byNameSection.getKeys()) {
|
||||
String targetServer = byNameSection.getString(uuid);
|
||||
forceServerByName.put(uuid, targetServer);
|
||||
}
|
||||
log.info(forceServerByName.size() +" Names");
|
||||
|
||||
forceServerByPermission = new HashMap<>();
|
||||
Configuration byPermissionSection = config.getSection("forceServer.byPermission");
|
||||
for(String uuid : byPermissionSection.getKeys()) {
|
||||
String targetServer = byPermissionSection.getString(uuid);
|
||||
forceServerByPermission.put(uuid, targetServer);
|
||||
}
|
||||
log.info(forceServerByPermission.size() +" Permission-Nodes");
|
||||
}
|
||||
}
|
||||
39
src/eu/ruekov/ruakij/forceServer/command/forceServer.java
Normal file
39
src/eu/ruekov/ruakij/forceServer/command/forceServer.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package eu.ruekov.ruakij.forceServer.command;
|
||||
|
||||
import eu.ruekov.ruakij.forceServer.Main;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class forceServer extends Command {
|
||||
public forceServer(String name) { super(name); }
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
|
||||
// Permission check if sender is player
|
||||
if(sender instanceof ProxiedPlayer){
|
||||
ProxiedPlayer p = (ProxiedPlayer)sender;
|
||||
|
||||
if(!p.hasPermission("forceServer.cmd")){
|
||||
p.sendMessage("§cNo Permission!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Main.log.info("Reloading config..");
|
||||
sender.sendMessage("§eReloading..");
|
||||
try {
|
||||
Main.plugin.loadConfig();
|
||||
} catch (IOException ex) {
|
||||
Main.log.severe("Reloading config failed!");
|
||||
ex.printStackTrace();
|
||||
|
||||
sender.sendMessage("§cFailed! See config for details");
|
||||
return;
|
||||
}
|
||||
sender.sendMessage("§aDone");
|
||||
}
|
||||
}
|
||||
43
src/eu/ruekov/ruakij/forceServer/listener/PostLogin.java
Normal file
43
src/eu/ruekov/ruakij/forceServer/listener/PostLogin.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package eu.ruekov.ruakij.forceServer.listener;
|
||||
|
||||
import eu.ruekov.ruakij.forceServer.Main;
|
||||
import net.md_5.bungee.BungeeCord;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.event.PostLoginEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PostLogin implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPostLogin(PostLoginEvent e) {
|
||||
|
||||
ProxiedPlayer p = e.getPlayer();
|
||||
|
||||
// byUUID
|
||||
String forcedTargetServer = Main.forceServerByUUID.get(p.getUniqueId().toString());
|
||||
// byName
|
||||
if(forcedTargetServer == null) Main.forceServerByName.get(p.getName());
|
||||
// byPermission
|
||||
if(forcedTargetServer == null) {
|
||||
for(String permission : Main.forceServerByPermission.keySet()) {
|
||||
if(p.hasPermission(permission)){
|
||||
forcedTargetServer = Main.forceServerByPermission.get(permission);
|
||||
}
|
||||
}
|
||||
Main.forceServerByPermission.get(p);
|
||||
}
|
||||
if(forcedTargetServer == null) return;
|
||||
|
||||
ServerInfo sInfo = BungeeCord.getInstance().getServerInfo(forcedTargetServer);
|
||||
if(sInfo == null) {
|
||||
Main.log.severe("TargetServer '"+ forcedTargetServer +"' not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
p.connect(sInfo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user