Skip to content

Commit

Permalink
Improve configuration handling for worlds
Browse files Browse the repository at this point in the history
(and fix bug with old configuration handling)
  • Loading branch information
AliceDTRH committed Jun 1, 2022
1 parent 161784b commit b5e573f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/main/java/xyz/alicedtrh/happyday/HappyDay.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.alicedtrh.happyday;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static void setSuspended(boolean suspended) {
monsterRemover.debouncer.stopAllTasks();
} else {
log().info("Players are online. Enabling plugin");
monsterRemover.schedule(Bukkit.getWorld(WORLD));
monsterRemover.schedule(WORLD);
}
}

Expand All @@ -58,22 +59,22 @@ public void onEnable() {
}

//Fail early if there's no valid world setup.
Objects.requireNonNull(Bukkit.getWorld(WORLD));
try {
Objects.requireNonNull(WORLD);
} catch (NullPointerException e) {
log().severe("Couldn't find a valid world! Make sure your configuration is correct.");
throw new InvalidWorldException("Couldn't find a valid world! Make sure your configuration is correct.", e);
}


getServer().getPluginManager().registerEvents(new HappyDayEventHandler(), this);

setSuspended(true);
}

private void setupConfiguration() {
File oldConfig = new File(getDataFolder() + "config.yml");
if(oldConfig.exists() && oldConfig.canWrite()) {
if(oldConfig.delete()) {
log().warning("Deleted old config file. Please make any changes in the new configuration file if needed.");
}
}

ConfigManager config = ConfigManager.create(this);
config.addConverter(World.class, Bukkit::getWorld, World::getName);
config.target(HappyDayConfig.class);
config.saveDefaults();
config.load();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/xyz/alicedtrh/happyday/HappyDayConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.alicedtrh.happyday;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.entity.CreatureSpawnEvent;
import redempt.redlib.config.annotations.Comment;
import redempt.redlib.config.annotations.ConfigMappable;
Expand Down Expand Up @@ -29,8 +31,8 @@ public class HappyDayConfig {
@Comment("When false, disables the entire mod. (default: true)")
public static boolean ENABLED = true;

@Comment("The world that HappyDay is active on. (Default: world)")
public static String WORLD = "world";
@Comment("The world that HappyDay is active on.")
public static World WORLD = Bukkit.getWorlds().get(0);

@Comment("Times used internally by the mod to figure out when night and day are")
@Comment("This is an advanced option, don't touch this unless you know what you're doing.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public final class HappyDayEventHandler implements Listener {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onTimeSkipEvent(TimeSkipEvent event) {
debouncer.debounce(() -> HappyDay.monsterRemover.schedule(Bukkit.getWorld(WORLD)), 20);
debouncer.debounce(() -> HappyDay.monsterRemover.schedule(WORLD), 20);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuitEvent(PlayerQuitEvent event) {
World world = Objects.requireNonNull(Bukkit.getWorld(WORLD));
World world = Objects.requireNonNull(WORLD);
// The player hasn't quit at this point so the player-count should be 1 if this is the last person
if(world.getPlayerCount() <= 1) {
HappyDay.setSuspended(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package xyz.alicedtrh.happyday;

public class InvalidWorldException extends RuntimeException {
public InvalidWorldException(String msg, NullPointerException e) {
super(msg, e);
}
}

0 comments on commit b5e573f

Please sign in to comment.