From 7eb642793c2d878ef003972afdb0fb56d3040531 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 May 2021 16:07:32 +0200 Subject: [PATCH] Update DepthCounter to save in PlayerConfig Signed-off-by: yoyosource --- .../src/de/steamwar/bausystem/BauSystem.java | 2 ++ .../bausystem/configplayer/Config.java | 8 +++++ .../testblock/depthcounter/DepthCounter.java | 36 +++++++++---------- .../depthcounter/DepthCounterListener.java | 13 ------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index da7f4ee9..6e58f6c6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.linkage.LinkageUtils; import de.steamwar.bausystem.region.loader.PrototypeLoader; import de.steamwar.bausystem.region.loader.RegionLoader; @@ -75,6 +76,7 @@ public class BauSystem extends JavaPlugin implements Listener { LinkageUtils.unlink(); RegionLoader.save(); + Config.getInstance().saveAll(); } private void fixLogging() { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java index 7ad889c4..bc9cde30 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java @@ -96,6 +96,14 @@ public class Config implements Listener { return playerConfigurations.get(uuid); } + public void saveAll() { + playerConfigurations.forEach((uuid, yapionObject) -> { + String string = yapionObject.toYAPION(new StringOutput()).getResult(); + UserConfig.updatePlayerConfig(uuid, "bausystem", string); + }); + playerConfigurations.clear(); + } + /** * Save a PlayerConfig, this does not remove the key value mapping from the map. * diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java index be963a13..d4194ae9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java @@ -21,10 +21,12 @@ package de.steamwar.bausystem.features.testblock.depthcounter; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.region.Region; import lombok.experimental.UtilityClass; import org.bukkit.entity.Player; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONObject; import java.util.*; import java.util.stream.Collectors; @@ -33,7 +35,8 @@ import java.util.stream.Collectors; public class DepthCounter { public final Map> depthMap = new HashMap<>(); - public final Map> playerSettings = new HashMap<>(); + + private static final YAPIONObject DEFAULT = new YAPIONObject().add("X", "").add("Y", "").add("Z", ""); public void toggleMode(final Player p, final CountMode countMode) { if (isActive(p, countMode)) { @@ -44,35 +47,29 @@ public class DepthCounter { } public boolean isActive(final Player p, final CountMode countMode) { - final Set countModes = playerSettings.get(p); - return countModes != null && countModes.contains(countMode); + return Config.getInstance().get(p).getYAPIONObjectOrSetDefault("depth-counter", (YAPIONObject) DEFAULT.copy()).containsKey(countMode.name()); } public void addMode(final Player p, final CountMode countMode) { - final Set countModes = playerSettings.get(p); - if (countModes != null) { - countModes.add(countMode); - } else { - playerSettings.put(p, new HashSet<>(Collections.singletonList(countMode))); + if (!isActive(p, countMode)) { + Config.getInstance().get(p).getObject("depth-counter").add(countMode.name(), ""); } } public void removeMode(final Player p, final CountMode countMode) { - final Set countModes = playerSettings.get(p); - if (countModes != null) { - countModes.remove(countMode); - if (countModes.isEmpty()) { - removePlayer(p); - } + if (isActive(p, countMode)) { + Config.getInstance().get(p).getObject("depth-counter").remove(countMode.name()); } } public void setModes(final Player p, final Set countModes) { - playerSettings.put(p, countModes); + YAPIONObject yapionObject = new YAPIONObject(); + countModes.forEach(countMode -> yapionObject.put(countMode.name(), "")); + Config.getInstance().get(p).put("depth-counter", yapionObject); } public void removePlayer(final Player p) { - playerSettings.remove(p); + Config.getInstance().get(p).put("depth-counter", new YAPIONObject()); } public Vector blockVector(Vector vector) { @@ -80,8 +77,11 @@ public class DepthCounter { } public Set getModes(Player p) { - final Set countModes = playerSettings.get(p); - return countModes == null ? Collections.emptySet() : countModes.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)); + Set countModes = new HashSet<>(); + Config.getInstance().get(p).getYAPIONObjectOrSetDefault("depth-counter", (YAPIONObject) DEFAULT.copy()).forEach((s, yapionAnyType) -> { + countModes.add(CountMode.valueOf(s)); + }); + return countModes.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)); } public boolean hasModes(Player p) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java index 47f98554..43e52d70 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java @@ -27,13 +27,10 @@ import de.steamwar.bausystem.region.utils.RegionType; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; import java.util.HashSet; import java.util.Set; - @Linked(LinkageType.LISTENER) public class DepthCounterListener implements Listener { @@ -54,14 +51,4 @@ public class DepthCounterListener implements Listener { } depthSet.add(new Depth(region, event.blockList())); } - - @EventHandler - public void onLeave(PlayerQuitEvent event) { - DepthCounter.removePlayer(event.getPlayer()); - } - - @EventHandler - public void onJoin(PlayerJoinEvent event) { - DepthCounter.setModes(event.getPlayer(), CountMode.ALL()); - } } \ No newline at end of file