forked from SteamWar/SteamWar
Improve LaufbauUtils
This commit is contained in:
+19
-28
@@ -20,6 +20,7 @@
|
|||||||
package de.steamwar.bausystem.features.slaves.laufbau;
|
package de.steamwar.bausystem.features.slaves.laufbau;
|
||||||
|
|
||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
|
import de.steamwar.core.SWPlayer;
|
||||||
import de.steamwar.inventory.SWItem;
|
import de.steamwar.inventory.SWItem;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import de.steamwar.sql.UserConfig;
|
import de.steamwar.sql.UserConfig;
|
||||||
@@ -27,10 +28,7 @@ import lombok.Cleanup;
|
|||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import yapion.hierarchy.output.StreamOutput;
|
import yapion.hierarchy.output.StreamOutput;
|
||||||
import yapion.hierarchy.types.YAPIONObject;
|
import yapion.hierarchy.types.YAPIONObject;
|
||||||
@@ -39,7 +37,10 @@ import yapion.parser.options.StreamOptions;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
@@ -47,36 +48,34 @@ import java.util.zip.GZIPOutputStream;
|
|||||||
@Linked
|
@Linked
|
||||||
public class LaufbauUtils implements Listener {
|
public class LaufbauUtils implements Listener {
|
||||||
|
|
||||||
private static Map<Player, YAPIONObject> yapionObjectMap = new HashMap<>();
|
public static class LaufbauComponent implements SWPlayer.Component {
|
||||||
|
|
||||||
@EventHandler
|
private YAPIONObject yapionObject = new YAPIONObject();
|
||||||
|
|
||||||
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
public void onMount(SWPlayer player) {
|
||||||
String config = UserConfig.getConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau");
|
String config = UserConfig.getConfig(player.getPlayer().getUniqueId(), "bausystem-laufbau");
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
byte[] bytes = Base64.getDecoder().decode(config);
|
byte[] bytes = Base64.getDecoder().decode(config);
|
||||||
@Cleanup GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes));
|
@Cleanup GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes));
|
||||||
YAPIONObject yapionObject = new YAPIONParser(gzipInputStream, new StreamOptions().stopOnStreamEnd(true)).parse().result();
|
yapionObject = new YAPIONParser(gzipInputStream, new StreamOptions().stopOnStreamEnd(true)).parse().result();
|
||||||
yapionObjectMap.put(event.getPlayer(), yapionObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
public void onUnmount(SWPlayer player) {
|
||||||
YAPIONObject yapionObject = yapionObjectMap.get(event.getPlayer());
|
|
||||||
if (yapionObject == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (yapionObject.isEmpty()) {
|
if (yapionObject.isEmpty()) {
|
||||||
UserConfig.removePlayerConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau");
|
UserConfig.removePlayerConfig(player.getPlayer().getUniqueId(), "bausystem-laufbau");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@Cleanup ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
@Cleanup ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
@Cleanup GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
|
@Cleanup GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
|
||||||
yapionObject.toYAPION(new StreamOutput(gzipOutputStream)).close();
|
yapionObject.toYAPION(new StreamOutput(gzipOutputStream)).close();
|
||||||
UserConfig.updatePlayerConfig(event.getPlayer().getUniqueId(), "bausystem-laufbau", Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()));
|
UserConfig.updatePlayerConfig(player.getPlayer().getUniqueId(), "bausystem-laufbau", Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Cuboid pixelCuboid(double pixelX, double pixelY, double pixelZ, double pixelDX, double pixelDY, double pixelDZ) {
|
public static Cuboid pixelCuboid(double pixelX, double pixelY, double pixelZ, double pixelDX, double pixelDY, double pixelDZ) {
|
||||||
@@ -125,11 +124,7 @@ public class LaufbauUtils implements Listener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String identifier = identifier(bb);
|
String identifier = identifier(bb);
|
||||||
YAPIONObject yapionObject = yapionObjectMap.get(p);
|
return SWPlayer.of(p).getComponentOrDefault(LaufbauComponent.class, LaufbauComponent::new).yapionObject.containsKey(identifier);
|
||||||
if (yapionObject == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return yapionObject.containsKey(identifier);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@@ -138,11 +133,7 @@ public class LaufbauUtils implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String identifier = identifier(bb);
|
String identifier = identifier(bb);
|
||||||
YAPIONObject yapionObject = yapionObjectMap.get(p);
|
YAPIONObject yapionObject = SWPlayer.of(p).getComponentOrDefault(LaufbauComponent.class, LaufbauComponent::new).yapionObject;
|
||||||
if (yapionObject == null) {
|
|
||||||
yapionObject = new YAPIONObject();
|
|
||||||
yapionObjectMap.put(p, yapionObject);
|
|
||||||
}
|
|
||||||
if (yapionObject.containsKey(identifier)) {
|
if (yapionObject.containsKey(identifier)) {
|
||||||
yapionObject.remove(identifier);
|
yapionObject.remove(identifier);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user