SteamWar/BauSystem2.0
Archiviert
12
0

Update DepthCounter to save in PlayerConfig

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-05-24 16:07:32 +02:00
Ursprung 399171f58e
Commit 7eb642793c
4 geänderte Dateien mit 28 neuen und 31 gelöschten Zeilen

Datei anzeigen

@ -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() {

Datei anzeigen

@ -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.
*

Datei anzeigen

@ -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<Region, Set<Depth>> depthMap = new HashMap<>();
public final Map<Player, Set<CountMode>> 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<CountMode> 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<CountMode> 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<CountMode> 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<CountMode> 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<CountMode> getModes(Player p) {
final Set<CountMode> countModes = playerSettings.get(p);
return countModes == null ? Collections.emptySet() : countModes.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new));
Set<CountMode> 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) {

Datei anzeigen

@ -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());
}
}