1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Updated PerWorldSettings

This commit is contained in:
TheBusyBiscuit 2020-05-07 20:32:37 +02:00
parent 9e63f1ce79
commit 2d3cfaee73
2 changed files with 32 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
/bin/ /bin/
/target/ /target/
/plugins/
/sonar/ /sonar/
/.settings/ /.settings/
/.idea/ /.idea/

View File

@ -11,6 +11,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Server; import org.bukkit.Server;
@ -34,7 +35,7 @@ public class PerWorldSettingsService {
private final OptionalMap<String, Set<String>> disabledItems = new OptionalMap<>(HashMap::new); private final OptionalMap<String, Set<String>> disabledItems = new OptionalMap<>(HashMap::new);
private final Map<SlimefunAddon, Set<String>> disabledAddons = new HashMap<>(); private final Map<SlimefunAddon, Set<String>> disabledAddons = new HashMap<>();
private final Set<String> disabledWorlds = new HashSet<>(); private final Set<UUID> disabledWorlds = new HashSet<>();
public PerWorldSettingsService(SlimefunPlugin plugin) { public PerWorldSettingsService(SlimefunPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -66,7 +67,7 @@ public class PerWorldSettingsService {
* The {@link World} to load * The {@link World} to load
*/ */
public void load(World world) { 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} * @return Whether the given {@link SlimefunItem} is enabled in that {@link World}
*/ */
public boolean isEnabled(World world, SlimefunItem item) { public boolean isEnabled(World world, SlimefunItem item) {
Set<String> items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); Set<String> items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world));
if (disabledWorlds.contains(world.getName())) { if (disabledWorlds.contains(world.getUID())) {
return false; return false;
} }
@ -131,7 +132,7 @@ public class PerWorldSettingsService {
* Whether the given {@link SlimefunItem} should be enabled in that world * Whether the given {@link SlimefunItem} should be enabled in that world
*/ */
public void setEnabled(World world, SlimefunItem item, boolean enabled) { public void setEnabled(World world, SlimefunItem item, boolean enabled) {
Set<String> items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); Set<String> items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world));
if (enabled) { if (enabled) {
items.remove(item.getID()); 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. * This checks whether the given {@link World} is enabled or not.
* *
@ -151,7 +171,8 @@ public class PerWorldSettingsService {
*/ */
public boolean isWorldEnabled(World world) { public boolean isWorldEnabled(World world) {
load(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 * The {@link World} to save
*/ */
public void save(World world) { public void save(World world) {
Set<String> items = disabledItems.computeIfAbsent(world.getName(), this::loadWorldFromConfig); Set<String> items = disabledItems.computeIfAbsent(world.getName(), name -> loadWorldFromConfig(world));
Config config = new Config(plugin, "world-settings/" + world + ".yml"); Config config = new Config(plugin, "world-settings/" + world + ".yml");
@ -191,7 +212,8 @@ public class PerWorldSettingsService {
config.save(); config.save();
} }
private Set<String> loadWorldFromConfig(String name) { private Set<String> loadWorldFromConfig(World world) {
String name = world.getName();
Optional<Set<String>> optional = disabledItems.get(name); Optional<Set<String>> optional = disabledItems.get(name);
if (optional.isPresent()) { if (optional.isPresent()) {
@ -228,7 +250,7 @@ public class PerWorldSettingsService {
config.save(); config.save();
} }
else { else {
disabledWorlds.add(name); disabledWorlds.add(world.getUID());
} }
return items; return items;