Skip to content

Commit

Permalink
Added automatic world detection
Browse files Browse the repository at this point in the history
Up until now, the server disabled the plugin if it couldn't find the world defined in the config. It now searches for an available world and sets it automatically.
  • Loading branch information
strumswell committed Oct 19, 2018
1 parent f75cf80 commit cfe2e34
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
24 changes: 10 additions & 14 deletions src/cloud/bolte/serverlistmotd/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand All @@ -16,6 +17,7 @@
import cloud.bolte.serverlistmotd.events.Ping;
import cloud.bolte.serverlistmotd.events.ProtocolLibImplementation;
import cloud.bolte.serverlistmotd.events.RestrictedModeJoin;
import cloud.bolte.serverlistmotd.motd.MotdState;
import cloud.bolte.serverlistmotd.util.IO;
import cloud.bolte.serverlistmotd.util.VaultIntegration;

Expand All @@ -42,33 +44,27 @@ public void onDisable() {

@Override
public void onEnable() {
//Write config if necessary
//Write config if necessary and load Userdata in HashMap
saveDefaultConfig();
IO.loadFlatfileIntoHashMap(new File("plugins/ServerlistMOTD/IP_UUID.dat"), IP_UUID);

//Handover plugin instance to SpigotConfig
SpigotConfig config = new SpigotConfig(this);

//Check if world set in config exists (time, weather var!)
//Has to be called upon right after config loading to avoid errors
if (!config.configWorldExists()) {
Bukkit.getPluginManager().disablePlugin(this);
return;
}

//Load Userdata in HashMap
IO.loadFlatfileIntoHashMap(new File("plugins/ServerlistMOTD/IP_UUID.dat"), IP_UUID);

//Handover plugin instance to ProtocolLibImplementation
ProtocolLibImplementation pli = new ProtocolLibImplementation(this);

//Start ProtocolLib for slots stuff
pli.listenToServerlistPackets();

//Register listeners
Bukkit.getServer().getPluginManager().registerEvents(new Ping(), this);
Bukkit.getServer().getPluginManager().registerEvents(new IpLogging(), this);
Bukkit.getServer().getPluginManager().registerEvents(new RestrictedModeJoin(), this);

//Check if world set in config exists (time, weather var!)
config.configWorldCheck();

//Start ProtocolLib for slots stuff
pli.listenToServerlistPackets();

//Register command
this.getCommand("serverlist").setExecutor(new Serverlist());

Expand Down
39 changes: 29 additions & 10 deletions src/cloud/bolte/serverlistmotd/SpigotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -375,19 +376,37 @@ public static void saveSmotdConfig() {

/**
* Checks if world name set in config is existent
* And fixes problems automatically
*/
public boolean configWorldExists() {
public void configWorldCheck() {
if (Bukkit.getWorld(getWeatherWorld()) == null || Bukkit.getWorld(getTimeWorld()) == null) {
System.out.println("[ServerlistMOTD] ------------------------");
//Informing user of mismatch
Bukkit.getLogger().severe(
"[ServerlistMOTD] Can't find the defined world from config. Please set your world name in config!");
System.out.println("[ServerlistMOTD] |------------------------------------|");
System.out.println("[ServerlistMOTD] | |");
System.out.println("[ServerlistMOTD] |Please change WORLD NAME in config! |");
System.out.println("[ServerlistMOTD] | |");
System.out.println("[ServerlistMOTD] |------------------------------------|");
return false;
} else {
return true;
"[ServerlistMOTD] CAN'T FIND THE DEFINED WORLD FROM YOUR CONFIG!");
System.out.println("[ServerlistMOTD] Searching for available world...");

//Search shortest world name
String worldName = "";
for(World w : Bukkit.getServer().getWorlds()) {
if (worldName.equalsIgnoreCase("") | w.getName().length() < worldName.length()) {
worldName = w.getName();
}
}

//If found world is not correct disable plugin and return
if (Bukkit.getWorld(worldName) == null || Bukkit.getWorld(worldName) == null) {
Bukkit.getPluginManager().disablePlugin(main);
return;
}

//Update config with found world name
main.getConfig().set("Variables.TimeVariable.World", worldName);
main.getConfig().set("Variables.WeatherVariable.World", worldName);
SpigotConfig.saveSmotdConfig();
System.out.println("[ServerlistMOTD] Found '" + worldName + "‘ and saved it to config.");
System.out.println("[ServerlistMOTD] We're good now. ;-)");
System.out.println("[ServerlistMOTD] ------------------------");
}
}

Expand Down

0 comments on commit cfe2e34

Please sign in to comment.