From 2d3cfaee731612c03436c377603782a15e255a09 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 7 May 2020 20:32:37 +0200 Subject: [PATCH] Updated PerWorldSettings --- .gitignore | 1 + .../services/PerWorldSettingsService.java | 40 ++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 7139df706..4a193c6a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /bin/ /target/ +/plugins/ /sonar/ /.settings/ /.idea/ diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PerWorldSettingsService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PerWorldSettingsService.java index 636702237..34e03bf96 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PerWorldSettingsService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PerWorldSettingsService.java @@ -11,6 +11,7 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.UUID; import java.util.logging.Level; import org.bukkit.Server; @@ -34,7 +35,7 @@ public class PerWorldSettingsService { private final OptionalMap> disabledItems = new OptionalMap<>(HashMap::new); private final Map> disabledAddons = new HashMap<>(); - private final Set disabledWorlds = new HashSet<>(); + private final Set disabledWorlds = new HashSet<>(); public PerWorldSettingsService(SlimefunPlugin plugin) { this.plugin = plugin; @@ -66,7 +67,7 @@ public class PerWorldSettingsService { * The {@link World} to load */ public void load(World world) { - disabledItems.putIfAbsent(world.getName(), loadWorldFromConfig(world.getName())); + disabledItems.putIfAbsent(world.getName(), loadWorldFromConfig(world)); } /** @@ -111,9 +112,9 @@ public class PerWorldSettingsService { * @return Whether the given {@link SlimefunItem} is enabled in that {@link World} */ public boolean isEnabled(World world, SlimefunItem item) { - Set items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); + Set items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world)); - if (disabledWorlds.contains(world.getName())) { + if (disabledWorlds.contains(world.getUID())) { return false; } @@ -131,7 +132,7 @@ public class PerWorldSettingsService { * Whether the given {@link SlimefunItem} should be enabled in that world */ public void setEnabled(World world, SlimefunItem item, boolean enabled) { - Set items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); + Set items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world)); if (enabled) { items.remove(item.getID()); @@ -141,6 +142,25 @@ public class PerWorldSettingsService { } } + /** + * This method enables or disables the given {@link World}. + * + * @param world + * The {@link World} to enable or disable + * @param enabled + * Whether this {@link World} should be enabled or not + */ + public void setEnabled(World world, boolean enabled) { + load(world); + + if (enabled) { + disabledWorlds.remove(world.getUID()); + } + else { + disabledWorlds.add(world.getUID()); + } + } + /** * This checks whether the given {@link World} is enabled or not. * @@ -151,7 +171,8 @@ public class PerWorldSettingsService { */ public boolean isWorldEnabled(World world) { load(world); - return !disabledWorlds.contains(world.getName()); + + return !disabledWorlds.contains(world.getUID()); } /** @@ -177,7 +198,7 @@ public class PerWorldSettingsService { * The {@link World} to save */ public void save(World world) { - Set items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); + Set items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world)); Config config = new Config(plugin, "world-settings/" + world + ".yml"); @@ -191,7 +212,8 @@ public class PerWorldSettingsService { config.save(); } - private Set loadWorldFromConfig(String name) { + private Set loadWorldFromConfig(World world) { + String name = world.getName(); Optional> optional = disabledItems.get(name); if (optional.isPresent()) { @@ -228,7 +250,7 @@ public class PerWorldSettingsService { config.save(); } else { - disabledWorlds.add(name); + disabledWorlds.add(world.getUID()); } return items;