diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java index a2506042..993c5cac 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java @@ -59,7 +59,7 @@ public class Laufbau { editSession = WorldEditUtils.getEditSession(player); elements = BlockBoundingBox.elements.stream().filter(blockBoundingBox -> { - return !LaufbauUtils.isDeactivated(player, blockBoundingBox); + return !LaufbauUtils.get(player).isDeactivated(blockBoundingBox); }).collect(Collectors.toList()); if (preferingBlastResistance) { elements.sort((o1, o2) -> { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauSettings.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauSettings.java index c52f9a1d..43faaba3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauSettings.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauSettings.java @@ -55,7 +55,7 @@ public class LaufbauSettings { } SWListInv, List>> inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_GUI_NAME", p), false, list, (clickType, entry) -> { if (entry.getValue().size() == 1) { - LaufbauUtils.toggle(p, entry.getValue().get(0)); + LaufbauUtils.get(p).toggle(entry.getValue().get(0)); open(); return; } @@ -80,13 +80,13 @@ public class LaufbauSettings { lore = itemMeta.getLore(); } lore.add(""); - lore.add(BauSystem.MESSAGE.parse(LaufbauUtils.isDeactivated(p, bb) ? "LAUFBAU_SETTINGS_INACTIVE" : "LAUFBAU_SETTINGS_ACTIVE", p)); + lore.add(BauSystem.MESSAGE.parse(LaufbauUtils.get(p).isDeactivated(bb) ? "LAUFBAU_SETTINGS_INACTIVE" : "LAUFBAU_SETTINGS_ACTIVE", p)); itemMeta.setLore(lore); swItem.setItemMeta(itemMeta); list.add(new SWListInv.SWListEntry<>(swItem, bb)); } SWListInv inv = new SWListInv<>(p, BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_GUI_NAME", p), false, list, (clickType, blockBoundingBox) -> { - LaufbauUtils.toggle(p, blockBoundingBox); + LaufbauUtils.get(p).toggle(blockBoundingBox); open(key); }); inv.setItem(49, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_GUI_BACK", p), clickType -> { @@ -105,14 +105,14 @@ public class LaufbauSettings { lore = itemMeta.getLore(); } lore.add(""); - lore.add(BauSystem.MESSAGE.parse(LaufbauUtils.isDeactivated(p, bb) ? "LAUFBAU_SETTINGS_INACTIVE" : "LAUFBAU_SETTINGS_ACTIVE", p)); + lore.add(BauSystem.MESSAGE.parse(LaufbauUtils.get(p).isDeactivated(bb) ? "LAUFBAU_SETTINGS_INACTIVE" : "LAUFBAU_SETTINGS_ACTIVE", p)); lore.add(""); lore.add(BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_TOGGLE", p)); itemMeta.setLore(lore); swItem.setItemMeta(itemMeta); return swItem; } else { - long count = blockBoundingBoxes.stream().filter(bb -> LaufbauUtils.isDeactivated(p, bb)).count(); + long count = blockBoundingBoxes.stream().filter(bb -> LaufbauUtils.get(p).isDeactivated(bb)).count(); if (count == 0) { return new SWItem(material, BauSystem.MESSAGE.parse(group, p), Arrays.asList("", BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_ACTIVE", p), "", BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_ADVANCED", p), BauSystem.MESSAGE.parse("LAUFBAU_SETTINGS_TOGGLE", p)), false, clickType -> {}); } @@ -124,16 +124,16 @@ public class LaufbauSettings { } public void toggle(List blockBoundingBoxes) { - long count = blockBoundingBoxes.stream().filter(bb -> LaufbauUtils.isDeactivated(p, bb)).count(); + long count = blockBoundingBoxes.stream().filter(bb -> LaufbauUtils.get(p).isDeactivated(bb)).count(); if (count != blockBoundingBoxes.size()) { blockBoundingBoxes.forEach(bb -> { - if (!LaufbauUtils.isDeactivated(p, bb)) { - LaufbauUtils.toggle(p, bb); + if (!LaufbauUtils.get(p).isDeactivated(bb)) { + LaufbauUtils.get(p).toggle(bb); } }); } else { blockBoundingBoxes.forEach(bb -> { - LaufbauUtils.toggle(p, bb); + LaufbauUtils.get(p).toggle(bb); }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauUtils.java index 9a9fb24a..25a84ce2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/LaufbauUtils.java @@ -20,63 +20,28 @@ package de.steamwar.bausystem.features.slaves.laufbau; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.config.ConfigField; +import de.steamwar.bausystem.config.YamlConfig; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; import de.steamwar.sql.UserConfig; -import lombok.Cleanup; import lombok.SneakyThrows; import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.meta.ItemMeta; -import yapion.hierarchy.output.StreamOutput; -import yapion.hierarchy.types.YAPIONObject; -import yapion.parser.YAPIONParser; -import yapion.parser.options.StreamOptions; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.util.*; import java.util.stream.Collectors; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; -@Linked -public class LaufbauUtils implements Listener { +public class LaufbauUtils extends YamlConfig { - private static Map yapionObjectMap = new HashMap<>(); - - @EventHandler - @SneakyThrows - public void onPlayerJoin(PlayerJoinEvent event) { - String config = UserConfig.getConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau"); - if (config == null) { - return; - } - byte[] bytes = Base64.getDecoder().decode(config); - @Cleanup GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes)); - YAPIONObject yapionObject = new YAPIONParser(gzipInputStream, new StreamOptions().stopOnStreamEnd(true)).parse().result(); - yapionObjectMap.put(event.getPlayer(), yapionObject); - } - - @EventHandler - @SneakyThrows - public void onPlayerQuit(PlayerQuitEvent event) { - YAPIONObject yapionObject = yapionObjectMap.get(event.getPlayer()); - if (yapionObject == null) { - return; - } - if (yapionObject.isEmpty()) { - UserConfig.removePlayerConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau"); - return; - } - @Cleanup ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - @Cleanup GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); - yapionObject.toYAPION(new StreamOutput(gzipOutputStream)).close(); - UserConfig.updatePlayerConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau", Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray())); + private static final Map utilsMap = new HashMap<>(); + public static LaufbauUtils get(Player p) { + return utilsMap.computeIfAbsent(p, player -> new LaufbauUtils(fromUserConfig(player.getUniqueId(), "bausystem-laufbau"))); } public static Cuboid pixelCuboid(double pixelX, double pixelY, double pixelZ, double pixelDX, double pixelDY, double pixelDZ) { @@ -119,34 +84,42 @@ public class LaufbauUtils implements Listener { return name + "[" + String.join(",", lore) + "]"; } - @SneakyThrows - public static boolean isDeactivated(Player p, BlockBoundingBox bb) { - if (bb.getSwItem() == null) { - return false; - } - String identifier = identifier(bb); - YAPIONObject yapionObject = yapionObjectMap.get(p); - if (yapionObject == null) { - return false; - } - return yapionObject.containsKey(identifier); + @ConfigField + private final List deactivated = new ArrayList<>(); + + public LaufbauUtils(ConfigurationSection config) { + super(config); } @SneakyThrows - public static void toggle(Player p, BlockBoundingBox bb) { + public boolean isDeactivated(BlockBoundingBox bb) { if (bb.getSwItem() == null) { + return false; + } + return deactivated.contains(identifier(bb)); + } + + @SneakyThrows + public void toggle(BlockBoundingBox bb) { + if (bb.getSwItem() == null) return; - } + String identifier = identifier(bb); - YAPIONObject yapionObject = yapionObjectMap.get(p); - if (yapionObject == null) { - yapionObject = new YAPIONObject(); - yapionObjectMap.put(p, yapionObject); - } - if (yapionObject.containsKey(identifier)) { - yapionObject.remove(identifier); - } else { - yapionObject.put(identifier, new YAPIONObject()); + if(deactivated.remove(identifier)) + return; + deactivated.add(identifier); + } + + @Linked + public static class LaufbauUtilListener implements Listener { + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + LaufbauUtils util = utilsMap.remove(event.getPlayer()); + if(util == null) + return; + + UserConfig.updatePlayerConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau", util.toYaml().saveToString()); } } }