From e20fc16a819ec187858b46c7e3c0df2796c37f6b Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 24 Feb 2021 13:32:55 -0600 Subject: [PATCH] Fixes #11 --- .idea/compiler.xml | 3 +- .idea/jarRepositories.xml | 40 +++++++++++++++++++ pom.xml | 22 ++++++++-- .../net/seanomik/energeticstorage/Skulls.java | 37 +++++++++-------- .../commands/ESGiveCommand.java | 5 +++ .../energeticstorage/files/PlayersFile.java | 1 + .../energeticstorage/gui/ESDriveGUI.java | 19 ++++++--- .../listeners/BlockBreakListener.java | 19 ++++++--- .../listeners/BlockPlaceListener.java | 25 +++++++----- .../energeticstorage/objects/ESDrive.java | 2 +- .../energeticstorage/utils/Utils.java | 15 ++++--- 11 files changed, 136 insertions(+), 52 deletions(-) create mode 100644 .idea/jarRepositories.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index eae676c..9c41e5e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,11 +6,12 @@ - + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..2c484da --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 137b448..611fac7 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,8 @@ - D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.4_\plugins\EnergeticStorage-v${project.version}.jar + + D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.4_\plugins\EnergeticStorage-v${project.version}.jar @@ -90,19 +91,34 @@ org.spigotmc - spigot + spigot-api 1.14-R0.1-SNAPSHOT provided de.tr7zw item-nbt-api - 2.6.0 + 2.7.1 com.github.WesJD.AnvilGUI anvilgui master-5d0f592c63-1 + + com.googlecode.json-simple + json-simple + 1.1.1 + + + org.apache.commons + commons-text + 1.9 + + + org.jetbrains + annotations + 13.0 + diff --git a/src/main/java/net/seanomik/energeticstorage/Skulls.java b/src/main/java/net/seanomik/energeticstorage/Skulls.java index 3fc1b34..22783b0 100644 --- a/src/main/java/net/seanomik/energeticstorage/Skulls.java +++ b/src/main/java/net/seanomik/energeticstorage/Skulls.java @@ -1,14 +1,10 @@ package net.seanomik.energeticstorage; -import com.mojang.authlib.GameProfile; -import com.mojang.authlib.properties.Property; -import net.seanomik.energeticstorage.utils.Utils; +import de.tr7zw.changeme.nbtapi.NBTCompound; +import de.tr7zw.changeme.nbtapi.NBTItem; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.SkullMeta; - -import java.lang.reflect.Field; -import java.util.UUID; public enum Skulls { @@ -19,30 +15,33 @@ public enum Skulls { private ItemStack item; private String name; private String texture; + private String uuid; Skulls(String name, int id, String texture, String uuid) { - item = createSkull(texture, name); this.texture = texture; this.name = name; + this.uuid = uuid; + item = createSkull(uuid, name); } private ItemStack createSkull (String url, String name) { ItemStack head = new ItemStack(Material.PLAYER_HEAD, 1, (short) 3); if (url.isEmpty()) return head; - SkullMeta headMeta = (SkullMeta) head.getItemMeta(); - GameProfile gameProfile = new GameProfile(UUID.randomUUID(), null); + NBTItem headNBT = new NBTItem(head); + String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - gameProfile.getProperties().put("textures", new Property("textures", url)); - - try { - Field profileField = headMeta.getClass().getDeclaredField("profile"); - profileField.setAccessible(true); - profileField.set(headMeta, gameProfile); - } catch (IllegalArgumentException | NoSuchFieldException | SecurityException | IllegalAccessException e) { - e.printStackTrace(); + NBTCompound ownerNBT; + if (version.startsWith("v1_16")) { + ownerNBT = headNBT.addCompound("SkullOwner"); + } else { + ownerNBT = headNBT.addCompound("Owner"); } - head.setItemMeta(headMeta); + + ownerNBT.addCompound("Properties").getCompoundList("textures").addCompound().setString("Value", texture); + ownerNBT.setString("Id", uuid); + head = headNBT.getItem(); + return head; } diff --git a/src/main/java/net/seanomik/energeticstorage/commands/ESGiveCommand.java b/src/main/java/net/seanomik/energeticstorage/commands/ESGiveCommand.java index 01792b1..27d84db 100644 --- a/src/main/java/net/seanomik/energeticstorage/commands/ESGiveCommand.java +++ b/src/main/java/net/seanomik/energeticstorage/commands/ESGiveCommand.java @@ -72,6 +72,11 @@ public class ESGiveCommand implements TabExecutor { return true; } + if (args.length == 0) { + sender.sendMessage(generateCommandUsage(args)); + return true; + } + switch (args[0]) { // Switch on item type case "save": PlayersFile.savePlayerSystems(Reference.ES_SYSTEMS.get(((Player) sender).getUniqueId())); diff --git a/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java b/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java index 3e6c423..ca933d3 100644 --- a/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java +++ b/src/main/java/net/seanomik/energeticstorage/files/PlayersFile.java @@ -240,6 +240,7 @@ public class PlayersFile extends YamlConfiguration { getConfig().set(systemPath + "drives", null); for (ESDrive drive : esSystem.getESDrives()) { + if (drive == null) continue; getConfig().set(systemPath + "drives." + drive.getUUID() + ".size", drive.getSize()); JSONArray itemsJson = new JSONArray(); diff --git a/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java b/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java index 05408ed..9cd4061 100644 --- a/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java +++ b/src/main/java/net/seanomik/energeticstorage/gui/ESDriveGUI.java @@ -1,6 +1,7 @@ package net.seanomik.energeticstorage.gui; import de.tr7zw.changeme.nbtapi.NBTItem; +import net.seanomik.energeticstorage.EnergeticStorage; import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.objects.ESDrive; import net.seanomik.energeticstorage.objects.ESSystem; @@ -58,7 +59,9 @@ public class ESDriveGUI implements InventoryHolder, Listener { for (int i = 2; i < esSystem.getESDrives().size() + 2; i++) { ESDrive drive = esSystem.getESDrives().get(i - 2); - inv.setItem(i , drive.getDriveItem()); + if (drive != null) { + inv.setItem(i, drive.getDriveItem()); + } } } @@ -149,7 +152,11 @@ public class ESDriveGUI implements InventoryHolder, Listener { return; } else { Player player = (Player) event.getPlayer(); - PlayersFile.savePlayerSystem(openSystems.get(player.getUniqueId())); + ESSystem openSystem = openSystems.get(player.getUniqueId()); + // Serialize null drives + openSystem.getESDrives().removeIf(Objects::isNull); + + PlayersFile.savePlayerSystem(openSystem); openSystems.remove(player); } @@ -219,12 +226,12 @@ public class ESDriveGUI implements InventoryHolder, Listener { Reference.ES_TERMINAL_GUI.openInventory(player, esSystem); } else if (slot != 1 && slot != 7 && slot != 8) { - if (Utils.isItemADrive(cursor)) { - event.setCancelled(false); - + if (Utils.isItemADrive(clickedItem)) { List drives = esSystem.getESDrives(); - drives.remove(slot - 2); + drives.set(slot - 2, null); esSystem.setESDrives(drives); + + event.setCancelled(false); } } break; diff --git a/src/main/java/net/seanomik/energeticstorage/listeners/BlockBreakListener.java b/src/main/java/net/seanomik/energeticstorage/listeners/BlockBreakListener.java index f4601d9..8ef7340 100644 --- a/src/main/java/net/seanomik/energeticstorage/listeners/BlockBreakListener.java +++ b/src/main/java/net/seanomik/energeticstorage/listeners/BlockBreakListener.java @@ -1,5 +1,6 @@ package net.seanomik.energeticstorage.listeners; +import net.seanomik.energeticstorage.EnergeticStorage; import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.objects.ESDrive; import net.seanomik.energeticstorage.objects.ESSystem; @@ -7,7 +8,9 @@ import net.seanomik.energeticstorage.utils.ItemConstructor; import net.seanomik.energeticstorage.utils.PermissionChecks; import net.seanomik.energeticstorage.utils.Reference; import net.seanomik.energeticstorage.utils.Utils; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -36,15 +39,19 @@ public class BlockBreakListener implements Listener { } // Remove the system from cache and storage - PlayersFile.removePlayerSystem(player.getUniqueId(), esSystem.getUUID()); + Bukkit.getScheduler().runTaskAsynchronously(EnergeticStorage.getPlugin(), () -> { + PlayersFile.removePlayerSystem(player.getUniqueId(), esSystem.getUUID()); - List systems = new LinkedList<>(Reference.ES_SYSTEMS.get(player.getUniqueId())); - systems.removeIf(esSystem::equals); - Reference.ES_SYSTEMS.replace(player.getUniqueId(), systems); + List systems = new LinkedList<>(Reference.ES_SYSTEMS.get(player.getUniqueId())); + systems.removeIf(esSystem::equals); + Reference.ES_SYSTEMS.replace(player.getUniqueId(), systems); + }); - // Drop an ES System + // Only drop the system if they're not in creative. event.setDropItems(false); - event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), ItemConstructor.createSystemBlock()); + if (player.getGameMode() != GameMode.CREATIVE) { + event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), ItemConstructor.createSystemBlock()); + } } else { event.setCancelled(true); player.sendMessage(Reference.PREFIX + ChatColor.RED + "You are not trusted to this system!"); diff --git a/src/main/java/net/seanomik/energeticstorage/listeners/BlockPlaceListener.java b/src/main/java/net/seanomik/energeticstorage/listeners/BlockPlaceListener.java index 3190d21..cccb03e 100644 --- a/src/main/java/net/seanomik/energeticstorage/listeners/BlockPlaceListener.java +++ b/src/main/java/net/seanomik/energeticstorage/listeners/BlockPlaceListener.java @@ -1,12 +1,14 @@ package net.seanomik.energeticstorage.listeners; import de.tr7zw.changeme.nbtapi.NBTTileEntity; +import net.seanomik.energeticstorage.EnergeticStorage; import net.seanomik.energeticstorage.Skulls; import net.seanomik.energeticstorage.files.PlayersFile; import net.seanomik.energeticstorage.objects.ESSystem; import net.seanomik.energeticstorage.utils.PermissionChecks; import net.seanomik.energeticstorage.utils.Reference; import net.seanomik.energeticstorage.utils.Utils; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -26,18 +28,21 @@ public class BlockPlaceListener implements Listener { if (Utils.isBlockASystem(block)) { if (PermissionChecks.canCreateSystem(player)) { - ESSystem newSystem = new ESSystem(player.getUniqueId(), UUID.randomUUID(), block.getLocation()); - PlayersFile.savePlayerSystem(newSystem); + // Cache and store the new system on another thread + Bukkit.getScheduler().runTaskAsynchronously(EnergeticStorage.getPlugin(), () -> { + ESSystem newSystem = new ESSystem(player.getUniqueId(), UUID.randomUUID(), block.getLocation()); + PlayersFile.savePlayerSystem(newSystem); - // If the player already has a system then add it to their cached systems, else just add it. - if (Reference.ES_SYSTEMS.containsKey(player.getUniqueId())) { - List playerESSystems = new LinkedList<>(Reference.ES_SYSTEMS.get(player.getUniqueId())); - playerESSystems.add(newSystem); + // If the player already has a system then add it to their cached systems, else just add it. + if (Reference.ES_SYSTEMS.containsKey(player.getUniqueId())) { + List playerESSystems = new LinkedList<>(Reference.ES_SYSTEMS.get(player.getUniqueId())); + playerESSystems.add(newSystem); - Reference.ES_SYSTEMS.replace(player.getUniqueId(), playerESSystems); - } else { - Reference.ES_SYSTEMS.put(player.getUniqueId(), Collections.singletonList(newSystem)); - } + Reference.ES_SYSTEMS.replace(player.getUniqueId(), playerESSystems); + } else { + Reference.ES_SYSTEMS.put(player.getUniqueId(), Collections.singletonList(newSystem)); + } + }); } } } diff --git a/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java b/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java index 1c74c5f..36f2f74 100644 --- a/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java +++ b/src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java @@ -5,9 +5,9 @@ import net.seanomik.energeticstorage.utils.ItemConstructor; import net.seanomik.energeticstorage.utils.ItemSerialization; import net.seanomik.energeticstorage.utils.Reference; import net.seanomik.energeticstorage.utils.Utils; +import org.apache.commons.text.StringEscapeUtils; import org.bukkit.Material; import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.StringEscapeUtils; import org.bukkit.inventory.ItemStack; import org.json.simple.JSONArray; import org.json.simple.JSONObject; diff --git a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java index cc845df..61d465c 100644 --- a/src/main/java/net/seanomik/energeticstorage/utils/Utils.java +++ b/src/main/java/net/seanomik/energeticstorage/utils/Utils.java @@ -1,5 +1,7 @@ package net.seanomik.energeticstorage.utils; +import de.tr7zw.changeme.nbtapi.NBTCompound; +import org.jetbrains.annotations.Nullable; import de.tr7zw.changeme.nbtapi.NBTItem; import de.tr7zw.changeme.nbtapi.NBTTileEntity; import net.seanomik.energeticstorage.Skulls; @@ -12,7 +14,6 @@ import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import javax.annotation.Nullable; import java.util.*; public class Utils { @@ -132,13 +133,15 @@ public class Utils { public static boolean isBlockASystem(Block block) { NBTTileEntity blockNBT = new NBTTileEntity(block.getState()); String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - if (version.substring(0, 5).equals("v1_15") || version.substring(0, 5).equals("v1_14")) { - return blockNBT.getCompound("Owner").getCompound("Properties").getCompoundList("textures").get(0).getString("Value").equals(Skulls.Computer.getTexture()); - } else if (version.substring(0, 5).equals("v1_16")) { - return blockNBT.getCompound("SkullOwner").getCompound("Properties").getCompoundList("textures").get(0).getString("Value").equals(Skulls.Computer.getTexture()); + + NBTCompound ownerNBT; + if (version.startsWith("v1_16")) { + ownerNBT = blockNBT.getCompound("SkullOwner"); + } else { + ownerNBT = blockNBT.getCompound("Owner"); } - return false; + return ownerNBT.getCompound("Properties").getCompoundList("textures").get(0).getString("Value").equals(Skulls.Computer.getTexture()); } public static boolean isItemADrive(ItemStack item) {