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