From c7d50ab2b6ff665555a6c22156dadb2379515d50 Mon Sep 17 00:00:00 2001 From: copokbl Date: Thu, 23 May 2024 22:28:45 +1000 Subject: [PATCH 01/74] Abstract away basic bukkit api, incomplete --- pom.xml | 10 ++ .../net/serble/estools/Commands/Back.java | 24 +-- .../net/serble/estools/Commands/Buddha.java | 33 ++-- .../net/serble/estools/Commands/CChest.java | 56 +++--- .../net/serble/estools/Commands/ClearInv.java | 13 +- .../java/net/serble/estools/Commands/Day.java | 9 +- .../net/serble/estools/Commands/Dismount.java | 24 +-- .../net/serble/estools/Commands/EditSign.java | 38 +--- .../serble/estools/Commands/Give/Give.java | 30 ++-- .../estools/Commands/Give/GiveItem.java | 15 +- .../estools/Commands/Give/SetHandItem.java | 17 +- .../estools/Commands/MoveSpeed/FlySpeed.java | 11 +- .../estools/Commands/MoveSpeed/MoveSpeed.java | 12 +- .../estools/Commands/MoveSpeed/WalkSpeed.java | 11 +- .../estools/Commands/PowerPick/PowerAxe.java | 19 +- .../estools/Commands/PowerPick/PowerHoe.java | 19 +- .../estools/Commands/PowerPick/PowerPick.java | 19 +- .../Commands/PowerPick/PowerShovel.java | 25 ++- .../Commands/PowerPick/PowerSword.java | 21 +-- .../estools/Commands/PowerPick/PowerTool.java | 24 ++- .../estools/Commands/Teleport/TpAll.java | 18 +- .../estools/Commands/Teleport/TpHere.java | 15 +- .../serble/estools/Commands/Warps/Warp.java | 15 +- .../estools/Commands/Warps/WarpLocation.java | 6 +- .../estools/Commands/Warps/WarpManager.java | 21 ++- .../net/serble/estools/EntityCommand.java | 74 +++----- .../estools/Entrypoints/EsToolsBukkit.java | 18 ++ .../java/net/serble/estools/EsLocation.java | 61 +++++++ .../net/serble/estools/EsToolsCommand.java | 167 +++--------------- .../serble/estools/EsToolsTabCompleter.java | 4 +- src/main/java/net/serble/estools/Main.java | 93 ++++------ .../serble/estools/MultiPlayerCommand.java | 25 ++- .../java/net/serble/estools/Position.java | 62 +++++++ ...luginVersion.java => SemanticVersion.java} | 13 +- .../serble/estools/ServerApi/EsGameMode.java | 18 ++ .../Bukkit/BukkitCommandBlockSender.java | 27 +++ .../Bukkit/BukkitConsoleSender.java | 27 +++ .../Implementations/Bukkit/BukkitEntity.java | 80 +++++++++ .../Implementations/Bukkit/BukkitHelper.java | 98 ++++++++++ .../Bukkit/BukkitInventory.java | 65 +++++++ .../Bukkit/BukkitItemMeta.java | 25 +++ .../Bukkit/BukkitItemStack.java | 59 +++++++ .../Bukkit/BukkitLivingEntity.java | 88 +++++++++ .../Implementations/Bukkit/BukkitPlayer.java | 122 +++++++++++++ .../Implementations/Bukkit/BukkitServer.java | 104 +++++++++++ .../Implementations/Bukkit/BukkitWorld.java | 47 +++++ .../Interfaces/EsCommandBlockSender.java | 5 + .../ServerApi/Interfaces/EsCommandSender.java | 7 + .../ServerApi/Interfaces/EsConsoleSender.java | 5 + .../ServerApi/Interfaces/EsEntity.java | 13 ++ .../ServerApi/Interfaces/EsInventory.java | 10 ++ .../ServerApi/Interfaces/EsItemMeta.java | 6 + .../ServerApi/Interfaces/EsItemStack.java | 11 ++ .../ServerApi/Interfaces/EsLivingEntity.java | 9 + .../ServerApi/Interfaces/EsPlayer.java | 20 +++ .../Interfaces/EsServerSoftware.java | 15 ++ .../estools/ServerApi/Interfaces/EsWorld.java | 12 ++ .../estools/ServerApi/ServerPlatform.java | 19 ++ .../serble/estools/TabCompleteGenerator.java | 16 +- src/main/java/net/serble/estools/Updater.java | 4 +- src/main/java/net/serble/estools/Vault.java | 12 +- src/main/resources/plugin.yml | 2 +- 62 files changed, 1401 insertions(+), 547 deletions(-) create mode 100644 src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java create mode 100644 src/main/java/net/serble/estools/EsLocation.java create mode 100644 src/main/java/net/serble/estools/Position.java rename src/main/java/net/serble/estools/{PluginVersion.java => SemanticVersion.java} (74%) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsGameMode.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandBlockSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsConsoleSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java create mode 100644 src/main/java/net/serble/estools/ServerApi/ServerPlatform.java diff --git a/pom.xml b/pom.xml index 84e9508..340ea97 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,10 @@ jitpack.io https://jitpack.io + + papermc + https://repo.papermc.io/repository/maven-public/ + @@ -96,5 +100,11 @@ gson 2.11.0 + + dev.folia + folia-api + 1.20.4-R0.1-SNAPSHOT + provided + diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index 6db555a..6c34087 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -3,12 +3,12 @@ import java.util.HashMap; import java.util.UUID; +import net.serble.estools.EsLocation; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityEvent; @@ -18,20 +18,20 @@ import org.bukkit.event.player.PlayerTeleportEvent; public class Back extends EsToolsCommand implements Listener { - private static final HashMap prevLocations = new HashMap<>(); + private static final HashMap prevLocations = new HashMap<>(); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; if (prevLocations.containsKey(p.getUniqueId())) { p.teleport(prevLocations.get(p.getUniqueId())); send(sender, "&aTeleported to last location!"); @@ -44,13 +44,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St @EventHandler public void onDeath(PlayerDeathEvent e) { - if (Main.majorVersion > 1) { - prevLocations.put(e.getEntity().getUniqueId(), e.getEntity().getLocation()); + if (Main.minecraftVersion.getMinor() > 1) { + prevLocations.put(e.getEntity().getUniqueId(), BukkitHelper.fromBukkitLocation(e.getEntity().getLocation())); return; } try { - Player p = (Player)EntityEvent.class.getMethod("getEntity").invoke(e); + EsPlayer p = (EsPlayer)EntityEvent.class.getMethod("getEntity").invoke(e); prevLocations.put(p.getUniqueId(), p.getLocation()); } catch (Exception ex) { Bukkit.getLogger().severe(ex.toString()); @@ -60,7 +60,7 @@ public void onDeath(PlayerDeathEvent e) { @EventHandler public void onTeleport(PlayerTeleportEvent e) { if (equalsOr(e.getCause(), PlayerTeleportEvent.TeleportCause.COMMAND, PlayerTeleportEvent.TeleportCause.PLUGIN)) { - prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); + prevLocations.put(e.getPlayer().getUniqueId(), BukkitHelper.fromBukkitLocation(e.getPlayer().getLocation())); } } } diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 2aa7029..2e7a3e6 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -3,12 +3,11 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.LivingEntity; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; @@ -21,7 +20,7 @@ public class Buddha extends EntityCommand implements Listener { @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); FileConfiguration f = ConfigManager.load("gods.yml"); List buddhas = f.getStringList("buddhas"); @@ -29,8 +28,8 @@ public void onEnable() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - LivingEntity p; + public boolean execute(EsCommandSender sender, String[] args) { + EsLivingEntity p; int timer = -1; if (args.length == 0) { @@ -38,7 +37,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - p = (LivingEntity) sender; + p = (EsLivingEntity) sender; } else { p = getEntity(sender, args[0]); @@ -68,7 +67,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St Bukkit.getScheduler().cancelTask(taskId); } - send(sender, "&aBuddha mode &6disabled&a for &6%s", getEntityName(p)); + send(sender, "&aBuddha mode &6disabled&a for &6%s", p.getName()); } else { int taskId = -1; @@ -77,7 +76,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St if (timer >= 0) { timerStr = timer / 20d + " seconds"; - taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin, () -> currentPlayers.remove(uid), timer); + taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(Main.bukkitPlugin, () -> currentPlayers.remove(uid), timer); } currentPlayers.put(uid, taskId); @@ -85,14 +84,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St save(); } - send(sender, "&aBuddha mode &6enabled&a for &6%s&a for &6%s", getEntityName(p), timerStr); + send(sender, "&aBuddha mode &6enabled&a for &6%s&a for &6%s", p.getName(), timerStr); } return true; } private double getDamageFromEvent(EntityDamageEvent e) { - if (Main.majorVersion > 5) { + if (Main.minecraftVersion.getMinor() > 5) { return e.getDamage(); } @@ -105,7 +104,7 @@ private double getDamageFromEvent(EntityDamageEvent e) { } private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SameParameterValue") double d) { - if (Main.majorVersion > 5) { + if (Main.minecraftVersion.getMinor() > 5) { e.setDamage(d); return; } @@ -120,16 +119,16 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void damage(EntityDamageEvent e) { - if (!(e.getEntity() instanceof LivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { + if (!(e.getEntity() instanceof EsLivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { return; } - LivingEntity entity = (LivingEntity) e.getEntity(); + EsLivingEntity entity = (EsLivingEntity) e.getEntity(); // Get all our vars since Minecraft broke everything in 1.6 double damage = getDamageFromEvent(e); - double health = getHealth(entity); - double maxHealth = getMaxHealth(entity); + double health = entity.getHealth(); + double maxHealth = entity.getMaxHealth(); if (damage < health) { // Not lethal return; @@ -140,7 +139,7 @@ public void damage(EntityDamageEvent e) { double resultingDamageTaken = extraDamage % maxHealth; setDamageFromEvent(e, 0); - setHealth(entity, maxHealth - resultingDamageTaken); + entity.setHealth(maxHealth - resultingDamageTaken); } private static void save() { diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 271ad1c..c92fbca 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -3,47 +3,49 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.ConfigManager; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.*; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.*; +// TODO: This class has config and events public class CChest extends EsToolsCommand implements Listener { - public static HashMap cChests = new HashMap<>(); + public static HashMap cChests = new HashMap<>(); @Override public void onEnable() { - if (Main.majorVersion > 4) { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + if (Main.minecraftVersion.getMinor() > 4) { + Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); } } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - if (Main.majorVersion < 7) { + if (Main.minecraftVersion.getMinor() < 7) { send(sender, "&cWarning: CChest doesn't work to full capacity"); } - Player p = (Player)sender; + EsPlayer p = (EsPlayer) sender; - if (p.getGameMode().equals(GameMode.CREATIVE)) { + if (p.getGameMode().equals(EsGameMode.Creative)) { if (checkPerms(sender, "cchest.creative")) { return false; } @@ -54,14 +56,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } UUID uid = p.getUniqueId(); - Inventory inv = cChests.get(uid); + EsInventory inv = cChests.get(uid); if (inv == null) { - Inventory loaded = loadPlayer(p); + EsInventory loaded = loadPlayer(p); if (loaded != null) { inv = loaded; } else { - inv = Bukkit.createInventory(null, 54, translate("&1Creative Chest")); + inv = Main.server.createInventory(null, 54, translate("&1Creative Chest")); } } @@ -90,7 +92,7 @@ public void inventoryClick(final InventoryClickEvent e) { return; } - Inventory pInv = e.getWhoClicked().getInventory(); + org.bukkit.inventory.Inventory pInv = e.getWhoClicked().getInventory(); for (Map.Entry i : pInv.all(cursor.getType()).entrySet()) { ItemStack item = i.getValue(); @@ -113,7 +115,7 @@ public void inventoryClick(final InventoryClickEvent e) { } // If player inventory - if (!e.getClickedInventory().equals(cChests.get(uid))) { + if (!e.getClickedInventory().equals(cChests.get(uid))) { // TODO: This comparison doesn't work // Shift click into cChest if (equalsOr(e.getClick(), ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT) && currentItem != null) { e.setCancelled(true); @@ -157,14 +159,14 @@ public void inventoryClick(final InventoryClickEvent e) { } } - private static void setItemTask(Inventory inv, int slot, ItemStack item) { - Bukkit.getScheduler().runTask(Main.plugin, () -> inv.setItem(slot, item)); + private static void setItemTask(org.bukkit.inventory.Inventory inv, int slot, ItemStack item) { + Bukkit.getScheduler().runTask(Main.bukkitPlugin, () -> inv.setItem(slot, item)); } // Cancel drag if inside cChest @EventHandler public void onInventoryDrag(final InventoryDragEvent e) { - Inventory inv = cChests.get(e.getWhoClicked().getUniqueId()); + EsInventory inv = cChests.get(e.getWhoClicked().getUniqueId()); if (!e.getInventory().equals(inv)) { return; } @@ -178,7 +180,7 @@ public void onInventoryDrag(final InventoryDragEvent e) { } } - public static Inventory loadPlayer(Player p) { + public static EsInventory loadPlayer(EsPlayer p) { UUID uid = p.getUniqueId(); FileConfiguration f = ConfigManager.load("cchests/" + uid + ".yml"); @@ -188,16 +190,16 @@ public static Inventory loadPlayer(Player p) { } @SuppressWarnings("unchecked") - ItemStack[] content = ((ArrayList) Objects.requireNonNull(f.get("items"))).toArray(new ItemStack[0]); + EsItemStack[] content = ((ArrayList) Objects.requireNonNull(f.get("items"))).toArray(new EsItemStack[0]); - Inventory inv = Bukkit.createInventory(null, 54, translate("&1Creative Chest")); + EsInventory inv = Main.server.createInventory(null, 54, translate("&1Creative Chest")); inv.setContents(content); cChests.put(uid, inv); return inv; } - public static void savePlayer(Player p) { + public static void savePlayer(EsPlayer p) { UUID uid = p.getUniqueId(); if (!cChests.containsKey(uid)) { @@ -214,19 +216,19 @@ public void onClose(InventoryCloseEvent e) { UUID uid = e.getPlayer().getUniqueId(); if (e.getInventory().equals(cChests.get(uid))) { - savePlayer((Player)e.getPlayer()); + savePlayer((EsPlayer)e.getPlayer()); } } @EventHandler public void onQuit(PlayerQuitEvent e) { - savePlayer(e.getPlayer()); + savePlayer(new BukkitPlayer(e.getPlayer())); cChests.remove(e.getPlayer().getUniqueId()); } @EventHandler public void onKick(PlayerKickEvent e) { - savePlayer(e.getPlayer()); + savePlayer(new BukkitPlayer(e.getPlayer())); cChests.remove(e.getPlayer().getUniqueId()); } } diff --git a/src/main/java/net/serble/estools/Commands/ClearInv.java b/src/main/java/net/serble/estools/Commands/ClearInv.java index 99983b7..d84f683 100644 --- a/src/main/java/net/serble/estools/Commands/ClearInv.java +++ b/src/main/java/net/serble/estools/Commands/ClearInv.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.MultiPlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; @@ -11,15 +10,15 @@ public class ClearInv extends MultiPlayerCommand { private static final String usage = genUsage("/ci [player2]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - ArrayList players = new ArrayList<>(); + public boolean execute(EsCommandSender sender, String[] args) { + ArrayList players = new ArrayList<>(); if (args.length == 0) { if (isNotPlayer(sender, usage)) { return false; } - players.add((Player)sender); + players.add((EsPlayer) sender); } else { players = getPlayers(sender, args); @@ -28,7 +27,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - for (Player p : players) { + for (EsPlayer p : players) { p.getInventory().clear(); } diff --git a/src/main/java/net/serble/estools/Commands/Day.java b/src/main/java/net/serble/estools/Commands/Day.java index ff18a30..7b60114 100644 --- a/src/main/java/net/serble/estools/Commands/Day.java +++ b/src/main/java/net/serble/estools/Commands/Day.java @@ -1,19 +1,18 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Day extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ((Player)sender).getWorld().setTime(1000); + ((EsPlayer) sender).getWorld().setTime(1000); send(sender, "&aSet time to &6day"); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Dismount.java b/src/main/java/net/serble/estools/Commands/Dismount.java index dd76759..7a1f84f 100644 --- a/src/main/java/net/serble/estools/Commands/Dismount.java +++ b/src/main/java/net/serble/estools/Commands/Dismount.java @@ -2,10 +2,8 @@ import net.serble.estools.EntityCommand; import net.serble.estools.Main; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEntity; import java.util.ArrayList; import java.util.List; @@ -13,10 +11,10 @@ public class Dismount extends EntityCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - List targets = new ArrayList<>(); + public boolean execute(EsCommandSender sender, String[] args) { + List targets = new ArrayList<>(); for (String arg : args) { // Can only be living Entity in pre 1.2 - Entity entity = Main.majorVersion >= 2 ? getNonLivingEntity(sender, arg) : getEntity(sender, arg); + EsEntity entity = Main.minecraftVersion.getMinor() >= 2 ? getNonLivingEntity(sender, arg) : getEntity(sender, arg); if (entity == null) { return false; } @@ -24,21 +22,17 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } if (targets.isEmpty()) { - if (!(sender instanceof Entity)) { + if (!(sender instanceof EsEntity)) { send(sender, "&cConsole must specify a rider"); return false; } - targets.add((Entity) sender); + targets.add((EsEntity) sender); } boolean anySuccess = false; - for (Entity entity : targets) { - if (Main.majorVersion >= 2) { - anySuccess = anySuccess || entity.leaveVehicle(); - } else { - anySuccess = anySuccess || ((LivingEntity) sender).leaveVehicle(); - } + for (EsEntity entity : targets) { + anySuccess = anySuccess || entity.leaveVehicle(); } if (!anySuccess) { diff --git a/src/main/java/net/serble/estools/Commands/EditSign.java b/src/main/java/net/serble/estools/Commands/EditSign.java index 521d8ee..9a5e3a7 100644 --- a/src/main/java/net/serble/estools/Commands/EditSign.java +++ b/src/main/java/net/serble/estools/Commands/EditSign.java @@ -2,24 +2,21 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Bukkit; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.block.sign.SignSide; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; +// TODO: Command incomplete public class EditSign extends EsToolsCommand { private static final String usage = genUsage("/editsign [line]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } @@ -29,9 +26,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; - Block signB = getTargetBlock(p); + Block signB = p.getTargetBlock(); if (signB == null || !(signB.getState() instanceof Sign)) { send(sender, "&cYou must be looking at a sign!"); @@ -79,7 +76,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St String lineText = translate(lineTextBuilder.toString()).trim(); - if (Main.majorVersion >= 20) { + if (Main.minecraftVersion.getMinor() >= 20) { SignSide side = sign.getTargetSide(p); side.setLine(lineNum - 1, lineText); } else { @@ -98,7 +95,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 1) { @@ -107,7 +104,7 @@ public List tabComplete(CommandSender sender, String[] args, String lArg tab.add("3"); tab.add("4"); - if (Main.majorVersion >= 17) { // only add glow autocomplete if it exists + if (Main.minecraftVersion.getMinor() >= 17) { // only add glow autocomplete if it exists tab.add("glow"); tab.add("unglow"); } @@ -116,23 +113,6 @@ public List tabComplete(CommandSender sender, String[] args, String lArg return tab; } - public Block getTargetBlock(Player p) { - if (Main.majorVersion > 12) { - return p.getTargetBlockExact(5); - } else if (Main.majorVersion > 7) { - return p.getTargetBlock(null, 5); - } else { - try { - //noinspection JavaReflectionMemberAccess - return (Block) LivingEntity.class.getMethod("getTargetBlock", HashSet.class, int.class).invoke(p, null, 5); - } - catch (Exception e) { - Bukkit.getLogger().severe(e.toString()); - return null; - } - } - } - @SuppressWarnings("deprecation") public boolean setGlow(Sign sign, Player p, boolean glow) { if (Main.majorVersion >= 20) { diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index 98ec0f3..e3fcca7 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -1,13 +1,13 @@ package net.serble.estools.Commands.Give; import net.serble.estools.*; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.inventory.ItemStack; import java.io.*; import java.nio.file.Files; @@ -15,21 +15,21 @@ import java.util.Map.Entry; public class Give implements EsToolsTabCompleter { - private static HashMap materialNames; + private static HashMap materialNames; - public static ItemStack getItem(String name, int amount) { + public static EsItemStack getItem(String name, int amount) { name = name.toUpperCase(); - Material mat = materialNames.get(name); + String mat = materialNames.get(name); if (mat == null) { return null; } - return new ItemStack(mat, amount); + return Main.server.createItemStack(mat, amount); } @Override - public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + public List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args) { List tab = new ArrayList<>(); if (args.length == 1) { @@ -49,7 +49,7 @@ public List onTabComplete(CommandSender sender, Command command, String public static void enable() { // initialise hashmaps //noinspection Convert2Diamond - materialNames = new HashMap(); + materialNames = new HashMap(); // Load config FileConfiguration f = ConfigManager.load("give.yml"); @@ -69,7 +69,7 @@ public static void enable() { try { copyDefaultGiveYML(); - f = ConfigManager.load(new File(Main.plugin.getDataFolder(), "give.yml")); + f = ConfigManager.load(new File(Main.bukkitPlugin.getDataFolder(), "give.yml")); materials = loadItems(f); } catch (Exception ignored) { } } @@ -79,19 +79,19 @@ public static void enable() { boolean removeWithUnderscores = (boolean) f.get("settings.removeWithUnderscores"); for (Material mat : Material.values()) { - if (Main.majorVersion >= 12 && !mat.isItem()) continue; + if (Main.minecraftVersion.getMinor() >= 12 && !mat.isItem()) continue; String name = mat.toString().toUpperCase(); if (name.contains("_") && addWithoutUnderscores) { - materialNames.put(name.replace("_",""), mat); + materialNames.put(name.replace("_",""), mat.name()); if (removeWithUnderscores) { continue; } } - materialNames.put(name, mat); + materialNames.put(name, mat.name()); } // Load custom items @@ -99,7 +99,7 @@ public static void enable() { Material mat = Material.getMaterial(s.getValue().toUpperCase()); if (mat != null) { - materialNames.put(s.getKey().toUpperCase(), mat); + materialNames.put(s.getKey().toUpperCase(), mat.name()); } } } @@ -134,7 +134,7 @@ private static void copyDefaultGiveYML() throws IOException { //noinspection ResultOfMethodCallIgnored inputStream.read(buffer); - File targetFile = new File(Main.plugin.getDataFolder(), "give.yml"); + File targetFile = new File(Main.bukkitPlugin.getDataFolder(), "give.yml"); OutputStream outStream = Files.newOutputStream(targetFile.toPath()); outStream.write(buffer); @@ -142,7 +142,7 @@ private static void copyDefaultGiveYML() throws IOException { outStream.close(); } - public static ItemStack parseArgs(String[] args) { + public static EsItemStack parseArgs(String[] args) { if (args.length == 0) { throw new IllegalArgumentException(); } diff --git a/src/main/java/net/serble/estools/Commands/Give/GiveItem.java b/src/main/java/net/serble/estools/Commands/Give/GiveItem.java index 5bbae96..0c61b88 100644 --- a/src/main/java/net/serble/estools/Commands/Give/GiveItem.java +++ b/src/main/java/net/serble/estools/Commands/Give/GiveItem.java @@ -1,22 +1,21 @@ package net.serble.estools.Commands.Give; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class GiveItem extends EsToolsCommand { private static final String usage = genUsage("/i [amount]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ItemStack item; + EsItemStack item; try { item = Give.parseArgs(args); } catch (IllegalArgumentException e) { @@ -24,11 +23,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; if (item != null) { p.getInventory().addItem(item); - send(sender, "&aGave &6%s", item.getType().name()); + send(sender, "&aGave &6%s", item.getType()); } else { send(sender, "&cItem &6%s&c not found", args[0]); } diff --git a/src/main/java/net/serble/estools/Commands/Give/SetHandItem.java b/src/main/java/net/serble/estools/Commands/Give/SetHandItem.java index 617bc7d..b4b5ab9 100644 --- a/src/main/java/net/serble/estools/Commands/Give/SetHandItem.java +++ b/src/main/java/net/serble/estools/Commands/Give/SetHandItem.java @@ -1,22 +1,21 @@ package net.serble.estools.Commands.Give; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class SetHandItem extends EsToolsCommand { private static final String usage = genUsage("/h [amount]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ItemStack item; + EsItemStack item; try { item = Give.parseArgs(args); } catch (IllegalArgumentException e) { @@ -24,11 +23,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; if (item != null) { - setMainHand(p, item); - send(sender, "&aGave &6%s", item.getType().name()); + p.setMainHand(item); + send(sender, "&aGave &6%s", item.getType()); } else { send(sender, "&cItem &6%s&c not found", args[0]); } diff --git a/src/main/java/net/serble/estools/Commands/MoveSpeed/FlySpeed.java b/src/main/java/net/serble/estools/Commands/MoveSpeed/FlySpeed.java index ac4c109..98379c7 100644 --- a/src/main/java/net/serble/estools/Commands/MoveSpeed/FlySpeed.java +++ b/src/main/java/net/serble/estools/Commands/MoveSpeed/FlySpeed.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands.MoveSpeed; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Tuple; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import java.util.List; @@ -16,14 +15,14 @@ public String getUsage() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - Tuple, Float> result = getTargets(sender, args); + public boolean execute(EsCommandSender sender, String[] args) { + Tuple, Float> result = getTargets(sender, args); if (result == null) { return false; } - for (Player p : result.a()) { + for (EsPlayer p : result.a()) { p.setFlySpeed(result.b()); } diff --git a/src/main/java/net/serble/estools/Commands/MoveSpeed/MoveSpeed.java b/src/main/java/net/serble/estools/Commands/MoveSpeed/MoveSpeed.java index f1080a4..209dea2 100644 --- a/src/main/java/net/serble/estools/Commands/MoveSpeed/MoveSpeed.java +++ b/src/main/java/net/serble/estools/Commands/MoveSpeed/MoveSpeed.java @@ -1,9 +1,9 @@ package net.serble.estools.Commands.MoveSpeed; import net.serble.estools.MultiPlayerCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Tuple; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; @@ -11,20 +11,20 @@ public abstract class MoveSpeed extends MultiPlayerCommand { protected abstract String getUsage(); - public Tuple, Float> getTargets(CommandSender sender, String[] args) { + public Tuple, Float> getTargets(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, getUsage()); return null; } - ArrayList players = new ArrayList<>(); + ArrayList players = new ArrayList<>(); if (args.length == 1) { if (isNotPlayer(sender, getUsage())) { return null; } - players.add((Player)sender); + players.add((EsPlayer)sender); } else { players = getPlayers(sender, removeArgs(args, 1)); @@ -51,7 +51,7 @@ public Tuple, Float> getTargets(CommandSender sender, String[] args } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 1) { diff --git a/src/main/java/net/serble/estools/Commands/MoveSpeed/WalkSpeed.java b/src/main/java/net/serble/estools/Commands/MoveSpeed/WalkSpeed.java index e012537..479e01d 100644 --- a/src/main/java/net/serble/estools/Commands/MoveSpeed/WalkSpeed.java +++ b/src/main/java/net/serble/estools/Commands/MoveSpeed/WalkSpeed.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands.MoveSpeed; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Tuple; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import java.util.List; @@ -16,14 +15,14 @@ protected String getUsage() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - Tuple, Float> result = getTargets(sender, args); + public boolean execute(EsCommandSender sender, String[] args) { + Tuple, Float> result = getTargets(sender, args); if (result == null) { return false; } - for (Player p : result.a()) { + for (EsPlayer p : result.a()) { p.setWalkSpeed(result.b()); } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java index 54c7749..640725b 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java @@ -2,28 +2,25 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; public class PowerAxe extends EsToolsCommand { - private static ItemStack powerItem; + private static EsItemStack powerItem; public static void init() { - if (Main.majorVersion > 15) { - powerItem = new ItemStack(Material.NETHERITE_AXE); + if (Main.minecraftVersion.getMinor() > 15) { + powerItem = Main.server.createItemStack("NETHERITE_AXE", 1); } else { - powerItem = new ItemStack(Material.DIAMOND_AXE, 1); + powerItem = Main.server.createItemStack("DIAMOND_AXE", 1); } - PowerTool.setupItem(powerItem, Enchantment.DIG_SPEED); + PowerTool.setupItem(powerItem, "DIG_SPEED"); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { PowerTool.cmd(sender, powerItem); return true; } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java index c194248..0e93b09 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java @@ -2,28 +2,25 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; public class PowerHoe extends EsToolsCommand { - private static ItemStack powerItem; + private static EsItemStack powerItem; public static void init() { - if (Main.majorVersion > 15) { - powerItem = new ItemStack(Material.NETHERITE_HOE); + if (Main.minecraftVersion.getMinor() > 15) { + powerItem = Main.server.createItemStack("NETHERITE_HOE", 1); } else { - powerItem = new ItemStack(Material.DIAMOND_HOE, 1); + powerItem = Main.server.createItemStack("DIAMOND_HOE", 1); } - PowerTool.setupItem(powerItem, Enchantment.DIG_SPEED); + PowerTool.setupItem(powerItem, "DIG_SPEED"); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { PowerTool.cmd(sender, powerItem); return true; } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java index c9e59fb..663ca88 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java @@ -2,28 +2,25 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; public class PowerPick extends EsToolsCommand { - private static ItemStack powerItem; + private static EsItemStack powerItem; public static void init() { - if (Main.majorVersion > 15) { - powerItem = new ItemStack(Material.NETHERITE_PICKAXE); + if (Main.minecraftVersion.getMinor() > 15) { + powerItem = Main.server.createItemStack("NETHERITE_PICKAXE", 1); } else { - powerItem = new ItemStack(Material.DIAMOND_PICKAXE, 1); + powerItem = Main.server.createItemStack("DIAMOND_PICKAXE", 1); } - PowerTool.setupItem(powerItem, Enchantment.DIG_SPEED); + PowerTool.setupItem(powerItem, "DIG_SPEED"); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { PowerTool.cmd(sender, powerItem); return true; } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java index 9f20a2e..2cfb263 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java @@ -2,31 +2,26 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; - -import java.util.Objects; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; public class PowerShovel extends EsToolsCommand { - private static ItemStack powerItem; + private static EsItemStack powerItem; public static void init() { - if (Main.majorVersion > 15) { - powerItem = new ItemStack(Material.NETHERITE_SHOVEL); - } else if (Main.majorVersion > 12) { - powerItem = new ItemStack(Material.DIAMOND_SHOVEL); + if (Main.minecraftVersion.getMinor() > 15) { + powerItem = Main.server.createItemStack("NETHERITE_SHOVEL", 1); + } else if (Main.minecraftVersion.getMinor() > 12) { + powerItem = Main.server.createItemStack("DIAMOND_SHOVEL", 1); } else { - powerItem = new ItemStack(Objects.requireNonNull(Material.getMaterial("DIAMOND_SPADE")), 1); + powerItem = Main.server.createItemStack("DIAMOND_SPADE", 1); } - PowerTool.setupItem(powerItem, Enchantment.DIG_SPEED); + PowerTool.setupItem(powerItem, "DIG_SPEED"); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { PowerTool.cmd(sender, powerItem); return true; } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java index a4cba1a..94116f5 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java @@ -2,29 +2,24 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; - -import java.util.Objects; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; public class PowerSword extends EsToolsCommand { - private static ItemStack powerPick; + private static EsItemStack powerPick; public static void init() { - if (Main.majorVersion > 12) { - powerPick = new ItemStack(Material.SALMON); + if (Main.minecraftVersion.getMinor() > 12) { + powerPick = Main.server.createItemStack("SALMON", 1); } else { - powerPick = new ItemStack(Objects.requireNonNull(Material.getMaterial("RAW_FISH")), 1); + powerPick = Main.server.createItemStack("RAW_FISH", 1); } - PowerTool.setupItem(powerPick, Enchantment.DAMAGE_ALL); + PowerTool.setupItem(powerPick, "DAMAGE_ALL"); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { PowerTool.cmd(sender, powerPick); return true; } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java index c35b416..b30c213 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java @@ -2,11 +2,10 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class PowerTool { public static void init() { @@ -17,25 +16,24 @@ public static void init() { PowerSword.init(); } - public static void setupItem(ItemStack item, Enchantment enchantment) { - item.addUnsafeEnchantment(enchantment, 32767); + public static void setupItem(EsItemStack item, String enchantment) { + item.addEnchantment(enchantment, 32767); - if (Main.majorVersion > 10) { - ItemMeta im = item.getItemMeta(); - assert im != null; + if (Main.minecraftVersion.getMinor() > 10) { + EsItemMeta im = item.getItemMeta(); im.setUnbreakable(true); item.setItemMeta(im); } else { - item.addUnsafeEnchantment(Enchantment.DURABILITY, 32767); + item.addEnchantment("DURABILITY", 32767); } } - public static void cmd(CommandSender sender, ItemStack pp) { + public static void cmd(EsCommandSender sender, EsItemStack pp) { if (EsToolsCommand.isNotPlayer(sender)) { return; } - Player p = (Player)sender; + EsPlayer p = (EsPlayer) sender; p.getInventory().addItem(pp); EsToolsCommand.send(sender, "&aThere you go!"); diff --git a/src/main/java/net/serble/estools/Commands/Teleport/TpAll.java b/src/main/java/net/serble/estools/Commands/Teleport/TpAll.java index b48bf03..d721fd0 100644 --- a/src/main/java/net/serble/estools/Commands/Teleport/TpAll.java +++ b/src/main/java/net/serble/estools/Commands/Teleport/TpAll.java @@ -1,19 +1,19 @@ package net.serble.estools.Commands.Teleport; import net.serble.estools.EntityCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class TpAll extends EntityCommand { private static final String usage = genUsage("/tpall [entity]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - LivingEntity p; + public boolean execute(EsCommandSender sender, String[] args) { + EsLivingEntity p; if (args.length == 0) { - p = (LivingEntity) sender; + p = (EsLivingEntity) sender; if (isNotEntity(sender, usage)) { return false; @@ -22,13 +22,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St p = getEntity(sender, args[0]); } - for (LivingEntity t : getOnlinePlayers()) { + for (EsLivingEntity t : Main.server.getOnlinePlayers()) { assert p != null; - t.teleport(p); + t.teleport(p.getLocation()); } assert p != null; - send(sender, "&aTeleported all players to &6%s", getEntityName(p)); + send(sender, "&aTeleported all players to &6%s", p.getName()); return true; } } diff --git a/src/main/java/net/serble/estools/Commands/Teleport/TpHere.java b/src/main/java/net/serble/estools/Commands/Teleport/TpHere.java index df845c1..2ffed2d 100644 --- a/src/main/java/net/serble/estools/Commands/Teleport/TpHere.java +++ b/src/main/java/net/serble/estools/Commands/Teleport/TpHere.java @@ -1,15 +1,14 @@ package net.serble.estools.Commands.Teleport; import net.serble.estools.EntityCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class TpHere extends EntityCommand { private static final String usage = genUsage("/tphere [entity2] [entity3]..."); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotEntity(sender)) { return false; } @@ -19,18 +18,18 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - LivingEntity p = (LivingEntity) sender; + EsLivingEntity p = (EsLivingEntity) sender; for (String arg : args) { - LivingEntity t = getEntity(sender, arg); + EsLivingEntity t = getEntity(sender, arg); if (t != null) { - t.teleport(p); + t.teleport(p.getLocation()); } else { return false; } } - send(sender, "&aTeleported &6%s&a to &6%s", argsToString(args, 0), getEntityName(p)); + send(sender, "&aTeleported &6%s&a to &6%s", argsToString(args, 0), p.getName()); return true; } } diff --git a/src/main/java/net/serble/estools/Commands/Warps/Warp.java b/src/main/java/net/serble/estools/Commands/Warps/Warp.java index 93b20f4..354871c 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/Warp.java +++ b/src/main/java/net/serble/estools/Commands/Warps/Warp.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands.Warps; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.List; @@ -12,12 +11,12 @@ public class Warp extends EsToolsCommand { private static final String usage = genUsage("/warp "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - Player p = (Player)sender; + EsPlayer p = (EsPlayer) sender; if (args.length == 0) { send(p, usage); @@ -42,7 +41,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - private static boolean canUseWarp(Player p, WarpLocation warp) { + private static boolean canUseWarp(EsPlayer p, WarpLocation warp) { if (warp == null) { return false; } @@ -62,10 +61,10 @@ private static boolean canUseWarp(Player p, WarpLocation warp) { } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); - Player p = (Player)sender; + EsPlayer p = (EsPlayer)sender; if (args.length == 1) { for (WarpLocation warp : WarpManager.warps.values()) { diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java index eb7ce60..b5436e2 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java @@ -1,6 +1,6 @@ package net.serble.estools.Commands.Warps; -import org.bukkit.Location; +import net.serble.estools.EsLocation; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; @@ -9,7 +9,7 @@ @SerializableAs("WarpLocation") public class WarpLocation implements ConfigurationSerializable { - public Location location; + public EsLocation location; public String name; public boolean global; @@ -28,7 +28,7 @@ public Map serialize() { public static WarpLocation deserialize(Map args) { WarpLocation warp = new WarpLocation(); - warp.location = (Location)args.get("location"); + warp.location = (EsLocation)args.get("location"); warp.name = (String)args.get("name"); warp.global = (boolean)args.get("global"); diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java index 42d6a67..0442a2c 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java @@ -2,13 +2,12 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.ConfigManager; -import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; +import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; -import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.HashMap; @@ -64,7 +63,7 @@ private static void saveWarps() { ConfigManager.save("warps.yml", f); } - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { if (isNotPlayer(sender, consoleUsage)) { return false; @@ -142,7 +141,7 @@ warp.name, locationToString(warp.location), return false; } - if (createWarp((Player)sender, warpName, args, global)) { + if (createWarp((EsPlayer)sender, warpName, args, global)) { return false; } @@ -156,7 +155,7 @@ warp.name, locationToString(warp.location), return false; } - if (createWarp((Player)sender, warpName, args, global)) { + if (createWarp((EsPlayer)sender, warpName, args, global)) { return false; } @@ -172,10 +171,10 @@ warp.name, locationToString(warp.location), } // returns true if failed - private static boolean createWarp(Player p, String warpName, String[] args, boolean global) { + private static boolean createWarp(EsPlayer p, String warpName, String[] args, boolean global) { // /warps [LOCAL/global] [x] [y] [z] [yaw] [pitch] - Location loc; + EsLocation loc; if (args.length <= 3) { loc = p.getLocation(); } @@ -184,7 +183,7 @@ else if (args.length < 6) { // if you supplied only some of the coordinates, wha return true; } else { - loc = new Location(p.getWorld(), + loc = new EsLocation(p.getWorld(), parseCoordinate(args[3], p.getLocation().getX()), parseCoordinate(args[4], p.getLocation().getY()), parseCoordinate(args[5], p.getLocation().getZ()) @@ -221,7 +220,7 @@ else if (args.length < 6) { // if you supplied only some of the coordinates, wha } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); // /warps [LOCAL/global] [x] [y] [z] [yaw] [pitch] // /warps remove diff --git a/src/main/java/net/serble/estools/EntityCommand.java b/src/main/java/net/serble/estools/EntityCommand.java index ceccabb..3f3ac06 100644 --- a/src/main/java/net/serble/estools/EntityCommand.java +++ b/src/main/java/net/serble/estools/EntityCommand.java @@ -1,71 +1,55 @@ package net.serble.estools; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + import java.util.ArrayList; import java.util.List; import java.util.UUID; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.util.Vector; - public abstract class EntityCommand extends EsToolsCommand { @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); - if (sender instanceof Player) { - Player p = (Player) sender; + if (sender instanceof EsPlayer) { + EsPlayer p = (EsPlayer) sender; - List ens = p.getNearbyEntities(5, 5, 5); + List ens = p.getWorld().getNearbyEntities(p.getLocation(), 5, 5, 5); - Entity en = getTarget(p, ens); + EsEntity en = getTarget(p, ens); - if (en != null && !(en instanceof Player)) { + if (en != null && !(en instanceof EsPlayer)) { String eu = en.getUniqueId().toString(); tab.add(eu); } } - for (Player p : Bukkit.getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { tab.add(p.getName()); } return tab; } - public static LivingEntity getEntity(CommandSender sender, String name) { - Entity entity = getNonLivingEntity(sender, name); - if (entity instanceof LivingEntity) return (LivingEntity) entity; + public static EsLivingEntity getEntity(EsCommandSender sender, String name) { + EsEntity entity = getNonLivingEntity(sender, name); + if (entity instanceof EsLivingEntity) return (EsLivingEntity) entity; if (entity != null) { send(sender, "&cPlayer/Entity not found."); } return null; } - public static Entity getNonLivingEntity(CommandSender sender, String name) { - Entity p = Bukkit.getPlayer(name); + public static EsEntity getNonLivingEntity(EsCommandSender sender, String name) { + EsEntity p = Main.server.getPlayer(name); if (p == null) { try { UUID uid = UUID.fromString(name); - - if (Main.majorVersion > 11) { - p = Bukkit.getEntity(uid); - } else { - if (sender instanceof Player) { - List entities = ((Player)sender).getWorld().getEntities(); - - for (Entity e : entities) { - if (e.getUniqueId().equals(uid)) { - p = e; - break; - } - } - } - } + p = Main.server.getEntity(uid); if (p != null) { return p; @@ -78,19 +62,19 @@ public static Entity getNonLivingEntity(CommandSender sender, String name) { return p; } - public static T getTarget(final Entity entity, - final Iterable entities) { + public static T getTarget(final EsEntity entity, + final Iterable entities) { if (entity == null) return null; T target = null; final double threshold = 1; for (final T other : entities) { - final Vector n = other.getLocation().toVector() - .subtract(entity.getLocation().toVector()); - if (entity.getLocation().getDirection().normalize().crossProduct(n) + final Position n = other.getLocation() + .subtract(entity.getLocation()); + if (entity.getLocation().getDirection().normalise().crossProduct(n) .lengthSquared() < threshold - && n.normalize().dot( - entity.getLocation().getDirection().normalize()) >= 0) { + && n.normalise().dot( + entity.getLocation().getDirection().normalise()) >= 0) { if (target == null || target.getLocation().distanceSquared( entity.getLocation()) > other.getLocation() @@ -101,16 +85,16 @@ public static T getTarget(final Entity entity, return target; } - public static boolean isNotEntity(CommandSender sender, String usage, Object... a) { - if (!(sender instanceof LivingEntity)) { + public static boolean isNotEntity(EsCommandSender sender, String usage, Object... a) { + if (!(sender instanceof EsLivingEntity)) { send(sender, usage, a); return true; } return false; } - public static boolean isNotEntity(CommandSender sender) { - if (!(sender instanceof LivingEntity)) { + public static boolean isNotEntity(EsCommandSender sender) { + if (!(sender instanceof EsLivingEntity)) { send(sender, "&cYou must be a player to run this command!"); return true; } diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java new file mode 100644 index 0000000..9bd3f1a --- /dev/null +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -0,0 +1,18 @@ +package net.serble.estools.Entrypoints; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.ServerPlatform; +import org.bukkit.plugin.java.JavaPlugin; + +public class EsToolsBukkit extends JavaPlugin { + + @Override + public void onEnable() { + Main.bukkitPlugin = this; + Main main = new Main(ServerPlatform.Bukkit); + main.enable(); + } + + @Override + public void onDisable() { /* Needed for older versions, which require an onDisable method */ } +} diff --git a/src/main/java/net/serble/estools/EsLocation.java b/src/main/java/net/serble/estools/EsLocation.java new file mode 100644 index 0000000..73b8d0c --- /dev/null +++ b/src/main/java/net/serble/estools/EsLocation.java @@ -0,0 +1,61 @@ +package net.serble.estools; + +import net.serble.estools.ServerApi.Interfaces.EsWorld; + +public class EsLocation extends Position { + private final EsWorld world; + private Position direction; + private double yaw; + private double pitch; + + public EsLocation(EsWorld world, double x, double y, double z) { + super(x, y, z); + this.world = world; + } + + public EsLocation(EsWorld world, Position dir, double x, double y, double z) { + super(x, y, z); + this.world = world; + direction = dir; + } + + public EsLocation(EsWorld world, Position dir, double x, double y, double z, double yaw, double pitch) { + super(x, y, z); + this.world = world; + direction = dir; + this.yaw = yaw; + this.pitch = pitch; + } + + public EsWorld getWorld() { + return world; + } + + public double distanceTo(EsLocation other) { + return super.distanceTo(other); + } + + public Position getDirection() { + return direction; + } + + public void setDirection(Position direction) { + this.direction = direction; + } + + public double getPitch() { + return pitch; + } + + public double getYaw() { + return yaw; + } + + public void setPitch(double pitch) { + this.pitch = pitch; + } + + public void setYaw(double yaw) { + this.yaw = yaw; + } +} diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index cb8e49d..a0e1d98 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -1,26 +1,24 @@ package net.serble.estools; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.attribute.Attribute; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import java.util.*; public abstract class EsToolsCommand implements CommandExecutor, EsToolsTabCompleter { - public void onEnable() {} + public void onEnable() { } @Override - public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { - if (Main.majorVersion < 7) { + public List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args) { + if (Main.minecraftVersion.getMinor() < 7) { return new ArrayList<>(); } @@ -28,6 +26,11 @@ public List onTabComplete(CommandSender sender, Command command, String return fixTabComplete(tabComplete(sender, args, lArg), lArg); } + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + return execute(BukkitHelper.fromBukkitCommandSender(sender), args); + } + public static List fixTabComplete(List inList, String arg) { final String argL = arg.toLowerCase(); inList.removeIf(n -> !n.toLowerCase().startsWith(argL)); @@ -35,12 +38,14 @@ public static List fixTabComplete(List inList, String arg) { return inList; } - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { return new ArrayList<>(); } + + public abstract boolean execute(EsCommandSender sender, String[] args); - public static void send(CommandSender s, String m, Object... a) { - if (Main.majorVersion > 1) { + public static void send(EsCommandSender s, String m, Object... a) { + if (Main.minecraftVersion.getMinor() > 1) { s.sendMessage(translate(m, a)); return; } @@ -53,12 +58,8 @@ public static void send(CommandSender s, String m, Object... a) { } } - public static void send(Player s, String m, Object... a) { - s.sendMessage(translate(m, a)); - } - public static String translate(String m, Object... a) { - if (Main.majorVersion > 1) { + if (Main.minecraftVersion.getMinor() > 1) { return ChatColor.translateAlternateColorCodes('&', String.format(m, a)); } @@ -67,15 +68,15 @@ public static String translate(String m, Object... a) { return String.format(m, a).replace('&', '§'); } - public static boolean isNotPlayer(CommandSender sender, String usage, Object... a) { - if (!(sender instanceof Player)) { + public static boolean isNotPlayer(EsCommandSender sender, String usage, Object... a) { + if (!(sender instanceof EsPlayer)) { send(sender, usage, a); return true; } return false; } - public static boolean isNotPlayer(CommandSender sender) { + public static boolean isNotPlayer(EsCommandSender sender) { if (!(sender instanceof Player)) { send(sender, "&cYou must be a player to run this command!"); return true; @@ -100,11 +101,11 @@ public static double tryParseDouble(String obj, double defaultValue) { } // This is different from checkPerms because it doesn't display any messages - public static boolean hasPerm(CommandSender sender, String perm) { + public static boolean hasPerm(EsCommandSender sender, String perm) { return sender.hasPermission("estools." + perm); } - public static boolean checkPerms(CommandSender sender, String perm) { + public static boolean checkPerms(EsCommandSender sender, String perm) { if (!sender.hasPermission("estools." + perm)) { send(sender, "&cYou do not have permission to run this command."); return true; @@ -116,7 +117,7 @@ public static String genUsage(String use) { return "&r&c&lUsage: &r&c" + use.replace("\n", "\n "); } - public static Player getPlayer(CommandSender sender, String name) { + public static Player getPlayer(EsCommandSender sender, String name) { Player p = Bukkit.getPlayer(name); if (p == null) { @@ -158,127 +159,7 @@ public static double parseCoordinate(String coord, double playerLoc) { return tryParseDouble(coord, 0); } - public static String locationToString(Location loc) { + public static String locationToString(EsLocation loc) { return String.format("%d %d %d", Math.round(loc.getX()), Math.round(loc.getY()), Math.round(loc.getZ())); } - - public static ItemStack getMainHand(Player p) { - if (Main.majorVersion > 8) { - return p.getInventory().getItemInMainHand(); - } else { - //noinspection deprecation - return p.getInventory().getItemInHand(); - } - } - - public static void setMainHand(Player p, ItemStack is) { - if (Main.majorVersion > 8) { - p.getInventory().setItemInMainHand(is); - } else { - //noinspection deprecation - p.getInventory().setItemInHand(is); - } - } - - public static double getMaxHealth(LivingEntity p) { - if (Main.majorVersion > 8) { - return Objects.requireNonNull(p.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue(); - } else { - if (Main.majorVersion > 5) { - //noinspection deprecation - return p.getMaxHealth(); - } - - try { - return (double)(int)LivingEntity.class.getMethod("getMaxHealth").invoke(p); - } catch(Exception e) { - Bukkit.getLogger().severe(e.toString()); - return 20d; - } - } - } - - public static void setMaxHealth(LivingEntity p, double value) { - if (Main.majorVersion > 8) { - Objects.requireNonNull(p.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(value); - } else { - if (Main.majorVersion > 5) { - //noinspection deprecation - p.setMaxHealth(value); - return; - } - - try { - //noinspection JavaReflectionMemberAccess - LivingEntity.class.getMethod("setMaxHealth", int.class).invoke(p, (int)value); - } - catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - } - } - } - - public static double getHealth(LivingEntity p) { - if (Main.majorVersion > 5) { - return p.getHealth(); - } - - try { - return (double)(int)LivingEntity.class.getMethod("getHealth").invoke(p); - } - catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - return 20d; - } - } - - public static void setHealth(LivingEntity p, double value) { - if (Main.majorVersion > 5) { - p.setHealth(value); - return; - } - - try { - //noinspection JavaReflectionMemberAccess - LivingEntity.class.getMethod("setHealth", int.class).invoke(p, (int)value); - } - catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - } - } - - public static String getEntityName(Entity p) { - if (Main.majorVersion > 7) { - return p.getName(); - } - - if (p instanceof Player) { - return ((Player)p).getDisplayName(); - } - - if (p instanceof LivingEntity) { - String name = ((LivingEntity)p).getCustomName(); - if (name != null) { - return name; - } - } - - return "Entity"; - } - - public static Collection getOnlinePlayers() { - try { - if (Bukkit.class.getMethod("getOnlinePlayers").getReturnType() == Collection.class) { - return Bukkit.getOnlinePlayers(); - } - else { - Player[] players = (Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); - return Arrays.asList(players); - } - - } catch (Exception e) { - Bukkit.getLogger().severe(e.toString()); - return new ArrayList<>(); - } - } } diff --git a/src/main/java/net/serble/estools/EsToolsTabCompleter.java b/src/main/java/net/serble/estools/EsToolsTabCompleter.java index e539383..bcc779e 100644 --- a/src/main/java/net/serble/estools/EsToolsTabCompleter.java +++ b/src/main/java/net/serble/estools/EsToolsTabCompleter.java @@ -1,7 +1,7 @@ package net.serble.estools; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.command.PluginCommand; import java.util.List; @@ -11,5 +11,5 @@ default void register(PluginCommand cmd) { cmd.setTabCompleter(TabCompleteGenerator.generate(this)); } - List onTabComplete(CommandSender sender, Command command, String alias, String[] args); + List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args); } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index a301b6e..7365811 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -8,6 +8,8 @@ import net.serble.estools.Commands.PowerPick.*; import net.serble.estools.Commands.Teleport.*; import net.serble.estools.Commands.Warps.*; +import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; +import net.serble.estools.ServerApi.ServerPlatform; import net.serble.estools.Signs.SignMain; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; @@ -15,9 +17,9 @@ import org.bukkit.GameMode; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.plugin.java.JavaPlugin; import net.serble.estools.Commands.*; +import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; @@ -25,51 +27,59 @@ import java.nio.file.Files; @SuppressWarnings("UnusedReturnValue") -public class Main extends JavaPlugin { +public class Main { public static Main plugin; - public static int majorVersion; - public static int minorVersion; + public static ServerPlatform platform; + public static SemanticVersion minecraftVersion; public static boolean tabCompleteEnabled = true; private static FileConfiguration config; // Get with overriden getConfig() method - public static PluginVersion newVersion = null; // The version available to download + public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; + public static EsServerSoftware server; + public static JavaPlugin bukkitPlugin; private static final int bStatsId = 21760; - - @Override - public void onEnable() { + + public Main(ServerPlatform plat) { + platform = plat; + } + + public void enable() { plugin = this; + server = platform.getServerInstance(); - try { - Vault.setupEconomy(); - } catch (Exception e) { - Bukkit.getLogger().warning("No Vault plugin found, please install vault for economy functionality."); - } + minecraftVersion = server.getVersion(); - getVersion(); // Set the major and minor version variables + if (platform == ServerPlatform.Bukkit) { + try { + Vault.setupEconomy(); + } catch (Exception e) { + Bukkit.getLogger().warning("No Vault plugin found, please install vault for economy functionality."); + } + } // Create config if not exists, saveDefaultConfig() doesn't exist in 1.0 - File configFile = new File(plugin.getDataFolder(), "config.yml"); + File configFile = new File(bukkitPlugin.getDataFolder(), "config.yml"); if (!configFile.exists()) { saveResource("config.yml", false); } config = ConfigManager.load("config.yml"); // Add keys that don't exist from the default config - if (ConfigManager.patchDefaults(getConfig(), getResource("config.yml"))) { + if (ConfigManager.patchDefaults(getConfig(), bukkitPlugin.getResource("config.yml"))) { ConfigManager.save("config.yml", getConfig()); // Only save if something changed } // Metrics if (getConfig().getBoolean("metrics", true)) { - Metrics metrics = new Metrics(this, bStatsId); + Metrics metrics = new Metrics(bukkitPlugin, bStatsId); metrics.addCustomChart(new SimplePie("vault_enabled", () -> String.valueOf(Vault.economy != null))); Bukkit.getLogger().info("Started bStat metrics"); } else { Bukkit.getLogger().info("Metrics are disabled"); } - if (majorVersion <= 2) { + if (minecraftVersion.getMinor() <= 2) { Bukkit.getLogger().info("Tab completion is not supported for versions 1.2 and below."); tabCompleteEnabled = false; } @@ -146,7 +156,7 @@ public void onEnable() { sc("dismount", "mount", new Dismount()); // Load other features - if (majorVersion > 0) { // Enchants and events don't work on 1.0.0 + if (minecraftVersion.getMinor() > 0) { // Enchants and events don't work on 1.0.0 PowerTool.init(); SignMain.init(); } @@ -155,14 +165,10 @@ public void onEnable() { Updater.checkForUpdate(); } - @Override - public void onDisable() { /* Needed for older versions, which require an onDisable method */ } - - // Setup Command Overloads public PluginCommand sc(String name, EsToolsCommand ce) { - PluginCommand cmd = getCommand(name); + PluginCommand cmd = bukkitPlugin.getCommand(name); assert cmd != null; cmd.setExecutor(ce); ce.onEnable(); @@ -174,7 +180,7 @@ public PluginCommand sc(String name, EsToolsCommand ce) { } public PluginCommand sc(String name, EsToolsCommand ce, int minVer) { - if (Main.majorVersion >= minVer) return sc(name, ce); + if (minecraftVersion.getMinor() >= minVer) return sc(name, ce); else return sc(name, new WrongVersion(minVer)); } @@ -188,7 +194,7 @@ public PluginCommand sc(String name, EsToolsCommand ce, EsToolsTabCompleter tc) public PluginCommand sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter tc, int minMajor, int minMinor) { String versionName = minMajor + "." + minMinor; - if (Main.majorVersion > minMajor || (Main.majorVersion == minMajor && Main.minorVersion >= minMinor)) { + if (minecraftVersion.getMinor() > minMajor || (minecraftVersion.getMinor() == minMajor && minecraftVersion.getPatch() >= minMinor)) { if (tc == null) { return sc(name, perm, ce); } @@ -201,7 +207,7 @@ public PluginCommand sc(String name, String perm, EsToolsCommand ce) { PluginCommand cmd = sc(name, ce); cmd.setPermission("estools." + perm); - if (Main.majorVersion > 0) { // Permission errors weren't a thing in 1.0 + if (minecraftVersion.getMinor() > 0) { // Permission errors weren't a thing in 1.0 //noinspection deprecation, is still useful in pre 1.13 and technically is useful in rare situations post 1.13 cmd.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); } @@ -213,7 +219,7 @@ public PluginCommand sc(String name, String perm, EsToolsCommand ce) { } public PluginCommand sc(String name, String perm, EsToolsCommand ce, int minVer) { - if (Main.majorVersion >= minVer) return sc(name, perm, ce); + if (minecraftVersion.getMinor() >= minVer) return sc(name, perm, ce); else return sc(name, perm, new WrongVersion(minVer)); } @@ -225,42 +231,15 @@ public PluginCommand sc(String name, String perm, EsToolsCommand ce, EsToolsTabC return cmd; } - private void getVersion() { // Parse the minecraft version from the Bukkit version string - String versionS = Bukkit.getVersion(); - - if (versionS.contains("(MC: ")) { - int posOfMC = versionS.indexOf("(MC: ") + 5; - versionS = versionS.substring(posOfMC, versionS.indexOf(')', posOfMC)); - } else { - Bukkit.getLogger().warning("Could not detect version from: " + versionS); - return; - } - - for (int i = 0; i < 99; i++) { - if (versionS.contains("1." + i)) { - majorVersion = i; - } - } - - for (int i = 0; i < 99; i++) { - if (versionS.contains("1." + majorVersion + '.' + i)) { - minorVersion = i; - } - } - - Bukkit.getLogger().info("Version detected as: 1." + majorVersion + '.' + minorVersion + " from: " + versionS); - } - // Overriding to create more predictable behaviour between versions - @Override public FileConfiguration getConfig() { return config; } // Overriding because method doesn't exist in very early versions public void saveResource(String res, boolean replace) { - InputStream resource = getResource(res); - File file = new File(getDataFolder(), res); + InputStream resource = bukkitPlugin.getResource(res); + File file = new File(bukkitPlugin.getDataFolder(), res); if (file.exists()) { Bukkit.getLogger().info("Tried to copy resource but it already exists: " + res); diff --git a/src/main/java/net/serble/estools/MultiPlayerCommand.java b/src/main/java/net/serble/estools/MultiPlayerCommand.java index 0e40e23..3f69b3b 100644 --- a/src/main/java/net/serble/estools/MultiPlayerCommand.java +++ b/src/main/java/net/serble/estools/MultiPlayerCommand.java @@ -1,8 +1,7 @@ package net.serble.estools; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.List; @@ -10,10 +9,10 @@ public abstract class MultiPlayerCommand extends EsToolsCommand { @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); - for (Player p : getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { tab.add(p.getName()); } @@ -22,19 +21,19 @@ public List tabComplete(CommandSender sender, String[] args, String lArg return tab; } - public static ArrayList getPlayers(CommandSender sender, String[] names) { + public static ArrayList getPlayers(EsCommandSender sender, String[] names) { if (names.length == 0) { return new ArrayList<>(); } if (names[0].equals("*")) { - return new ArrayList<>(getOnlinePlayers()); + return new ArrayList<>(Main.server.getOnlinePlayers()); } - ArrayList players = new ArrayList<>(); + ArrayList players = new ArrayList<>(); for (String name : names) { - Player p = Bukkit.getPlayer(name); + EsPlayer p = Main.server.getPlayer(name); if (p == null) { send(sender, "&cPlayer &6%s&c not found.", name); @@ -46,14 +45,14 @@ public static ArrayList getPlayers(CommandSender sender, String[] names) return players; } - public static ArrayList getPlayers(CommandSender sender, String name) { + public static ArrayList getPlayers(EsCommandSender sender, String name) { if (name.equals("*")) { - return new ArrayList<>(getOnlinePlayers()); + return new ArrayList<>(Main.server.getOnlinePlayers()); } - ArrayList players = new ArrayList<>(); + ArrayList players = new ArrayList<>(); - Player p = Bukkit.getPlayer(name); + EsPlayer p = Main.server.getPlayer(name); if (p == null) { send(sender, "&cPlayer &6%s&c not found.", name); diff --git a/src/main/java/net/serble/estools/Position.java b/src/main/java/net/serble/estools/Position.java new file mode 100644 index 0000000..e95c4d2 --- /dev/null +++ b/src/main/java/net/serble/estools/Position.java @@ -0,0 +1,62 @@ +package net.serble.estools; + +public class Position { + private double x; + private double y; + private double z; + + public Position() { + + } + + public Position(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + + public double getZ() { + return z; + } + + public double distanceTo(Position other) { + return Math.sqrt(distanceSquared(other)); + } + + public double distanceSquared(Position other) { + return Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2) + Math.pow(z - other.z, 2); + } + + public Position subtract(Position other) { + return new Position(x - other.x, y - other.y, z - other.z); + } + + public Position normalise() { + double length = length(); + return new Position(x / length, y / length, z / length); + } + + public Position crossProduct(Position other) { + return new Position(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); + } + + public double lengthSquared() { + return (x * x + y * y + z * z); + } + + public double length() { + return Math.sqrt(x * x + y * y + z * z); + } + + public double dot(Position other) { + return x * other.x + y * other.y + z * other.z; + } +} diff --git a/src/main/java/net/serble/estools/PluginVersion.java b/src/main/java/net/serble/estools/SemanticVersion.java similarity index 74% rename from src/main/java/net/serble/estools/PluginVersion.java rename to src/main/java/net/serble/estools/SemanticVersion.java index 31d3537..be01462 100644 --- a/src/main/java/net/serble/estools/PluginVersion.java +++ b/src/main/java/net/serble/estools/SemanticVersion.java @@ -1,12 +1,12 @@ package net.serble.estools; -public class PluginVersion { +public class SemanticVersion { private final int major; private final int minor; private final int patch; private final String string; - public PluginVersion(String str) { + public SemanticVersion(String str) { String[] parts = str.split("\\."); major = Integer.parseInt(parts[0]); minor = Integer.parseInt(parts[1]); @@ -14,6 +14,13 @@ public PluginVersion(String str) { string = str; } + public SemanticVersion(int major, int minor, int patch) { + this.major = major; + this.minor = minor; + this.patch = patch; + string = major + "." + minor + "." + patch; + } + public int getMajor() { return major; } @@ -30,7 +37,7 @@ public String getString() { return string; } - public boolean isLowerThan(PluginVersion other) { + public boolean isLowerThan(SemanticVersion other) { if (getMajor() < other.getMajor()) { return true; } else if (getMajor() > other.getMajor()) { diff --git a/src/main/java/net/serble/estools/ServerApi/EsGameMode.java b/src/main/java/net/serble/estools/ServerApi/EsGameMode.java new file mode 100644 index 0000000..8a5bae2 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsGameMode.java @@ -0,0 +1,18 @@ +package net.serble.estools.ServerApi; + +public enum EsGameMode { + Survival(0), + Creative(1), + Adventure(2), + Spectator(3); + + private final int ordinal; + + EsGameMode(int val) { + ordinal = val; + } + + public int getOrdinal() { + return ordinal; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java new file mode 100644 index 0000000..0df47a2 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java @@ -0,0 +1,27 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsConsoleSender; +import org.bukkit.command.BlockCommandSender; + +public class BukkitCommandBlockSender implements EsConsoleSender { + private final BlockCommandSender bukkitSender; + + public BukkitCommandBlockSender(BlockCommandSender child) { + bukkitSender = child; + } + + @Override + public void sendMessage(String... msg) { + bukkitSender.sendMessage(msg); + } + + @Override + public boolean hasPermission(String node) { + return bukkitSender.hasPermission(node); + } + + @Override + public boolean isPermissionSet(String node) { + return bukkitSender.isPermissionSet(node); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java new file mode 100644 index 0000000..2d30ebd --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java @@ -0,0 +1,27 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsConsoleSender; +import org.bukkit.command.ConsoleCommandSender; + +public class BukkitConsoleSender implements EsConsoleSender { + private final ConsoleCommandSender bukkitSender; + + public BukkitConsoleSender(ConsoleCommandSender child) { + bukkitSender = child; + } + + @Override + public void sendMessage(String... msg) { + bukkitSender.sendMessage(msg); + } + + @Override + public boolean hasPermission(String node) { + return bukkitSender.hasPermission(node); + } + + @Override + public boolean isPermissionSet(String node) { + return bukkitSender.isPermissionSet(node); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java new file mode 100644 index 0000000..d7d0046 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -0,0 +1,80 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.EsLocation; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; + +import java.util.UUID; + +import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.toBukkitLocation; + +public class BukkitEntity implements EsEntity { + private final org.bukkit.entity.Entity bukkitEntity; + + public BukkitEntity(org.bukkit.entity.Entity bukkitEntity) { + this.bukkitEntity = bukkitEntity; + } + + @Override + public void teleport(EsLocation loc) { + bukkitEntity.teleport(toBukkitLocation(loc)); + } + + @Override + public String getName() { + if (Main.minecraftVersion.getMinor() > 7) { + return bukkitEntity.getName(); + } + + if (bukkitEntity instanceof Player) { + return ((Player)bukkitEntity).getDisplayName(); + } + + if (bukkitEntity instanceof LivingEntity) { + String name = ((LivingEntity)bukkitEntity).getCustomName(); + if (name != null) { + return name; + } + } + + return "Entity"; + } + + @Override + public UUID getUniqueId() { + return bukkitEntity.getUniqueId(); + } + + @Override + public boolean hasPermission(String node) { + return bukkitEntity.hasPermission(node); + } + + @Override + public boolean isPermissionSet(String node) { + return bukkitEntity.isPermissionSet(node); + } + + @Override + public void sendMessage(String... msg) { + bukkitEntity.sendMessage(msg); + } + + @Override + public EsLocation getLocation() { + return BukkitHelper.fromBukkitLocation(bukkitEntity.getLocation()); + } + + @Override + public boolean leaveVehicle() { + if (bukkitEntity instanceof LivingEntity) { + // Not redundant for below 1.3 + //noinspection RedundantCast + return ((LivingEntity) bukkitEntity).leaveVehicle(); + } else { + return bukkitEntity.leaveVehicle(); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java new file mode 100644 index 0000000..a8d9936 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -0,0 +1,98 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.EsLocation; +import net.serble.estools.Position; +import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +public class BukkitHelper { + public static Location toBukkitLocation(EsLocation loc) { + return new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); + } + + public static EsLocation fromBukkitLocation(Location loc) { + return new EsLocation( + new BukkitWorld(loc.getWorld()), + fromVector(loc.getDirection()), + loc.getX(), + loc.getY(), + loc.getZ(), + loc.getYaw(), + loc.getPitch()); + } + + public static Position fromVector(Vector vec) { + return new Position(vec.getX(), vec.getY(), vec.getZ()); + } + + public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { + if (sender instanceof Player) { + return new BukkitPlayer((Player) sender); + } + + if (sender instanceof org.bukkit.entity.LivingEntity) { + return new BukkitLivingEntity((org.bukkit.entity.LivingEntity) sender); + } + + if (sender instanceof org.bukkit.entity.Entity) { + return new BukkitEntity((org.bukkit.entity.Entity) sender); + } + + if (sender instanceof ConsoleCommandSender) { + return new BukkitConsoleSender((ConsoleCommandSender) sender); + } + + if (sender instanceof BlockCommandSender) { + return new BukkitCommandBlockSender((BlockCommandSender) sender); + } + + throw new RuntimeException("Unrecognised command sender"); + } + + public static EsEntity fromBukkitEntity(org.bukkit.entity.Entity entity) { + if (entity instanceof org.bukkit.entity.LivingEntity) { + return new BukkitLivingEntity((LivingEntity) entity); + } + + return new BukkitEntity(entity); + } + + public static GameMode toBukkitGameMode(EsGameMode mode) { + switch (mode) { + case Creative: + return GameMode.CREATIVE; + case Survival: + return GameMode.SURVIVAL; + case Adventure: + return GameMode.ADVENTURE; + case Spectator: + return GameMode.SPECTATOR; + } + + throw new RuntimeException("Invalid GameMode"); + } + + public static EsGameMode fromBukkitGameMode(GameMode mode) { + switch (mode) { + case CREATIVE: + return EsGameMode.Creative; + case SURVIVAL: + return EsGameMode.Survival; + case ADVENTURE: + return EsGameMode.Adventure; + case SPECTATOR: + return EsGameMode.Spectator; + } + + throw new RuntimeException("Invalid GameMode"); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java new file mode 100644 index 0000000..2148952 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -0,0 +1,65 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class BukkitInventory implements EsInventory { + private final org.bukkit.inventory.Inventory bukkitInv; + + public BukkitInventory(Inventory inv) { + bukkitInv = inv; + } + + @Override + public void setItem(int slot, EsItemStack item) { + bukkitInv.setItem(slot, ((BukkitItemStack) item).getBukkitItem()); + } + + @Override + public EsItemStack getItem(int slot) { + return new BukkitItemStack(bukkitInv.getItem(slot)); + } + + @Override + public void addItem(EsItemStack stack) { + bukkitInv.addItem(((BukkitItemStack) stack).getBukkitItem()); + } + + @Override + public void setContents(EsItemStack[] items) { + ItemStack[] bukkitItems = new ItemStack[items.length]; + for (int i = 0; i < items.length; i++) { + if (items[i] == null) { + bukkitItems[i] = null; + continue; + } + bukkitItems[i] = ((BukkitItemStack) items[i]).getBukkitItem(); + } + bukkitInv.setContents(bukkitItems); + } + + @Override + public EsItemStack[] getContents() { + org.bukkit.inventory.ItemStack[] bukkitItems = bukkitInv.getContents(); + EsItemStack[] items = new EsItemStack[bukkitItems.length]; + for (int i = 0; i < bukkitItems.length; i++) { + if (bukkitItems[i] == null) { + items[i] = null; + continue; + } + items[i] = new BukkitItemStack(bukkitItems[i]); + } + return items; + } + + @Override + public void clear() { + bukkitInv.clear(); + } + + public Inventory getBukkitInventory() { + return bukkitInv; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java new file mode 100644 index 0000000..746fca3 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -0,0 +1,25 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; + +public class BukkitItemMeta implements EsItemMeta { + private final org.bukkit.inventory.meta.ItemMeta bukkitMeta; + + public BukkitItemMeta(org.bukkit.inventory.meta.ItemMeta meta) { + bukkitMeta = meta; + } + + @Override + public void setUnbreakable(boolean val) { + bukkitMeta.setUnbreakable(val); + } + + @Override + public boolean isUnbreakable() { + return bukkitMeta.isUnbreakable(); + } + + public org.bukkit.inventory.meta.ItemMeta getBukkitMeta() { + return bukkitMeta; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java new file mode 100644 index 0000000..cd9e615 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -0,0 +1,59 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import org.bukkit.Material; +import org.bukkit.Registry; + +import java.util.Objects; + +public class BukkitItemStack implements EsItemStack { + private final org.bukkit.inventory.ItemStack bukkitItem; + + public BukkitItemStack(String mat, int amount) { + bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); + } + + public BukkitItemStack(org.bukkit.inventory.ItemStack child) { + bukkitItem = child; + } + + public org.bukkit.inventory.ItemStack getBukkitItem() { + return bukkitItem; + } + + @Override + public String getType() { + return bukkitItem.getType().name(); + } + + @Override + public void setType(String val) { + bukkitItem.setType(Material.valueOf(val)); + } + + @Override + public int getAmount() { + return bukkitItem.getAmount(); + } + + @Override + public void setAmount(int val) { + bukkitItem.setAmount(val); + } + + @Override + public void addEnchantment(String enchantment, int level) { + bukkitItem.addUnsafeEnchantment(Objects.requireNonNull(Registry.ENCHANTMENT.match(enchantment)), level); + } + + @Override + public EsItemMeta getItemMeta() { + return new BukkitItemMeta(bukkitItem.getItemMeta()); + } + + @Override + public void setItemMeta(EsItemMeta meta) { + bukkitItem.setItemMeta(((BukkitItemMeta) meta).getBukkitMeta()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java new file mode 100644 index 0000000..50ff4c8 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -0,0 +1,88 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; +import org.bukkit.Bukkit; +import org.bukkit.attribute.Attribute; + +import java.util.Objects; + +public class BukkitLivingEntity extends BukkitEntity implements EsLivingEntity { + private final org.bukkit.entity.LivingEntity bukkitEntity; + + public BukkitLivingEntity(org.bukkit.entity.LivingEntity entity) { + super(entity); + bukkitEntity = entity; + } + + @Override + public double getMaxHealth() { + if (Main.minecraftVersion.getMinor() > 8) { + return Objects.requireNonNull(bukkitEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue(); + } else { + if (Main.minecraftVersion.getMinor() > 5) { + //noinspection deprecation + return bukkitEntity.getMaxHealth(); + } + + try { + return (double)(int) org.bukkit.entity.LivingEntity.class.getMethod("getMaxHealth").invoke(bukkitEntity); + } catch(Exception e) { + Bukkit.getLogger().severe(e.toString()); + return 20d; + } + } + } + + @Override + public void setMaxHealth(double val) { + if (Main.minecraftVersion.getMinor() > 8) { + Objects.requireNonNull(bukkitEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(val); + } else { + if (Main.minecraftVersion.getMinor() > 5) { + //noinspection deprecation + bukkitEntity.setMaxHealth(val); + return; + } + + try { + //noinspection JavaReflectionMemberAccess + org.bukkit.entity.LivingEntity.class.getMethod("setMaxHealth", int.class).invoke(bukkitEntity, (int)val); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } + } + + @Override + public double getHealth() { + if (Main.minecraftVersion.getMinor() > 5) { + return bukkitEntity.getHealth(); + } + + try { + return (double)(int) org.bukkit.entity.LivingEntity.class.getMethod("getHealth").invoke(bukkitEntity); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + return 20d; + } + } + + @Override + public void setHealth(double val) { + if (Main.minecraftVersion.getMinor() > 5) { + bukkitEntity.setHealth(val); + return; + } + + try { + //noinspection JavaReflectionMemberAccess + org.bukkit.entity.LivingEntity.class.getMethod("setHealth", int.class).invoke(bukkitEntity, (int)val); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java new file mode 100644 index 0000000..028403e --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -0,0 +1,122 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsWorld; +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.entity.LivingEntity; + +import java.util.HashSet; + +public class BukkitPlayer extends BukkitLivingEntity implements EsPlayer { + private final org.bukkit.entity.Player bukkitPlayer; + + public BukkitPlayer(org.bukkit.entity.Player entity) { + super(entity); + bukkitPlayer = entity; + } + + public org.bukkit.entity.Player getBukkitPlayer() { + return bukkitPlayer; + } + + @Override + public int getFoodLevel() { + return bukkitPlayer.getFoodLevel(); + } + + @Override + public void setFoodLevel(int val) { + bukkitPlayer.setFoodLevel(val); + } + + @Override + public EsItemStack getMainHand() { + return new BukkitItemStack(bukkitGetMainHand()); + } + + @Override + public void setMainHand(EsItemStack item) { + setMainHandBukkit(bukkitPlayer, ((BukkitItemStack) item).getBukkitItem()); + } + + @Override + public void openInventory(EsInventory inv) { + bukkitPlayer.openInventory(((BukkitInventory) inv).getBukkitInventory()); + } + + @Override + public void closeInventory() { + bukkitPlayer.closeInventory(); + } + + @Override + public EsInventory getInventory() { + return new BukkitInventory(bukkitPlayer.getInventory()); + } + + @Override + public void setFlySpeed(float val) { + bukkitPlayer.setFlySpeed(val); + } + + @Override + public void setWalkSpeed(float val) { + bukkitPlayer.setWalkSpeed(val); + } + + @Override + public EsWorld getWorld() { + return new BukkitWorld(bukkitPlayer.getWorld()); + } + + @Override + public void setGameMode(EsGameMode mode) { + bukkitPlayer.setGameMode(BukkitHelper.toBukkitGameMode(mode)); + } + + @Override + public EsGameMode getGameMode() { + return BukkitHelper.fromBukkitGameMode(bukkitPlayer.getGameMode()); + } + + @Override + public Block getTargetBlock() { + if (Main.minecraftVersion.getMinor() > 12) { + return bukkitPlayer.getTargetBlockExact(5); + } else if (Main.minecraftVersion.getMinor() > 7) { + return bukkitPlayer.getTargetBlock(null, 5); + } else { + try { + //noinspection JavaReflectionMemberAccess + return (Block) LivingEntity.class.getMethod("getTargetBlock", HashSet.class, int.class).invoke(bukkitPlayer, null, 5); + } + catch (Exception e) { + Bukkit.getLogger().severe(e.toString()); + return null; + } + } + } + + private org.bukkit.inventory.ItemStack bukkitGetMainHand() { + if (Main.minecraftVersion.getMinor() > 8) { + return bukkitPlayer.getInventory().getItemInMainHand(); + } else { + //noinspection deprecation + return bukkitPlayer.getInventory().getItemInHand(); + } + } + + private void setMainHandBukkit(org.bukkit.entity.Player p, org.bukkit.inventory.ItemStack is) { + if (Main.minecraftVersion.getMinor() > 8) { + p.getInventory().setItemInMainHand(is); + } else { + //noinspection deprecation + p.getInventory().setItemInHand(is); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java new file mode 100644 index 0000000..fa21bf8 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -0,0 +1,104 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; +import net.serble.estools.SemanticVersion; +import net.serble.estools.ServerApi.Interfaces.*; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.InventoryHolder; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; + +public class BukkitServer implements EsServerSoftware { + @Override + public EsPlayer getPlayer(String name) { + return new BukkitPlayer(Bukkit.getPlayer(name)); + } + + @Override + public EsEntity getEntity(UUID uuid) { + if (Main.minecraftVersion.getMinor() > 11) { + return new BukkitEntity(Bukkit.getEntity(uuid)); + } + + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getUniqueId() == uuid) { + return new BukkitEntity(entity); + } + } + } + + return null; + } + + @Override + public SemanticVersion getVersion() { // Parse the minecraft version from the Bukkit version string + String versionS = Bukkit.getVersion(); + int minor = 0; + int patch = 0; + + if (versionS.contains("(MC: ")) { + int posOfMC = versionS.indexOf("(MC: ") + 5; + versionS = versionS.substring(posOfMC, versionS.indexOf(')', posOfMC)); + } else { + Bukkit.getLogger().warning("Could not detect version from: " + versionS); + throw new RuntimeException("Could not detect version"); + } + + for (int i = 0; i < 99; i++) { + if (versionS.contains("1." + i)) { + minor = i; + } + } + + for (int i = 0; i < 99; i++) { + if (versionS.contains("1." + minor + '.' + i)) { + patch = i; + } + } + + Bukkit.getLogger().info("Version detected as: 1." + minor + '.' + patch + " from: " + versionS); + return new SemanticVersion(1, minor, patch); + } + + @Override + public Collection getOnlinePlayers() { + try { + if (Bukkit.class.getMethod("getOnlinePlayers").getReturnType() == Collection.class) { + List players = new ArrayList<>(); + for (org.bukkit.entity.Player p : Bukkit.getOnlinePlayers()) { + players.add(new BukkitPlayer(p)); + } + return players; + } + else { + org.bukkit.entity.Player[] players = (org.bukkit.entity.Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); + List users = new ArrayList<>(); + for (org.bukkit.entity.Player p : players) { + users.add(new BukkitPlayer(p)); + } + return users; + } + + } catch (Exception e) { + Bukkit.getLogger().severe(e.toString()); + return new ArrayList<>(); + } + } + + @Override + public EsItemStack createItemStack(String material, int amount) { + return new BukkitItemStack(material, amount); + } + + @Override + public EsInventory createInventory(EsPlayer owner, int size, String title) { + InventoryHolder holder = owner == null ? null : ((BukkitPlayer) owner).getBukkitPlayer(); + return new BukkitInventory(Bukkit.createInventory(holder, size, title)); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java new file mode 100644 index 0000000..52ce8c2 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -0,0 +1,47 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import net.serble.estools.ServerApi.Interfaces.EsWorld; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class BukkitWorld implements EsWorld { + private final org.bukkit.World bukkitWorld; + + public BukkitWorld(org.bukkit.World world) { + bukkitWorld = world; + } + + @Override + public String getName() { + return bukkitWorld.getName(); + } + + @Override + public List getEntities() { + List bEntities = bukkitWorld.getEntities(); + List entities = new ArrayList<>(); + for (org.bukkit.entity.Entity bEntity : bEntities) { + entities.add(BukkitHelper.fromBukkitEntity(bEntity)); + } + return entities; + } + + @Override + public List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff) { + Collection bEntities = bukkitWorld.getNearbyEntities(BukkitHelper.toBukkitLocation(loc), xoff, yoff, zoff); + List entities = new ArrayList<>(); + for (org.bukkit.entity.Entity bEntity : bEntities) { + entities.add(BukkitHelper.fromBukkitEntity(bEntity)); + } + return entities; + } + + @Override + public void setTime(long time) { + bukkitWorld.setTime(time); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandBlockSender.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandBlockSender.java new file mode 100644 index 0000000..8eae6eb --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandBlockSender.java @@ -0,0 +1,5 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsCommandBlockSender extends EsCommandSender { + +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandSender.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandSender.java new file mode 100644 index 0000000..d260ccc --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCommandSender.java @@ -0,0 +1,7 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsCommandSender { + void sendMessage(String... msg); + boolean hasPermission(String node); + boolean isPermissionSet(String node); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsConsoleSender.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsConsoleSender.java new file mode 100644 index 0000000..951f672 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsConsoleSender.java @@ -0,0 +1,5 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsConsoleSender extends EsCommandSender { + +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java new file mode 100644 index 0000000..f95bdea --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.EsLocation; + +import java.util.UUID; + +public interface EsEntity extends EsCommandSender { + String getName(); + UUID getUniqueId(); + void teleport(EsLocation loc); + EsLocation getLocation(); + boolean leaveVehicle(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java new file mode 100644 index 0000000..114ba9c --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java @@ -0,0 +1,10 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsInventory { + void setItem(int slot, EsItemStack item); + EsItemStack getItem(int slot); + void addItem(EsItemStack stack); + void setContents(EsItemStack[] items); + EsItemStack[] getContents(); + void clear(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java new file mode 100644 index 0000000..1b141c4 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java @@ -0,0 +1,6 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsItemMeta { + void setUnbreakable(boolean val); + boolean isUnbreakable(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java new file mode 100644 index 0000000..9f8b048 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -0,0 +1,11 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsItemStack { + String getType(); + void setType(String val); + int getAmount(); + void setAmount(int amount); + void addEnchantment(String enchantment, int level); + EsItemMeta getItemMeta(); + void setItemMeta(EsItemMeta meta); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java new file mode 100644 index 0000000..2547115 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java @@ -0,0 +1,9 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsLivingEntity extends EsEntity { + double getMaxHealth(); + void setMaxHealth(double val); + double getHealth(); + void setHealth(double val); + void sendMessage(String... msg); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java new file mode 100644 index 0000000..15163d8 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -0,0 +1,20 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.ServerApi.EsGameMode; +import org.bukkit.block.Block; + +public interface EsPlayer extends EsLivingEntity { + int getFoodLevel(); + void setFoodLevel(int val); + EsItemStack getMainHand(); + void setMainHand(EsItemStack item); + void openInventory(EsInventory inv); + void closeInventory(); + EsInventory getInventory(); + void setFlySpeed(float val); + void setWalkSpeed(float val); + EsWorld getWorld(); + void setGameMode(EsGameMode mode); + EsGameMode getGameMode(); + Block getTargetBlock(); // TODO: Implement own Block class +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java new file mode 100644 index 0000000..6774eee --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -0,0 +1,15 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.SemanticVersion; + +import java.util.Collection; +import java.util.UUID; + +public interface EsServerSoftware { + EsPlayer getPlayer(String name); + EsEntity getEntity(UUID uuid); + SemanticVersion getVersion(); + Collection getOnlinePlayers(); + EsItemStack createItemStack(String material, int amount); + EsInventory createInventory(EsPlayer owner, int size, String title); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java new file mode 100644 index 0000000..93e9672 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java @@ -0,0 +1,12 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.EsLocation; + +import java.util.List; + +public interface EsWorld { + String getName(); + List getEntities(); + List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff); + void setTime(long time); +} diff --git a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java new file mode 100644 index 0000000..4a3c9fd --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java @@ -0,0 +1,19 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitServer; +import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; + +public enum ServerPlatform { + Bukkit, // Includes all derivatives + Folia; + + public EsServerSoftware getServerInstance() { + switch (this) { + case Bukkit: + return new BukkitServer(); + + default: + throw new IllegalArgumentException("Unsupported server software"); + } + } +} diff --git a/src/main/java/net/serble/estools/TabCompleteGenerator.java b/src/main/java/net/serble/estools/TabCompleteGenerator.java index 0572679..ddae793 100644 --- a/src/main/java/net/serble/estools/TabCompleteGenerator.java +++ b/src/main/java/net/serble/estools/TabCompleteGenerator.java @@ -1,9 +1,23 @@ package net.serble.estools; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; public class TabCompleteGenerator { + public static TabCompleter generate(EsToolsTabCompleter tabCompleter) { - return tabCompleter::onTabComplete; + return new TabCompleter() { + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) { + return tabCompleter.onTabComplete(BukkitHelper.fromBukkitCommandSender(sender), cmd, label, args); + } + }; } } diff --git a/src/main/java/net/serble/estools/Updater.java b/src/main/java/net/serble/estools/Updater.java index 00bb3d5..e8cd6c8 100644 --- a/src/main/java/net/serble/estools/Updater.java +++ b/src/main/java/net/serble/estools/Updater.java @@ -44,14 +44,14 @@ private static void checkForUpdateBlocking() { .get("browser_download_url") .getAsString(); - PluginVersion onlineVersion = new PluginVersion(response.get("tag_name").getAsString()); + SemanticVersion onlineVersion = new SemanticVersion(response.get("tag_name").getAsString()); String cVersion = Main.plugin.getDescription().getVersion(); if (cVersion.contains("-")) { cVersion = cVersion.substring(0, cVersion.indexOf("-")); } - PluginVersion currentVersion = new PluginVersion(cVersion); + SemanticVersion currentVersion = new SemanticVersion(cVersion); // Provide download URL just in case we force update waitingDownloadUrl = downloadUrl; diff --git a/src/main/java/net/serble/estools/Vault.java b/src/main/java/net/serble/estools/Vault.java index f376b0d..36bfc86 100644 --- a/src/main/java/net/serble/estools/Vault.java +++ b/src/main/java/net/serble/estools/Vault.java @@ -2,6 +2,8 @@ import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; @@ -30,7 +32,7 @@ public static void setupEconomy() { } - public static boolean takeMoney(double price, Player p) { + public static boolean takeMoney(double price, EsPlayer p) { if (price <= 0) { return true; } @@ -40,22 +42,22 @@ public static boolean takeMoney(double price, Player p) { return false; } - if (Vault.economy.getBalance(p) < price) { + if (Vault.economy.getBalance(((BukkitPlayer) p).getBukkitPlayer()) < price) { EsToolsCommand.send(p, "&cYou do not have enough money to purchase this item."); return false; } - Vault.economy.withdrawPlayer(p, price); + Vault.economy.withdrawPlayer(((BukkitPlayer) p).getBukkitPlayer(), price); return true; } - public static boolean payMoney(double price, Player p) { + public static boolean payMoney(double price, EsPlayer p) { if (Vault.economy == null) { EsToolsCommand.send(p, "&cVault is required for economy!"); return false; } - EconomyResponse economyResponse = Vault.economy.depositPlayer(p, price); + EconomyResponse economyResponse = Vault.economy.depositPlayer(((BukkitPlayer) p).getBukkitPlayer(), price); return economyResponse.transactionSuccess(); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fa7a96b..8f2d1d8 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ -main: net.serble.estools.Main +main: net.serble.estools.Entrypoints.EsToolsBukkit name: EsTools description: Simple highly compatible general purpose plugin. version: '${project.version}' From 0e824e4848ae57c66a97ae8a1aecdcf0bf6ea5ea Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Fri, 24 May 2024 10:32:53 +1000 Subject: [PATCH 02/74] Abstract blocks and effects and enchantments --- .../net/serble/estools/Commands/EditSign.java | 25 ++++------- .../java/net/serble/estools/Commands/Eff.java | 25 +++++------ .../net/serble/estools/Commands/Ench.java | 40 ++++++----------- .../net/serble/estools/Commands/Potion.java | 2 +- src/main/java/net/serble/estools/Effects.java | 40 +++++++---------- .../net/serble/estools/EsToolsCommand.java | 4 +- src/main/java/net/serble/estools/Main.java | 2 + .../Implementations/Bukkit/BukkitBlock.java | 37 ++++++++++++++++ .../Implementations/Bukkit/BukkitHelper.java | 32 ++++++++++++-- .../Bukkit/BukkitItemStack.java | 2 +- .../Bukkit/BukkitLivingEntity.java | 13 ++++++ .../Implementations/Bukkit/BukkitPlayer.java | 13 +++--- .../Implementations/Bukkit/BukkitServer.java | 36 ++++++++++++++++ .../Implementations/Bukkit/BukkitSign.java | 43 +++++++++++++++++++ .../Bukkit/BukkitSignSide.java | 27 ++++++++++++ .../estools/ServerApi/Interfaces/EsBlock.java | 8 ++++ .../ServerApi/Interfaces/EsLivingEntity.java | 2 + .../ServerApi/Interfaces/EsPlayer.java | 3 +- .../Interfaces/EsServerSoftware.java | 3 ++ .../estools/ServerApi/Interfaces/EsSign.java | 15 +++++++ .../ServerApi/Interfaces/EsSignSide.java | 8 ++++ 21 files changed, 285 insertions(+), 95 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSignSide.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsSignSide.java diff --git a/src/main/java/net/serble/estools/Commands/EditSign.java b/src/main/java/net/serble/estools/Commands/EditSign.java index 9a5e3a7..d7940ec 100644 --- a/src/main/java/net/serble/estools/Commands/EditSign.java +++ b/src/main/java/net/serble/estools/Commands/EditSign.java @@ -2,16 +2,11 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.block.Block; -import org.bukkit.block.Sign; -import org.bukkit.block.sign.SignSide; +import net.serble.estools.ServerApi.Interfaces.*; import java.util.ArrayList; import java.util.List; -// TODO: Command incomplete public class EditSign extends EsToolsCommand { private static final String usage = genUsage("/editsign [line]"); @@ -28,14 +23,14 @@ public boolean execute(EsCommandSender sender, String[] args) { EsPlayer p = (EsPlayer) sender; - Block signB = p.getTargetBlock(); + EsBlock signB = p.getTargetBlock(); - if (signB == null || !(signB.getState() instanceof Sign)) { + if (!(signB instanceof EsSign)) { send(sender, "&cYou must be looking at a sign!"); return false; } - Sign sign = (Sign) signB.getState(); + EsSign sign = (EsSign) signB; switch (args[0].toLowerCase()) { case "glow": @@ -77,10 +72,9 @@ public boolean execute(EsCommandSender sender, String[] args) { String lineText = translate(lineTextBuilder.toString()).trim(); if (Main.minecraftVersion.getMinor() >= 20) { - SignSide side = sign.getTargetSide(p); + EsSignSide side = sign.getTargetSide(p); side.setLine(lineNum - 1, lineText); } else { - //noinspection deprecation sign.setLine(lineNum - 1, lineText); } sign.update(); @@ -113,10 +107,9 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA return tab; } - @SuppressWarnings("deprecation") - public boolean setGlow(Sign sign, Player p, boolean glow) { - if (Main.majorVersion >= 20) { - SignSide side = sign.getTargetSide(p); + public boolean setGlow(EsSign sign, EsPlayer p, boolean glow) { + if (Main.minecraftVersion.getMinor() >= 20) { + EsSignSide side = sign.getTargetSide(p); if (side.isGlowingText() == glow) { if (glow) { @@ -130,7 +123,7 @@ public boolean setGlow(Sign sign, Player p, boolean glow) { side.setGlowingText(glow); } - else if (Main.majorVersion >= 17) { + else if (Main.minecraftVersion.getMinor() >= 17) { if (sign.isGlowingText() == glow) { if (glow) { send(p, "&cThis sign is already &6glowing!"); diff --git a/src/main/java/net/serble/estools/Commands/Eff.java b/src/main/java/net/serble/estools/Commands/Eff.java index 5fe7935..a7ed8ac 100644 --- a/src/main/java/net/serble/estools/Commands/Eff.java +++ b/src/main/java/net/serble/estools/Commands/Eff.java @@ -3,11 +3,8 @@ import net.serble.estools.Effects; import net.serble.estools.Main; import net.serble.estools.MultiPlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.List; @@ -17,13 +14,13 @@ public class Eff extends MultiPlayerCommand { private static final String usage = genUsage("/eff [amplifier] [duration] [players]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - PotionEffectType effect = Effects.getByName(args[0]); + String effect = Effects.getByName(args[0]); if (effect == null) { send(sender, "&cEffect not found!"); @@ -54,7 +51,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St durationStr = String.format("%s seconds", duration / 20); if (duration == 32767) { - if (Main.majorVersion >= 19) { + if (Main.minecraftVersion.getMinor() >= 19) { duration = -1; } @@ -62,14 +59,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - ArrayList players = new ArrayList<>(); + ArrayList players = new ArrayList<>(); if (args.length < 4) { if (isNotPlayer(sender, usage)) { return false; } - players.add((Player)sender); + players.add((EsPlayer) sender); } else { players = getPlayers(sender, removeArgs(args, 3)); @@ -78,9 +75,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - for (Player p : players) { + for (EsPlayer p : players) { p.removePotionEffect(effect); - p.addPotionEffect(new PotionEffect(effect, duration, amplifier)); + p.addPotionEffect(effect, duration, amplifier); } send(sender, "&aAdded effect &6%s&a at level &6%s&a for &6%s", Effects.getName(effect), amplifier + 1, durationStr); @@ -106,12 +103,12 @@ private int calculateDuration(String input) { } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); switch (args.length) { case 1: - for (Map.Entry e : Effects.entrySet()) { + for (Map.Entry e : Effects.entrySet()) { tab.add(e.getKey().toLowerCase()); } break; diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index 8af2cc8..6d1840b 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -6,13 +6,12 @@ import net.serble.estools.Enchantments; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.NamespacedKey; import org.bukkit.Registry; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import net.serble.estools.EsToolsCommand; @@ -20,13 +19,13 @@ public class Ench extends EsToolsCommand { private static final String usage = genUsage("/ench [level] [player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - ItemStack is; + EsItemStack is; int level = 1; if (args.length > 1) { @@ -43,32 +42,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - is = getMainHand(((Player) sender)); + is = ((EsPlayer) sender).getMainHand(); } else { - Player p = getPlayer(sender, args[2]); + EsPlayer p = getPlayer(sender, args[2]); if (p == null) { return false; } - is = getMainHand(p); + is = p.getMainHand(); } - Enchantment ench; - try { - if (Main.majorVersion > 12) { - ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(args[0].toLowerCase())); - } else { - ench = Enchantments.getByName(args[0].toLowerCase()); - } - } catch (IllegalArgumentException e) { - send(sender, usage); - return false; - } - - - if (ench != null && is != null) { - is.addUnsafeEnchantment(ench, level); + if (Main.server.doesEnchantmentExist(args[0].toLowerCase()) && is != null) { + is.addEnchantment(args[0].toLowerCase(), level); send(sender, "&aEnchantment &6%s&a at level &6%s&a was added!", args[0].toLowerCase(), level); } else { @@ -78,12 +64,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); switch (args.length) { case 1: - if (Main.majorVersion > 12) { + if (Main.minecraftVersion.getMinor() > 12) { for (Enchantment e : Registry.ENCHANTMENT) { tab.add(e.getKey().getKey()); } @@ -96,7 +82,7 @@ public List tabComplete(CommandSender sender, String[] args, String lArg break; case 3: - for (Player p : getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { tab.add(p.getName()); } break; diff --git a/src/main/java/net/serble/estools/Commands/Potion.java b/src/main/java/net/serble/estools/Commands/Potion.java index 154abd0..3ef9a20 100644 --- a/src/main/java/net/serble/estools/Commands/Potion.java +++ b/src/main/java/net/serble/estools/Commands/Potion.java @@ -106,7 +106,7 @@ public List tabComplete(CommandSender sender, String[] args, String lArg switch (args.length) { case 1: if (Main.majorVersion <= 8) { - for (Map.Entry e : Effects.potionEntrySet()) { + for (Map.Entry e : Effects.getPotionList()) { tab.add(e.getKey().toLowerCase()); } } diff --git a/src/main/java/net/serble/estools/Effects.java b/src/main/java/net/serble/estools/Effects.java index 6bb0915..fa00012 100644 --- a/src/main/java/net/serble/estools/Effects.java +++ b/src/main/java/net/serble/estools/Effects.java @@ -3,26 +3,22 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; -@SuppressWarnings("deprecation") public class Effects { - private static final Map EFFECTS = new HashMap<>(); + private static final Map EFFECTS = new HashMap<>(); private static final Map POTIONS = new HashMap<>(); - public static PotionEffectType getByName(String name) { - return EFFECTS.get(name.toLowerCase(Locale.ENGLISH)); + public static String getByName(String name) { + return EFFECTS.get(name); } public static PotionType getPotionByName(String name) { return POTIONS.get(name.toLowerCase(Locale.ENGLISH)); } - public static String getName(PotionEffectType effect) { - for (Map.Entry a : EFFECTS.entrySet()) { + public static String getName(String effect) { + for (Map.Entry a : EFFECTS.entrySet()) { if (a.getValue().equals(effect)) { return a.getKey(); } @@ -31,16 +27,16 @@ public static String getName(PotionEffectType effect) { return "invalid"; } - public static Set> entrySet() { + public static Set> entrySet() { return EFFECTS.entrySet(); } - public static Set> potionEntrySet() { + public static Set> getPotionList() { return POTIONS.entrySet(); } - static { - if (Main.majorVersion <= 8) { // 1.8 only potions be funky + public static void load() { + if (Main.minecraftVersion.getMinor() <= 8) { // 1.8 only potions be funky POTIONS.put("regeneration", PotionType.REGEN); POTIONS.put("speed", PotionType.SPEED); POTIONS.put("fire_resistance", PotionType.FIRE_RESISTANCE); @@ -51,16 +47,16 @@ public static Set> potionEntrySet() { POTIONS.put("slowness", PotionType.SLOWNESS); POTIONS.put("instant_damage", PotionType.INSTANT_DAMAGE); - if (Main.majorVersion >= 4 && Main.minorVersion >= 2) { // Night vision was added in 1.4.2 + if (Main.minecraftVersion.getMinor() >= 4 && Main.minecraftVersion.getMinor() >= 2) { // Night vision was added in 1.4.2 POTIONS.put("night_vision", PotionType.NIGHT_VISION); POTIONS.put("invisibility", PotionType.INVISIBILITY); } - if (Main.majorVersion >= 7 && Main.minorVersion >= 2) { // 1.7.2 + if (Main.minecraftVersion.getMinor() >= 7 && Main.minecraftVersion.getMinor() >= 2) { // 1.7.2 POTIONS.put("water_breathing", PotionType.WATER); } - if (Main.majorVersion >= 8) { // 1.8 + if (Main.minecraftVersion.getMinor() >= 8) { // 1.8 POTIONS.put("jump_boost", PotionType.JUMP); } } @@ -77,18 +73,14 @@ public static Set> potionEntrySet() { put("jump", "jump_boost"); }}; - for (PotionEffectType p : PotionEffectType.values()) { - if (p == null) { // 1.1 has null values in here... - continue; - } - - String name = p.getName().toLowerCase(Locale.ENGLISH); + for (String s : Main.server.getPotionEffectTypes()) { + String name = s.toLowerCase(); if (nameReplacers.containsKey(name)) { name = nameReplacers.get(name); } - EFFECTS.put(name, p); + EFFECTS.put(name, s.toLowerCase()); } } } diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index a0e1d98..aef4fed 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -117,8 +117,8 @@ public static String genUsage(String use) { return "&r&c&lUsage: &r&c" + use.replace("\n", "\n "); } - public static Player getPlayer(EsCommandSender sender, String name) { - Player p = Bukkit.getPlayer(name); + public static EsPlayer getPlayer(EsCommandSender sender, String name) { + EsPlayer p = Main.server.getPlayer(name); if (p == null) { send(sender, "&cPlayer not found."); diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 7365811..96a05d8 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -50,6 +50,8 @@ public void enable() { minecraftVersion = server.getVersion(); + Effects.load(); + if (platform == ServerPlatform.Bukkit) { try { Vault.setupEconomy(); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java new file mode 100644 index 0000000..db715a4 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java @@ -0,0 +1,37 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsBlock; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; + +public class BukkitBlock implements EsBlock { + private final BlockState bukkitState; + + public BukkitBlock(BlockState block) { + bukkitState = block; + } + + private Block getBukkitBlock() { + return bukkitState.getBlock(); + } + + @Override + public boolean breakNaturally() { + return getBukkitBlock().breakNaturally(); + } + + @Override + public int getX() { + return getBukkitBlock().getX(); + } + + @Override + public int getY() { + return getBukkitBlock().getY(); + } + + @Override + public int getZ() { + return getBukkitBlock().getZ(); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index a8d9936..14496a4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -1,19 +1,25 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.EsLocation; +import net.serble.estools.Main; import net.serble.estools.Position; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsBlock; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEntity; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.util.Vector; +import java.util.Objects; + public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { return new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); @@ -95,4 +101,24 @@ public static EsGameMode fromBukkitGameMode(GameMode mode) { throw new RuntimeException("Invalid GameMode"); } + + public static EsBlock fromBukkitBlock(Block block) { + BlockState state = block.getState(); + + if (state instanceof Sign) { + return new BukkitSign((Sign) state); + } + + return new BukkitBlock(state); + } + + @SuppressWarnings("deprecation") + public static Enchantment getBukkitEnchantment(String name) { + if (Main.minecraftVersion.getMinor() >= 13) { + Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name))); + } + + // We have to use deprecated method for pre 1.13 + return Enchantment.getByName(name); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index cd9e615..bb1394e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -44,7 +44,7 @@ public void setAmount(int val) { @Override public void addEnchantment(String enchantment, int level) { - bukkitItem.addUnsafeEnchantment(Objects.requireNonNull(Registry.ENCHANTMENT.match(enchantment)), level); + bukkitItem.addUnsafeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment), level); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index 50ff4c8..fc11b38 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -3,7 +3,10 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; +import org.bukkit.Registry; import org.bukkit.attribute.Attribute; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import java.util.Objects; @@ -85,4 +88,14 @@ public void setHealth(double val) { Bukkit.getLogger().severe(ex.toString()); } } + + @Override + public void addPotionEffect(String effect, int duration, int amplifier) { + bukkitEntity.addPotionEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect)), duration, amplifier)); + } + + @Override + public void removePotionEffect(String effect) { + bukkitEntity.removePotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect))); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 028403e..ae6211b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -2,15 +2,14 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.Interfaces.EsInventory; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import net.serble.estools.ServerApi.Interfaces.EsWorld; +import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.Nullable; import java.util.HashSet; +import java.util.Objects; public class BukkitPlayer extends BukkitLivingEntity implements EsPlayer { private final org.bukkit.entity.Player bukkitPlayer; @@ -85,7 +84,11 @@ public EsGameMode getGameMode() { } @Override - public Block getTargetBlock() { + public @Nullable EsBlock getTargetBlock() { + return new BukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); + } + + private Block bukkitGetTargetBlock() { if (Main.minecraftVersion.getMinor() > 12) { return bukkitPlayer.getTargetBlockExact(5); } else if (Main.minecraftVersion.getMinor() > 7) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index fa21bf8..ce4cbac 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -4,9 +4,12 @@ import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; +import org.bukkit.Registry; import org.bukkit.World; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; import java.util.Collection; @@ -101,4 +104,37 @@ public EsInventory createInventory(EsPlayer owner, int size, String title) { InventoryHolder holder = owner == null ? null : ((BukkitPlayer) owner).getBukkitPlayer(); return new BukkitInventory(Bukkit.createInventory(holder, size, title)); } + + @SuppressWarnings("deprecation") + @Override + public String[] getPotionEffectTypes() { + // We need to use the deprecated .values() method because Registry doesn't exist in old versions + PotionEffectType[] effectTypes = PotionEffectType.values(); + String[] out = new String[effectTypes.length]; + for (int i = 0; i < effectTypes.length; i++) { + out[i] = effectTypes[i].getName(); // Same reason as above for deprecated method + } + return out; + } + + @SuppressWarnings("deprecation") + @Override + public String[] getEnchantments() { + Enchantment[] enchantments = (Enchantment[]) Registry.ENCHANTMENT.stream().toArray(); + String[] out = new String[enchantments.length]; + for (int i = 0; i < enchantments.length; i++) { + Enchantment enchantment = enchantments[i]; + out[i] = enchantment.getName(); + } + return out; + } + + @Override + public boolean doesEnchantmentExist(String name) { + try { + return BukkitHelper.getBukkitEnchantment(name) != null; + } catch (Exception e) { + return false; + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java new file mode 100644 index 0000000..b4f88d9 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java @@ -0,0 +1,43 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsSign; +import net.serble.estools.ServerApi.Interfaces.EsSignSide; +import org.bukkit.block.Sign; + +public class BukkitSign extends BukkitBlock implements EsSign { + private final Sign bukkitSign; + + public BukkitSign(Sign block) { + super(block); + bukkitSign = block; + } + + @Override + public void update() { + bukkitSign.update(); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public void setGlowingText(boolean glowing) { + bukkitSign.setGlowingText(glowing); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public boolean isGlowingText() { + return bukkitSign.isGlowingText(); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public void setLine(int line, String text) { + bukkitSign.setLine(line, text); + } + + @Override + public EsSignSide getTargetSide(EsPlayer player) { + return new BukkitSignSide(bukkitSign.getTargetSide(((BukkitPlayer) player).getBukkitPlayer())); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSignSide.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSignSide.java new file mode 100644 index 0000000..0de9198 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSignSide.java @@ -0,0 +1,27 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsSignSide; +import org.bukkit.block.sign.SignSide; + +public class BukkitSignSide implements EsSignSide { + private final SignSide bukkitSide; + + public BukkitSignSide(SignSide side) { + this.bukkitSide = side; + } + + @Override + public void setGlowingText(boolean glowing) { + bukkitSide.setGlowingText(glowing); + } + + @Override + public boolean isGlowingText() { + return bukkitSide.isGlowingText(); + } + + @Override + public void setLine(int line, String text) { + bukkitSide.setLine(line, text); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java new file mode 100644 index 0000000..e7ffeed --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsBlock { + boolean breakNaturally(); + int getX(); + int getY(); + int getZ(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java index 2547115..c9874a6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java @@ -6,4 +6,6 @@ public interface EsLivingEntity extends EsEntity { double getHealth(); void setHealth(double val); void sendMessage(String... msg); + void addPotionEffect(String effect, int duration, int amplifier); + void removePotionEffect(String effect); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 15163d8..6935641 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -1,7 +1,6 @@ package net.serble.estools.ServerApi.Interfaces; import net.serble.estools.ServerApi.EsGameMode; -import org.bukkit.block.Block; public interface EsPlayer extends EsLivingEntity { int getFoodLevel(); @@ -16,5 +15,5 @@ public interface EsPlayer extends EsLivingEntity { EsWorld getWorld(); void setGameMode(EsGameMode mode); EsGameMode getGameMode(); - Block getTargetBlock(); // TODO: Implement own Block class + EsBlock getTargetBlock(); // TODO: Implement own Block class } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 6774eee..4b38648 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -12,4 +12,7 @@ public interface EsServerSoftware { Collection getOnlinePlayers(); EsItemStack createItemStack(String material, int amount); EsInventory createInventory(EsPlayer owner, int size, String title); + String[] getPotionEffectTypes(); + String[] getEnchantments(); + boolean doesEnchantmentExist(String name); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java new file mode 100644 index 0000000..b1db1e0 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java @@ -0,0 +1,15 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsSign extends EsBlock { + void update(); + + /** Only for 1.17-1.19 where signs are single sided but support glow */ + void setGlowingText(boolean glowing); + boolean isGlowingText(); + + /** Only for pre 1.20 where signs are singlesided */ + void setLine(int line, String text); + + /** Gets the side targeted by the player, only for 1.20+ */ + EsSignSide getTargetSide(EsPlayer player); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSignSide.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSignSide.java new file mode 100644 index 0000000..63826e3 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSignSide.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Interfaces; + +/** This should only be used if the version is 1.20+ where double-sided signs exist */ +public interface EsSignSide { + void setGlowingText(boolean glowing); + boolean isGlowingText(); + void setLine(int line, String text); +} From a21d0689c64672c6938197919045b0033a144859 Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Fri, 24 May 2024 13:43:45 +1000 Subject: [PATCH 03/74] More item data, tester, player invs, general improvements --- .../net/serble/estools/Commands/Back.java | 2 +- .../net/serble/estools/Commands/Ench.java | 10 +-- .../net/serble/estools/Commands/EsTools.java | 25 +++--- .../net/serble/estools/Commands/Feed.java | 13 ++-- .../java/net/serble/estools/Commands/Fix.java | 46 ++++------- .../java/net/serble/estools/Commands/Fly.java | 13 ++-- .../estools/Commands/GameModeCommand.java | 23 +++--- .../net/serble/estools/Commands/GetInfo.java | 52 ++++++------- .../estools/Commands/GetPersistentData.java | 21 ++--- .../estools/Commands/Warps/WarpLocation.java | 2 +- .../estools/Commands/Warps/WarpManager.java | 2 +- .../java/net/serble/estools/Enchantments.java | 76 +++++++++---------- .../net/serble/estools/EntityCommand.java | 1 + .../estools/Entrypoints/EsToolsBukkit.java | 2 +- .../net/serble/estools/EsToolsCommand.java | 2 +- src/main/java/net/serble/estools/Main.java | 17 +++-- .../estools/{ => ServerApi}/EsLocation.java | 14 +++- .../ServerApi/EsPersistentDataType.java | 15 ++++ .../Bukkit/BukkitCommandBlockSender.java | 7 +- .../Implementations/Bukkit/BukkitEntity.java | 40 +++++++++- .../Implementations/Bukkit/BukkitHelper.java | 38 ++++++++-- .../Bukkit/BukkitItemMeta.java | 8 +- .../Bukkit/BukkitItemStack.java | 34 +++++++++ .../Bukkit/BukkitLivingEntity.java | 19 +++++ .../Implementations/Bukkit/BukkitPlayer.java | 29 ++++++- .../Bukkit/BukkitPlayerInventory.java | 49 ++++++++++++ .../Implementations/Bukkit/BukkitServer.java | 33 ++++++++ .../Implementations/Bukkit/BukkitWorld.java | 2 +- .../ServerApi/Interfaces/EsEntity.java | 9 ++- .../ServerApi/Interfaces/EsItemStack.java | 2 + .../ServerApi/Interfaces/EsLivingEntity.java | 3 + .../Interfaces/EsPersistentDataContainer.java | 8 ++ .../ServerApi/Interfaces/EsPlayer.java | 9 ++- .../Interfaces/EsPlayerInventory.java | 10 +++ .../Interfaces/EsServerSoftware.java | 6 ++ .../estools/ServerApi/Interfaces/EsWorld.java | 2 +- .../estools/{ => ServerApi}/Position.java | 2 +- .../estools/ServerApi/ServerPlatform.java | 4 +- src/main/java/net/serble/estools/Tester.java | 14 ++-- src/main/java/net/serble/estools/Updater.java | 16 ++-- 40 files changed, 475 insertions(+), 205 deletions(-) rename src/main/java/net/serble/estools/{ => ServerApi}/EsLocation.java (83%) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsPersistentDataType.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java rename src/main/java/net/serble/estools/{ => ServerApi}/Position.java (97%) diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index 6c34087..e169f95 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -3,7 +3,7 @@ import java.util.HashMap; import java.util.UUID; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index 6d1840b..77ed7f6 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -1,6 +1,7 @@ package net.serble.estools.Commands; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -9,9 +10,6 @@ import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.NamespacedKey; -import org.bukkit.Registry; -import org.bukkit.enchantments.Enchantment; import net.serble.estools.EsToolsCommand; @@ -70,11 +68,9 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA switch (args.length) { case 1: if (Main.minecraftVersion.getMinor() > 12) { - for (Enchantment e : Registry.ENCHANTMENT) { - tab.add(e.getKey().getKey()); - } + tab.addAll(Arrays.asList(Main.server.getEnchantments())); } else { - for (Map.Entry e : Enchantments.entrySet()) { + for (Map.Entry e : Enchantments.entrySet()) { tab.add(e.getKey()); } } diff --git a/src/main/java/net/serble/estools/Commands/EsTools.java b/src/main/java/net/serble/estools/Commands/EsTools.java index 85b7ee9..68efdf9 100644 --- a/src/main/java/net/serble/estools/Commands/EsTools.java +++ b/src/main/java/net/serble/estools/Commands/EsTools.java @@ -4,11 +4,10 @@ import java.util.ArrayList; import java.util.List; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Tester; import net.serble.estools.Updater; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import net.serble.estools.EsToolsCommand; import net.serble.estools.Commands.Give.Give; @@ -17,7 +16,7 @@ public class EsTools extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { if (checkPerms(sender, "version")) { return false; @@ -36,11 +35,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St Give.enable(); - for (Player p : getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { CChest.savePlayer(p); } - for (Player p : getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { CChest.loadPlayer(p); } @@ -51,7 +50,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } if (args.length > 1 && args[1].equalsIgnoreCase("confirm")) { - File f = new File(Main.plugin.getDataFolder(), "give.yml"); + File f = new File(Main.server.getDataFolder(), "give.yml"); if (!f.delete()) { send(sender, "&cFailed to delete data."); return false; @@ -68,12 +67,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - if (!(sender instanceof Player)) { + if (!(sender instanceof EsPlayer)) { send(sender, "&cYou must be a player to use this command."); return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; Tester tester = Tester.runningTests.get(p.getUniqueId()); if (tester != null) { @@ -117,7 +116,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 1) { @@ -134,11 +133,11 @@ public List tabComplete(CommandSender sender, String[] args, String lArg return tab; } - private void sendVersion(CommandSender sender) { + private void sendVersion(EsCommandSender sender) { if (Main.newVersion == null) { - send(sender, "&aEsTools v" + Main.plugin.getDescription().getVersion()); + send(sender, "&aEsTools v" + Main.server.getPluginVersion()); } else { - send(sender, "&aEsTools &cv" + Main.plugin.getDescription().getVersion() + ".&c An update is available, use " + + send(sender, "&aEsTools &cv" + Main.server.getPluginVersion() + ".&c An update is available, use " + "&6/estools update&c to update to &6v" + Main.newVersion.getString()); } } diff --git a/src/main/java/net/serble/estools/Commands/Feed.java b/src/main/java/net/serble/estools/Commands/Feed.java index 3a80dcd..55ac241 100644 --- a/src/main/java/net/serble/estools/Commands/Feed.java +++ b/src/main/java/net/serble/estools/Commands/Feed.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.MultiPlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; @@ -11,15 +10,15 @@ public class Feed extends MultiPlayerCommand { private static final String usage = genUsage("/feed "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - ArrayList players = new ArrayList<>(); + public boolean execute(EsCommandSender sender, String[] args) { + ArrayList players = new ArrayList<>(); if (args.length == 0) { if (isNotPlayer(sender, usage)) { return false; } - players.add((Player) sender); + players.add((EsPlayer) sender); } else { players = getPlayers(sender, args); @@ -28,7 +27,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - for (Player p : players) { + for (EsPlayer p : players) { p.setFoodLevel(20); p.setSaturation(20); } diff --git a/src/main/java/net/serble/estools/Commands/Fix.java b/src/main/java/net/serble/estools/Commands/Fix.java index a55bf81..b272787 100644 --- a/src/main/java/net/serble/estools/Commands/Fix.java +++ b/src/main/java/net/serble/estools/Commands/Fix.java @@ -4,13 +4,7 @@ import java.util.List; import net.serble.estools.Main; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; +import net.serble.estools.ServerApi.Interfaces.*; import net.serble.estools.PlayerCommand; @@ -18,15 +12,15 @@ public class Fix extends PlayerCommand { private static final String usage = genUsage("/fix [hand/offhand/helmet/chestplate/leggings/boots/all] [player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - Player p; + public boolean execute(EsCommandSender sender, String[] args) { + EsPlayer p; if (args.length < 2) { if (isNotPlayer(sender, usage)) { return false; } - p = (Player) sender; + p = (EsPlayer) sender; } else { p = getPlayer(sender, args[1]); @@ -35,16 +29,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - PlayerInventory pInv = p.getInventory(); - ItemStack is = getMainHand(p); + EsPlayerInventory pInv = p.getInventory(); + EsItemStack is = p.getMainHand(); boolean all = false; if (args.length > 0) { switch (args[0].toLowerCase()) { case "offhand": - if (Main.majorVersion > 8) { - is = pInv.getItemInOffHand(); + if (Main.minecraftVersion.getMinor() > 8) { + is = pInv.getOffHand(); } break; @@ -71,8 +65,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } if (all) { - ItemStack[] contents = p.getInventory().getContents(); - for (ItemStack i : contents) { + EsItemStack[] contents = p.getInventory().getContents(); + for (EsItemStack i : contents) { repair(i); } @@ -81,37 +75,27 @@ public boolean onCommand(CommandSender sender, Command command, String label, St repair(is); } - send(sender, "&aRepaired &6%s's &aitem(s)!", getEntityName(p)); + send(sender, "&aRepaired &6%s's &aitem(s)!", p.getName()); return true; } - public static void repair(ItemStack is) { + public static void repair(EsItemStack is) { if (is == null) { return; } - if (Main.majorVersion > 12) { - ItemMeta im = is.getItemMeta(); - if (im == null) return; - - ((Damageable) im).setDamage(0); - - is.setItemMeta(im); - } else { - //noinspection deprecation - is.setDurability((short) 0); - } + is.setDamage(0); } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 1) { tab.add("hand"); tab.add("helmet"); tab.add("chestplate"); tab.add("leggings"); tab.add("boots"); tab.add("all"); - if (Main.majorVersion > 8) { + if (Main.minecraftVersion.getMinor() > 8) { tab.add("offhand"); } } else if (args.length == 2) { diff --git a/src/main/java/net/serble/estools/Commands/Fly.java b/src/main/java/net/serble/estools/Commands/Fly.java index 62ca1eb..9306f56 100644 --- a/src/main/java/net/serble/estools/Commands/Fly.java +++ b/src/main/java/net/serble/estools/Commands/Fly.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.MultiPlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.Objects; @@ -12,15 +11,15 @@ public class Fly extends MultiPlayerCommand { private static final String usage = genUsage("/fly [Player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - ArrayList players = new ArrayList<>(); + public boolean execute(EsCommandSender sender, String[] args) { + ArrayList players = new ArrayList<>(); if (args.length == 0) { if (isNotPlayer(sender, usage)) { return false; } - players.add((Player) sender); + players.add((EsPlayer) sender); } else { players = getPlayers(sender, args[0]); @@ -29,7 +28,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - for (Player p : players) { + for (EsPlayer p : players) { boolean isFly = p.getAllowFlight(); if (args.length != 0 && !Objects.equals(args[0], "*")) { diff --git a/src/main/java/net/serble/estools/Commands/GameModeCommand.java b/src/main/java/net/serble/estools/Commands/GameModeCommand.java index 0e91a4a..fceea8d 100644 --- a/src/main/java/net/serble/estools/Commands/GameModeCommand.java +++ b/src/main/java/net/serble/estools/Commands/GameModeCommand.java @@ -1,30 +1,31 @@ package net.serble.estools.Commands; import net.serble.estools.MultiPlayerCommand; -import org.bukkit.GameMode; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; public class GameModeCommand extends MultiPlayerCommand { - private final GameMode gameMode; + private final EsGameMode gameMode; + private final String cmd; - public GameModeCommand(GameMode gameMode) { + public GameModeCommand(EsGameMode gameMode, String cmd) { this.gameMode = gameMode; + this.cmd = cmd; } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - ArrayList ps = new ArrayList<>(); + public boolean execute(EsCommandSender sender, String[] args) { + ArrayList ps = new ArrayList<>(); if (args.length == 0) { - if (isNotPlayer(sender, genUsage("/%s [player]"), label)) { + if (isNotPlayer(sender, genUsage("/%s [player]"), cmd)) { return false; } - ps.add((Player) sender); + ps.add((EsPlayer) sender); send(sender, "&aGamemode set to &6%s", gameMode.toString()); } else { @@ -36,7 +37,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St send(sender, "&aGamemode set to &6%s&a for &6%s", gameMode.toString(), argsToString(args, 0)); } - for (Player p : ps) { + for (EsPlayer p : ps) { p.setGameMode(gameMode); } diff --git a/src/main/java/net/serble/estools/Commands/GetInfo.java b/src/main/java/net/serble/estools/Commands/GetInfo.java index d6116a9..f744eb6 100644 --- a/src/main/java/net/serble/estools/Commands/GetInfo.java +++ b/src/main/java/net/serble/estools/Commands/GetInfo.java @@ -1,16 +1,14 @@ package net.serble.estools.Commands; import net.serble.estools.Effects; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; -import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.EntityCommand; -import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.Objects; @@ -18,19 +16,19 @@ public class GetInfo extends EntityCommand { private static final String usage = genUsage("/getinfo "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - Entity entity = getNonLivingEntity(sender, args[0]); + EsEntity entity = getNonLivingEntity(sender, args[0]); if (entity == null) { return false; } - Location loc = entity.getLocation(); - String name = getEntityName(entity); + EsLocation loc = entity.getLocation(); + String name = entity.getName(); // Global Values String info = @@ -43,7 +41,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St info = String.format(info, name, - entity.getType().name(), + entity.getType(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), @@ -51,11 +49,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St entity.getUniqueId()); // Passengers - if (Main.majorVersion > 12) { + if (Main.minecraftVersion.getMinor() > 12) { StringBuilder passengerText = new StringBuilder("&aPassengers: &6"); boolean passengersExist = false; - for (Entity passenger : entity.getPassengers()) { - passengerText.append(getEntityName(passenger)).append(" (").append(passenger.getType()).append("), "); + for (EsEntity passenger : entity.getPassengers()) { + passengerText.append(passenger.getName()).append(" (").append(passenger.getType()).append("), "); passengersExist = true; } @@ -68,23 +66,23 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - if (entity instanceof LivingEntity) { // Living entity - LivingEntity le = (LivingEntity) entity; - String maxHealth = String.valueOf(Math.round(getMaxHealth(le))); + if (entity instanceof EsLivingEntity) { // Living entity + EsLivingEntity le = (EsLivingEntity) entity; + String maxHealth = String.valueOf(Math.round(le.getMaxHealth())); StringBuilder potionEffects = new StringBuilder(); { - Object[] pos = le.getActivePotionEffects().toArray(); + String[] pos = le.getActivePotionEffects().toArray(new String[0]); if (pos.length == 0) { potionEffects.append("None"); } else { for (int i = 0; i < pos.length - 1; i++) { - PotionEffect po = (PotionEffect) pos[i]; - potionEffects.append(Effects.getName(po.getType())).append(", "); + String po = pos[i]; + potionEffects.append(Effects.getName(po)).append(", "); } - potionEffects.append(Effects.getName(((PotionEffect) pos[pos.length - 1]).getType())); + potionEffects.append(Effects.getName(pos[pos.length - 1])); } } @@ -93,13 +91,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St "&aMax Health: &6%s\n" + "&aPotion Effects: &6%s\n"; livingEntityInfo = String.format(livingEntityInfo, - getHealth(le), + le.getHealth(), maxHealth, potionEffects); info += livingEntityInfo; - if (Main.majorVersion > 12) { + if (Main.minecraftVersion.getMinor() > 12) { // Scoreboard tags StringBuilder tags = new StringBuilder("&aScoreboard Tags: &6"); boolean tagsExist = false; @@ -118,8 +116,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - if (entity instanceof Player) { // Players - Player player = (Player) entity; + if (entity instanceof EsPlayer) { // Players + EsPlayer player = (EsPlayer) entity; String playerInfo = "&aHunger: &6%s\n" + @@ -129,7 +127,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St player.getFoodLevel(), player.getSaturation()); - if (Main.majorVersion > 1) { + if (Main.minecraftVersion.getMinor() > 1) { playerInfo += "&aCan Fly: &6%s\n" + "&aIs Flying: &6%s\n"; diff --git a/src/main/java/net/serble/estools/Commands/GetPersistentData.java b/src/main/java/net/serble/estools/Commands/GetPersistentData.java index d1e356b..8e8e4b7 100644 --- a/src/main/java/net/serble/estools/Commands/GetPersistentData.java +++ b/src/main/java/net/serble/estools/Commands/GetPersistentData.java @@ -2,12 +2,13 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Tuple; import org.apache.commons.lang.ArrayUtils; import org.bukkit.NamespacedKey; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; @@ -20,7 +21,7 @@ public class GetPersistentData extends EsToolsCommand { public static final String usage = genUsage("/getpersistentdata "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) return false; @@ -32,14 +33,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St String tagString = args[0].toLowerCase(); String typeString = args[1].toLowerCase(); - ItemStack item = getMainHand((Player) sender); + EsItemStack item = ((EsPlayer) sender).getMainHand(); NamespacedKey key = getNamespacedKey(tagString); if (key == null) { send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); return false; } - ItemMeta meta = item.getItemMeta(); + EsItemMeta meta = item.getItemMeta(); if (meta == null) { send(sender, "&cItem does not have nbt tags!"); return false; @@ -163,15 +164,15 @@ private static Tuple getData(String type, NamespacedKey key, Pe @SuppressWarnings("UnstableApiUsage") public static NamespacedKey getNamespacedKey(String keyString) { - if (Main.majorVersion >= 16) { - return NamespacedKey.fromString(keyString, Main.plugin); + if (Main.minecraftVersion.getMinor() >= 16) { + return NamespacedKey.fromString(keyString, Main.bukkitPlugin); } String[] parts = keyString.split(":"); if (parts.length == 2) { return new NamespacedKey(parts[0], parts[1]); } else if (parts.length == 1) { // Incorrectly formatted key - String pluginName = Main.getPlugin(Main.class).getName(); + String pluginName = Main.server.getPluginName(); return new NamespacedKey(pluginName, parts[0]); } @@ -190,7 +191,7 @@ private static StringBuilder buildString(T[] values) { } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 2) { diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java index b5436e2..97fb07f 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java @@ -1,6 +1,6 @@ package net.serble.estools.Commands.Warps; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java index 0442a2c..5484e1e 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java @@ -2,7 +2,7 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.ConfigManager; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.configuration.file.FileConfiguration; diff --git a/src/main/java/net/serble/estools/Enchantments.java b/src/main/java/net/serble/estools/Enchantments.java index bdae7df..d7b5652 100644 --- a/src/main/java/net/serble/estools/Enchantments.java +++ b/src/main/java/net/serble/estools/Enchantments.java @@ -1,7 +1,5 @@ package net.serble.estools; -import org.bukkit.enchantments.Enchantment; - import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -10,57 +8,57 @@ // This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. // only enchantments before 1.13 need to be here because this class isn't used past 1.13 public class Enchantments { - private static final Map ENCHANTMENTS = new HashMap<>(); + private static final Map ENCHANTMENTS = new HashMap<>(); - public static Enchantment getByName(String name) { + public static String getByName(String name) { return ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH)); } - public static Set> entrySet() { + public static Set> entrySet() { return ENCHANTMENTS.entrySet(); } static { AddEnchants(); } private static void AddEnchants() { - ENCHANTMENTS.put("sharpness", Enchantment.DAMAGE_ALL); - ENCHANTMENTS.put("baneofarthropods", Enchantment.DAMAGE_ARTHROPODS); - ENCHANTMENTS.put("smite", Enchantment.DAMAGE_UNDEAD); - ENCHANTMENTS.put("efficiency", Enchantment.DIG_SPEED); - ENCHANTMENTS.put("unbreaking", Enchantment.DURABILITY); - ENCHANTMENTS.put("fireaspect", Enchantment.FIRE_ASPECT); - ENCHANTMENTS.put("knockback", Enchantment.KNOCKBACK); - ENCHANTMENTS.put("looting", Enchantment.LOOT_BONUS_MOBS); - ENCHANTMENTS.put("respiration", Enchantment.OXYGEN); - ENCHANTMENTS.put("protection", Enchantment.PROTECTION_ENVIRONMENTAL); - ENCHANTMENTS.put("blastprotect", Enchantment.PROTECTION_EXPLOSIONS); - ENCHANTMENTS.put("featherfall", Enchantment.PROTECTION_FALL); - ENCHANTMENTS.put("projectileprotection", Enchantment.PROTECTION_PROJECTILE); - ENCHANTMENTS.put("silktouch", Enchantment.SILK_TOUCH); - ENCHANTMENTS.put("aquaaffinity", Enchantment.WATER_WORKER); - ENCHANTMENTS.put("flame", Enchantment.ARROW_FIRE); - ENCHANTMENTS.put("power", Enchantment.ARROW_DAMAGE); - ENCHANTMENTS.put("punch", Enchantment.ARROW_KNOCKBACK); - ENCHANTMENTS.put("infinity", Enchantment.ARROW_INFINITE); - ENCHANTMENTS.put("fortune", Enchantment.LOOT_BONUS_BLOCKS); + ENCHANTMENTS.put("sharpness", "DAMAGE_ALL"); + ENCHANTMENTS.put("baneofarthropods", "DAMAGE_ARTHROPODS"); + ENCHANTMENTS.put("smite", "DAMAGE_UNDEAD"); + ENCHANTMENTS.put("efficiency", "DIG_SPEED"); + ENCHANTMENTS.put("unbreaking", "DURABILITY"); + ENCHANTMENTS.put("fireaspect", "FIRE_ASPECT"); + ENCHANTMENTS.put("knockback", "KNOCKBACK"); + ENCHANTMENTS.put("looting", "LOOT_BONUS_MOBS"); + ENCHANTMENTS.put("respiration", "OXYGEN"); + ENCHANTMENTS.put("protection", "PROTECTION_ENVIRONMENTAL"); + ENCHANTMENTS.put("blastprotect", "PROTECTION_EXPLOSIONS"); + ENCHANTMENTS.put("featherfall", "PROTECTION_FALL"); + ENCHANTMENTS.put("projectileprotection", "PROTECTION_PROJECTILE"); + ENCHANTMENTS.put("silktouch", "SILK_TOUCH"); + ENCHANTMENTS.put("aquaaffinity", "WATER_WORKER"); + ENCHANTMENTS.put("flame", "ARROW_FIRE"); + ENCHANTMENTS.put("power", "ARROW_DAMAGE"); + ENCHANTMENTS.put("punch", "ARROW_KNOCKBACK"); + ENCHANTMENTS.put("infinity", "ARROW_INFINITE"); + ENCHANTMENTS.put("fortune", "LOOT_BONUS_BLOCKS"); - if (Main.majorVersion <= 3) return; - ENCHANTMENTS.put("thorns", Enchantment.THORNS); + if (Main.minecraftVersion.getMinor() <= 3) return; + ENCHANTMENTS.put("thorns", "THORNS"); - if (Main.majorVersion <= 6) return; - ENCHANTMENTS.put("luck", Enchantment.LUCK); - ENCHANTMENTS.put("lure", Enchantment.LURE); + if (Main.minecraftVersion.getMinor() <= 6) return; + ENCHANTMENTS.put("luck", "LUCK"); + ENCHANTMENTS.put("lure", "LURE"); - if (Main.majorVersion <= 7) return; - ENCHANTMENTS.put("depthstrider", Enchantment.DEPTH_STRIDER); + if (Main.minecraftVersion.getMinor() <= 7) return; + ENCHANTMENTS.put("depthstrider", "DEPTH_STRIDER"); - if (Main.majorVersion <= 8) return; - ENCHANTMENTS.put("frostwalker", Enchantment.FROST_WALKER); - ENCHANTMENTS.put("mending", Enchantment.MENDING); + if (Main.minecraftVersion.getMinor() <= 8) return; + ENCHANTMENTS.put("frostwalker", "FROST_WALKER"); + ENCHANTMENTS.put("mending", "MENDING"); - if (Main.majorVersion <= 10) return; - ENCHANTMENTS.put("bindingcurse", Enchantment.BINDING_CURSE); - ENCHANTMENTS.put("vanishingcurse", Enchantment.VANISHING_CURSE); - ENCHANTMENTS.put("sweepingedge", Enchantment.SWEEPING_EDGE); + if (Main.minecraftVersion.getMinor() <= 10) return; + ENCHANTMENTS.put("bindingcurse", "BINDING_CURSE"); + ENCHANTMENTS.put("vanishingcurse", "VANISHING_CURSE"); + ENCHANTMENTS.put("sweepingedge", "SWEEPING_EDGE"); } } diff --git a/src/main/java/net/serble/estools/EntityCommand.java b/src/main/java/net/serble/estools/EntityCommand.java index 3f3ac06..928e0a9 100644 --- a/src/main/java/net/serble/estools/EntityCommand.java +++ b/src/main/java/net/serble/estools/EntityCommand.java @@ -4,6 +4,7 @@ import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Position; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java index 9bd3f1a..49ab70f 100644 --- a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -9,7 +9,7 @@ public class EsToolsBukkit extends JavaPlugin { @Override public void onEnable() { Main.bukkitPlugin = this; - Main main = new Main(ServerPlatform.Bukkit); + Main main = new Main(ServerPlatform.Bukkit, this); main.enable(); } diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index aef4fed..41e9b52 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -1,9 +1,9 @@ package net.serble.estools; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 96a05d8..229b91d 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -8,6 +8,7 @@ import net.serble.estools.Commands.PowerPick.*; import net.serble.estools.Commands.Teleport.*; import net.serble.estools.Commands.Warps.*; +import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; import net.serble.estools.ServerApi.ServerPlatform; import net.serble.estools.Signs.SignMain; @@ -36,17 +37,19 @@ public class Main { public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; public static EsServerSoftware server; - public static JavaPlugin bukkitPlugin; + @Deprecated public static JavaPlugin bukkitPlugin; // TODO: Just here until we get everything to work + private final Object context; private static final int bStatsId = 21760; - public Main(ServerPlatform plat) { + public Main(ServerPlatform plat, Object context) { platform = plat; + this.context = context; } public void enable() { plugin = this; - server = platform.getServerInstance(); + server = platform.getServerInstance(context); minecraftVersion = server.getVersion(); @@ -87,10 +90,10 @@ public void enable() { } // Commands - sc("gms", "gamemode.survival", new GameModeCommand(GameMode.SURVIVAL)); - sc("gmc", "gamemode.creative", new GameModeCommand(GameMode.CREATIVE)); - sc("gma", "gamemode.adventure", new GameModeCommand(GameMode.ADVENTURE), 3); - sc("gmsp", "gamemode.spectator", new GameModeCommand(GameMode.SPECTATOR), 8); + sc("gms", "gamemode.survival", new GameModeCommand(EsGameMode.Survival, "gms")); + sc("gmc", "gamemode.creative", new GameModeCommand(EsGameMode.Creative, "gmc")); + sc("gma", "gamemode.adventure", new GameModeCommand(EsGameMode.Adventure, "gma"), 3); + sc("gmsp", "gamemode.spectator", new GameModeCommand(EsGameMode.Spectator, "gmsp"), 8); sc("tphere", "tp", new TpHere()); sc("tpall", "tp", new TpAll()); sc("feed", "feed", new Feed()); diff --git a/src/main/java/net/serble/estools/EsLocation.java b/src/main/java/net/serble/estools/ServerApi/EsLocation.java similarity index 83% rename from src/main/java/net/serble/estools/EsLocation.java rename to src/main/java/net/serble/estools/ServerApi/EsLocation.java index 73b8d0c..c1fbcdd 100644 --- a/src/main/java/net/serble/estools/EsLocation.java +++ b/src/main/java/net/serble/estools/ServerApi/EsLocation.java @@ -1,4 +1,4 @@ -package net.serble.estools; +package net.serble.estools.ServerApi; import net.serble.estools.ServerApi.Interfaces.EsWorld; @@ -58,4 +58,16 @@ public void setPitch(double pitch) { public void setYaw(double yaw) { this.yaw = yaw; } + + public int getBlockX() { + return (int) Math.round(getX()); + } + + public int getBlockY() { + return (int) Math.round(getY()); + } + + public int getBlockZ() { + return (int) Math.round(getZ()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsPersistentDataType.java b/src/main/java/net/serble/estools/ServerApi/EsPersistentDataType.java new file mode 100644 index 0000000..286ad77 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsPersistentDataType.java @@ -0,0 +1,15 @@ +package net.serble.estools.ServerApi; + +public enum EsPersistentDataType { + String, + Boolean, + Integer, + Byte, + Float, + Double, + Short, + Long, + ByteArray, + IntArray, + LongArray +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java index 0df47a2..5374df2 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitCommandBlockSender.java @@ -1,15 +1,20 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.ServerApi.Interfaces.EsCommandBlockSender; import net.serble.estools.ServerApi.Interfaces.EsConsoleSender; import org.bukkit.command.BlockCommandSender; -public class BukkitCommandBlockSender implements EsConsoleSender { +public class BukkitCommandBlockSender implements EsCommandBlockSender { private final BlockCommandSender bukkitSender; public BukkitCommandBlockSender(BlockCommandSender child) { bukkitSender = child; } + public BlockCommandSender getBukkitSender() { + return bukkitSender; + } + @Override public void sendMessage(String... msg) { bukkitSender.sendMessage(msg); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index d7d0046..6848354 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -1,11 +1,15 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsEntity; +import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; import java.util.UUID; import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.toBukkitLocation; @@ -17,11 +21,20 @@ public BukkitEntity(org.bukkit.entity.Entity bukkitEntity) { this.bukkitEntity = bukkitEntity; } + public Entity getBukkitEntity() { + return bukkitEntity; + } + @Override public void teleport(EsLocation loc) { bukkitEntity.teleport(toBukkitLocation(loc)); } + @Override + public String getType() { + return bukkitEntity.getType().name(); + } + @Override public String getName() { if (Main.minecraftVersion.getMinor() > 7) { @@ -77,4 +90,29 @@ public boolean leaveVehicle() { return bukkitEntity.leaveVehicle(); } } + + @Override + public List getPassengers() { + List bukkitList = bukkitEntity.getPassengers(); + List list = new ArrayList<>(); + for (Entity entity : bukkitList) { + list.add(new BukkitEntity(entity)); + } + return list; + } + + @Override + public Set getScoreboardTags() { + return bukkitEntity.getScoreboardTags(); + } + + @Override + public void addScoreboardTag(String tag) { + bukkitEntity.addScoreboardTag(tag); + } + + @Override + public boolean hasScoreboardTag(String tag) { + return bukkitEntity.getScoreboardTags().contains(tag); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 14496a4..97b3197 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -1,19 +1,19 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; -import net.serble.estools.Position; +import net.serble.estools.ServerApi.Position; import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.Interfaces.EsBlock; -import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import net.serble.estools.ServerApi.Interfaces.EsEntity; +import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.util.Vector; @@ -45,11 +45,11 @@ public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.Command return new BukkitPlayer((Player) sender); } - if (sender instanceof org.bukkit.entity.LivingEntity) { + if (sender instanceof LivingEntity) { return new BukkitLivingEntity((org.bukkit.entity.LivingEntity) sender); } - if (sender instanceof org.bukkit.entity.Entity) { + if (sender instanceof Entity) { return new BukkitEntity((org.bukkit.entity.Entity) sender); } @@ -64,6 +64,30 @@ public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.Command throw new RuntimeException("Unrecognised command sender"); } + public static CommandSender toBukkitCommandSender(EsCommandSender sender) { + if (sender instanceof EsPlayer) { + return ((BukkitPlayer) sender).getBukkitPlayer(); + } + + if (sender instanceof EsLivingEntity) { + return ((BukkitLivingEntity) sender).getBukkitEntity(); + } + + if (sender instanceof EsEntity) { + return ((BukkitEntity) sender).getBukkitEntity(); + } + + if (sender instanceof EsConsoleSender) { + return Bukkit.getConsoleSender(); + } + + if (sender instanceof EsCommandBlockSender) { + return ((BukkitCommandBlockSender) sender).getBukkitSender(); + } + + throw new RuntimeException("Unrecognised command sender"); + } + public static EsEntity fromBukkitEntity(org.bukkit.entity.Entity entity) { if (entity instanceof org.bukkit.entity.LivingEntity) { return new BukkitLivingEntity((LivingEntity) entity); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java index 746fca3..3b3e582 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -1,11 +1,13 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; public class BukkitItemMeta implements EsItemMeta { - private final org.bukkit.inventory.meta.ItemMeta bukkitMeta; + private final ItemMeta bukkitMeta; - public BukkitItemMeta(org.bukkit.inventory.meta.ItemMeta meta) { + public BukkitItemMeta(ItemMeta meta) { bukkitMeta = meta; } @@ -19,7 +21,7 @@ public boolean isUnbreakable() { return bukkitMeta.isUnbreakable(); } - public org.bukkit.inventory.meta.ItemMeta getBukkitMeta() { + public ItemMeta getBukkitMeta() { return bukkitMeta; } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index bb1394e..0e15844 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -1,9 +1,12 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; import org.bukkit.Registry; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; import java.util.Objects; @@ -56,4 +59,35 @@ public EsItemMeta getItemMeta() { public void setItemMeta(EsItemMeta meta) { bukkitItem.setItemMeta(((BukkitItemMeta) meta).getBukkitMeta()); } + + @SuppressWarnings("deprecation") + @Override + public void setDamage(int val) { + if (Main.minecraftVersion.getMinor() > 12) { + ItemMeta meta = bukkitItem.getItemMeta(); + if (meta instanceof Damageable) { + ((Damageable) meta).setDamage(val); + } + } else { + if (val > Short.MAX_VALUE) { + val = Short.MAX_VALUE; + } + + bukkitItem.setDurability((short) val); + } + } + + @SuppressWarnings("deprecation") + @Override + public int getDamage() { + if (Main.minecraftVersion.getMinor() > 12) { + return bukkitItem.getDurability(); + } + + ItemMeta meta = bukkitItem.getItemMeta(); + if (meta instanceof Damageable) { + return ((Damageable) meta).getDamage(); + } + return 0; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index fc11b38..82e0e82 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -5,9 +5,13 @@ import org.bukkit.Bukkit; import org.bukkit.Registry; import org.bukkit.attribute.Attribute; +import org.bukkit.entity.LivingEntity; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.Objects; public class BukkitLivingEntity extends BukkitEntity implements EsLivingEntity { @@ -18,6 +22,10 @@ public BukkitLivingEntity(org.bukkit.entity.LivingEntity entity) { bukkitEntity = entity; } + public LivingEntity getBukkitEntity() { + return bukkitEntity; + } + @Override public double getMaxHealth() { if (Main.minecraftVersion.getMinor() > 8) { @@ -98,4 +106,15 @@ public void addPotionEffect(String effect, int duration, int amplifier) { public void removePotionEffect(String effect) { bukkitEntity.removePotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect))); } + + @SuppressWarnings("deprecation") // Gotta love backwards compatibility + @Override + public List getActivePotionEffects() { + Collection bukkitEffects = bukkitEntity.getActivePotionEffects(); + List out = new ArrayList<>(); + for (PotionEffect eff : bukkitEffects) { + out.add(eff.getType().getName()); + } + return out; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index ae6211b..9411883 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -33,6 +33,16 @@ public void setFoodLevel(int val) { bukkitPlayer.setFoodLevel(val); } + @Override + public double getSaturation() { + return bukkitPlayer.getSaturation(); + } + + @Override + public void setSaturation(double val) { + bukkitPlayer.setSaturation((float) val); + } + @Override public EsItemStack getMainHand() { return new BukkitItemStack(bukkitGetMainHand()); @@ -54,8 +64,8 @@ public void closeInventory() { } @Override - public EsInventory getInventory() { - return new BukkitInventory(bukkitPlayer.getInventory()); + public EsPlayerInventory getInventory() { + return new BukkitPlayerInventory(bukkitPlayer.getInventory()); } @Override @@ -88,6 +98,21 @@ public EsGameMode getGameMode() { return new BukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); } + @Override + public boolean getAllowFlight() { + return bukkitPlayer.getAllowFlight(); + } + + @Override + public void setAllowFlight(boolean val) { + bukkitPlayer.setAllowFlight(val); + } + + @Override + public boolean isFlying() { + return bukkitPlayer.isFlying(); + } + private Block bukkitGetTargetBlock() { if (Main.minecraftVersion.getMinor() > 12) { return bukkitPlayer.getTargetBlockExact(5); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java new file mode 100644 index 0000000..ba0cab9 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; +import org.bukkit.inventory.PlayerInventory; + +public class BukkitPlayerInventory extends BukkitInventory implements EsPlayerInventory { + private final PlayerInventory bukkitInv; + + public BukkitPlayerInventory(PlayerInventory inv) { + super(inv); + bukkitInv = inv; + } + + @Override + public PlayerInventory getBukkitInventory() { + return bukkitInv; + } + + @Override + public EsItemStack getOffHand() { + return new BukkitItemStack(bukkitInv.getItemInOffHand()); + } + + @Override + public EsItemStack getMainHand() { + return new BukkitItemStack(bukkitInv.getItemInMainHand()); + } + + @Override + public EsItemStack getHelmet() { + return new BukkitItemStack(bukkitInv.getHelmet()); + } + + @Override + public EsItemStack getLeggings() { + return new BukkitItemStack(bukkitInv.getLeggings()); + } + + @Override + public EsItemStack getChestplate() { + return new BukkitItemStack(bukkitInv.getChestplate()); + } + + @Override + public EsItemStack getBoots() { + return new BukkitItemStack(bukkitInv.getBoots()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index ce4cbac..f31f38d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -9,14 +9,22 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffectType; +import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; public class BukkitServer implements EsServerSoftware { + private final JavaPlugin plugin; + + public BukkitServer(Object pluginObj) { + plugin = (JavaPlugin) pluginObj; + } + @Override public EsPlayer getPlayer(String name) { return new BukkitPlayer(Bukkit.getPlayer(name)); @@ -137,4 +145,29 @@ public boolean doesEnchantmentExist(String name) { return false; } } + + @Override + public File getDataFolder() { + return plugin.getDataFolder(); + } + + @Override + public void dispatchCommand(EsCommandSender sender, String cmd) { + Bukkit.dispatchCommand(BukkitHelper.toBukkitCommandSender(sender), cmd); + } + + @Override + public EsCommandSender getConsoleSender() { + return null; + } + + @Override + public SemanticVersion getPluginVersion() { + return new SemanticVersion(plugin.getDescription().getVersion()); + } + + @Override + public String getPluginName() { + return plugin.getDescription().getName(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index 52ce8c2..0b57a7f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -1,6 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java index f95bdea..c337263 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java @@ -1,13 +1,20 @@ package net.serble.estools.ServerApi.Interfaces; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; +import java.util.List; +import java.util.Set; import java.util.UUID; public interface EsEntity extends EsCommandSender { + String getType(); String getName(); UUID getUniqueId(); void teleport(EsLocation loc); EsLocation getLocation(); boolean leaveVehicle(); + List getPassengers(); + Set getScoreboardTags(); + void addScoreboardTag(String tag); + boolean hasScoreboardTag(String tag); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 9f8b048..5d89357 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -8,4 +8,6 @@ public interface EsItemStack { void addEnchantment(String enchantment, int level); EsItemMeta getItemMeta(); void setItemMeta(EsItemMeta meta); + void setDamage(int val); + int getDamage(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java index c9874a6..4492273 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; +import java.util.List; + public interface EsLivingEntity extends EsEntity { double getMaxHealth(); void setMaxHealth(double val); @@ -8,4 +10,5 @@ public interface EsLivingEntity extends EsEntity { void sendMessage(String... msg); void addPotionEffect(String effect, int duration, int amplifier); void removePotionEffect(String effect); + List getActivePotionEffects(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java new file mode 100644 index 0000000..478b9c6 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.ServerApi.EsPersistentDataType; + +public interface EsPersistentDataContainer { + void set(String key, EsPersistentDataType type, Object val); + Object get(String key, EsPersistentDataType type); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 6935641..6176962 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -5,15 +5,20 @@ public interface EsPlayer extends EsLivingEntity { int getFoodLevel(); void setFoodLevel(int val); + double getSaturation(); + void setSaturation(double val); EsItemStack getMainHand(); void setMainHand(EsItemStack item); void openInventory(EsInventory inv); void closeInventory(); - EsInventory getInventory(); + EsPlayerInventory getInventory(); void setFlySpeed(float val); void setWalkSpeed(float val); EsWorld getWorld(); void setGameMode(EsGameMode mode); EsGameMode getGameMode(); - EsBlock getTargetBlock(); // TODO: Implement own Block class + EsBlock getTargetBlock(); + boolean getAllowFlight(); + void setAllowFlight(boolean val); + boolean isFlying(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java new file mode 100644 index 0000000..203a926 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java @@ -0,0 +1,10 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsPlayerInventory extends EsInventory { + EsItemStack getOffHand(); + EsItemStack getMainHand(); + EsItemStack getHelmet(); + EsItemStack getLeggings(); + EsItemStack getChestplate(); + EsItemStack getBoots(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 4b38648..575d562 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -2,6 +2,7 @@ import net.serble.estools.SemanticVersion; +import java.io.File; import java.util.Collection; import java.util.UUID; @@ -15,4 +16,9 @@ public interface EsServerSoftware { String[] getPotionEffectTypes(); String[] getEnchantments(); boolean doesEnchantmentExist(String name); + File getDataFolder(); + void dispatchCommand(EsCommandSender sender, String cmd); + EsCommandSender getConsoleSender(); + SemanticVersion getPluginVersion(); + String getPluginName(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java index 93e9672..4ef6bbd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java @@ -1,6 +1,6 @@ package net.serble.estools.ServerApi.Interfaces; -import net.serble.estools.EsLocation; +import net.serble.estools.ServerApi.EsLocation; import java.util.List; diff --git a/src/main/java/net/serble/estools/Position.java b/src/main/java/net/serble/estools/ServerApi/Position.java similarity index 97% rename from src/main/java/net/serble/estools/Position.java rename to src/main/java/net/serble/estools/ServerApi/Position.java index e95c4d2..59b2c4e 100644 --- a/src/main/java/net/serble/estools/Position.java +++ b/src/main/java/net/serble/estools/ServerApi/Position.java @@ -1,4 +1,4 @@ -package net.serble.estools; +package net.serble.estools.ServerApi; public class Position { private double x; diff --git a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java index 4a3c9fd..47a0479 100644 --- a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java +++ b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java @@ -7,10 +7,10 @@ public enum ServerPlatform { Bukkit, // Includes all derivatives Folia; - public EsServerSoftware getServerInstance() { + public EsServerSoftware getServerInstance(Object context) { switch (this) { case Bukkit: - return new BukkitServer(); + return new BukkitServer(context); default: throw new IllegalArgumentException("Unsupported server software"); diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index 8b0230b..6e1f753 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -1,8 +1,8 @@ package net.serble.estools; import net.serble.estools.Commands.SafeTp; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import java.io.PrintWriter; import java.io.StringWriter; @@ -10,7 +10,7 @@ import java.util.UUID; public class Tester { - private final Player player; + private final EsPlayer player; private int currentCommand = 0; public static final HashMap runningTests = new HashMap<>(); @@ -100,7 +100,7 @@ public class Tester { new Tuple<>("suicide", 4) }; - public Tester(Player p) { + public Tester(EsPlayer p) { player = p; } @@ -130,8 +130,8 @@ private void execNextCommand() { String cmd = cCmd.a(); if (cmd.contains("{randomplayer}")) { // Find a random player that isn't player - Player randomPlayer = null; - for (Player p : Bukkit.getOnlinePlayers()) { + EsPlayer randomPlayer = null; + for (EsPlayer p : Main.server.getOnlinePlayers()) { if (p != player) { randomPlayer = p; break; @@ -177,12 +177,12 @@ private void execNextCommand() { } @SuppressWarnings("WrapperTypeMayBePrimitive") Double timeInTicks = (secVal * 20.0); // The type cannot be primitive - Bukkit.getScheduler().runTaskLater(Main.plugin, this::execNextCommand, timeInTicks.longValue()); + /* TODO: Remove bukkit scheduler */ Bukkit.getScheduler().runTaskLater(Main.bukkitPlugin, this::execNextCommand, timeInTicks.longValue()); EsToolsCommand.send(player, "&bWaiting &6" + cCmd.b() + " seconds"); } private void exec(String cmd) { EsToolsCommand.send(player, "&aExecuting: " + cmd); - Bukkit.dispatchCommand(player, cmd); + Main.server.dispatchCommand(player, cmd); } } diff --git a/src/main/java/net/serble/estools/Updater.java b/src/main/java/net/serble/estools/Updater.java index e8cd6c8..284a134 100644 --- a/src/main/java/net/serble/estools/Updater.java +++ b/src/main/java/net/serble/estools/Updater.java @@ -2,8 +2,8 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import java.io.*; import java.net.HttpURLConnection; @@ -45,13 +45,7 @@ private static void checkForUpdateBlocking() { .getAsString(); SemanticVersion onlineVersion = new SemanticVersion(response.get("tag_name").getAsString()); - String cVersion = Main.plugin.getDescription().getVersion(); - - if (cVersion.contains("-")) { - cVersion = cVersion.substring(0, cVersion.indexOf("-")); - } - - SemanticVersion currentVersion = new SemanticVersion(cVersion); + SemanticVersion currentVersion = Main.server.getPluginVersion(); // Provide download URL just in case we force update waitingDownloadUrl = downloadUrl; @@ -65,10 +59,10 @@ private static void checkForUpdateBlocking() { // Announcements if (Main.plugin.getConfig().getBoolean("updater.warn-on-outdated", false)) { - Bukkit.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + cVersion + " -> " + onlineVersion.getString()), "estools.update"); + Bukkit.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString()), "estools.update"); } if (Main.plugin.getConfig().getBoolean("updater.log-on-outdated", false)) { - Bukkit.getLogger().info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + cVersion + " -> " + onlineVersion.getString())); + Bukkit.getLogger().info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString())); } if (!Main.plugin.getConfig().getBoolean("updater.auto-update", false)) { @@ -87,7 +81,7 @@ public static void downloadNewUpdate() { downloadNewUpdate(null); } - public static void downloadNewUpdate(CommandSender reportTo) { + public static void downloadNewUpdate(EsCommandSender reportTo) { if (waitingDownloadUrl == null) { throw new RuntimeException("We aren't waiting for an update"); } From 168f864d64f7abab214f2d3825bf9211f77e6775 Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Fri, 24 May 2024 15:01:50 +1000 Subject: [PATCH 04/74] More meta, nbt, general movement towards abstraction --- .../estools/Commands/GetPersistentData.java | 17 ------ .../java/net/serble/estools/Commands/God.java | 22 +++---- .../net/serble/estools/Commands/Heal.java | 17 +++--- .../serble/estools/Commands/HideFlags.java | 26 ++++---- .../net/serble/estools/Commands/Infinite.java | 20 ++++-- .../net/serble/estools/Commands/InvSee.java | 11 ++-- .../net/serble/estools/Commands/Lore.java | 21 +++---- .../net/serble/estools/Commands/Midnight.java | 9 ++- .../net/serble/estools/Commands/Mount.java | 25 +++----- .../net/serble/estools/Commands/Music.java | 20 +++--- .../estools/Entrypoints/EsToolsBukkit.java | 4 +- src/main/java/net/serble/estools/Main.java | 14 ++--- .../java/net/serble/estools/MetaHandler.java | 28 ++++----- .../serble/estools/ServerApi/EsItemFlag.java | 37 +++++++++++ .../serble/estools/ServerApi/EsLocation.java | 8 +++ .../Implementations/Bukkit/BukkitEntity.java | 15 +++++ .../Implementations/Bukkit/BukkitHelper.java | 49 +++++++++++++++ .../Bukkit/BukkitItemMeta.java | 61 ++++++++++++++++++- .../Bukkit/BukkitPersistentDataContainer.java | 29 +++++++++ .../Implementations/Bukkit/BukkitPlayer.java | 7 +++ .../ServerApi/Interfaces/EsEntity.java | 2 + .../ServerApi/Interfaces/EsItemMeta.java | 12 ++++ .../ServerApi/Interfaces/EsPlayer.java | 2 + 23 files changed, 327 insertions(+), 129 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsItemFlag.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java diff --git a/src/main/java/net/serble/estools/Commands/GetPersistentData.java b/src/main/java/net/serble/estools/Commands/GetPersistentData.java index 8e8e4b7..f99f5ab 100644 --- a/src/main/java/net/serble/estools/Commands/GetPersistentData.java +++ b/src/main/java/net/serble/estools/Commands/GetPersistentData.java @@ -162,23 +162,6 @@ private static Tuple getData(String type, NamespacedKey key, Pe return new Tuple<>(1, null); } - @SuppressWarnings("UnstableApiUsage") - public static NamespacedKey getNamespacedKey(String keyString) { - if (Main.minecraftVersion.getMinor() >= 16) { - return NamespacedKey.fromString(keyString, Main.bukkitPlugin); - } - - String[] parts = keyString.split(":"); - if (parts.length == 2) { - return new NamespacedKey(parts[0], parts[1]); - } else if (parts.length == 1) { // Incorrectly formatted key - String pluginName = Main.server.getPluginName(); - return new NamespacedKey(pluginName, parts[0]); - } - - return null; - } - private static StringBuilder buildString(T[] values) { StringBuilder sb = new StringBuilder("["); for (T value : values) { diff --git a/src/main/java/net/serble/estools/Commands/God.java b/src/main/java/net/serble/estools/Commands/God.java index c12950b..3aa9a5d 100644 --- a/src/main/java/net/serble/estools/Commands/God.java +++ b/src/main/java/net/serble/estools/Commands/God.java @@ -2,26 +2,26 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; -import net.serble.estools.Main; +import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.LivingEntity; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; import java.util.*; +// TODO: Event command needs migrating public class God extends EntityCommand implements Listener { private static final HashMap currentPlayers = new HashMap<>(); private static final String usage = genUsage("/god [entity] [time]"); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); FileConfiguration f = ConfigManager.load("gods.yml"); List godList = f.getStringList("gods"); @@ -29,8 +29,8 @@ public void onEnable() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - LivingEntity p; + public boolean execute(EsCommandSender sender, String[] args) { + EsLivingEntity p; int timer = -1; if (args.length == 0) { @@ -38,7 +38,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - p = (LivingEntity) sender; + p = (EsLivingEntity) sender; } else { p = getEntity(sender, args[0]); @@ -68,7 +68,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St Bukkit.getScheduler().cancelTask(taskId); } - send(sender, "&aGod mode &6disabled&a for &6%s", getEntityName(p)); + send(sender, "&aGod mode &6disabled&a for &6%s", p.getName()); } else { int taskId = -1; @@ -77,7 +77,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St if (timer >= 0) { timerStr = timer / 20d + " seconds"; - taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin, () -> currentPlayers.remove(uid), timer); + taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, () -> currentPlayers.remove(uid), timer); } currentPlayers.put(uid, taskId); @@ -85,7 +85,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St save(); } - send(sender, "&aGod mode &6enabled&a for &6%s&a for &6%s", getEntityName(p), timerStr); + send(sender, "&aGod mode &6enabled&a for &6%s&a for &6%s", p.getName(), timerStr); } return true; diff --git a/src/main/java/net/serble/estools/Commands/Heal.java b/src/main/java/net/serble/estools/Commands/Heal.java index 00af820..9c2f487 100644 --- a/src/main/java/net/serble/estools/Commands/Heal.java +++ b/src/main/java/net/serble/estools/Commands/Heal.java @@ -1,22 +1,21 @@ package net.serble.estools.Commands; import net.serble.estools.EntityCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class Heal extends EntityCommand { private static final String usage = genUsage("/heal [entity]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - LivingEntity entity; + public boolean execute(EsCommandSender sender, String[] args) { + EsLivingEntity entity; if (args.length == 0) { if (isNotEntity(sender, usage)) { return false; } - entity = (LivingEntity) sender; + entity = (EsLivingEntity) sender; send(sender, "&aHealed!"); } else { entity = getEntity(sender, args[0]); @@ -25,11 +24,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - send(sender, "&aHealed &6%s", getEntityName(entity)); + send(sender, "&aHealed &6%s", entity.getName()); } - setHealth(entity, getMaxHealth(entity)); - entity.setFireTicks(0); + entity.setHealth(entity.getMaxHealth()); + entity.setOnFireTicks(0); return true; } diff --git a/src/main/java/net/serble/estools/Commands/HideFlags.java b/src/main/java/net/serble/estools/Commands/HideFlags.java index 09c0c68..72e3167 100644 --- a/src/main/java/net/serble/estools/Commands/HideFlags.java +++ b/src/main/java/net/serble/estools/Commands/HideFlags.java @@ -1,37 +1,37 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; +import net.serble.estools.ServerApi.EsItemFlag; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +import java.util.Objects; public class HideFlags extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ItemStack is = getMainHand((Player) sender); + EsItemStack is = ((EsPlayer) sender).getMainHand(); - if (is == null || is.getType() == Material.AIR || is.getItemMeta() == null) { + if (is == null || Objects.equals(is.getType(), "AIR") || is.getItemMeta() == null) { send(sender, "&cMust be a damageable item"); return false; } - ItemMeta im = is.getItemMeta(); + EsItemMeta im = is.getItemMeta(); String un; if (!im.getItemFlags().isEmpty()) { - im.removeItemFlags(ItemFlag.values()); + im.removeItemFlags(EsItemFlag.values()); un = "&aRemoved Item Flags"; } else { - im.addItemFlags(ItemFlag.values()); + im.addItemFlags(EsItemFlag.values()); un = "&aAdded Item Flags"; } diff --git a/src/main/java/net/serble/estools/Commands/Infinite.java b/src/main/java/net/serble/estools/Commands/Infinite.java index 71ad4c0..18542ef 100644 --- a/src/main/java/net/serble/estools/Commands/Infinite.java +++ b/src/main/java/net/serble/estools/Commands/Infinite.java @@ -1,7 +1,13 @@ package net.serble.estools.Commands; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -14,6 +20,7 @@ import java.util.ArrayList; import java.util.UUID; +// TODO: Events command, need migrating public class Infinite extends EsToolsCommand implements Listener { private static final ArrayList currentPlayers = new ArrayList<>(); @@ -23,12 +30,12 @@ public void onEnable() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - UUID pu = ((Player)sender).getUniqueId(); + UUID pu = ((EsPlayer) sender).getUniqueId(); if (!currentPlayers.contains(pu)) { currentPlayers.add(pu); @@ -41,16 +48,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } + // TODO: This entire function depends on bukkit @EventHandler public void blockPlace(BlockPlaceEvent e) { if (!currentPlayers.contains(e.getPlayer().getUniqueId())) { return; } - ItemStack item = e.getItemInHand().clone(); + EsItemStack item = new BukkitItemStack(e.getItemInHand().clone()); + EsPlayer p = new BukkitPlayer(e.getPlayer()); - Bukkit.getScheduler().runTask(Main.plugin, () -> { - setMainHand(e.getPlayer(), item); + // TODO: This is a terrible way of implementing this btw + Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, () -> { + p.setMainHand(item); // A bug exists where the item is not updated in the player's inventory, this is needed for that //noinspection UnstableApiUsage e.getPlayer().updateInventory(); diff --git a/src/main/java/net/serble/estools/Commands/InvSee.java b/src/main/java/net/serble/estools/Commands/InvSee.java index 000d764..7a43aa4 100644 --- a/src/main/java/net/serble/estools/Commands/InvSee.java +++ b/src/main/java/net/serble/estools/Commands/InvSee.java @@ -1,15 +1,14 @@ package net.serble.estools.Commands; import net.serble.estools.PlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class InvSee extends PlayerCommand { private static final String usage = genUsage("/invsee "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } @@ -19,13 +18,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - Player target = getPlayer(sender, args[0]); + EsPlayer target = getPlayer(sender, args[0]); if (target == null) { return false; } - ((Player) sender).openInventory(target.getInventory()); + ((EsPlayer) sender).openInventory(target.getInventory()); send(sender, "&aOpened &6%s's &aInventory", target.getName()); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Lore.java b/src/main/java/net/serble/estools/Commands/Lore.java index ae1b157..b6af4ed 100644 --- a/src/main/java/net/serble/estools/Commands/Lore.java +++ b/src/main/java/net/serble/estools/Commands/Lore.java @@ -2,10 +2,9 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.MetaHandler; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.Arrays; @@ -17,7 +16,7 @@ public class Lore extends EsToolsCommand { "/lore "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } @@ -27,8 +26,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - Player p = (Player)sender; - ItemStack is = getMainHand(p); + EsPlayer p = (EsPlayer)sender; + EsItemStack is = p.getMainHand(); if (is.getItemMeta() == null) { send(sender, "&cThis item cannot have lore!"); @@ -86,7 +85,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - private int getLine(CommandSender sender, String num, int limit) { + private int getLine(EsCommandSender sender, String num, int limit) { try { int line = Integer.parseInt(num) - 1; @@ -108,7 +107,7 @@ private String getLore(String[] args, int start) { } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 1) { @@ -117,12 +116,12 @@ public List tabComplete(CommandSender sender, String[] args, String lArg tab.add("insert"); tab.add("remove"); } else if (args.length == 2 && equalsOr(args[0].toLowerCase(), "set", "insert", "remove")) { - if (!(sender instanceof Player)) { + if (!(sender instanceof EsPlayer)) { tab.add("1"); return tab; } - ItemStack is = getMainHand((Player)sender); + EsItemStack is = ((EsPlayer)sender).getMainHand(); List lore = MetaHandler.getLore(is); for (int i = 1; i <= lore.size(); i++) { tab.add(String.valueOf(i)); diff --git a/src/main/java/net/serble/estools/Commands/Midnight.java b/src/main/java/net/serble/estools/Commands/Midnight.java index fcc0c76..c1ce3c3 100644 --- a/src/main/java/net/serble/estools/Commands/Midnight.java +++ b/src/main/java/net/serble/estools/Commands/Midnight.java @@ -1,19 +1,18 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Midnight extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ((Player)sender).getWorld().setTime(18000); + ((EsPlayer)sender).getWorld().setTime(18000); send(sender, "&aSet time to &6midnight"); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Mount.java b/src/main/java/net/serble/estools/Commands/Mount.java index 3cc82af..592a6b3 100644 --- a/src/main/java/net/serble/estools/Commands/Mount.java +++ b/src/main/java/net/serble/estools/Commands/Mount.java @@ -1,10 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.EntityCommand; -import net.serble.estools.Main; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEntity; import java.util.ArrayList; import java.util.List; @@ -13,38 +11,33 @@ public class Mount extends EntityCommand { private static final String usage = genUsage("/mount [rider1] [rider2]..."); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - Entity target = getNonLivingEntity(sender, args[0]); + EsEntity target = getNonLivingEntity(sender, args[0]); if (target == null) { return false; } - List riders = new ArrayList<>(); + List riders = new ArrayList<>(); for (int i = 1; i < args.length; i++) { riders.add(getNonLivingEntity(sender, args[i])); } if (riders.isEmpty()) { - if (!(sender instanceof Entity)) { + if (!(sender instanceof EsEntity)) { send(sender, "&cConsole must specify a rider"); return false; } - riders.add((Entity) sender); + riders.add((EsEntity) sender); } - for (Entity entity : riders) { - if (Main.majorVersion > 11) { - target.addPassenger(entity); - } else { - //noinspection deprecation - target.setPassenger(entity); - } + for (EsEntity entity : riders) { + target.addPassenger(entity); target = entity; } diff --git a/src/main/java/net/serble/estools/Commands/Music.java b/src/main/java/net/serble/estools/Commands/Music.java index ed9f72d..46a653f 100644 --- a/src/main/java/net/serble/estools/Commands/Music.java +++ b/src/main/java/net/serble/estools/Commands/Music.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.*; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -11,6 +13,7 @@ import java.util.List; import java.util.Random; +// TODO: Incomplete public class Music extends EsToolsCommand { private static final String usage = genUsage("/music [song]"); private static final List musics = new ArrayList<>(); @@ -19,7 +22,7 @@ public class Music extends EsToolsCommand { @Override public void onEnable() { - if (Main.majorVersion > 12) { + if (Main.minecraftVersion.getMinor() > 12) { for (Sound s : Sound.values()) { if (s.name().startsWith("MUSIC_DISC")) { musics.add(s); @@ -46,17 +49,17 @@ public void onEnable() { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - Player p = (Player) sender; + EsPlayer p = (EsPlayer) sender; Sound sound; if (args.length > 0 && !args[0].equalsIgnoreCase("random")) { try { - if (Main.majorVersion > 12) { + if (Main.minecraftVersion.getMinor() > 12) { sound = Sound.valueOf("MUSIC_DISC_" + args[0].toUpperCase()); } else { @@ -70,16 +73,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St sound = musics.get(random.nextInt(musics.size())); } - if (Main.majorVersion >= 11) { - p.playSound(p.getLocation().add(0, 1000, 0), sound, SoundCategory.RECORDS, 100000, 1); - } - else { - p.playSound(p.getLocation().add(0, 1000, 0), sound, 100000, 1); - } + p.playSound(sound, p.getLocation().add(0, 1000, 0), 100000, 1); // Substring away the MUSIC_DISK_ or the RECORD_ depending on the version // then reformat to be all lowercase except for the first character - String name = sound.name().toLowerCase().substring(Main.majorVersion > 12 ? 11 : 7); + String name = sound.name().toLowerCase().substring(Main.minecraftVersion.getMinor() > 12 ? 11 : 7); name = String.valueOf(name.charAt(0)).toUpperCase() + name.substring(1); send(sender, "&aNow Playing: &6%s", name); diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java index 49ab70f..81a72f7 100644 --- a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -5,10 +5,12 @@ import org.bukkit.plugin.java.JavaPlugin; public class EsToolsBukkit extends JavaPlugin { + /** Will be null if plugin is not Bukkit */ + public static EsToolsBukkit plugin; @Override public void onEnable() { - Main.bukkitPlugin = this; + plugin = this; Main main = new Main(ServerPlatform.Bukkit, this); main.enable(); } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 229b91d..c7cad94 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -8,6 +8,7 @@ import net.serble.estools.Commands.PowerPick.*; import net.serble.estools.Commands.Teleport.*; import net.serble.estools.Commands.Warps.*; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; import net.serble.estools.ServerApi.ServerPlatform; @@ -37,7 +38,6 @@ public class Main { public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; public static EsServerSoftware server; - @Deprecated public static JavaPlugin bukkitPlugin; // TODO: Just here until we get everything to work private final Object context; private static final int bStatsId = 21760; @@ -64,20 +64,20 @@ public void enable() { } // Create config if not exists, saveDefaultConfig() doesn't exist in 1.0 - File configFile = new File(bukkitPlugin.getDataFolder(), "config.yml"); + File configFile = new File(EsToolsBukkit.plugin.getDataFolder(), "config.yml"); if (!configFile.exists()) { saveResource("config.yml", false); } config = ConfigManager.load("config.yml"); // Add keys that don't exist from the default config - if (ConfigManager.patchDefaults(getConfig(), bukkitPlugin.getResource("config.yml"))) { + if (ConfigManager.patchDefaults(getConfig(), EsToolsBukkit.plugin.getResource("config.yml"))) { ConfigManager.save("config.yml", getConfig()); // Only save if something changed } // Metrics if (getConfig().getBoolean("metrics", true)) { - Metrics metrics = new Metrics(bukkitPlugin, bStatsId); + Metrics metrics = new Metrics(EsToolsBukkit.plugin, bStatsId); metrics.addCustomChart(new SimplePie("vault_enabled", () -> String.valueOf(Vault.economy != null))); Bukkit.getLogger().info("Started bStat metrics"); } else { @@ -173,7 +173,7 @@ public void enable() { // Setup Command Overloads public PluginCommand sc(String name, EsToolsCommand ce) { - PluginCommand cmd = bukkitPlugin.getCommand(name); + PluginCommand cmd = EsToolsBukkit.plugin.getCommand(name); assert cmd != null; cmd.setExecutor(ce); ce.onEnable(); @@ -243,8 +243,8 @@ public FileConfiguration getConfig() { // Overriding because method doesn't exist in very early versions public void saveResource(String res, boolean replace) { - InputStream resource = bukkitPlugin.getResource(res); - File file = new File(bukkitPlugin.getDataFolder(), res); + InputStream resource = EsToolsBukkit.plugin.getResource(res); + File file = new File(EsToolsBukkit.plugin.getDataFolder(), res); if (file.exists()) { Bukkit.getLogger().info("Tried to copy resource but it already exists: " + res); diff --git a/src/main/java/net/serble/estools/MetaHandler.java b/src/main/java/net/serble/estools/MetaHandler.java index d8ac910..4d72cba 100644 --- a/src/main/java/net/serble/estools/MetaHandler.java +++ b/src/main/java/net/serble/estools/MetaHandler.java @@ -1,24 +1,25 @@ package net.serble.estools; import net.serble.estools.Commands.Potion; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; -import java.util.ArrayList; import java.util.List; import java.util.Objects; // This class exists because Minecraft < 1.4 doesn't have ItemMeta. It just encapsulates all ItemMeta methods. public class MetaHandler { - public static ItemStack getPotion(CommandSender sender, Potion.PotType potType, String effect, int duration, int amp, int amount) { - if (Main.majorVersion >= 9) { + public static ItemStack getPotion(EsCommandSender sender, Potion.PotType potType, String effect, int duration, int amp, int amount) { + if (Main.minecraftVersion.getMinor() >= 9) { String type = potType == Potion.PotType.drink ? "POTION" : potType.toString().toUpperCase() + "_POTION"; @@ -37,7 +38,7 @@ public static ItemStack getPotion(CommandSender sender, Potion.PotType potType, meta.addCustomEffect(new PotionEffect(effType, duration, amp-1), true); pot.setItemMeta(meta); return pot; - } else if (Main.majorVersion >= 4) { + } else if (Main.minecraftVersion.getMinor() >= 4) { PotionType effType; try { effType = Effects.getPotionByName(effect); @@ -56,23 +57,18 @@ public static ItemStack getPotion(CommandSender sender, Potion.PotType potType, } } - public static void renameItem(ItemStack item, String name) { - ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); + public static void renameItem(EsItemStack item, String name) { + EsItemMeta meta = Objects.requireNonNull(item.getItemMeta()); meta.setDisplayName(name); item.setItemMeta(meta); } - public static List getLore(ItemStack item) { - ItemMeta meta = item.getItemMeta(); - if (meta == null || !meta.hasLore()) { - return new ArrayList<>(); - } - - return meta.getLore(); + public static List getLore(EsItemStack item) { + return item.getItemMeta().getLore(); } - public static void setLore(ItemStack item, List lore) { - ItemMeta meta = Objects.requireNonNull(item.getItemMeta()); + public static void setLore(EsItemStack item, List lore) { + EsItemMeta meta = Objects.requireNonNull(item.getItemMeta()); meta.setLore(lore); item.setItemMeta(meta); } diff --git a/src/main/java/net/serble/estools/ServerApi/EsItemFlag.java b/src/main/java/net/serble/estools/ServerApi/EsItemFlag.java new file mode 100644 index 0000000..5e5cc56 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsItemFlag.java @@ -0,0 +1,37 @@ +package net.serble.estools.ServerApi; + +public enum EsItemFlag { + /** + * Setting to show/hide enchants + */ + HIDE_ENCHANTS, + /** + * Setting to show/hide Attributes like Damage + */ + HIDE_ATTRIBUTES, + /** + * Setting to show/hide the unbreakable State + */ + HIDE_UNBREAKABLE, + /** + * Setting to show/hide what the ItemStack can break/destroy + */ + HIDE_DESTROYS, + /** + * Setting to show/hide where this ItemStack can be build/placed on + */ + HIDE_PLACED_ON, + /** + * Setting to show/hide potion effects, book and firework information, map + * tooltips, patterns of banners, and enchantments of enchanted books. + */ + HIDE_POTION_EFFECTS, + /** + * Setting to show/hide dyes from colored leather armor. + */ + HIDE_DYE, + /** + * Setting to show/hide armor trim from leather armor. + */ + HIDE_ARMOR_TRIM; +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsLocation.java b/src/main/java/net/serble/estools/ServerApi/EsLocation.java index c1fbcdd..b500d33 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsLocation.java +++ b/src/main/java/net/serble/estools/ServerApi/EsLocation.java @@ -70,4 +70,12 @@ public int getBlockY() { public int getBlockZ() { return (int) Math.round(getZ()); } + + public EsLocation add(Position other) { + return add(other.getX(), other.getY(), other.getZ()); + } + + public EsLocation add(double x, double y, double z) { + return new EsLocation(world, getX() + x, getY() + y, getZ() + z); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 6848354..8bb7954 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -115,4 +115,19 @@ public void addScoreboardTag(String tag) { public boolean hasScoreboardTag(String tag) { return bukkitEntity.getScoreboardTags().contains(tag); } + + @Override + public void setOnFireTicks(int ticks) { + bukkitEntity.setFireTicks(ticks); + } + + @Override + public void addPassenger(EsEntity entity) { + if (Main.minecraftVersion.getMinor() > 11) { + bukkitEntity.addPassenger(((BukkitEntity) entity).getBukkitEntity()); + } else { + //noinspection deprecation + bukkitEntity.setPassenger(((BukkitEntity) entity).getBukkitEntity()); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 97b3197..94281c7 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -1,7 +1,9 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPersistentDataType; import net.serble.estools.ServerApi.Position; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Interfaces.*; @@ -16,6 +18,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.persistence.PersistentDataType; import org.bukkit.util.Vector; import java.util.Objects; @@ -145,4 +148,50 @@ public static Enchantment getBukkitEnchantment(String name) { // We have to use deprecated method for pre 1.13 return Enchantment.getByName(name); } + + public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType type) { + switch (type) { + case Byte: + return PersistentDataType.BYTE; + case Long: + return PersistentDataType.LONG; + case Float: + return PersistentDataType.FLOAT; + case Short: + return PersistentDataType.SHORT; + case Double: + return PersistentDataType.DOUBLE; + case String: + return PersistentDataType.STRING; + case Boolean: + return PersistentDataType.BOOLEAN; + case Integer: + return PersistentDataType.INTEGER; + case IntArray: + return PersistentDataType.INTEGER_ARRAY; + case ByteArray: + return PersistentDataType.BYTE_ARRAY; + case LongArray: + return PersistentDataType.LONG_ARRAY; + } + + throw new RuntimeException("Unsupported data type"); + } + + @SuppressWarnings("UnstableApiUsage") + public static NamespacedKey getNamespacedKey(String keyString) { + if (Main.minecraftVersion.getMinor() >= 16) { + return NamespacedKey.fromString(keyString, EsToolsBukkit.plugin); + } + + String[] parts = keyString.split(":"); + if (parts.length == 2) { + return new NamespacedKey(parts[0], parts[1]); + } else if (parts.length == 1) { // Incorrectly formatted key + String pluginName = Main.server.getPluginName(); + return new NamespacedKey(pluginName, parts[0]); + } + + return null; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java index 3b3e582..5a8ab35 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -1,9 +1,15 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.ServerApi.EsItemFlag; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; -import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.meta.ItemMeta; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + public class BukkitItemMeta implements EsItemMeta { private final ItemMeta bukkitMeta; @@ -21,6 +27,59 @@ public boolean isUnbreakable() { return bukkitMeta.isUnbreakable(); } + @Override + public Set getItemFlags() { + Set bukkitFlags = bukkitMeta.getItemFlags(); + Set out = new HashSet<>(); + for (ItemFlag flag : bukkitFlags) { + out.add(EsItemFlag.valueOf(flag.name())); + } + return out; + } + + @Override + public void addItemFlags(EsItemFlag... flags) { + ItemFlag[] bukkitFlags = new ItemFlag[flags.length]; + for (int i = 0; i < flags.length; i++) { + EsItemFlag flag = flags[i]; + bukkitFlags[i] = ItemFlag.valueOf(flag.name()); + } + bukkitMeta.addItemFlags(bukkitFlags); + } + + @Override + public void removeItemFlags(EsItemFlag... flags) { + ItemFlag[] bukkitFlags = new ItemFlag[flags.length]; + for (int i = 0; i < flags.length; i++) { + EsItemFlag flag = flags[i]; + bukkitFlags[i] = ItemFlag.valueOf(flag.name()); + } + bukkitMeta.removeItemFlags(bukkitFlags); + } + + @Override + public void setDisplayName(String val) { + bukkitMeta.setDisplayName(val); + } + + @Override + public String getDisplayName() { + return bukkitMeta.getDisplayName(); + } + + @Override + public List getLore() { + if (!bukkitMeta.hasLore()) { + return new ArrayList<>(); + } + return bukkitMeta.getLore(); + } + + @Override + public void setLore(List val) { + bukkitMeta.setLore(val); + } + public ItemMeta getBukkitMeta() { return bukkitMeta; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java new file mode 100644 index 0000000..334ff7b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java @@ -0,0 +1,29 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPersistentDataType; +import net.serble.estools.ServerApi.Interfaces.EsPersistentDataContainer; +import org.bukkit.NamespacedKey; +import org.bukkit.persistence.PersistentDataContainer; + +import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.*; + +@SuppressWarnings("unchecked") // Trust me bro +public class BukkitPersistentDataContainer implements EsPersistentDataContainer { + private final PersistentDataContainer bukkitContainer; + + public BukkitPersistentDataContainer(PersistentDataContainer container) { + bukkitContainer = container; + } + + @Override + public void set(String key, EsPersistentDataType type, Object val) { + bukkitContainer.set(getNamespacedKey(key), BukkitHelper.toBukkitPersistentDataType(type), val); + } + + @Override + public Object get(String key, EsPersistentDataType type) { + return null; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 9411883..755fd64 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -2,8 +2,10 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; +import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; import org.jetbrains.annotations.Nullable; @@ -113,6 +115,11 @@ public boolean isFlying() { return bukkitPlayer.isFlying(); } + @Override + public void playSound(String sound, EsLocation loc, int volume, int pitch) { + bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + } + private Block bukkitGetTargetBlock() { if (Main.minecraftVersion.getMinor() > 12) { return bukkitPlayer.getTargetBlockExact(5); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java index c337263..a1e3fec 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java @@ -17,4 +17,6 @@ public interface EsEntity extends EsCommandSender { Set getScoreboardTags(); void addScoreboardTag(String tag); boolean hasScoreboardTag(String tag); + void setOnFireTicks(int ticks); + void addPassenger(EsEntity entity); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java index 1b141c4..c76b432 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java @@ -1,6 +1,18 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.ServerApi.EsItemFlag; + +import java.util.List; +import java.util.Set; + public interface EsItemMeta { void setUnbreakable(boolean val); boolean isUnbreakable(); + Set getItemFlags(); + void addItemFlags(EsItemFlag... flags); + void removeItemFlags(EsItemFlag... flags); + void setDisplayName(String val); + String getDisplayName(); + List getLore(); + void setLore(List val); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 6176962..3279433 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.EsLocation; public interface EsPlayer extends EsLivingEntity { int getFoodLevel(); @@ -21,4 +22,5 @@ public interface EsPlayer extends EsLivingEntity { boolean getAllowFlight(); void setAllowFlight(boolean val); boolean isFlying(); + void playSound(String sound, EsLocation loc, int volume, int pitch); } From 3b142631b4183cf37cc06d15a5f2f9640cf284f4 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 24 May 2024 19:12:23 +1000 Subject: [PATCH 05/74] IT BUILDS --- .../net/serble/estools/Commands/Back.java | 3 +- .../net/serble/estools/Commands/Buddha.java | 5 +- .../net/serble/estools/Commands/CChest.java | 5 +- .../estools/Commands/GetPersistentData.java | 54 ++++++++---------- .../serble/estools/Commands/Give/Give.java | 5 +- .../net/serble/estools/Commands/Infinite.java | 7 +-- .../net/serble/estools/Commands/Music.java | 41 +++++++------- .../net/serble/estools/Commands/Night.java | 9 ++- .../net/serble/estools/Commands/Noon.java | 9 ++- .../net/serble/estools/Commands/Potion.java | 44 ++++++--------- .../net/serble/estools/Commands/Rain.java | 13 ++--- .../Commands/RemovePersistentData.java | 30 +++++----- .../net/serble/estools/Commands/Rename.java | 13 ++--- .../net/serble/estools/Commands/SafeTp.java | 11 ++-- .../serble/estools/Commands/SetHealth.java | 17 +++--- .../serble/estools/Commands/SetHunger.java | 11 ++-- .../serble/estools/Commands/SetMaxHealth.java | 15 +++-- .../estools/Commands/SetPersistentData.java | 56 +++++++++---------- .../estools/Commands/SetSaturation.java | 11 ++-- .../net/serble/estools/Commands/SetStack.java | 9 ++- .../estools/Commands/SetUnbreakable.java | 30 +++++----- .../net/serble/estools/Commands/Smite.java | 10 ++-- .../net/serble/estools/Commands/Sudo.java | 16 +++--- .../net/serble/estools/Commands/Suicide.java | 12 ++-- .../java/net/serble/estools/Commands/Sun.java | 13 ++--- .../net/serble/estools/Commands/Thunder.java | 13 ++--- .../serble/estools/Commands/WrongVersion.java | 5 +- .../net/serble/estools/ConfigManager.java | 4 +- src/main/java/net/serble/estools/Effects.java | 35 ++++++------ .../java/net/serble/estools/MetaHandler.java | 50 +++-------------- .../net/serble/estools/PlayerCommand.java | 8 +-- .../serble/estools/ServerApi/EsPotType.java | 7 +++ .../Bukkit/BukkitItemMeta.java | 6 ++ .../Bukkit/BukkitItemStack.java | 13 ++++- .../Bukkit/BukkitLivingEntity.java | 7 ++- .../Bukkit/BukkitPersistentDataContainer.java | 12 ++-- .../Implementations/Bukkit/BukkitPlayer.java | 5 -- .../Implementations/Bukkit/BukkitServer.java | 53 ++++++++++++++++-- .../Implementations/Bukkit/BukkitWorld.java | 15 +++++ .../ServerApi/Interfaces/EsItemMeta.java | 1 + .../ServerApi/Interfaces/EsItemStack.java | 2 + .../ServerApi/Interfaces/EsLivingEntity.java | 1 + .../Interfaces/EsPersistentDataContainer.java | 1 + .../ServerApi/Interfaces/EsPlayer.java | 1 - .../Interfaces/EsServerSoftware.java | 3 + .../estools/ServerApi/Interfaces/EsWorld.java | 3 + .../net/serble/estools/Signs/Balance.java | 8 ++- .../net/serble/estools/Signs/Disposal.java | 9 +-- .../java/net/serble/estools/Signs/Feed.java | 4 +- .../java/net/serble/estools/Signs/Give.java | 8 +-- .../java/net/serble/estools/Signs/Heal.java | 12 ++-- .../java/net/serble/estools/Signs/Repair.java | 10 ++-- .../java/net/serble/estools/Signs/Sell.java | 11 ++-- .../net/serble/estools/Signs/SignMain.java | 8 ++- .../net/serble/estools/Signs/SignType.java | 14 ++--- src/main/java/net/serble/estools/Tester.java | 3 +- 56 files changed, 400 insertions(+), 381 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsPotType.java diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index e169f95..28f6ee5 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -3,6 +3,7 @@ import java.util.HashMap; import java.util.UUID; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; @@ -22,7 +23,7 @@ public class Back extends EsToolsCommand implements Listener { @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); } @Override diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 2e7a3e6..34315a2 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -2,6 +2,7 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; @@ -20,7 +21,7 @@ public class Buddha extends EntityCommand implements Listener { @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); FileConfiguration f = ConfigManager.load("gods.yml"); List buddhas = f.getStringList("buddhas"); @@ -76,7 +77,7 @@ public boolean execute(EsCommandSender sender, String[] args) { if (timer >= 0) { timerStr = timer / 20d + " seconds"; - taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(Main.bukkitPlugin, () -> currentPlayers.remove(uid), timer); + taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, () -> currentPlayers.remove(uid), timer); } currentPlayers.put(uid, taskId); diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index c92fbca..77c4591 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -1,5 +1,6 @@ package net.serble.estools.Commands; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.ConfigManager; import net.serble.estools.Main; @@ -29,7 +30,7 @@ public class CChest extends EsToolsCommand implements Listener { @Override public void onEnable() { if (Main.minecraftVersion.getMinor() > 4) { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.bukkitPlugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); } } @@ -160,7 +161,7 @@ public void inventoryClick(final InventoryClickEvent e) { } private static void setItemTask(org.bukkit.inventory.Inventory inv, int slot, ItemStack item) { - Bukkit.getScheduler().runTask(Main.bukkitPlugin, () -> inv.setItem(slot, item)); + Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, () -> inv.setItem(slot, item)); } // Cancel drag if inside cChest diff --git a/src/main/java/net/serble/estools/Commands/GetPersistentData.java b/src/main/java/net/serble/estools/Commands/GetPersistentData.java index f99f5ab..97eac1e 100644 --- a/src/main/java/net/serble/estools/Commands/GetPersistentData.java +++ b/src/main/java/net/serble/estools/Commands/GetPersistentData.java @@ -1,18 +1,10 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import net.serble.estools.Main; -import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import net.serble.estools.ServerApi.Interfaces.EsItemMeta; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.EsPersistentDataType; +import net.serble.estools.ServerApi.Interfaces.*; import net.serble.estools.Tuple; import org.apache.commons.lang.ArrayUtils; -import org.bukkit.NamespacedKey; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.persistence.PersistentDataType; import java.util.ArrayList; import java.util.List; @@ -30,15 +22,15 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - String tagString = args[0].toLowerCase(); + String key = args[0].toLowerCase(); String typeString = args[1].toLowerCase(); EsItemStack item = ((EsPlayer) sender).getMainHand(); - NamespacedKey key = getNamespacedKey(tagString); - if (key == null) { - send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); - return false; - } +// NamespacedKey key = getNamespacedKey(key); +// if (key == null) { +// send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); +// return false; +// } EsItemMeta meta = item.getItemMeta(); if (meta == null) { @@ -46,11 +38,11 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - PersistentDataContainer data = meta.getPersistentDataContainer(); + EsPersistentDataContainer data = meta.getPersistentDataContainer(); Tuple value = getData(typeString, key, data); if (value.a() == 3) { - send(sender, "&cNBT tag &e\"" + tagString + "\"&c does not exist!"); + send(sender, "&cNBT tag &e\"" + key + "\"&c does not exist!"); return false; } @@ -60,19 +52,19 @@ public boolean execute(EsCommandSender sender, String[] args) { } if (value.a() == 2) { - send(sender, "&cNBT tag &e\"" + tagString + "\"&c exists, but is not a " + typeString + "!"); + send(sender, "&cNBT tag &e\"" + key + "\"&c exists, but is not a " + typeString + "!"); return false; } - send(sender, "&aNBT tag &e\"" + tagString + "\"&a is &e" + value.b() + "&a!"); + send(sender, "&aNBT tag &e\"" + key + "\"&a is &e" + value.b() + "&a!"); return true; } - private static Tuple getData(String type, NamespacedKey key, PersistentDataContainer data) { + private static Tuple getData(String type, String key, EsPersistentDataContainer data) { try { switch (type) { case "string": { - String value = data.get(key, PersistentDataType.STRING); + String value = (String) data.get(key, EsPersistentDataType.String); if (value == null) { return new Tuple<>(3, null); } @@ -81,7 +73,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "integer": { - Integer value = data.get(key, PersistentDataType.INTEGER); + Integer value = (Integer) data.get(key, EsPersistentDataType.Integer); if (value == null) { return new Tuple<>(3, null); } @@ -89,7 +81,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "double": { - Double value = data.get(key, PersistentDataType.DOUBLE); + Double value = (Double) data.get(key, EsPersistentDataType.Double); if (value == null) { return new Tuple<>(3, null); } @@ -97,7 +89,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "float": { - Float value = data.get(key, PersistentDataType.FLOAT); + Float value = (Float) data.get(key, EsPersistentDataType.Float); if (value == null) { return new Tuple<>(3, null); } @@ -105,7 +97,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "long": { - Long value = data.get(key, PersistentDataType.LONG); + Long value = (Long) data.get(key, EsPersistentDataType.Long); if (value == null) { return new Tuple<>(3, null); } @@ -113,7 +105,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "short": { - Short value = data.get(key, PersistentDataType.SHORT); + Short value = (Short) data.get(key, EsPersistentDataType.Short); if (value == null) { return new Tuple<>(3, null); } @@ -121,7 +113,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "byte": { - Byte value = data.get(key, PersistentDataType.BYTE); + Byte value = (Byte) data.get(key, EsPersistentDataType.Byte); if (value == null) { return new Tuple<>(3, null); } @@ -129,7 +121,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "byte_array": { - byte[] value = data.get(key, PersistentDataType.BYTE_ARRAY); + byte[] value = (byte[]) data.get(key, EsPersistentDataType.ByteArray); if (value == null) { return new Tuple<>(3, null); } @@ -138,7 +130,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "int_array": { - int[] value = data.get(key, PersistentDataType.INTEGER_ARRAY); + int[] value = (int[]) data.get(key, EsPersistentDataType.IntArray); if (value == null) { return new Tuple<>(3, null); } @@ -147,7 +139,7 @@ private static Tuple getData(String type, NamespacedKey key, Pe } case "long_array": { - long[] value = data.get(key, PersistentDataType.LONG_ARRAY); + long[] value = (long[]) data.get(key, EsPersistentDataType.LongArray); if (value == null) { return new Tuple<>(3, null); } diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index e3fcca7..bae3f8b 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -1,6 +1,7 @@ package net.serble.estools.Commands.Give; import net.serble.estools.*; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Bukkit; @@ -69,7 +70,7 @@ public static void enable() { try { copyDefaultGiveYML(); - f = ConfigManager.load(new File(Main.bukkitPlugin.getDataFolder(), "give.yml")); + f = ConfigManager.load(new File(EsToolsBukkit.plugin.getDataFolder(), "give.yml")); materials = loadItems(f); } catch (Exception ignored) { } } @@ -134,7 +135,7 @@ private static void copyDefaultGiveYML() throws IOException { //noinspection ResultOfMethodCallIgnored inputStream.read(buffer); - File targetFile = new File(Main.bukkitPlugin.getDataFolder(), "give.yml"); + File targetFile = new File(EsToolsBukkit.plugin.getDataFolder(), "give.yml"); OutputStream outStream = Files.newOutputStream(targetFile.toPath()); outStream.write(buffer); diff --git a/src/main/java/net/serble/estools/Commands/Infinite.java b/src/main/java/net/serble/estools/Commands/Infinite.java index 18542ef..5f412fd 100644 --- a/src/main/java/net/serble/estools/Commands/Infinite.java +++ b/src/main/java/net/serble/estools/Commands/Infinite.java @@ -2,20 +2,15 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; -import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.UUID; @@ -26,7 +21,7 @@ public class Infinite extends EsToolsCommand implements Listener { @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); } @Override diff --git a/src/main/java/net/serble/estools/Commands/Music.java b/src/main/java/net/serble/estools/Commands/Music.java index 46a653f..226887e 100644 --- a/src/main/java/net/serble/estools/Commands/Music.java +++ b/src/main/java/net/serble/estools/Commands/Music.java @@ -4,41 +4,37 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.*; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Random; // TODO: Incomplete public class Music extends EsToolsCommand { private static final String usage = genUsage("/music [song]"); - private static final List musics = new ArrayList<>(); + private static final List musics = new ArrayList<>(); private static final List tabComplete = new ArrayList<>(); private static final Random random = new Random(); @Override public void onEnable() { if (Main.minecraftVersion.getMinor() > 12) { - for (Sound s : Sound.values()) { - if (s.name().startsWith("MUSIC_DISC")) { + for (String s : Main.server.getSounds()) { + if (s.startsWith("MUSIC_DISC")) { musics.add(s); - tabComplete.add(s.name().toLowerCase().substring(11)); + tabComplete.add(s.toLowerCase().substring(11)); } } - musics.remove(Sound.MUSIC_DISC_13); - musics.remove(Sound.MUSIC_DISC_11); + musics.remove("MUSIC_DISC_13"); + musics.remove("MUSIC_DISC_11"); } else { - for (Sound s : Sound.values()) { - String name = s.name(); - if (name.startsWith("RECORD")) { - tabComplete.add(s.name().toLowerCase().substring(7)); + for (String s : Main.server.getSounds()) { + if (s.startsWith("RECORD")) { + tabComplete.add(s.toLowerCase().substring(7)); - if (!name.equals("RECORD_11") && !name.equals("RECORD_13")) { + if (!s.equals("RECORD_11") && !s.equals("RECORD_13")) { musics.add(s); } } @@ -56,14 +52,14 @@ public boolean execute(EsCommandSender sender, String[] args) { EsPlayer p = (EsPlayer) sender; - Sound sound; + String sound; if (args.length > 0 && !args[0].equalsIgnoreCase("random")) { try { if (Main.minecraftVersion.getMinor() > 12) { - sound = Sound.valueOf("MUSIC_DISC_" + args[0].toUpperCase()); + sound = "MUSIC_DISC_" + args[0].toUpperCase(); } else { - sound = Sound.valueOf("RECORD_" + args[0].toUpperCase()); + sound = "RECORD_" + args[0].toUpperCase(); } } catch (IllegalArgumentException e) { send(sender, usage); @@ -73,11 +69,16 @@ public boolean execute(EsCommandSender sender, String[] args) { sound = musics.get(random.nextInt(musics.size())); } + if (Arrays.stream(Main.server.getSounds()).noneMatch(s -> s.equalsIgnoreCase(sound))) { + send(sender, usage); + return false; + } + p.playSound(sound, p.getLocation().add(0, 1000, 0), 100000, 1); // Substring away the MUSIC_DISK_ or the RECORD_ depending on the version // then reformat to be all lowercase except for the first character - String name = sound.name().toLowerCase().substring(Main.minecraftVersion.getMinor() > 12 ? 11 : 7); + String name = sound.toLowerCase().substring(Main.minecraftVersion.getMinor() > 12 ? 11 : 7); name = String.valueOf(name.charAt(0)).toUpperCase() + name.substring(1); send(sender, "&aNow Playing: &6%s", name); @@ -85,7 +86,7 @@ public boolean execute(EsCommandSender sender, String[] args) { } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { if (args.length == 1) { return new ArrayList<>(tabComplete); } diff --git a/src/main/java/net/serble/estools/Commands/Night.java b/src/main/java/net/serble/estools/Commands/Night.java index 3d4233e..4089b4d 100644 --- a/src/main/java/net/serble/estools/Commands/Night.java +++ b/src/main/java/net/serble/estools/Commands/Night.java @@ -1,19 +1,18 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Night extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ((Player)sender).getWorld().setTime(13000); + ((EsPlayer)sender).getWorld().setTime(13000); send(sender, "&aSet time to &6night"); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Noon.java b/src/main/java/net/serble/estools/Commands/Noon.java index d177ad2..fb115aa 100644 --- a/src/main/java/net/serble/estools/Commands/Noon.java +++ b/src/main/java/net/serble/estools/Commands/Noon.java @@ -1,19 +1,18 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Noon extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ((Player)sender).getWorld().setTime(6000); + ((EsPlayer)sender).getWorld().setTime(6000); send(sender, "&aSet time to &6noon"); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Potion.java b/src/main/java/net/serble/estools/Commands/Potion.java index 3ef9a20..8173550 100644 --- a/src/main/java/net/serble/estools/Commands/Potion.java +++ b/src/main/java/net/serble/estools/Commands/Potion.java @@ -4,12 +4,10 @@ import net.serble.estools.EntityCommand; import net.serble.estools.Main; import net.serble.estools.MetaHandler; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionType; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.List; @@ -19,7 +17,7 @@ public class Potion extends EntityCommand { private static final String usage = genUsage("/potion [amplifier] [duration] [amount] [drink/splash/lingering] [player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; @@ -40,31 +38,31 @@ public boolean onCommand(CommandSender sender, Command command, String label, St duration = tryParseInt(args[2], 60*5)*20; } - PotType potType = PotType.drink; + EsPotType potType = EsPotType.drink; if (args.length >= 5) { try { - potType = PotType.valueOf(args[4].toLowerCase()); + potType = EsPotType.valueOf(args[4].toLowerCase()); } catch (IllegalArgumentException ignored) { send(sender, "&cInvalid potion type, must be drink, splash, or lingering (if on 1.9+)"); return false; } // Check versions - if (Main.majorVersion < 9 && potType == PotType.lingering) { + if (Main.minecraftVersion.getMinor() < 9 && potType == EsPotType.lingering) { // Doesn't exist send(sender, "&cLingering potions don't exist in this version"); return false; } } - Player player; + EsPlayer player; if (args.length >= 6) { player = getPlayer(sender, args[5]); if (player == null) return false; } else { - if (sender instanceof Player) { + if (sender instanceof EsPlayer) { // Give to them - player = (Player) sender; + player = (EsPlayer) sender; } else { // They are console send(sender, "&cConsole must specify a player"); @@ -72,12 +70,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } } - if (Main.majorVersion <= 3) { + if (Main.minecraftVersion.getMinor() <= 3) { send(sender, "&cPotions are not yet supported in this version, they may be in the future."); return false; } - ItemStack pot = MetaHandler.getPotion(sender, potType, args[0], duration, amp, amount); + EsItemStack pot = MetaHandler.getPotion(sender, potType, args[0], duration, amp, amount); if (pot == null) { return false; } @@ -92,26 +90,20 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - public enum PotType { - drink, - splash, - lingering - } - @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); // /potion [amplifier] [duration] [amount] [drink/splash/lingering] [player] switch (args.length) { case 1: - if (Main.majorVersion <= 8) { - for (Map.Entry e : Effects.getPotionList()) { + if (Main.minecraftVersion.getMinor() <= 8) { + for (Map.Entry e : Effects.getPotionList()) { tab.add(e.getKey().toLowerCase()); } } else { - for (Map.Entry e : Effects.entrySet()) { + for (Map.Entry e : Effects.entrySet()) { tab.add(e.getKey().toLowerCase()); } } @@ -129,7 +121,7 @@ public List tabComplete(CommandSender sender, String[] args, String lArg case 5: tab.add("drink"); tab.add("splash"); - if (Main.majorVersion >= 9) tab.add("lingering"); + if (Main.minecraftVersion.getMinor() >= 9) tab.add("lingering"); break; case 6: diff --git a/src/main/java/net/serble/estools/Commands/Rain.java b/src/main/java/net/serble/estools/Commands/Rain.java index f67b839..5453b1e 100644 --- a/src/main/java/net/serble/estools/Commands/Rain.java +++ b/src/main/java/net/serble/estools/Commands/Rain.java @@ -1,21 +1,20 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsWorld; public class Rain extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - World world = ((Player) sender).getWorld(); - world.setStorm(true); + EsWorld world = ((EsPlayer) sender).getWorld(); + world.setStorming(true); world.setThundering(false); send(sender, "&aSet weather to &6rain"); diff --git a/src/main/java/net/serble/estools/Commands/RemovePersistentData.java b/src/main/java/net/serble/estools/Commands/RemovePersistentData.java index f694ff4..25cf4f5 100644 --- a/src/main/java/net/serble/estools/Commands/RemovePersistentData.java +++ b/src/main/java/net/serble/estools/Commands/RemovePersistentData.java @@ -1,18 +1,16 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.NamespacedKey; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class RemovePersistentData extends EsToolsCommand { public static final String usage = genUsage("/removepersistentdata "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) return false; @@ -21,16 +19,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - String tagString = args[0].toLowerCase(); + String key = args[0].toLowerCase(); - ItemStack item = getMainHand((Player) sender); - NamespacedKey key = GetPersistentData.getNamespacedKey(tagString); - if (key == null) { - send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); - return false; - } + EsItemStack item = ((EsPlayer) sender).getMainHand(); +// NamespacedKey key = GetPersistentData.getNamespacedKey(tagString); +// if (key == null) { +// send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); +// return false; +// } - ItemMeta meta = item.getItemMeta(); + EsItemMeta meta = item.getItemMeta(); if (meta == null) { send(sender, "&cItem does not have nbt tags!"); return false; @@ -39,7 +37,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St meta.getPersistentDataContainer().remove(key); item.setItemMeta(meta); - send(sender, "&aRemove NBT tag &e\"" + tagString + "\"&a!"); + send(sender, "&aRemove NBT tag &e\"" + key + "\"&a!"); return true; } } diff --git a/src/main/java/net/serble/estools/Commands/Rename.java b/src/main/java/net/serble/estools/Commands/Rename.java index 26da7aa..dd7f58f 100644 --- a/src/main/java/net/serble/estools/Commands/Rename.java +++ b/src/main/java/net/serble/estools/Commands/Rename.java @@ -1,24 +1,23 @@ package net.serble.estools.Commands; import net.serble.estools.MetaHandler; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Rename extends EsToolsCommand { public static final String usage = genUsage("/rename [name]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - Player p = (Player)sender; - ItemStack is = getMainHand(p); + EsPlayer p = (EsPlayer) sender; + EsItemStack is = p.getMainHand(); if (args.length == 0) { MetaHandler.renameItem(is, ""); diff --git a/src/main/java/net/serble/estools/Commands/SafeTp.java b/src/main/java/net/serble/estools/Commands/SafeTp.java index f03ec1f..aaf3c21 100644 --- a/src/main/java/net/serble/estools/Commands/SafeTp.java +++ b/src/main/java/net/serble/estools/Commands/SafeTp.java @@ -1,25 +1,26 @@ package net.serble.estools.Commands; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerTeleportEvent; +// TODO: Events command, needs migrating public class SafeTp extends EsToolsCommand implements Listener { public static boolean enabled = true; @Override public void onEnable() { enabled = Main.plugin.getConfig().getBoolean("safetp", true); - Bukkit.getServer().getPluginManager().registerEvents(this, Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (enabled) { enabled = false; send(sender, "&aTeleporting now &6&lWILL&a make you take fall damage!"); @@ -29,7 +30,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } Main.plugin.getConfig().set("safetp", enabled); - Main.plugin.saveConfig(); + EsToolsBukkit.plugin.saveConfig(); // TODO: How does this work? return true; } diff --git a/src/main/java/net/serble/estools/Commands/SetHealth.java b/src/main/java/net/serble/estools/Commands/SetHealth.java index 61f1f59..ea4c885 100644 --- a/src/main/java/net/serble/estools/Commands/SetHealth.java +++ b/src/main/java/net/serble/estools/Commands/SetHealth.java @@ -1,20 +1,19 @@ package net.serble.estools.Commands; import net.serble.estools.EntityCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class SetHealth extends EntityCommand { private static final String usage = genUsage("/sethealth [entity]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); } - LivingEntity entity; + EsLivingEntity entity; if (args.length > 1) { entity = getEntity(sender, args[1]); @@ -26,7 +25,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - entity = (LivingEntity) sender; + entity = (EsLivingEntity) sender; } double health; @@ -42,15 +41,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - double maxhealth = getMaxHealth(entity); + double maxhealth = entity.getMaxHealth(); if (maxhealth < health) { health = maxhealth; } - setHealth(entity, health); + entity.setHealth(health); if (args.length > 1) { - send(sender, "&aSet health for &6%s&a to &6%s", getEntityName(entity), String.valueOf(health)); + send(sender, "&aSet health for &6%s&a to &6%s", entity.getName(), String.valueOf(health)); } else { send(sender, "&aSet health to &6%s", String.valueOf(health)); } diff --git a/src/main/java/net/serble/estools/Commands/SetHunger.java b/src/main/java/net/serble/estools/Commands/SetHunger.java index d358956..28da151 100644 --- a/src/main/java/net/serble/estools/Commands/SetHunger.java +++ b/src/main/java/net/serble/estools/Commands/SetHunger.java @@ -2,21 +2,20 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.PlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class SetHunger extends PlayerCommand { private static final String usage = genUsage("/sethunger [player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - Player p; + EsPlayer p; int hunger; if (args.length > 1) { @@ -30,7 +29,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - p = (Player)sender; + p = (EsPlayer)sender; } try { diff --git a/src/main/java/net/serble/estools/Commands/SetMaxHealth.java b/src/main/java/net/serble/estools/Commands/SetMaxHealth.java index 9a740e4..aea70d3 100644 --- a/src/main/java/net/serble/estools/Commands/SetMaxHealth.java +++ b/src/main/java/net/serble/estools/Commands/SetMaxHealth.java @@ -1,15 +1,14 @@ package net.serble.estools.Commands; import net.serble.estools.EntityCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class SetMaxHealth extends EntityCommand { private static final String usage = genUsage("/setmaxhealth [entity]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); } @@ -22,7 +21,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - LivingEntity entity; + EsLivingEntity entity; if (args.length > 1) { entity = getEntity(sender, args[1]); @@ -30,18 +29,18 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - send(sender, "&aSet max health for &6%s&a to &6%s", getEntityName(entity), String.valueOf(health)); + send(sender, "&aSet max health for &6%s&a to &6%s", entity.getName(), String.valueOf(health)); } else { if (isNotEntity(sender)) { return false; } - entity = (LivingEntity) sender; + entity = (EsLivingEntity) sender; send(sender, "&aSet max health to &6%s", String.valueOf(health)); } - setMaxHealth(entity, health); + entity.setMaxHealth(health); return true; } diff --git a/src/main/java/net/serble/estools/Commands/SetPersistentData.java b/src/main/java/net/serble/estools/Commands/SetPersistentData.java index 468ef94..8c2af43 100644 --- a/src/main/java/net/serble/estools/Commands/SetPersistentData.java +++ b/src/main/java/net/serble/estools/Commands/SetPersistentData.java @@ -1,14 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.NamespacedKey; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.persistence.PersistentDataType; +import net.serble.estools.ServerApi.EsPersistentDataType; +import net.serble.estools.ServerApi.Interfaces.*; import java.util.ArrayList; import java.util.List; @@ -17,7 +11,7 @@ public class SetPersistentData extends EsToolsCommand { public static final String usage = genUsage("/setpersistentdata "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) return false; @@ -26,7 +20,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - String keyString = args[0].toLowerCase(); + String key = args[0].toLowerCase(); String typeString = args[1].toLowerCase(); String valueString; @@ -40,21 +34,21 @@ public boolean onCommand(CommandSender sender, Command command, String label, St valueString = valueBuilder.toString(); } - ItemStack item = getMainHand((Player) sender); - NamespacedKey key = GetPersistentData.getNamespacedKey(keyString); + EsItemStack item = ((EsPlayer) sender).getMainHand(); +// NamespacedKey key = GetPersistentData.getNamespacedKey(key); +// +// if (key == null) { +// send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); +// return false; +// } - if (key == null) { - send(sender, "&cInvalid key! examples: 'estools:count', 'backpacks:size', etc"); - return false; - } - - ItemMeta meta = item.getItemMeta(); + EsItemMeta meta = item.getItemMeta(); if (meta == null) { send(sender, "&cItem does not have nbt tags!"); return false; } - PersistentDataContainer data = meta.getPersistentDataContainer(); + EsPersistentDataContainer data = meta.getPersistentDataContainer(); int code = setNbt(typeString, key, data, valueString); if (code == 1) { @@ -74,15 +68,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } // 0 = success, 1 = unsupported, 2 = invalid value - public static int setNbt(String type, NamespacedKey key, PersistentDataContainer data, String value) { + public static int setNbt(String type, String key, EsPersistentDataContainer data, String value) { switch (type) { case "string": - data.set(key, PersistentDataType.STRING, value); + data.set(key, EsPersistentDataType.String, value); break; case "integer": try { - data.set(key, PersistentDataType.INTEGER, Integer.parseInt(value)); + data.set(key, EsPersistentDataType.Integer, Integer.parseInt(value)); } catch (NumberFormatException e) { return 2; @@ -91,7 +85,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer case "double": try { - data.set(key, PersistentDataType.DOUBLE, Double.parseDouble(value)); + data.set(key, EsPersistentDataType.Double, Double.parseDouble(value)); } catch (NumberFormatException e) { return 2; @@ -100,7 +94,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer case "float": try { - data.set(key, PersistentDataType.FLOAT, Float.parseFloat(value)); + data.set(key, EsPersistentDataType.Float, Float.parseFloat(value)); } catch (NumberFormatException e) { return 2; @@ -109,7 +103,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer case "long": try { - data.set(key, PersistentDataType.LONG, Long.parseLong(value)); + data.set(key, EsPersistentDataType.Long, Long.parseLong(value)); } catch (NumberFormatException e) { return 2; @@ -118,7 +112,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer case "short": try { - data.set(key, PersistentDataType.SHORT, Short.parseShort(value)); + data.set(key, EsPersistentDataType.Short, Short.parseShort(value)); } catch (NumberFormatException e) { return 2; @@ -127,7 +121,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer case "byte": try { - data.set(key, PersistentDataType.BYTE, Byte.parseByte(value)); + data.set(key, EsPersistentDataType.Byte, Byte.parseByte(value)); } catch (NumberFormatException e) { return 2; @@ -147,7 +141,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer } } - data.set(key, PersistentDataType.BYTE_ARRAY, bytes); + data.set(key, EsPersistentDataType.ByteArray, bytes); break; } @@ -164,7 +158,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer } } - data.set(key, PersistentDataType.INTEGER_ARRAY, ints); + data.set(key, EsPersistentDataType.IntArray, ints); break; } @@ -181,7 +175,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer } } - data.set(key, PersistentDataType.LONG_ARRAY, longs); + data.set(key, EsPersistentDataType.LongArray, longs); break; } @@ -194,7 +188,7 @@ public static int setNbt(String type, NamespacedKey key, PersistentDataContainer } @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); if (args.length == 2) { diff --git a/src/main/java/net/serble/estools/Commands/SetSaturation.java b/src/main/java/net/serble/estools/Commands/SetSaturation.java index fbc9f55..7408a70 100644 --- a/src/main/java/net/serble/estools/Commands/SetSaturation.java +++ b/src/main/java/net/serble/estools/Commands/SetSaturation.java @@ -2,21 +2,20 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.PlayerCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class SetSaturation extends PlayerCommand { private static final String usage = genUsage("/setsaturation [player]"); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } - Player p; + EsPlayer p; if (args.length > 1) { p = EsToolsCommand.getPlayer(sender, args[1]); @@ -28,7 +27,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - p = (Player)sender; + p = (EsPlayer) sender; } float saturation; diff --git a/src/main/java/net/serble/estools/Commands/SetStack.java b/src/main/java/net/serble/estools/Commands/SetStack.java index d1690ff..cd2b708 100644 --- a/src/main/java/net/serble/estools/Commands/SetStack.java +++ b/src/main/java/net/serble/estools/Commands/SetStack.java @@ -1,16 +1,15 @@ package net.serble.estools.Commands; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class SetStack extends EsToolsCommand { private static final String usage = genUsage("/setstack "); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } @@ -28,7 +27,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return false; } - getMainHand(((Player)sender)).setAmount(amount); + ((EsPlayer)sender).getMainHand().setAmount(amount); send(sender, "&aSet stack size to &6%d", amount); return true; } diff --git a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java index 69cc8b8..732f104 100644 --- a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java +++ b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java @@ -2,32 +2,32 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +import java.util.Arrays; +import java.util.Objects; public class SetUnbreakable extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - ItemStack item = getMainHand((Player) sender); - if (item == null || item.getType() == Material.AIR || (Main.majorVersion > 10 && item.getItemMeta() == null)) { + EsItemStack item = ((EsPlayer) sender).getMainHand(); + if (item == null || Objects.equals(item.getType(), "AIR") || (Main.minecraftVersion.getMinor() > 10 && item.getItemMeta() == null)) { send(sender, "&cMust be a damageable item"); return false; } String message = "&aSet item to &6Breakable!"; - if (Main.majorVersion > 10) { - ItemMeta itemMeta = item.getItemMeta(); + if (Main.minecraftVersion.getMinor() > 10) { + EsItemMeta itemMeta = item.getItemMeta(); itemMeta.setUnbreakable(!itemMeta.isUnbreakable()); item.setItemMeta(itemMeta); @@ -35,10 +35,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St message = "&aSet item to &6Unbreakable!"; } } else { - if (item.getEnchantments().containsKey(Enchantment.DURABILITY)) { - item.removeEnchantment(Enchantment.DURABILITY); + if (Arrays.stream(Main.server.getEnchantments()).anyMatch(c -> c.equalsIgnoreCase("DURABILITY"))) { + item.removeEnchantment("DURABILITY"); } else { - item.addUnsafeEnchantment(Enchantment.DURABILITY, 32767); + item.addEnchantment("DURABILITY", 32767); message = "&aSet item to &6Unbreakable!"; } } diff --git a/src/main/java/net/serble/estools/Commands/Smite.java b/src/main/java/net/serble/estools/Commands/Smite.java index 4fc7a27..11fd29c 100644 --- a/src/main/java/net/serble/estools/Commands/Smite.java +++ b/src/main/java/net/serble/estools/Commands/Smite.java @@ -1,23 +1,21 @@ package net.serble.estools.Commands; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; - import net.serble.estools.EntityCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class Smite extends EntityCommand { private static final String usage = genUsage("/smite [entity2] [entity3]..."); @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { send(sender, usage); return false; } for (String arg : args) { - LivingEntity target = getEntity(sender, arg); + EsLivingEntity target = getEntity(sender, arg); if (target == null) { return false; diff --git a/src/main/java/net/serble/estools/Commands/Sudo.java b/src/main/java/net/serble/estools/Commands/Sudo.java index ebbf79f..137bbc3 100644 --- a/src/main/java/net/serble/estools/Commands/Sudo.java +++ b/src/main/java/net/serble/estools/Commands/Sudo.java @@ -1,31 +1,29 @@ package net.serble.estools.Commands; -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - +import net.serble.estools.Main; import net.serble.estools.PlayerCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Sudo extends PlayerCommand { private static final String usage = genUsage("/sudo "); @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (args.length < 2) { send(sender, usage); return false; } - Player p = getPlayer(sender, args[0]); + EsPlayer p = getPlayer(sender, args[0]); if (p == null) { return false; } String command = argsToString(args, 1); - - Bukkit.dispatchCommand(p, command); + + Main.server.dispatchCommand(p, command); send(sender, "&aExecuted command &6%s&a as &6%s", command, p.getName()); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Suicide.java b/src/main/java/net/serble/estools/Commands/Suicide.java index b4b7f46..8a3f076 100644 --- a/src/main/java/net/serble/estools/Commands/Suicide.java +++ b/src/main/java/net/serble/estools/Commands/Suicide.java @@ -1,22 +1,20 @@ package net.serble.estools.Commands; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.LivingEntity; - import net.serble.estools.PlayerCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; public class Suicide extends PlayerCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - LivingEntity p = (LivingEntity) sender; + EsLivingEntity p = (EsLivingEntity) sender; - setHealth(p, 0); + p.setHealth(0); send(sender, "&aRest in peace"); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Sun.java b/src/main/java/net/serble/estools/Commands/Sun.java index 0e97077..510188c 100644 --- a/src/main/java/net/serble/estools/Commands/Sun.java +++ b/src/main/java/net/serble/estools/Commands/Sun.java @@ -1,21 +1,20 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsWorld; public class Sun extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - World world = ((Player) sender).getWorld(); - world.setStorm(false); + EsWorld world = ((EsPlayer) sender).getWorld(); + world.setStorming(false); world.setThundering(false); send(sender, "&aSet weather to &6clear"); diff --git a/src/main/java/net/serble/estools/Commands/Thunder.java b/src/main/java/net/serble/estools/Commands/Thunder.java index 88920c7..05b514e 100644 --- a/src/main/java/net/serble/estools/Commands/Thunder.java +++ b/src/main/java/net/serble/estools/Commands/Thunder.java @@ -1,21 +1,20 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.World; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsWorld; public class Thunder extends EsToolsCommand { @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - World world = ((Player) sender).getWorld(); - world.setStorm(true); + EsWorld world = ((EsPlayer) sender).getWorld(); + world.setStorming(true); world.setThundering(true); send(sender, "&aSet weather to &6thunder"); diff --git a/src/main/java/net/serble/estools/Commands/WrongVersion.java b/src/main/java/net/serble/estools/Commands/WrongVersion.java index e3665e8..f11dc39 100644 --- a/src/main/java/net/serble/estools/Commands/WrongVersion.java +++ b/src/main/java/net/serble/estools/Commands/WrongVersion.java @@ -1,8 +1,7 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; public class WrongVersion extends EsToolsCommand { private final String minVersion; @@ -16,7 +15,7 @@ public WrongVersion(String ver) { } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + public boolean execute(EsCommandSender sender, String[] args) { send(sender, "&cThis command requires Minecraft to be at least 1.%s", minVersion); return true; } diff --git a/src/main/java/net/serble/estools/ConfigManager.java b/src/main/java/net/serble/estools/ConfigManager.java index af3acbf..e908b05 100644 --- a/src/main/java/net/serble/estools/ConfigManager.java +++ b/src/main/java/net/serble/estools/ConfigManager.java @@ -14,7 +14,7 @@ public class ConfigManager { public static final Main plugin = Main.plugin; public static FileConfiguration load(String fileName) { - return load(new File(plugin.getDataFolder(), fileName)); + return load(new File(Main.server.getDataFolder(), fileName)); } @SuppressWarnings("ResultOfMethodCallIgnored") @@ -40,7 +40,7 @@ public static FileConfiguration load(File configFile) { @SuppressWarnings("ResultOfMethodCallIgnored") public static void save(String file, FileConfiguration config) { - File configFile = new File(plugin.getDataFolder(), file); + File configFile = new File(Main.server.getDataFolder(), file); if (!configFile.exists()) { configFile.getParentFile().mkdirs(); try { diff --git a/src/main/java/net/serble/estools/Effects.java b/src/main/java/net/serble/estools/Effects.java index fa00012..2f84efd 100644 --- a/src/main/java/net/serble/estools/Effects.java +++ b/src/main/java/net/serble/estools/Effects.java @@ -1,19 +1,16 @@ package net.serble.estools; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionType; - import java.util.*; public class Effects { private static final Map EFFECTS = new HashMap<>(); - private static final Map POTIONS = new HashMap<>(); + private static final Map POTIONS = new HashMap<>(); public static String getByName(String name) { return EFFECTS.get(name); } - public static PotionType getPotionByName(String name) { + public static String getPotionByName(String name) { return POTIONS.get(name.toLowerCase(Locale.ENGLISH)); } @@ -31,33 +28,33 @@ public static Set> entrySet() { return EFFECTS.entrySet(); } - public static Set> getPotionList() { + public static Set> getPotionList() { return POTIONS.entrySet(); } public static void load() { if (Main.minecraftVersion.getMinor() <= 8) { // 1.8 only potions be funky - POTIONS.put("regeneration", PotionType.REGEN); - POTIONS.put("speed", PotionType.SPEED); - POTIONS.put("fire_resistance", PotionType.FIRE_RESISTANCE); - POTIONS.put("poison", PotionType.POISON); - POTIONS.put("instant_health", PotionType.INSTANT_HEAL); - POTIONS.put("weakness", PotionType.WEAKNESS); - POTIONS.put("strength", PotionType.STRENGTH); - POTIONS.put("slowness", PotionType.SLOWNESS); - POTIONS.put("instant_damage", PotionType.INSTANT_DAMAGE); + POTIONS.put("regeneration", "REGEN"); + POTIONS.put("speed", "SPEED"); + POTIONS.put("fire_resistance", "FIRE_RESISTANCE"); + POTIONS.put("poison", "POISON"); + POTIONS.put("instant_health", "INSTANT_HEAL"); + POTIONS.put("weakness", "WEAKNESS"); + POTIONS.put("strength", "STRENGTH"); + POTIONS.put("slowness", "SLOWNESS"); + POTIONS.put("instant_damage", "INSTANT_DAMAGE"); if (Main.minecraftVersion.getMinor() >= 4 && Main.minecraftVersion.getMinor() >= 2) { // Night vision was added in 1.4.2 - POTIONS.put("night_vision", PotionType.NIGHT_VISION); - POTIONS.put("invisibility", PotionType.INVISIBILITY); + POTIONS.put("night_vision", "NIGHT_VISION"); + POTIONS.put("invisibility", "INVISIBILITY"); } if (Main.minecraftVersion.getMinor() >= 7 && Main.minecraftVersion.getMinor() >= 2) { // 1.7.2 - POTIONS.put("water_breathing", PotionType.WATER); + POTIONS.put("water_breathing", "WATER"); } if (Main.minecraftVersion.getMinor() >= 8) { // 1.8 - POTIONS.put("jump_boost", PotionType.JUMP); + POTIONS.put("jump_boost", "JUMP"); } } diff --git a/src/main/java/net/serble/estools/MetaHandler.java b/src/main/java/net/serble/estools/MetaHandler.java index 4d72cba..7b016d5 100644 --- a/src/main/java/net/serble/estools/MetaHandler.java +++ b/src/main/java/net/serble/estools/MetaHandler.java @@ -1,16 +1,9 @@ package net.serble.estools; -import net.serble.estools.Commands.Potion; +import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionType; import java.util.List; import java.util.Objects; @@ -18,43 +11,14 @@ // This class exists because Minecraft < 1.4 doesn't have ItemMeta. It just encapsulates all ItemMeta methods. public class MetaHandler { - public static ItemStack getPotion(EsCommandSender sender, Potion.PotType potType, String effect, int duration, int amp, int amount) { - if (Main.minecraftVersion.getMinor() >= 9) { - String type = potType == Potion.PotType.drink ? - "POTION" : - potType.toString().toUpperCase() + "_POTION"; - ItemStack pot = new ItemStack(Material.valueOf(type), amount); - - PotionEffectType effType; - try { - effType = Effects.getByName(effect); - } catch (IllegalArgumentException e) { - EsToolsCommand.send(sender, "&cInvalid potion effect"); - return null; - } - - PotionMeta meta = (PotionMeta) pot.getItemMeta(); - assert meta != null; - meta.addCustomEffect(new PotionEffect(effType, duration, amp-1), true); - pot.setItemMeta(meta); - return pot; - } else if (Main.minecraftVersion.getMinor() >= 4) { - PotionType effType; - try { - effType = Effects.getPotionByName(effect); - } catch (IllegalArgumentException e) { - EsToolsCommand.send(sender, "&cInvalid potion effect"); - return null; - } - - @SuppressWarnings("deprecation") - org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(effType, amp); - potion.setSplash(potType == Potion.PotType.splash); - return potion.toItemStack(1); - } else { // This isn't possible to get to because this class won't load on 1.3 and below - EsToolsCommand.send(sender, "&cPotions are not yet supported in this version, they may be in the future."); + public static EsItemStack getPotion(EsCommandSender sender, EsPotType potType, String effect, int duration, int amp, int amount) { + EsItemStack pot = Main.server.createPotion(potType, effect, duration, amp, amount); + if (pot == null) { + EsToolsCommand.send(sender, "&cInvalid potion effect"); return null; } + + return pot; } public static void renameItem(EsItemStack item, String name) { diff --git a/src/main/java/net/serble/estools/PlayerCommand.java b/src/main/java/net/serble/estools/PlayerCommand.java index c7b3480..f557cd1 100644 --- a/src/main/java/net/serble/estools/PlayerCommand.java +++ b/src/main/java/net/serble/estools/PlayerCommand.java @@ -1,7 +1,7 @@ package net.serble.estools; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; import java.util.List; @@ -9,10 +9,10 @@ public abstract class PlayerCommand extends EsToolsCommand { @Override - public List tabComplete(CommandSender sender, String[] args, String lArg) { + public List tabComplete(EsCommandSender sender, String[] args, String lArg) { List tab = new ArrayList<>(); - for (Player p : getOnlinePlayers()) { + for (EsPlayer p : Main.server.getOnlinePlayers()) { tab.add(p.getName()); } diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotType.java b/src/main/java/net/serble/estools/ServerApi/EsPotType.java new file mode 100644 index 0000000..b6e9de8 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsPotType.java @@ -0,0 +1,7 @@ +package net.serble.estools.ServerApi; + +public enum EsPotType { + drink, + lingering, + splash +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java index 5a8ab35..1cbd59b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -2,6 +2,7 @@ import net.serble.estools.ServerApi.EsItemFlag; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsPersistentDataContainer; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.meta.ItemMeta; @@ -80,6 +81,11 @@ public void setLore(List val) { bukkitMeta.setLore(val); } + @Override + public EsPersistentDataContainer getPersistentDataContainer() { + return new BukkitPersistentDataContainer(bukkitMeta.getPersistentDataContainer()); + } + public ItemMeta getBukkitMeta() { return bukkitMeta; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index 0e15844..7ba38cd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -4,12 +4,9 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; -import org.bukkit.Registry; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; -import java.util.Objects; - public class BukkitItemStack implements EsItemStack { private final org.bukkit.inventory.ItemStack bukkitItem; @@ -50,6 +47,11 @@ public void addEnchantment(String enchantment, int level) { bukkitItem.addUnsafeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment), level); } + @Override + public void removeEnchantment(String enchantment) { + bukkitItem.removeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment)); + } + @Override public EsItemMeta getItemMeta() { return new BukkitItemMeta(bukkitItem.getItemMeta()); @@ -90,4 +92,9 @@ public int getDamage() { } return 0; } + + @Override + public EsItemStack clone() { + return new BukkitItemStack(bukkitItem.clone()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index 82e0e82..a60aceb 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -2,12 +2,12 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; import org.bukkit.Registry; import org.bukkit.attribute.Attribute; import org.bukkit.entity.LivingEntity; import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; import java.util.Collection; @@ -117,4 +117,9 @@ public List getActivePotionEffects() { } return out; } + + @Override + public EsWorld getWorld() { + return new BukkitWorld(bukkitEntity.getWorld()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java index 334ff7b..9028649 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java @@ -1,12 +1,11 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPersistentDataType; import net.serble.estools.ServerApi.Interfaces.EsPersistentDataContainer; -import org.bukkit.NamespacedKey; import org.bukkit.persistence.PersistentDataContainer; +import java.util.Objects; + import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.*; @SuppressWarnings("unchecked") // Trust me bro @@ -19,11 +18,16 @@ public BukkitPersistentDataContainer(PersistentDataContainer container) { @Override public void set(String key, EsPersistentDataType type, Object val) { - bukkitContainer.set(getNamespacedKey(key), BukkitHelper.toBukkitPersistentDataType(type), val); + bukkitContainer.set(Objects.requireNonNull(getNamespacedKey(key)), BukkitHelper.toBukkitPersistentDataType(type), val); } @Override public Object get(String key, EsPersistentDataType type) { return null; } + + @Override + public void remove(String key) { + bukkitContainer.remove(Objects.requireNonNull(getNamespacedKey(key))); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 755fd64..d466fc6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -80,11 +80,6 @@ public void setWalkSpeed(float val) { bukkitPlayer.setWalkSpeed(val); } - @Override - public EsWorld getWorld() { - return new BukkitWorld(bukkitPlayer.getWorld()); - } - @Override public void setGameMode(EsGameMode mode) { bukkitPlayer.setGameMode(BukkitHelper.toBukkitGameMode(mode)); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index f31f38d..6a88d0b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -1,22 +1,25 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Effects; import net.serble.estools.Main; import net.serble.estools.SemanticVersion; +import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.Registry; import org.bukkit.World; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.UUID; +import java.util.*; public class BukkitServer implements EsServerSoftware { private final JavaPlugin plugin; @@ -107,6 +110,43 @@ public EsItemStack createItemStack(String material, int amount) { return new BukkitItemStack(material, amount); } + @SuppressWarnings("deprecation") + @Override + public EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + if (Main.minecraftVersion.getMinor() >= 9) { + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), amount); + + String effType; + try { + effType = Effects.getByName(effect); + } catch (IllegalArgumentException e) { + return null; + } + + PotionMeta meta = (PotionMeta) pot.getItemMeta(); + assert meta != null; + meta.addCustomEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effType)), duration, amp-1), true); + pot.setItemMeta(meta); + return new BukkitItemStack(pot); + } else if (Main.minecraftVersion.getMinor() >= 4) { + String effType; + try { + effType = Effects.getPotionByName(effect); + } catch (IllegalArgumentException e) { + return null; + } + + org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); + potion.setSplash(potType == EsPotType.splash); + return new BukkitItemStack(potion.toItemStack(1)); + } else { // This isn't possible to get to because this class won't load on 1.3 and below + return null; + } + } + @Override public EsInventory createInventory(EsPlayer owner, int size, String title) { InventoryHolder holder = owner == null ? null : ((BukkitPlayer) owner).getBukkitPlayer(); @@ -137,6 +177,11 @@ public String[] getEnchantments() { return out; } + @Override + public String[] getSounds() { + return new String[0]; + } + @Override public boolean doesEnchantmentExist(String name) { try { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index 0b57a7f..aefece8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -44,4 +44,19 @@ public List getNearbyEntities(EsLocation loc, double xoff, double yoff public void setTime(long time) { bukkitWorld.setTime(time); } + + @Override + public void setStorming(boolean val) { + bukkitWorld.setStorm(false); + } + + @Override + public void setThundering(boolean val) { + bukkitWorld.setThundering(val); + } + + @Override + public void strikeLightning(EsLocation loc) { + bukkitWorld.strikeLightning(BukkitHelper.toBukkitLocation(loc)); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java index c76b432..004b415 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemMeta.java @@ -15,4 +15,5 @@ public interface EsItemMeta { String getDisplayName(); List getLore(); void setLore(List val); + EsPersistentDataContainer getPersistentDataContainer(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 5d89357..2f2d5df 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -6,8 +6,10 @@ public interface EsItemStack { int getAmount(); void setAmount(int amount); void addEnchantment(String enchantment, int level); + void removeEnchantment(String enchantment); EsItemMeta getItemMeta(); void setItemMeta(EsItemMeta meta); void setDamage(int val); int getDamage(); + EsItemStack clone(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java index 4492273..d5f56e9 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java @@ -11,4 +11,5 @@ public interface EsLivingEntity extends EsEntity { void addPotionEffect(String effect, int duration, int amplifier); void removePotionEffect(String effect); List getActivePotionEffects(); + EsWorld getWorld(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java index 478b9c6..6bba113 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPersistentDataContainer.java @@ -5,4 +5,5 @@ public interface EsPersistentDataContainer { void set(String key, EsPersistentDataType type, Object val); Object get(String key, EsPersistentDataType type); + void remove(String key); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 3279433..4e13e88 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -15,7 +15,6 @@ public interface EsPlayer extends EsLivingEntity { EsPlayerInventory getInventory(); void setFlySpeed(float val); void setWalkSpeed(float val); - EsWorld getWorld(); void setGameMode(EsGameMode mode); EsGameMode getGameMode(); EsBlock getTargetBlock(); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 575d562..4924b7c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; import net.serble.estools.SemanticVersion; +import net.serble.estools.ServerApi.EsPotType; import java.io.File; import java.util.Collection; @@ -12,9 +13,11 @@ public interface EsServerSoftware { SemanticVersion getVersion(); Collection getOnlinePlayers(); EsItemStack createItemStack(String material, int amount); + EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount); EsInventory createInventory(EsPlayer owner, int size, String title); String[] getPotionEffectTypes(); String[] getEnchantments(); + String[] getSounds(); boolean doesEnchantmentExist(String name); File getDataFolder(); void dispatchCommand(EsCommandSender sender, String cmd); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java index 4ef6bbd..41dabd9 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsWorld.java @@ -9,4 +9,7 @@ public interface EsWorld { List getEntities(); List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff); void setTime(long time); + void setStorming(boolean val); + void setThundering(boolean val); + void strikeLightning(EsLocation loc); } diff --git a/src/main/java/net/serble/estools/Signs/Balance.java b/src/main/java/net/serble/estools/Signs/Balance.java index e022d16..3fd9c32 100644 --- a/src/main/java/net/serble/estools/Signs/Balance.java +++ b/src/main/java/net/serble/estools/Signs/Balance.java @@ -1,11 +1,13 @@ package net.serble.estools.Signs; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Vault; -import org.bukkit.entity.Player; public class Balance extends SignType { + @Override - public void run(Player p, String[] lines) { - send(p, "&aYour current balance is &6$%s", String.valueOf(Vault.economy.getBalance(p))); + public void run(EsPlayer p, String[] lines) { + send(p, "&aYour current balance is &6$%s", String.valueOf(Vault.economy.getBalance(((BukkitPlayer)p).getBukkitPlayer()))); } } diff --git a/src/main/java/net/serble/estools/Signs/Disposal.java b/src/main/java/net/serble/estools/Signs/Disposal.java index f6ba08a..16f7ce4 100644 --- a/src/main/java/net/serble/estools/Signs/Disposal.java +++ b/src/main/java/net/serble/estools/Signs/Disposal.java @@ -1,12 +1,13 @@ package net.serble.estools.Signs; import net.serble.estools.EsToolsCommand; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Disposal extends SignType { + @Override - public void run(Player p, String[] lines) { - p.openInventory(Bukkit.createInventory(null, 54, EsToolsCommand.translate("&cDisposal"))); + public void run(EsPlayer p, String[] lines) { + p.openInventory(Main.server.createInventory(null, 54, EsToolsCommand.translate("&cDisposal"))); } } diff --git a/src/main/java/net/serble/estools/Signs/Feed.java b/src/main/java/net/serble/estools/Signs/Feed.java index 3576deb..11e976f 100644 --- a/src/main/java/net/serble/estools/Signs/Feed.java +++ b/src/main/java/net/serble/estools/Signs/Feed.java @@ -1,11 +1,11 @@ package net.serble.estools.Signs; -import org.bukkit.entity.Player; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Feed extends SignType { @Override - public void run(Player p, String[] lines) { + public void run(EsPlayer p, String[] lines) { if (!takeMoney(lines[1], p)) { return; } diff --git a/src/main/java/net/serble/estools/Signs/Give.java b/src/main/java/net/serble/estools/Signs/Give.java index 5e8ee1c..e6a1a3d 100644 --- a/src/main/java/net/serble/estools/Signs/Give.java +++ b/src/main/java/net/serble/estools/Signs/Give.java @@ -1,18 +1,18 @@ package net.serble.estools.Signs; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Give extends SignType { @Override - public void run(Player p, String[] lines) { + public void run(EsPlayer p, String[] lines) { int amount = 1; try { amount = Integer.parseInt(lines[2]); } catch (NumberFormatException e) { /* Ignored */ } - ItemStack item = net.serble.estools.Commands.Give.Give.getItem(lines[1], amount); + EsItemStack item = net.serble.estools.Commands.Give.Give.getItem(lines[1], amount); if (item == null) { send(p, "&cItem not found!"); diff --git a/src/main/java/net/serble/estools/Signs/Heal.java b/src/main/java/net/serble/estools/Signs/Heal.java index 663a6ea..b5d592b 100644 --- a/src/main/java/net/serble/estools/Signs/Heal.java +++ b/src/main/java/net/serble/estools/Signs/Heal.java @@ -1,18 +1,16 @@ package net.serble.estools.Signs; -import net.serble.estools.EsToolsCommand; -import org.bukkit.entity.Player; - -import static net.serble.estools.EsToolsCommand.setHealth; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Heal extends SignType { + @Override - public void run(Player p, String[] lines) { + public void run(EsPlayer p, String[] lines) { if (!takeMoney(lines[1], p)) { return; } - setHealth(p, EsToolsCommand.getMaxHealth(p)); - p.setFireTicks(0); + p.setHealth(p.getMaxHealth()); + p.setOnFireTicks(0); } } diff --git a/src/main/java/net/serble/estools/Signs/Repair.java b/src/main/java/net/serble/estools/Signs/Repair.java index 823afd0..453ab2e 100644 --- a/src/main/java/net/serble/estools/Signs/Repair.java +++ b/src/main/java/net/serble/estools/Signs/Repair.java @@ -1,18 +1,18 @@ package net.serble.estools.Signs; import net.serble.estools.Commands.Fix; -import net.serble.estools.EsToolsCommand; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Repair extends SignType { + @Override - public void run(Player p, String[] lines) { + public void run(EsPlayer p, String[] lines) { if (!takeMoney(lines[1], p)) { return; } - ItemStack item = EsToolsCommand.getMainHand(p); + EsItemStack item = p.getMainHand(); Fix.repair(item); } } diff --git a/src/main/java/net/serble/estools/Signs/Sell.java b/src/main/java/net/serble/estools/Signs/Sell.java index 8aeb8ee..74f1618 100644 --- a/src/main/java/net/serble/estools/Signs/Sell.java +++ b/src/main/java/net/serble/estools/Signs/Sell.java @@ -1,24 +1,25 @@ package net.serble.estools.Signs; import net.serble.estools.Commands.Give.Give; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; public class Sell extends SignType { + @Override - public void run(Player p, String[] lines) { + public void run(EsPlayer p, String[] lines) { int amount = 1; try { amount = Integer.parseInt(lines[3]); } catch (Exception e) { /* ignore */ } - ItemStack item = Give.getItem(lines[1], amount); + EsItemStack item = Give.getItem(lines[1], amount); if (item == null) { send(p, "&cItem not found!"); return; } - for (ItemStack e : p.getInventory().getContents()) { + for (EsItemStack e : p.getInventory().getContents()) { if (!e.getType().equals(item.getType()) || e.getAmount() < item.getAmount()) { continue; } diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index 0b2d6b0..8d808f2 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -1,7 +1,8 @@ package net.serble.estools.Signs; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; -import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import org.bukkit.Bukkit; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; @@ -12,6 +13,7 @@ import java.util.HashMap; +// TODO: Migrate away from bukkit events public class SignMain implements Listener { private static final HashMap signs = new HashMap<>(); private static final HashMap signConversions = new HashMap<>(); @@ -25,7 +27,7 @@ public static void init() { addSign(new Repair(), "[repair]", EsToolsCommand.translate("&1[Repair]")); addSign(new Sell(), "[sell]", EsToolsCommand.translate("&1[Sell]")); - Bukkit.getServer().getPluginManager().registerEvents(new SignMain(), Main.plugin); + Bukkit.getServer().getPluginManager().registerEvents(new SignMain(), EsToolsBukkit.plugin); } public static void addSign(SignType signType, String conversion, String sign) { @@ -43,7 +45,7 @@ public void interact(PlayerInteractEvent e) { if (signType != null) { e.setCancelled(true); - signType.run(e.getPlayer(), state.getLines()); + signType.run(new BukkitPlayer(e.getPlayer()), state.getLines()); } } } diff --git a/src/main/java/net/serble/estools/Signs/SignType.java b/src/main/java/net/serble/estools/Signs/SignType.java index 68db0c4..74d839a 100644 --- a/src/main/java/net/serble/estools/Signs/SignType.java +++ b/src/main/java/net/serble/estools/Signs/SignType.java @@ -1,14 +1,14 @@ package net.serble.estools.Signs; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Vault; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; public abstract class SignType { - public void run(Player p, String[] lines) { } + public void run(EsPlayer p, String[] lines) { } - public static double getSignMoney(String line, Player p) { + public static double getSignMoney(String line, EsPlayer p) { double price = 0; try { String priceString = line.substring(1); @@ -24,15 +24,15 @@ public static double getSignMoney(String line, Player p) { return price; } - public static boolean takeMoney(String line, Player p) { + public static boolean takeMoney(String line, EsPlayer p) { return Vault.takeMoney(getSignMoney(line, p), p); } - public static boolean payMoney(String line, Player p) { + public static boolean payMoney(String line, EsPlayer p) { return Vault.payMoney(getSignMoney(line, p), p); } - public static void send(CommandSender s, String msg, Object... args) { + public static void send(EsCommandSender s, String msg, Object... args) { EsToolsCommand.send(s, msg, args); } } diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index 6e1f753..ab17bb0 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -1,6 +1,7 @@ package net.serble.estools; import net.serble.estools.Commands.SafeTp; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; @@ -177,7 +178,7 @@ private void execNextCommand() { } @SuppressWarnings("WrapperTypeMayBePrimitive") Double timeInTicks = (secVal * 20.0); // The type cannot be primitive - /* TODO: Remove bukkit scheduler */ Bukkit.getScheduler().runTaskLater(Main.bukkitPlugin, this::execNextCommand, timeInTicks.longValue()); + /* TODO: Remove bukkit scheduler */ Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, this::execNextCommand, timeInTicks.longValue()); EsToolsCommand.send(player, "&bWaiting &6" + cCmd.b() + " seconds"); } From 74cd83e70fc339d15472770417d9a0033719ffca Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 24 May 2024 21:12:56 +1000 Subject: [PATCH 06/74] The plugin now enables --- .../net/serble/estools/Commands/Ench.java | 11 +------ .../estools/Commands/PowerPick/PowerAxe.java | 2 +- .../estools/Commands/PowerPick/PowerHoe.java | 2 +- .../estools/Commands/PowerPick/PowerPick.java | 2 +- .../Commands/PowerPick/PowerShovel.java | 2 +- .../Commands/PowerPick/PowerSword.java | 2 +- .../estools/Commands/PowerPick/PowerTool.java | 2 +- .../Bukkit/BukkitEnchantmentsHelper.java} | 31 ++++++++++--------- .../Implementations/Bukkit/BukkitHelper.java | 14 ++++++--- .../Implementations/Bukkit/BukkitServer.java | 25 +++++++++++---- 10 files changed, 53 insertions(+), 40 deletions(-) rename src/main/java/net/serble/estools/{Enchantments.java => ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java} (66%) diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index 77ed7f6..c07d6cb 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -3,9 +3,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; -import net.serble.estools.Enchantments; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -67,14 +65,7 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA switch (args.length) { case 1: - if (Main.minecraftVersion.getMinor() > 12) { - tab.addAll(Arrays.asList(Main.server.getEnchantments())); - } else { - for (Map.Entry e : Enchantments.entrySet()) { - tab.add(e.getKey()); - } - } - + tab.addAll(Arrays.asList(Main.server.getEnchantments())); break; case 3: diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java index 640725b..67383b4 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java @@ -16,7 +16,7 @@ public static void init() { powerItem = Main.server.createItemStack("DIAMOND_AXE", 1); } - PowerTool.setupItem(powerItem, "DIG_SPEED"); + PowerTool.setupItem(powerItem, "efficiency"); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java index 0e93b09..84bb752 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java @@ -16,7 +16,7 @@ public static void init() { powerItem = Main.server.createItemStack("DIAMOND_HOE", 1); } - PowerTool.setupItem(powerItem, "DIG_SPEED"); + PowerTool.setupItem(powerItem, "efficiency"); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java index 663ca88..4deef7c 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java @@ -16,7 +16,7 @@ public static void init() { powerItem = Main.server.createItemStack("DIAMOND_PICKAXE", 1); } - PowerTool.setupItem(powerItem, "DIG_SPEED"); + PowerTool.setupItem(powerItem, "efficiency"); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java index 2cfb263..ea8913b 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java @@ -17,7 +17,7 @@ public static void init() { powerItem = Main.server.createItemStack("DIAMOND_SPADE", 1); } - PowerTool.setupItem(powerItem, "DIG_SPEED"); + PowerTool.setupItem(powerItem, "efficiency"); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java index 94116f5..bf5ff82 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java @@ -15,7 +15,7 @@ public static void init() { powerPick = Main.server.createItemStack("RAW_FISH", 1); } - PowerTool.setupItem(powerPick, "DAMAGE_ALL"); + PowerTool.setupItem(powerPick, "sharpness"); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java index b30c213..b722a6e 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java @@ -24,7 +24,7 @@ public static void setupItem(EsItemStack item, String enchantment) { im.setUnbreakable(true); item.setItemMeta(im); } else { - item.addEnchantment("DURABILITY", 32767); + item.addEnchantment("unbreaking", 32767); } } diff --git a/src/main/java/net/serble/estools/Enchantments.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java similarity index 66% rename from src/main/java/net/serble/estools/Enchantments.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java index d7b5652..691a747 100644 --- a/src/main/java/net/serble/estools/Enchantments.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java @@ -1,4 +1,6 @@ -package net.serble.estools; +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; import java.util.HashMap; import java.util.Locale; @@ -7,9 +9,10 @@ // This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. // only enchantments before 1.13 need to be here because this class isn't used past 1.13 -public class Enchantments { +public class BukkitEnchantmentsHelper { private static final Map ENCHANTMENTS = new HashMap<>(); + /** Converts the natural vanilla names to the Bukkit enum values */ public static String getByName(String name) { return ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH)); } @@ -22,20 +25,20 @@ public static Set> entrySet() { private static void AddEnchants() { ENCHANTMENTS.put("sharpness", "DAMAGE_ALL"); - ENCHANTMENTS.put("baneofarthropods", "DAMAGE_ARTHROPODS"); + ENCHANTMENTS.put("bane_of_arthropods", "DAMAGE_ARTHROPODS"); ENCHANTMENTS.put("smite", "DAMAGE_UNDEAD"); ENCHANTMENTS.put("efficiency", "DIG_SPEED"); ENCHANTMENTS.put("unbreaking", "DURABILITY"); - ENCHANTMENTS.put("fireaspect", "FIRE_ASPECT"); + ENCHANTMENTS.put("fire_aspect", "FIRE_ASPECT"); ENCHANTMENTS.put("knockback", "KNOCKBACK"); ENCHANTMENTS.put("looting", "LOOT_BONUS_MOBS"); ENCHANTMENTS.put("respiration", "OXYGEN"); ENCHANTMENTS.put("protection", "PROTECTION_ENVIRONMENTAL"); - ENCHANTMENTS.put("blastprotect", "PROTECTION_EXPLOSIONS"); - ENCHANTMENTS.put("featherfall", "PROTECTION_FALL"); - ENCHANTMENTS.put("projectileprotection", "PROTECTION_PROJECTILE"); - ENCHANTMENTS.put("silktouch", "SILK_TOUCH"); - ENCHANTMENTS.put("aquaaffinity", "WATER_WORKER"); + ENCHANTMENTS.put("blast_protection", "PROTECTION_EXPLOSIONS"); + ENCHANTMENTS.put("feather_falling", "PROTECTION_FALL"); + ENCHANTMENTS.put("projectile_protection", "PROTECTION_PROJECTILE"); + ENCHANTMENTS.put("silk_touch", "SILK_TOUCH"); + ENCHANTMENTS.put("aqua_affinity", "WATER_WORKER"); ENCHANTMENTS.put("flame", "ARROW_FIRE"); ENCHANTMENTS.put("power", "ARROW_DAMAGE"); ENCHANTMENTS.put("punch", "ARROW_KNOCKBACK"); @@ -50,15 +53,15 @@ private static void AddEnchants() { ENCHANTMENTS.put("lure", "LURE"); if (Main.minecraftVersion.getMinor() <= 7) return; - ENCHANTMENTS.put("depthstrider", "DEPTH_STRIDER"); + ENCHANTMENTS.put("depth_strider", "DEPTH_STRIDER"); if (Main.minecraftVersion.getMinor() <= 8) return; - ENCHANTMENTS.put("frostwalker", "FROST_WALKER"); + ENCHANTMENTS.put("frost_walker", "FROST_WALKER"); ENCHANTMENTS.put("mending", "MENDING"); if (Main.minecraftVersion.getMinor() <= 10) return; - ENCHANTMENTS.put("bindingcurse", "BINDING_CURSE"); - ENCHANTMENTS.put("vanishingcurse", "VANISHING_CURSE"); - ENCHANTMENTS.put("sweepingedge", "SWEEPING_EDGE"); + ENCHANTMENTS.put("binding_curse", "BINDING_CURSE"); + ENCHANTMENTS.put("vanishing_curse", "VANISHING_CURSE"); + ENCHANTMENTS.put("sweeping_edge", "SWEEPING_EDGE"); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 94281c7..a253c47 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -21,8 +21,6 @@ import org.bukkit.persistence.PersistentDataType; import org.bukkit.util.Vector; -import java.util.Objects; - public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { return new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); @@ -142,11 +140,19 @@ public static EsBlock fromBukkitBlock(Block block) { @SuppressWarnings("deprecation") public static Enchantment getBukkitEnchantment(String name) { if (Main.minecraftVersion.getMinor() >= 13) { - Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name))); + Enchantment ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); + if (ench == null) { + // Dump info and throw + Bukkit.getLogger().severe("Failed to find enchantment: " + name); + for (Enchantment e : Registry.ENCHANTMENT) { + Bukkit.getLogger().severe("This exists: " + e.getName()); + } + throw new RuntimeException("Could not find enchantment: " + name); + } } // We have to use deprecated method for pre 1.13 - return Enchantment.getByName(name); + return Enchantment.getByName(BukkitEnchantmentsHelper.getByName(name)); } public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType type) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 6a88d0b..bbe3f62 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -168,13 +168,26 @@ public String[] getPotionEffectTypes() { @SuppressWarnings("deprecation") @Override public String[] getEnchantments() { - Enchantment[] enchantments = (Enchantment[]) Registry.ENCHANTMENT.stream().toArray(); - String[] out = new String[enchantments.length]; - for (int i = 0; i < enchantments.length; i++) { - Enchantment enchantment = enchantments[i]; - out[i] = enchantment.getName(); + if (Main.minecraftVersion.getMinor() > 12) { + Enchantment[] enchantments = (Enchantment[]) Registry.ENCHANTMENT.stream().toArray(); + String[] out = new String[enchantments.length]; + for (int i = 0; i < enchantments.length; i++) { + Enchantment enchantment = enchantments[i]; + out[i] = enchantment.getName(); + } + return out; } - return out; + + // Pre 1.13, we need to use the helper to get all the keys + Set> enchSet = BukkitEnchantmentsHelper.entrySet(); + String[] enchs = new String[enchSet.size()]; + int i = 0; + for (Map.Entry enchEntry : enchSet) { + enchs[i] = enchEntry.getKey(); + i++; + } + + return enchs; } @Override From f0298d654886d59dc32d0315ec11be0b19bd257d Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 24 May 2024 21:45:51 +1000 Subject: [PATCH 07/74] Make everything work on regular bukkit --- .../net/serble/estools/Commands/CChest.java | 17 ++++++++++---- .../net/serble/estools/Commands/EsTools.java | 4 ++-- .../net/serble/estools/EsToolsCommand.java | 5 ++-- .../Implementations/Bukkit/BukkitHelper.java | 18 ++++++++++++++- .../Implementations/Bukkit/BukkitPlayer.java | 2 +- .../Implementations/Bukkit/BukkitServer.java | 23 ++++++++++--------- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 77c4591..cc4ace9 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -5,6 +5,7 @@ import net.serble.estools.ConfigManager; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventory; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsInventory; @@ -18,6 +19,7 @@ import org.bukkit.event.inventory.*; import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.*; @@ -81,7 +83,7 @@ public void inventoryClick(final InventoryClickEvent e) { ItemStack currentItem = e.getCurrentItem(); // Check if inventory is cChest - if (!cChests.containsKey(uid) || !cChests.get(uid).equals(e.getInventory()) || e.getClickedInventory() == null) { + if (!cChests.containsKey(uid) || !isSameInv(e.getInventory(), cChests.get(uid)) || e.getClickedInventory() == null) { return; } @@ -116,7 +118,7 @@ public void inventoryClick(final InventoryClickEvent e) { } // If player inventory - if (!e.getClickedInventory().equals(cChests.get(uid))) { // TODO: This comparison doesn't work + if (!e.getClickedInventory().equals(((BukkitInventory)cChests.get(uid)).getBukkitInventory())) { // Shift click into cChest if (equalsOr(e.getClick(), ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT) && currentItem != null) { e.setCancelled(true); @@ -163,18 +165,23 @@ public void inventoryClick(final InventoryClickEvent e) { private static void setItemTask(org.bukkit.inventory.Inventory inv, int slot, ItemStack item) { Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, () -> inv.setItem(slot, item)); } + + // TODO: Remove reliance on Bukkit + private static boolean isSameInv(Inventory a, EsInventory b) { + return a.equals(((BukkitInventory) b).getBukkitInventory()); + } // Cancel drag if inside cChest @EventHandler public void onInventoryDrag(final InventoryDragEvent e) { EsInventory inv = cChests.get(e.getWhoClicked().getUniqueId()); - if (!e.getInventory().equals(inv)) { + if (!isSameInv(e.getInventory(), inv)) { return; } // Check if any of the slots dragged are in the cChest for (int slot : e.getRawSlots()) { - if (inv.equals(e.getView().getInventory(slot))) { + if (isSameInv(Objects.requireNonNull(e.getView().getInventory(slot)), inv)) { e.setCancelled(true); return; } @@ -216,7 +223,7 @@ public static void savePlayer(EsPlayer p) { public void onClose(InventoryCloseEvent e) { UUID uid = e.getPlayer().getUniqueId(); - if (e.getInventory().equals(cChests.get(uid))) { + if (isSameInv(e.getInventory(), cChests.get(uid))) { savePlayer((EsPlayer)e.getPlayer()); } } diff --git a/src/main/java/net/serble/estools/Commands/EsTools.java b/src/main/java/net/serble/estools/Commands/EsTools.java index 68efdf9..77ab5ed 100644 --- a/src/main/java/net/serble/estools/Commands/EsTools.java +++ b/src/main/java/net/serble/estools/Commands/EsTools.java @@ -135,9 +135,9 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA private void sendVersion(EsCommandSender sender) { if (Main.newVersion == null) { - send(sender, "&aEsTools v" + Main.server.getPluginVersion()); + send(sender, "&aEsTools v" + Main.server.getPluginVersion().getString()); } else { - send(sender, "&aEsTools &cv" + Main.server.getPluginVersion() + ".&c An update is available, use " + + send(sender, "&aEsTools &cv" + Main.server.getPluginVersion().getString() + ".&c An update is available, use " + "&6/estools update&c to update to &6v" + Main.newVersion.getString()); } } diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index 41e9b52..e4d7db2 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -4,11 +4,12 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +// TODO: Remove these import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import java.util.*; @@ -77,7 +78,7 @@ public static boolean isNotPlayer(EsCommandSender sender, String usage, Object.. } public static boolean isNotPlayer(EsCommandSender sender) { - if (!(sender instanceof Player)) { + if (!(sender instanceof EsPlayer)) { send(sender, "&cYou must be a player to run this command!"); return true; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index a253c47..644739f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -23,7 +23,11 @@ public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { - return new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); + Location bLoc = new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); + if (loc.getDirection() != null) bLoc.setDirection(toVector(loc.getDirection())); + bLoc.setPitch((float) loc.getPitch()); + bLoc.setYaw((float) loc.getYaw()); + return bLoc; } public static EsLocation fromBukkitLocation(Location loc) { @@ -41,6 +45,10 @@ public static Position fromVector(Vector vec) { return new Position(vec.getX(), vec.getY(), vec.getZ()); } + public static Vector toVector(Position pos) { + return new Vector(pos.getX(), pos.getY(), pos.getZ()); + } + public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { if (sender instanceof Player) { return new BukkitPlayer((Player) sender); @@ -200,4 +208,12 @@ public static NamespacedKey getNamespacedKey(String keyString) { return null; } + + public static EsBlock fromBukkitBlock(BlockState block) { + if (block instanceof Sign) { + return new BukkitSign((Sign) block); + } + + return new BukkitBlock(block); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index d466fc6..90c5382 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -92,7 +92,7 @@ public EsGameMode getGameMode() { @Override public @Nullable EsBlock getTargetBlock() { - return new BukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); + return BukkitHelper.fromBukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index bbe3f62..f2fc9f1 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -5,10 +5,7 @@ import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.*; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.Registry; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.inventory.InventoryHolder; @@ -165,15 +162,14 @@ public String[] getPotionEffectTypes() { return out; } - @SuppressWarnings("deprecation") @Override public String[] getEnchantments() { if (Main.minecraftVersion.getMinor() > 12) { - Enchantment[] enchantments = (Enchantment[]) Registry.ENCHANTMENT.stream().toArray(); - String[] out = new String[enchantments.length]; - for (int i = 0; i < enchantments.length; i++) { - Enchantment enchantment = enchantments[i]; - out[i] = enchantment.getName(); + String[] out = new String[(int) Registry.ENCHANTMENT.stream().count()]; + int i = 0; + for (Enchantment e : Registry.ENCHANTMENT) { + out[i] = e.getKey().getKey(); + i++; } return out; } @@ -192,7 +188,12 @@ public String[] getEnchantments() { @Override public String[] getSounds() { - return new String[0]; + Sound[] sounds = Sound.values(); + String[] strSounds = new String[sounds.length]; + for (int i = 0; i < sounds.length; i++) { + strSounds[i] = sounds[i].name(); + } + return strSounds; } @Override From 9a7ff278006ffa7efa662f71f6b86c4bbe01221f Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 24 May 2024 23:04:01 +1000 Subject: [PATCH 08/74] Abstract logger, fix depends, fix tp, start implement scheduler, fix bugs --- pom.xml | 10 ++++-- .../net/serble/estools/Commands/CChest.java | 9 +++++- .../net/serble/estools/Commands/Infinite.java | 10 +++--- src/main/java/net/serble/estools/Main.java | 26 +++++++++------- .../Implementations/Bukkit/BukkitEntity.java | 3 +- .../Implementations/Bukkit/BukkitHelper.java | 9 ++++++ .../Implementations/Bukkit/BukkitLogger.java | 26 ++++++++++++++++ .../Implementations/Bukkit/BukkitPlayer.java | 6 ++++ .../Implementations/Bukkit/BukkitServer.java | 31 +++++++++++++++++-- .../ServerApi/Interfaces/EsLogger.java | 8 +++++ .../ServerApi/Interfaces/EsPlayer.java | 1 + .../Interfaces/EsServerSoftware.java | 2 ++ src/main/java/net/serble/estools/Tester.java | 6 ++-- src/main/resources/plugin.yml | 1 + 14 files changed, 120 insertions(+), 28 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLogger.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsLogger.java diff --git a/pom.xml b/pom.xml index 340ea97..77c8069 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.serble.EsTools EsTools - 4.3.1 + 5.0.0 jar EsTools @@ -100,7 +100,13 @@ gson 2.11.0 - + + io.papermc + paperlib + 1.0.7 + compile + + dev.folia folia-api 1.20.4-R0.1-SNAPSHOT diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index cc4ace9..24dec68 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -14,6 +14,7 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.*; @@ -168,6 +169,12 @@ private static void setItemTask(org.bukkit.inventory.Inventory inv, int slot, It // TODO: Remove reliance on Bukkit private static boolean isSameInv(Inventory a, EsInventory b) { + if (a == null && b == null) { + return true; + } + if (a == null || b == null) { + return false; + } return a.equals(((BukkitInventory) b).getBukkitInventory()); } @@ -224,7 +231,7 @@ public void onClose(InventoryCloseEvent e) { UUID uid = e.getPlayer().getUniqueId(); if (isSameInv(e.getInventory(), cChests.get(uid))) { - savePlayer((EsPlayer)e.getPlayer()); + savePlayer(new BukkitPlayer((Player) e.getPlayer())); } } diff --git a/src/main/java/net/serble/estools/Commands/Infinite.java b/src/main/java/net/serble/estools/Commands/Infinite.java index 5f412fd..2f21b0c 100644 --- a/src/main/java/net/serble/estools/Commands/Infinite.java +++ b/src/main/java/net/serble/estools/Commands/Infinite.java @@ -2,6 +2,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; +import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -43,7 +44,6 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - // TODO: This entire function depends on bukkit @EventHandler public void blockPlace(BlockPlaceEvent e) { if (!currentPlayers.contains(e.getPlayer().getUniqueId())) { @@ -54,11 +54,9 @@ public void blockPlace(BlockPlaceEvent e) { EsPlayer p = new BukkitPlayer(e.getPlayer()); // TODO: This is a terrible way of implementing this btw - Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, () -> { + Main.server.runTaskLater(() -> { p.setMainHand(item); - // A bug exists where the item is not updated in the player's inventory, this is needed for that - //noinspection UnstableApiUsage - e.getPlayer().updateInventory(); - }); + p.updateInventory(); // A bug makes this necessary + }, 0); } } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index c7cad94..b8dd3e6 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -10,18 +10,16 @@ import net.serble.estools.Commands.Warps.*; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsLogger; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; import net.serble.estools.ServerApi.ServerPlatform; import net.serble.estools.Signs.SignMain; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; import net.serble.estools.Commands.*; -import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; @@ -38,6 +36,7 @@ public class Main { public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; public static EsServerSoftware server; + public static EsLogger logger; private final Object context; private static final int bStatsId = 21760; @@ -50,6 +49,7 @@ public Main(ServerPlatform plat, Object context) { public void enable() { plugin = this; server = platform.getServerInstance(context); + logger = server.getLogger(); minecraftVersion = server.getVersion(); @@ -59,7 +59,7 @@ public void enable() { try { Vault.setupEconomy(); } catch (Exception e) { - Bukkit.getLogger().warning("No Vault plugin found, please install vault for economy functionality."); + logger.warning("No Vault plugin found, please install vault for economy functionality."); } } @@ -79,13 +79,13 @@ public void enable() { if (getConfig().getBoolean("metrics", true)) { Metrics metrics = new Metrics(EsToolsBukkit.plugin, bStatsId); metrics.addCustomChart(new SimplePie("vault_enabled", () -> String.valueOf(Vault.economy != null))); - Bukkit.getLogger().info("Started bStat metrics"); + logger.info("Started bStat metrics"); } else { - Bukkit.getLogger().info("Metrics are disabled"); + logger.info("Metrics are disabled"); } if (minecraftVersion.getMinor() <= 2) { - Bukkit.getLogger().info("Tab completion is not supported for versions 1.2 and below."); + logger.info("Tab completion is not supported for versions 1.2 and below."); tabCompleteEnabled = false; } @@ -247,7 +247,7 @@ public void saveResource(String res, boolean replace) { File file = new File(EsToolsBukkit.plugin.getDataFolder(), res); if (file.exists()) { - Bukkit.getLogger().info("Tried to copy resource but it already exists: " + res); + logger.info("Tried to copy resource but it already exists: " + res); return; } @@ -255,10 +255,10 @@ public void saveResource(String res, boolean replace) { Files.createDirectories(file.getParentFile().toPath()); asrt(resource != null); Files.copy(resource, file.toPath()); - Bukkit.getLogger().info("Copied " + res + " to " + file.toPath()); + logger.info("Copied " + res + " to " + file.toPath()); } catch (IOException e) { e.printStackTrace(); - Bukkit.getLogger().severe("Failed to save resource: " + e); + logger.severe("Failed to save resource: " + e); } } @@ -270,11 +270,13 @@ public static void asrt(boolean condition) { public static void asrt(boolean condition, String msg) { assert condition : msg; - if (condition) { + // Asserts aren't enforced most of the time + //noinspection ConstantValue + if (condition) { return; } - Bukkit.getLogger().severe("Assertion failed: " + msg); + logger.severe("Assertion failed: " + msg); throw new AssertionError(msg); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 8bb7954..57b16e2 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import io.papermc.lib.PaperLib; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsEntity; @@ -27,7 +28,7 @@ public Entity getBukkitEntity() { @Override public void teleport(EsLocation loc) { - bukkitEntity.teleport(toBukkitLocation(loc)); + PaperLib.teleportAsync(bukkitEntity, BukkitHelper.toBukkitLocation(loc)); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 644739f..4ce9c60 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -216,4 +216,13 @@ public static EsBlock fromBukkitBlock(BlockState block) { return new BukkitBlock(block); } + + public static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLogger.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLogger.java new file mode 100644 index 0000000..9edcd40 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLogger.java @@ -0,0 +1,26 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsLogger; +import org.bukkit.Bukkit; + +public class BukkitLogger implements EsLogger { + @Override + public void debug(String msg) { + Bukkit.getLogger().info(msg); + } + + @Override + public void info(String msg) { + Bukkit.getLogger().info(msg); + } + + @Override + public void warning(String msg) { + Bukkit.getLogger().warning(msg); + } + + @Override + public void severe(String msg) { + Bukkit.getLogger().severe(msg); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 90c5382..c30e84d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -115,6 +115,12 @@ public void playSound(String sound, EsLocation loc, int volume, int pitch) { bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); } + @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug + @Override + public void updateInventory() { + bukkitPlayer.updateInventory(); + } + private Block bukkitGetTargetBlock() { if (Main.minecraftVersion.getMinor() > 12) { return bukkitPlayer.getTargetBlockExact(5); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index f2fc9f1..bf1514e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -1,13 +1,16 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Effects; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.*; +import org.apache.commons.lang.NotImplementedException; import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -27,13 +30,21 @@ public BukkitServer(Object pluginObj) { @Override public EsPlayer getPlayer(String name) { - return new BukkitPlayer(Bukkit.getPlayer(name)); + Player p = Bukkit.getPlayer(name); + if (p == null) { + return null; + } + return new BukkitPlayer(p); } @Override public EsEntity getEntity(UUID uuid) { if (Main.minecraftVersion.getMinor() > 11) { - return new BukkitEntity(Bukkit.getEntity(uuid)); + Entity entity = Bukkit.getEntity(uuid); + if (entity == null) { + return null; + } + return new BukkitEntity(entity); } for (World world : Bukkit.getWorlds()) { @@ -229,4 +240,20 @@ public SemanticVersion getPluginVersion() { public String getPluginName() { return plugin.getDescription().getName(); } + + @Override + public void runTaskLater(Runnable task, long ticks) { + if (!BukkitHelper.isFolia()) { + Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); + return; + } + + // TODO: Folia + throw new NotImplementedException(); + } + + @Override + public EsLogger getLogger() { + return new BukkitLogger(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLogger.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLogger.java new file mode 100644 index 0000000..836504e --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLogger.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsLogger { + void debug(String msg); + void info(String msg); + void warning(String msg); + void severe(String msg); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 4e13e88..06588ca 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -22,4 +22,5 @@ public interface EsPlayer extends EsLivingEntity { void setAllowFlight(boolean val); boolean isFlying(); void playSound(String sound, EsLocation loc, int volume, int pitch); + void updateInventory(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 4924b7c..d738c5a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -24,4 +24,6 @@ public interface EsServerSoftware { EsCommandSender getConsoleSender(); SemanticVersion getPluginVersion(); String getPluginName(); + void runTaskLater(Runnable task, long ticks); + EsLogger getLogger(); } diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index ab17bb0..bb0f1b3 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -1,9 +1,7 @@ package net.serble.estools; import net.serble.estools.Commands.SafeTp; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; import java.io.PrintWriter; import java.io.StringWriter; @@ -159,7 +157,7 @@ private void execNextCommand() { String stackTrace = sw.toString(); EsToolsCommand.send(player, "&c" + stackTrace); - Bukkit.getLogger().severe(stackTrace); + Main.logger.severe(stackTrace); return; } @@ -178,7 +176,7 @@ private void execNextCommand() { } @SuppressWarnings("WrapperTypeMayBePrimitive") Double timeInTicks = (secVal * 20.0); // The type cannot be primitive - /* TODO: Remove bukkit scheduler */ Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, this::execNextCommand, timeInTicks.longValue()); + Main.server.runTaskLater(this::execNextCommand, timeInTicks.longValue()); EsToolsCommand.send(player, "&bWaiting &6" + cCmd.b() + " seconds"); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8f2d1d8..4346032 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,6 +5,7 @@ version: '${project.version}' softdepend: [Vault] authors: [Calcilator, CoPokBl] api-version: 1.13 +folia-supported: true commands: estools: From b56a0a2b9b541ef2b213c29c7ca7e250b112cb2d Mon Sep 17 00:00:00 2001 From: copokbl Date: Sat, 25 May 2024 21:16:40 +1000 Subject: [PATCH 09/74] Folia partial support, fix bugs with stuff --- .../net/serble/estools/Commands/Buddha.java | 6 +- .../net/serble/estools/Commands/CChest.java | 14 +- .../net/serble/estools/Commands/Infinite.java | 13 +- .../estools/Entrypoints/EsToolsBukkit.java | 15 +- .../net/serble/estools/EsToolsCommand.java | 7 +- src/main/java/net/serble/estools/Main.java | 2 + .../Bukkit/BukkitPersistentDataContainer.java | 5 +- .../Implementations/Bukkit/BukkitServer.java | 12 +- .../Implementations/Folia/FoliaBlock.java | 13 + .../Folia/FoliaCommandBlockSender.java | 13 + .../Folia/FoliaConsoleSender.java | 13 + .../Folia/FoliaEnchantmentsHelper.java | 7 + .../Implementations/Folia/FoliaEntity.java | 20 ++ .../Implementations/Folia/FoliaHelper.java | 221 +++++++++++++++ .../Implementations/Folia/FoliaInventory.java | 33 +++ .../Implementations/Folia/FoliaItemMeta.java | 19 ++ .../Implementations/Folia/FoliaItemStack.java | 24 ++ .../Folia/FoliaLivingEntity.java | 126 +++++++++ .../Implementations/Folia/FoliaLogger.java | 5 + .../Folia/FoliaPersistentDataContainer.java | 13 + .../Implementations/Folia/FoliaPlayer.java | 159 +++++++++++ .../Folia/FoliaPlayerInventory.java | 49 ++++ .../Implementations/Folia/FoliaServer.java | 256 ++++++++++++++++++ .../Implementations/Folia/FoliaSign.java | 43 +++ .../Implementations/Folia/FoliaSignSide.java | 13 + .../Implementations/Folia/FoliaWorld.java | 60 ++++ .../Interfaces/EsServerSoftware.java | 1 + .../estools/ServerApi/ServerPlatform.java | 4 + .../net/serble/estools/Signs/Balance.java | 4 +- .../net/serble/estools/Signs/SignMain.java | 4 +- src/main/java/net/serble/estools/Vault.java | 9 +- 31 files changed, 1146 insertions(+), 37 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaBlock.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaCommandBlockSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaConsoleSender.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemMeta.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLogger.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPersistentDataContainer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSignSide.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 34315a2..720a21b 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -4,11 +4,13 @@ import net.serble.estools.EntityCommand; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.LivingEntity; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; @@ -120,11 +122,11 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void damage(EntityDamageEvent e) { - if (!(e.getEntity() instanceof EsLivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { + if (!(e.getEntity() instanceof LivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { return; } - EsLivingEntity entity = (EsLivingEntity) e.getEntity(); + EsLivingEntity entity = new FoliaLivingEntity((LivingEntity) e.getEntity()); // Get all our vars since Minecraft broke everything in 1.6 double damage = getDamageFromEvent(e); diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 24dec68..1ceab2a 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -5,8 +5,8 @@ import net.serble.estools.ConfigManager; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventory; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaInventory; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -119,7 +119,7 @@ public void inventoryClick(final InventoryClickEvent e) { } // If player inventory - if (!e.getClickedInventory().equals(((BukkitInventory)cChests.get(uid)).getBukkitInventory())) { + if (!e.getClickedInventory().equals(((FoliaInventory)cChests.get(uid)).getBukkitInventory())) { // TODO: Reliance // Shift click into cChest if (equalsOr(e.getClick(), ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT) && currentItem != null) { e.setCancelled(true); @@ -175,7 +175,7 @@ private static boolean isSameInv(Inventory a, EsInventory b) { if (a == null || b == null) { return false; } - return a.equals(((BukkitInventory) b).getBukkitInventory()); + return a.equals(((FoliaInventory) b).getBukkitInventory()); } // Cancel drag if inside cChest @@ -231,19 +231,19 @@ public void onClose(InventoryCloseEvent e) { UUID uid = e.getPlayer().getUniqueId(); if (isSameInv(e.getInventory(), cChests.get(uid))) { - savePlayer(new BukkitPlayer((Player) e.getPlayer())); + savePlayer(new FoliaPlayer((Player) e.getPlayer())); // TODO: Fix } } @EventHandler public void onQuit(PlayerQuitEvent e) { - savePlayer(new BukkitPlayer(e.getPlayer())); + savePlayer(new FoliaPlayer(e.getPlayer())); cChests.remove(e.getPlayer().getUniqueId()); } @EventHandler public void onKick(PlayerKickEvent e) { - savePlayer(new BukkitPlayer(e.getPlayer())); + savePlayer(new FoliaPlayer(e.getPlayer())); cChests.remove(e.getPlayer().getUniqueId()); } } diff --git a/src/main/java/net/serble/estools/Commands/Infinite.java b/src/main/java/net/serble/estools/Commands/Infinite.java index 2f21b0c..e1b308d 100644 --- a/src/main/java/net/serble/estools/Commands/Infinite.java +++ b/src/main/java/net/serble/estools/Commands/Infinite.java @@ -3,8 +3,8 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -50,13 +50,14 @@ public void blockPlace(BlockPlaceEvent e) { return; } - EsItemStack item = new BukkitItemStack(e.getItemInHand().clone()); - EsPlayer p = new BukkitPlayer(e.getPlayer()); + // TODO: Migrate + EsItemStack item = new FoliaItemStack(e.getItemInHand().clone()); + EsPlayer p = new FoliaPlayer(e.getPlayer()); // TODO: This is a terrible way of implementing this btw - Main.server.runTaskLater(() -> { + Main.server.runTask(() -> { p.setMainHand(item); p.updateInventory(); // A bug makes this necessary - }, 0); + }); } } diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java index 81a72f7..588f525 100644 --- a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -4,6 +4,7 @@ import net.serble.estools.ServerApi.ServerPlatform; import org.bukkit.plugin.java.JavaPlugin; +/** This will be called for Bukkit and Folia platforms */ public class EsToolsBukkit extends JavaPlugin { /** Will be null if plugin is not Bukkit */ public static EsToolsBukkit plugin; @@ -11,10 +12,22 @@ public class EsToolsBukkit extends JavaPlugin { @Override public void onEnable() { plugin = this; - Main main = new Main(ServerPlatform.Bukkit, this); + + ServerPlatform platform = isFolia() ? ServerPlatform.Folia : ServerPlatform.Bukkit; + + Main main = new Main(platform, this); main.enable(); } @Override public void onDisable() { /* Needed for older versions, which require an onDisable method */ } + + private static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } } diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index e4d7db2..4ee091b 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -1,7 +1,7 @@ package net.serble.estools; import net.serble.estools.ServerApi.EsLocation; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -10,6 +10,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; import java.util.*; @@ -28,8 +29,8 @@ public List onTabComplete(EsCommandSender sender, Command command, Strin } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return execute(BukkitHelper.fromBukkitCommandSender(sender), args); + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + return execute(FoliaHelper.fromBukkitCommandSender(sender), args); // TODO: This is dependent on Folia } public static List fixTabComplete(List inList, String arg) { diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index b8dd3e6..2452dce 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -51,6 +51,8 @@ public void enable() { server = platform.getServerInstance(context); logger = server.getLogger(); + logger.info("Starting EsTools on platform: " + platform.name()); + minecraftVersion = server.getVersion(); Effects.load(); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java index 9028649..7ea6ddb 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.ServerApi.EsPersistentDataType; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaHelper; import net.serble.estools.ServerApi.Interfaces.EsPersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer; @@ -18,12 +19,12 @@ public BukkitPersistentDataContainer(PersistentDataContainer container) { @Override public void set(String key, EsPersistentDataType type, Object val) { - bukkitContainer.set(Objects.requireNonNull(getNamespacedKey(key)), BukkitHelper.toBukkitPersistentDataType(type), val); + bukkitContainer.set(Objects.requireNonNull(getNamespacedKey(key)), FoliaHelper.toBukkitPersistentDataType(type), val); } @Override public Object get(String key, EsPersistentDataType type) { - return null; + return bukkitContainer.get(Objects.requireNonNull(getNamespacedKey(key)), FoliaHelper.toBukkitPersistentDataType(type)); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index bf1514e..38674ce 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -6,7 +6,6 @@ import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.*; -import org.apache.commons.lang.NotImplementedException; import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; @@ -243,13 +242,12 @@ public String getPluginName() { @Override public void runTaskLater(Runnable task, long ticks) { - if (!BukkitHelper.isFolia()) { - Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); - return; - } + Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); + } - // TODO: Folia - throw new NotImplementedException(); + @Override + public void runTask(Runnable task) { + Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, task); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaBlock.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaBlock.java new file mode 100644 index 0000000..ee95ac7 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaBlock.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitBlock; +import org.bukkit.block.BlockState; + +public class FoliaBlock extends BukkitBlock { + private final BlockState bukkitState; + + public FoliaBlock(BlockState block) { + super(block); + bukkitState = block; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaCommandBlockSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaCommandBlockSender.java new file mode 100644 index 0000000..996c140 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaCommandBlockSender.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitCommandBlockSender; +import org.bukkit.command.BlockCommandSender; + +public class FoliaCommandBlockSender extends BukkitCommandBlockSender { + private final BlockCommandSender bukkitSender; + + public FoliaCommandBlockSender(BlockCommandSender child) { + super(child); + bukkitSender = child; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaConsoleSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaConsoleSender.java new file mode 100644 index 0000000..bda8ea7 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaConsoleSender.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitConsoleSender; +import org.bukkit.command.ConsoleCommandSender; + +public class FoliaConsoleSender extends BukkitConsoleSender { + private final ConsoleCommandSender bukkitSender; + + public FoliaConsoleSender(ConsoleCommandSender child) { + super(child); + bukkitSender = child; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java new file mode 100644 index 0000000..103d6d0 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java @@ -0,0 +1,7 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitEnchantmentsHelper; + +// This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. +// only enchantments before 1.13 need to be here because this class isn't used past 1.13 +public class FoliaEnchantmentsHelper extends BukkitEnchantmentsHelper { } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java new file mode 100644 index 0000000..ff84924 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java @@ -0,0 +1,20 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import io.papermc.lib.PaperLib; +import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitEntity; +import org.bukkit.entity.Entity; + +public class FoliaEntity extends BukkitEntity { + private final org.bukkit.entity.Entity bukkitEntity; + + public FoliaEntity(Entity bukkitEntity) { + super(bukkitEntity); + this.bukkitEntity = bukkitEntity; + } + + @Override + public void teleport(EsLocation loc) { + PaperLib.teleportAsync(bukkitEntity, FoliaHelper.toBukkitLocation(loc)); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java new file mode 100644 index 0000000..b28a4c4 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -0,0 +1,221 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import io.papermc.paper.threadedregions.scheduler.EntityScheduler; +import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; +import io.papermc.paper.threadedregions.scheduler.RegionScheduler; +import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.*; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class FoliaHelper extends BukkitHelper { + + private static GlobalRegionScheduler getGlobalScheduler() { + GlobalRegionScheduler grs; + try { + Object serverInstance = EsToolsBukkit.plugin.getServer(); + + Method getSchedulerMethod = serverInstance.getClass().getMethod("getGlobalRegionScheduler"); + Object schObject = getSchedulerMethod.invoke(serverInstance); + + if (schObject instanceof GlobalRegionScheduler) { + grs = (GlobalRegionScheduler) schObject; + } else { + Main.logger.severe("schObject is not an instance of GlobalRegionScheduler"); + throw new RuntimeException("Failed to get global scheduler, are you running folia?"); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Failed to get global scheduler, are you running folia?"); + } + + return grs; + } + + private static RegionScheduler getRegionScheduler() { + RegionScheduler grs; + try { + Object serverInstance = EsToolsBukkit.plugin.getServer(); + + Method getSchedulerMethod = serverInstance.getClass().getMethod("getRegionScheduler"); + Object schObject = getSchedulerMethod.invoke(serverInstance); + + if (schObject instanceof RegionScheduler) { + grs = (RegionScheduler) schObject; + } else { + Main.logger.severe("schObject is not an instance of RegionScheduler"); + throw new RuntimeException("Failed to get regional scheduler, are you running folia?"); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Failed to get regional scheduler, are you running folia?"); + } + + return grs; + } + + private static EntityScheduler getEntityScheduler(Entity entity) { + EntityScheduler grs; + try { + Method getSchedulerMethod = entity.getClass().getMethod("getScheduler"); + Object schObject = getSchedulerMethod.invoke(entity); + + if (schObject instanceof EntityScheduler) { + grs = (EntityScheduler) schObject; + } else { + Main.logger.severe("schObject is not an instance of EntityScheduler"); + throw new RuntimeException("Failed to get entity scheduler, are you running folia?"); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Failed to get entity scheduler, are you running folia?"); + } + + return grs; + } + + public static void runTaskLater(Runnable task, long ticks) { + getGlobalScheduler().runDelayed(EsToolsBukkit.plugin, (t) -> task.run(), ticks); + } + + public static void runTaskOnNextTick(Runnable task) { + getGlobalScheduler().run(EsToolsBukkit.plugin, (t) -> task.run()); + } + + public static void runFromLocation(EsLocation loc, Runnable task) { + runFromLocation(toBukkitLocation(loc), task); + } + + public static void runFromLocation(Location loc, Runnable task) { + getRegionScheduler().run(EsToolsBukkit.plugin, loc, (t) -> task.run()); + } + + public static void runFromEntity(EsEntity entity, Runnable task) { + runFromEntity(((FoliaEntity)entity).getBukkitEntity(), task); + } + + public static void runFromEntity(Entity entity, Runnable task) { + getEntityScheduler(entity).run(EsToolsBukkit.plugin, (t) -> task.run(), null); + } + + public static void runSync(Runnable task) { + //getGlobalScheduler().execute(EsToolsBukkit.plugin, task); + //Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, task); + //runTaskLater(task, 1); + //getGlobalScheduler().run(EsToolsBukkit.plugin, (t) -> task.run()); + Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, task, 1); + } + + public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { + if (sender instanceof Player) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer((Player) sender); + } + + if (sender instanceof LivingEntity) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity((org.bukkit.entity.LivingEntity) sender); + } + + if (sender instanceof Entity) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity((org.bukkit.entity.Entity) sender); + } + + if (sender instanceof ConsoleCommandSender) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaConsoleSender((ConsoleCommandSender) sender); + } + + if (sender instanceof BlockCommandSender) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaCommandBlockSender((BlockCommandSender) sender); + } + + throw new RuntimeException("Unrecognised command sender"); + } + + public static CommandSender toBukkitCommandSender(EsCommandSender sender) { + if (sender instanceof EsPlayer) { + return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer) sender).getBukkitPlayer(); + } + + if (sender instanceof EsLivingEntity) { + return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity) sender).getBukkitEntity(); + } + + if (sender instanceof EsEntity) { + return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity) sender).getBukkitEntity(); + } + + if (sender instanceof EsConsoleSender) { + return Bukkit.getConsoleSender(); + } + + if (sender instanceof EsCommandBlockSender) { + return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaCommandBlockSender) sender).getBukkitSender(); + } + + throw new RuntimeException("Unrecognised command sender"); + } + + public static EsEntity fromBukkitEntity(org.bukkit.entity.Entity entity) { + if (entity instanceof org.bukkit.entity.LivingEntity) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity((LivingEntity) entity); + } + + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity(entity); + } + + public static EsBlock fromBukkitBlock(Block block) { + BlockState state = block.getState(); + + if (state instanceof Sign) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaSign((Sign) state); + } + + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaBlock(state); + } + + @SuppressWarnings("deprecation") + public static Enchantment getBukkitEnchantment(String name) { + if (Main.minecraftVersion.getMinor() >= 13) { + Enchantment ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); + if (ench == null) { + // Dump info and throw + Bukkit.getLogger().severe("Failed to find enchantment: " + name); + for (Enchantment e : Registry.ENCHANTMENT) { + Bukkit.getLogger().severe("This exists: " + e.getName()); + } + throw new RuntimeException("Could not find enchantment: " + name); + } + } + + // We have to use deprecated method for pre 1.13 + return Enchantment.getByName(FoliaEnchantmentsHelper.getByName(name)); + } + + public static EsBlock fromBukkitBlock(BlockState block) { + if (block instanceof Sign) { + return new FoliaSign((Sign) block); + } + + return new FoliaBlock(block); + } + + public static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java new file mode 100644 index 0000000..f6eb9cd --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -0,0 +1,33 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import org.bukkit.inventory.Inventory; + +public class FoliaInventory extends BukkitInventory { + private final Inventory bukkitInv; + + public FoliaInventory(Inventory inv) { + super(inv); + bukkitInv = inv; + } + + @Override + public EsItemStack[] getContents() { + org.bukkit.inventory.ItemStack[] bukkitItems = bukkitInv.getContents(); + EsItemStack[] items = new EsItemStack[bukkitItems.length]; + for (int i = 0; i < bukkitItems.length; i++) { + if (bukkitItems[i] == null) { + items[i] = null; + continue; + } + items[i] = new FoliaItemStack(bukkitItems[i]); + } + return items; + } + + @Override + public EsItemStack getItem(int slot) { + return new FoliaItemStack(bukkitInv.getItem(slot)); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemMeta.java new file mode 100644 index 0000000..3b02f1c --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemMeta.java @@ -0,0 +1,19 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsPersistentDataContainer; +import org.bukkit.inventory.meta.ItemMeta; + +public class FoliaItemMeta extends BukkitItemMeta { + private final ItemMeta bukkitMeta; + + public FoliaItemMeta(ItemMeta meta) { + super(meta); + bukkitMeta = meta; + } + + @Override + public EsPersistentDataContainer getPersistentDataContainer() { + return new FoliaPersistentDataContainer(bukkitMeta.getPersistentDataContainer()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java new file mode 100644 index 0000000..3bd9905 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -0,0 +1,24 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import org.bukkit.Material; + +public class FoliaItemStack extends BukkitItemStack { + private final org.bukkit.inventory.ItemStack bukkitItem; + + public FoliaItemStack(String mat, int amount) { + super(mat, amount); + bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); + } + + public FoliaItemStack(org.bukkit.inventory.ItemStack child) { + super(child); + bukkitItem = child; + } + + @Override + public EsItemMeta getItemMeta() { + return new FoliaItemMeta(bukkitItem.getItemMeta()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java new file mode 100644 index 0000000..13ad803 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java @@ -0,0 +1,126 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; +import net.serble.estools.ServerApi.Interfaces.EsWorld; +import org.bukkit.Bukkit; +import org.bukkit.Registry; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.LivingEntity; +import org.bukkit.potion.PotionEffect; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +// Can't extend BukkitEntity because it needs to extend FoliaEntity for it to work +public class FoliaLivingEntity extends FoliaEntity implements EsLivingEntity { + private final org.bukkit.entity.LivingEntity bukkitEntity; + + public FoliaLivingEntity(org.bukkit.entity.LivingEntity entity) { + super(entity); + bukkitEntity = entity; + } + + public LivingEntity getBukkitEntity() { + return bukkitEntity; + } + + @Override + public double getMaxHealth() { + if (Main.minecraftVersion.getMinor() > 8) { + return Objects.requireNonNull(bukkitEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue(); + } else { + if (Main.minecraftVersion.getMinor() > 5) { + //noinspection deprecation + return bukkitEntity.getMaxHealth(); + } + + try { + return (double)(int) org.bukkit.entity.LivingEntity.class.getMethod("getMaxHealth").invoke(bukkitEntity); + } catch(Exception e) { + Bukkit.getLogger().severe(e.toString()); + return 20d; + } + } + } + + @Override + public void setMaxHealth(double val) { + if (Main.minecraftVersion.getMinor() > 8) { + Objects.requireNonNull(bukkitEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(val); + } else { + if (Main.minecraftVersion.getMinor() > 5) { + //noinspection deprecation + bukkitEntity.setMaxHealth(val); + return; + } + + try { + //noinspection JavaReflectionMemberAccess + org.bukkit.entity.LivingEntity.class.getMethod("setMaxHealth", int.class).invoke(bukkitEntity, (int)val); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } + } + + @Override + public double getHealth() { + if (Main.minecraftVersion.getMinor() > 5) { + return bukkitEntity.getHealth(); + } + + try { + return (double)(int) org.bukkit.entity.LivingEntity.class.getMethod("getHealth").invoke(bukkitEntity); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + return 20d; + } + } + + @Override + public void setHealth(double val) { + if (Main.minecraftVersion.getMinor() > 5) { + bukkitEntity.setHealth(val); + return; + } + + try { + //noinspection JavaReflectionMemberAccess + org.bukkit.entity.LivingEntity.class.getMethod("setHealth", int.class).invoke(bukkitEntity, (int)val); + } + catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } + + @Override + public void addPotionEffect(String effect, int duration, int amplifier) { + bukkitEntity.addPotionEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect)), duration, amplifier)); + } + + @Override + public void removePotionEffect(String effect) { + bukkitEntity.removePotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect))); + } + + @SuppressWarnings("deprecation") // Gotta love backwards compatibility + @Override + public List getActivePotionEffects() { + Collection bukkitEffects = bukkitEntity.getActivePotionEffects(); + List out = new ArrayList<>(); + for (PotionEffect eff : bukkitEffects) { + out.add(eff.getType().getName()); + } + return out; + } + + @Override + public EsWorld getWorld() { + return new FoliaWorld(bukkitEntity.getWorld()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLogger.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLogger.java new file mode 100644 index 0000000..08fb961 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLogger.java @@ -0,0 +1,5 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitLogger; + +public class FoliaLogger extends BukkitLogger { } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPersistentDataContainer.java new file mode 100644 index 0000000..89c9e2b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPersistentDataContainer.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPersistentDataContainer; +import org.bukkit.persistence.PersistentDataContainer; + +public class FoliaPersistentDataContainer extends BukkitPersistentDataContainer { + private final PersistentDataContainer bukkitContainer; + + public FoliaPersistentDataContainer(PersistentDataContainer container) { + super(container); + bukkitContainer = container; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java new file mode 100644 index 0000000..d07d9c2 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -0,0 +1,159 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.Interfaces.*; +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.Objects; + +// Can't extend BukkitEntity because it needs to extend FoliaLivingEntity for it to work +public class FoliaPlayer extends FoliaLivingEntity implements EsPlayer { + private final org.bukkit.entity.Player bukkitPlayer; + + public FoliaPlayer(org.bukkit.entity.Player entity) { + super(entity); + bukkitPlayer = entity; + } + + public org.bukkit.entity.Player getBukkitPlayer() { + return bukkitPlayer; + } + + @Override + public int getFoodLevel() { + return bukkitPlayer.getFoodLevel(); + } + + @Override + public void setFoodLevel(int val) { + bukkitPlayer.setFoodLevel(val); + } + + @Override + public double getSaturation() { + return bukkitPlayer.getSaturation(); + } + + @Override + public void setSaturation(double val) { + bukkitPlayer.setSaturation((float) val); + } + + @Override + public EsItemStack getMainHand() { + return new FoliaItemStack(bukkitGetMainHand()); + } + + @Override + public void setMainHand(EsItemStack item) { + setMainHandBukkit(bukkitPlayer, ((FoliaItemStack) item).getBukkitItem()); + } + + @Override + public void openInventory(EsInventory inv) { + bukkitPlayer.openInventory(((FoliaInventory) inv).getBukkitInventory()); + } + + @Override + public void closeInventory() { + bukkitPlayer.closeInventory(); + } + + @Override + public EsPlayerInventory getInventory() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayerInventory(bukkitPlayer.getInventory()); + } + + @Override + public void setFlySpeed(float val) { + bukkitPlayer.setFlySpeed(val); + } + + @Override + public void setWalkSpeed(float val) { + bukkitPlayer.setWalkSpeed(val); + } + + @Override + public void setGameMode(EsGameMode mode) { + bukkitPlayer.setGameMode(FoliaHelper.toBukkitGameMode(mode)); + } + + @Override + public EsGameMode getGameMode() { + return FoliaHelper.fromBukkitGameMode(bukkitPlayer.getGameMode()); + } + + @Override + public @Nullable EsBlock getTargetBlock() { + return FoliaHelper.fromBukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); + } + + @Override + public boolean getAllowFlight() { + return bukkitPlayer.getAllowFlight(); + } + + @Override + public void setAllowFlight(boolean val) { + bukkitPlayer.setAllowFlight(val); + } + + @Override + public boolean isFlying() { + return bukkitPlayer.isFlying(); + } + + @Override + public void playSound(String sound, EsLocation loc, int volume, int pitch) { + bukkitPlayer.playSound(FoliaHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + } + + @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug + @Override + public void updateInventory() { + bukkitPlayer.updateInventory(); + } + + private Block bukkitGetTargetBlock() { + if (Main.minecraftVersion.getMinor() > 12) { + return bukkitPlayer.getTargetBlockExact(5); + } else if (Main.minecraftVersion.getMinor() > 7) { + return bukkitPlayer.getTargetBlock(null, 5); + } else { + try { + //noinspection JavaReflectionMemberAccess + return (Block) LivingEntity.class.getMethod("getTargetBlock", HashSet.class, int.class).invoke(bukkitPlayer, null, 5); + } + catch (Exception e) { + Bukkit.getLogger().severe(e.toString()); + return null; + } + } + } + + private org.bukkit.inventory.ItemStack bukkitGetMainHand() { + if (Main.minecraftVersion.getMinor() > 8) { + return bukkitPlayer.getInventory().getItemInMainHand(); + } else { + //noinspection deprecation + return bukkitPlayer.getInventory().getItemInHand(); + } + } + + private void setMainHandBukkit(org.bukkit.entity.Player p, org.bukkit.inventory.ItemStack is) { + if (Main.minecraftVersion.getMinor() > 8) { + p.getInventory().setItemInMainHand(is); + } else { + //noinspection deprecation + p.getInventory().setItemInHand(is); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java new file mode 100644 index 0000000..5e32d0a --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; +import org.bukkit.inventory.PlayerInventory; + +public class FoliaPlayerInventory extends FoliaInventory implements EsPlayerInventory { + private final PlayerInventory bukkitInv; + + public FoliaPlayerInventory(PlayerInventory inv) { + super(inv); + bukkitInv = inv; + } + + @Override + public PlayerInventory getBukkitInventory() { + return bukkitInv; + } + + @Override + public EsItemStack getOffHand() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getItemInOffHand()); + } + + @Override + public EsItemStack getMainHand() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getItemInMainHand()); + } + + @Override + public EsItemStack getHelmet() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getHelmet()); + } + + @Override + public EsItemStack getLeggings() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getLeggings()); + } + + @Override + public EsItemStack getChestplate() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getChestplate()); + } + + @Override + public EsItemStack getBoots() { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getBoots()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java new file mode 100644 index 0000000..96f5cea --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -0,0 +1,256 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.Effects; +import net.serble.estools.Main; +import net.serble.estools.SemanticVersion; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.Interfaces.*; +import org.bukkit.*; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import java.io.File; +import java.util.*; + +public class FoliaServer implements EsServerSoftware { + private final JavaPlugin plugin; + + public FoliaServer(Object pluginObj) { + plugin = (JavaPlugin) pluginObj; + } + + @Override + public EsPlayer getPlayer(String name) { + Player p = Bukkit.getPlayer(name); + if (p == null) { + return null; + } + return new FoliaPlayer(p); + } + + @Override + public EsEntity getEntity(UUID uuid) { + if (Main.minecraftVersion.getMinor() > 11) { + Entity entity = Bukkit.getEntity(uuid); + if (entity == null) { + return null; + } + return new FoliaEntity(entity); + } + + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getUniqueId() == uuid) { + return new FoliaEntity(entity); + } + } + } + + return null; + } + + @Override + public SemanticVersion getVersion() { // Parse the minecraft version from the Bukkit version string + String versionS = Bukkit.getVersion(); + int minor = 0; + int patch = 0; + + if (versionS.contains("(MC: ")) { + int posOfMC = versionS.indexOf("(MC: ") + 5; + versionS = versionS.substring(posOfMC, versionS.indexOf(')', posOfMC)); + } else { + Main.logger.warning("Could not detect version from: " + versionS); + throw new RuntimeException("Could not detect version"); + } + + for (int i = 0; i < 99; i++) { + if (versionS.contains("1." + i)) { + minor = i; + } + } + + for (int i = 0; i < 99; i++) { + if (versionS.contains("1." + minor + '.' + i)) { + patch = i; + } + } + + Main.logger.info("Version detected as: 1." + minor + '.' + patch + " from: " + versionS); + return new SemanticVersion(1, minor, patch); + } + + @Override + public Collection getOnlinePlayers() { + try { + if (Bukkit.class.getMethod("getOnlinePlayers").getReturnType() == Collection.class) { + List players = new ArrayList<>(); + for (org.bukkit.entity.Player p : Bukkit.getOnlinePlayers()) { + players.add(new FoliaPlayer(p)); + } + return players; + } + else { + org.bukkit.entity.Player[] players = (org.bukkit.entity.Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); + List users = new ArrayList<>(); + for (org.bukkit.entity.Player p : players) { + users.add(new FoliaPlayer(p)); + } + return users; + } + + } catch (Exception e) { + Bukkit.getLogger().severe(e.toString()); + return new ArrayList<>(); + } + } + + @Override + public EsItemStack createItemStack(String material, int amount) { + return new FoliaItemStack(material, amount); + } + + @SuppressWarnings("deprecation") + @Override + public EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + if (Main.minecraftVersion.getMinor() >= 9) { + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), amount); + + String effType; + try { + effType = Effects.getByName(effect); + } catch (IllegalArgumentException e) { + return null; + } + + PotionMeta meta = (PotionMeta) pot.getItemMeta(); + assert meta != null; + meta.addCustomEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effType)), duration, amp-1), true); + pot.setItemMeta(meta); + return new FoliaItemStack(pot); + } else if (Main.minecraftVersion.getMinor() >= 4) { + String effType; + try { + effType = Effects.getPotionByName(effect); + } catch (IllegalArgumentException e) { + return null; + } + + org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); + potion.setSplash(potType == EsPotType.splash); + return new FoliaItemStack(potion.toItemStack(1)); + } else { // This isn't possible to get to because this class won't load on 1.3 and below + return null; + } + } + + @Override + public EsInventory createInventory(EsPlayer owner, int size, String title) { + InventoryHolder holder = owner == null ? null : ((FoliaPlayer) owner).getBukkitPlayer(); + return new FoliaInventory(Bukkit.createInventory(holder, size, title)); + } + + @SuppressWarnings("deprecation") + @Override + public String[] getPotionEffectTypes() { + // We need to use the deprecated .values() method because Registry doesn't exist in old versions + PotionEffectType[] effectTypes = PotionEffectType.values(); + String[] out = new String[effectTypes.length]; + for (int i = 0; i < effectTypes.length; i++) { + out[i] = effectTypes[i].getName(); // Same reason as above for deprecated method + } + return out; + } + + @Override + public String[] getEnchantments() { + if (Main.minecraftVersion.getMinor() > 12) { + String[] out = new String[(int) Registry.ENCHANTMENT.stream().count()]; + int i = 0; + for (Enchantment e : Registry.ENCHANTMENT) { + out[i] = e.getKey().getKey(); + i++; + } + return out; + } + + // Pre 1.13, we need to use the helper to get all the keys + Set> enchSet = FoliaEnchantmentsHelper.entrySet(); + String[] enchs = new String[enchSet.size()]; + int i = 0; + for (Map.Entry enchEntry : enchSet) { + enchs[i] = enchEntry.getKey(); + i++; + } + + return enchs; + } + + @Override + public String[] getSounds() { + Sound[] sounds = Sound.values(); + String[] strSounds = new String[sounds.length]; + for (int i = 0; i < sounds.length; i++) { + strSounds[i] = sounds[i].name(); + } + return strSounds; + } + + @Override + public boolean doesEnchantmentExist(String name) { + try { + return FoliaHelper.getBukkitEnchantment(name) != null; + } catch (Exception e) { + return false; + } + } + + @Override + public File getDataFolder() { + return plugin.getDataFolder(); + } + + @Override + public void dispatchCommand(EsCommandSender sender, String cmd) { + FoliaHelper.runSync(() -> Bukkit.dispatchCommand(FoliaHelper.toBukkitCommandSender(sender), cmd)); + } + + @Override + public EsCommandSender getConsoleSender() { + return null; + } + + @Override + public SemanticVersion getPluginVersion() { + return new SemanticVersion(plugin.getDescription().getVersion()); + } + + @Override + public String getPluginName() { + return plugin.getDescription().getName(); + } + + @Override + public void runTaskLater(Runnable task, long ticks) { + FoliaHelper.runTaskLater(task, ticks); + } + + @Override + public void runTask(Runnable task) { + FoliaHelper.runTaskOnNextTick(task); + } + + @Override + public EsLogger getLogger() { + return new FoliaLogger(); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java new file mode 100644 index 0000000..8c0a10b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java @@ -0,0 +1,43 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.EsSign; +import net.serble.estools.ServerApi.Interfaces.EsSignSide; +import org.bukkit.block.Sign; + +public class FoliaSign extends net.serble.estools.ServerApi.Implementations.Folia.FoliaBlock implements EsSign { + private final Sign bukkitSign; + + public FoliaSign(Sign block) { + super(block); + bukkitSign = block; + } + + @Override + public void update() { + bukkitSign.update(); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public void setGlowingText(boolean glowing) { + bukkitSign.setGlowingText(glowing); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public boolean isGlowingText() { + return bukkitSign.isGlowingText(); + } + + @SuppressWarnings("deprecation") // Method is version specific + @Override + public void setLine(int line, String text) { + bukkitSign.setLine(line, text); + } + + @Override + public EsSignSide getTargetSide(EsPlayer player) { + return new net.serble.estools.ServerApi.Implementations.Folia.FoliaSignSide(bukkitSign.getTargetSide(((net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer) player).getBukkitPlayer())); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSignSide.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSignSide.java new file mode 100644 index 0000000..825457d --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSignSide.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitSignSide; +import org.bukkit.block.sign.SignSide; + +public class FoliaSignSide extends BukkitSignSide { + private final SignSide bukkitSide; + + public FoliaSignSide(SignSide side) { + super(side); + this.bukkitSide = side; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java new file mode 100644 index 0000000..fcc675d --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java @@ -0,0 +1,60 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitWorld; +import net.serble.estools.ServerApi.Interfaces.EsEntity; +import org.bukkit.World; +import org.bukkit.entity.Entity; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class FoliaWorld extends BukkitWorld { + private final org.bukkit.World bukkitWorld; + + public FoliaWorld(World world) { + super(world); + bukkitWorld = world; + } + + @Override + public List getEntities() { + List bEntities = bukkitWorld.getEntities(); + List entities = new ArrayList<>(); + for (org.bukkit.entity.Entity bEntity : bEntities) { + entities.add(FoliaHelper.fromBukkitEntity(bEntity)); + } + return entities; + } + + @Override + public List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff) { + Collection bEntities = bukkitWorld.getNearbyEntities(FoliaHelper.toBukkitLocation(loc), xoff, yoff, zoff); + List entities = new ArrayList<>(); + for (org.bukkit.entity.Entity bEntity : bEntities) { + entities.add(FoliaHelper.fromBukkitEntity(bEntity)); + } + return entities; + } + + @Override + public void setTime(long time) { + FoliaHelper.runTaskOnNextTick(() -> bukkitWorld.setTime(time)); + } + + @Override + public void setStorming(boolean val) { + FoliaHelper.runTaskOnNextTick(() -> bukkitWorld.setStorm(false)); + } + + @Override + public void setThundering(boolean val) { + FoliaHelper.runTaskOnNextTick(() -> bukkitWorld.setThundering(val)); + } + + @Override + public void strikeLightning(EsLocation loc) { + bukkitWorld.strikeLightning(FoliaHelper.toBukkitLocation(loc)); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index d738c5a..dd6e68a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -25,5 +25,6 @@ public interface EsServerSoftware { SemanticVersion getPluginVersion(); String getPluginName(); void runTaskLater(Runnable task, long ticks); + void runTask(Runnable task); EsLogger getLogger(); } diff --git a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java index 47a0479..43cf0e8 100644 --- a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java +++ b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitServer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaServer; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; public enum ServerPlatform { @@ -12,6 +13,9 @@ public EsServerSoftware getServerInstance(Object context) { case Bukkit: return new BukkitServer(context); + case Folia: + return new FoliaServer(context); + default: throw new IllegalArgumentException("Unsupported server software"); } diff --git a/src/main/java/net/serble/estools/Signs/Balance.java b/src/main/java/net/serble/estools/Signs/Balance.java index 3fd9c32..4787041 100644 --- a/src/main/java/net/serble/estools/Signs/Balance.java +++ b/src/main/java/net/serble/estools/Signs/Balance.java @@ -1,6 +1,6 @@ package net.serble.estools.Signs; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.Vault; @@ -8,6 +8,6 @@ public class Balance extends SignType { @Override public void run(EsPlayer p, String[] lines) { - send(p, "&aYour current balance is &6$%s", String.valueOf(Vault.economy.getBalance(((BukkitPlayer)p).getBukkitPlayer()))); + send(p, "&aYour current balance is &6$%s", String.valueOf(Vault.economy.getBalance(((FoliaPlayer)p).getBukkitPlayer()))); } } diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index 8d808f2..987fd81 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -2,7 +2,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; import org.bukkit.Bukkit; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; @@ -45,7 +45,7 @@ public void interact(PlayerInteractEvent e) { if (signType != null) { e.setCancelled(true); - signType.run(new BukkitPlayer(e.getPlayer()), state.getLines()); + signType.run(new FoliaPlayer(e.getPlayer()), state.getLines()); } } } diff --git a/src/main/java/net/serble/estools/Vault.java b/src/main/java/net/serble/estools/Vault.java index 36bfc86..182226b 100644 --- a/src/main/java/net/serble/estools/Vault.java +++ b/src/main/java/net/serble/estools/Vault.java @@ -2,10 +2,9 @@ import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; public class Vault { @@ -42,12 +41,12 @@ public static boolean takeMoney(double price, EsPlayer p) { return false; } - if (Vault.economy.getBalance(((BukkitPlayer) p).getBukkitPlayer()) < price) { + if (Vault.economy.getBalance(((FoliaPlayer) p).getBukkitPlayer()) < price) { EsToolsCommand.send(p, "&cYou do not have enough money to purchase this item."); return false; } - Vault.economy.withdrawPlayer(((BukkitPlayer) p).getBukkitPlayer(), price); + Vault.economy.withdrawPlayer(((FoliaPlayer) p).getBukkitPlayer(), price); return true; } @@ -57,7 +56,7 @@ public static boolean payMoney(double price, EsPlayer p) { return false; } - EconomyResponse economyResponse = Vault.economy.depositPlayer(((BukkitPlayer) p).getBukkitPlayer(), price); + EconomyResponse economyResponse = Vault.economy.depositPlayer(((FoliaPlayer) p).getBukkitPlayer(), price); return economyResponse.transactionSuccess(); } } From 16fa4b47a2319f02fa9f7aa2c8d266f267225cd6 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 26 May 2024 01:27:11 +1000 Subject: [PATCH 10/74] Start implementing events, more fixes --- abstraction.md | 6 + .../net/serble/estools/Commands/Back.java | 51 +++++---- .../net/serble/estools/Commands/Buddha.java | 73 ++++--------- .../java/net/serble/estools/Commands/God.java | 28 +++-- .../net/serble/estools/Commands/Infinite.java | 36 +++--- .../net/serble/estools/Commands/SafeTp.java | 25 +++-- src/main/java/net/serble/estools/Main.java | 17 +++ .../estools/ServerApi/EsEquipmentSlot.java | 10 ++ .../estools/ServerApi/EsEventListener.java | 7 ++ .../estools/ServerApi/EsTeleportCause.java | 15 +++ .../ServerApi/Events/EsBlockPlaceEvent.java | 30 +++++ .../ServerApi/Events/EsEntityDamageEvent.java | 26 +++++ .../ServerApi/Events/EsPlayerDeathEvent.java | 16 +++ .../Events/EsPlayerTeleportEvent.java | 26 +++++ .../Implementations/Bukkit/BukkitEntity.java | 7 +- .../Bukkit/BukkitEventsListener.java | 88 +++++++++++++++ .../Implementations/Bukkit/BukkitHelper.java | 103 +++++++++++++++++- .../Bukkit/BukkitPlayerInventory.java | 6 + .../Implementations/Bukkit/BukkitServer.java | 5 + .../Folia/FoliaEventsListener.java | 89 +++++++++++++++ .../Implementations/Folia/FoliaItemStack.java | 6 + .../Folia/FoliaPlayerInventory.java | 6 + .../Implementations/Folia/FoliaServer.java | 6 + .../Implementations/Folia/FoliaWorld.java | 2 +- .../Interfaces/EsCancellableEvent.java | 13 +++ .../ServerApi/Interfaces/EsEntity.java | 1 + .../estools/ServerApi/Interfaces/EsEvent.java | 4 + .../Interfaces/EsPlayerInventory.java | 3 + .../Interfaces/EsServerSoftware.java | 1 + 29 files changed, 586 insertions(+), 120 deletions(-) create mode 100644 abstraction.md create mode 100644 src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsEventListener.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsTeleportCause.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsBlockPlaceEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsEntityDamageEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsPlayerDeathEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsPlayerTeleportEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsCancellableEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsEvent.java diff --git a/abstraction.md b/abstraction.md new file mode 100644 index 0000000..6fa75c4 --- /dev/null +++ b/abstraction.md @@ -0,0 +1,6 @@ +# TODO +- Events +- Config +- Fix testing +- Fix /safetp +- Fix weather commands \ No newline at end of file diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index 28f6ee5..5516f28 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -3,27 +3,26 @@ import java.util.HashMap; import java.util.UUID; -import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.EsTeleportCause; +import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; +import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityEvent; -import org.bukkit.event.entity.PlayerDeathEvent; import net.serble.estools.EsToolsCommand; -import org.bukkit.event.player.PlayerTeleportEvent; -public class Back extends EsToolsCommand implements Listener { +public class Back extends EsToolsCommand implements EsEventListener { private static final HashMap prevLocations = new HashMap<>(); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); } @Override @@ -43,25 +42,31 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - @EventHandler - public void onDeath(PlayerDeathEvent e) { - if (Main.minecraftVersion.getMinor() > 1) { - prevLocations.put(e.getEntity().getUniqueId(), BukkitHelper.fromBukkitLocation(e.getEntity().getLocation())); + @Override + public void executeEvent(EsEvent event) { + if (event instanceof EsPlayerDeathEvent) { + EsPlayerDeathEvent e = (EsPlayerDeathEvent) event; + + if (Main.minecraftVersion.getMinor() > 1) { + prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); + return; + } + + try { + EsPlayer p = (EsPlayer)EntityEvent.class.getMethod("getEntity").invoke(e); + prevLocations.put(p.getUniqueId(), p.getLocation()); + } catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } return; } - try { - EsPlayer p = (EsPlayer)EntityEvent.class.getMethod("getEntity").invoke(e); - prevLocations.put(p.getUniqueId(), p.getLocation()); - } catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - } - } + if (event instanceof EsPlayerTeleportEvent) { + EsPlayerTeleportEvent e = (EsPlayerTeleportEvent) event; - @EventHandler - public void onTeleport(PlayerTeleportEvent e) { - if (equalsOr(e.getCause(), PlayerTeleportEvent.TeleportCause.COMMAND, PlayerTeleportEvent.TeleportCause.PLUGIN)) { - prevLocations.put(e.getPlayer().getUniqueId(), BukkitHelper.fromBukkitLocation(e.getPlayer().getLocation())); + if (equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin)) { + prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); + } } } } diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 720a21b..0459676 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -4,26 +4,24 @@ import net.serble.estools.EntityCommand; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity; +import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.LivingEntity; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDamageEvent; import java.util.*; -public class Buddha extends EntityCommand implements Listener { +public class Buddha extends EntityCommand implements EsEventListener { private static final HashMap currentPlayers = new HashMap<>(); private static final String usage = genUsage("/buddha [entity] [time]"); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); FileConfiguration f = ConfigManager.load("gods.yml"); List buddhas = f.getStringList("buddhas"); @@ -93,43 +91,35 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - private double getDamageFromEvent(EntityDamageEvent e) { - if (Main.minecraftVersion.getMinor() > 5) { - return e.getDamage(); - } + private static void save() { + FileConfiguration f = new YamlConfiguration(); - try { - return (double)(int)EntityDamageEvent.class.getMethod("getDamage").invoke(e); - } catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - return 0d; + List buddhas = new ArrayList<>(); + for (Map.Entry kv : currentPlayers.entrySet()) { + // only save if there isn't a time limit! + if (kv.getValue() == -1) { + buddhas.add(kv.getKey().toString()); + } } + + f.set("buddhas", buddhas); + ConfigManager.save("gods.yml", f); } - private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SameParameterValue") double d) { - if (Main.minecraftVersion.getMinor() > 5) { - e.setDamage(d); + @Override + public void executeEvent(EsEvent event) { + if (!(event instanceof EsEntityDamageEvent)) { return; } + EsEntityDamageEvent e = (EsEntityDamageEvent) event; - try { - //noinspection JavaReflectionMemberAccess, It's an int in older versions - EntityDamageEvent.class.getMethod("setDamage", int.class).invoke(e, (int) d); - } catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - } - } - - @EventHandler - public void damage(EntityDamageEvent e) { - if (!(e.getEntity() instanceof LivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { + if (!(e.getEntity() instanceof EsLivingEntity) || !currentPlayers.containsKey(e.getEntity().getUniqueId())) { return; } - - EsLivingEntity entity = new FoliaLivingEntity((LivingEntity) e.getEntity()); + EsLivingEntity entity = (EsLivingEntity) e.getEntity(); // Get all our vars since Minecraft broke everything in 1.6 - double damage = getDamageFromEvent(e); + double damage = e.getDamage(); double health = entity.getHealth(); double maxHealth = entity.getMaxHealth(); @@ -141,22 +131,7 @@ public void damage(EntityDamageEvent e) { double extraDamage = damage - health; double resultingDamageTaken = extraDamage % maxHealth; - setDamageFromEvent(e, 0); + e.setDamage(0); entity.setHealth(maxHealth - resultingDamageTaken); } - - private static void save() { - FileConfiguration f = new YamlConfiguration(); - - List buddhas = new ArrayList<>(); - for (Map.Entry kv : currentPlayers.entrySet()) { - // only save if there isn't a time limit! - if (kv.getValue() == -1) { - buddhas.add(kv.getKey().toString()); - } - } - - f.set("buddhas", buddhas); - ConfigManager.save("gods.yml", f); - } } diff --git a/src/main/java/net/serble/estools/Commands/God.java b/src/main/java/net/serble/estools/Commands/God.java index 3aa9a5d..69310be 100644 --- a/src/main/java/net/serble/estools/Commands/God.java +++ b/src/main/java/net/serble/estools/Commands/God.java @@ -3,25 +3,28 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageEvent; import java.util.*; // TODO: Event command needs migrating -public class God extends EntityCommand implements Listener { +public class God extends EntityCommand implements EsEventListener { private static final HashMap currentPlayers = new HashMap<>(); private static final String usage = genUsage("/god [entity] [time]"); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); FileConfiguration f = ConfigManager.load("gods.yml"); List godList = f.getStringList("gods"); @@ -91,13 +94,6 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - @EventHandler - public void damage(EntityDamageEvent e) { - if (currentPlayers.containsKey(e.getEntity().getUniqueId())) { - e.setCancelled(true); - } - } - private static void save() { FileConfiguration f = new YamlConfiguration(); @@ -112,4 +108,16 @@ private static void save() { f.set("gods", gods); ConfigManager.save("gods.yml", f); } + + @Override + public void executeEvent(EsEvent event) { + if (!(event instanceof EsEntityDamageEvent)) { + return; + } + EsEntityDamageEvent e = (EsEntityDamageEvent) event; + + if (currentPlayers.containsKey(e.getEntity().getUniqueId())) { + e.setCancelled(true); + } + } } diff --git a/src/main/java/net/serble/estools/Commands/Infinite.java b/src/main/java/net/serble/estools/Commands/Infinite.java index e1b308d..220dc30 100644 --- a/src/main/java/net/serble/estools/Commands/Infinite.java +++ b/src/main/java/net/serble/estools/Commands/Infinite.java @@ -1,28 +1,24 @@ package net.serble.estools.Commands; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; +import net.serble.estools.ServerApi.EsEquipmentSlot; +import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.Events.EsBlockPlaceEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPlaceEvent; import java.util.ArrayList; import java.util.UUID; -// TODO: Events command, need migrating -public class Infinite extends EsToolsCommand implements Listener { +public class Infinite extends EsToolsCommand implements EsEventListener { private static final ArrayList currentPlayers = new ArrayList<>(); @Override public void onEnable() { - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); } @Override @@ -44,20 +40,20 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - @EventHandler - public void blockPlace(BlockPlaceEvent e) { + @Override + public void executeEvent(EsEvent event) { + if (!(event instanceof EsBlockPlaceEvent)) { + return; + } + EsBlockPlaceEvent e = (EsBlockPlaceEvent) event; + if (!currentPlayers.contains(e.getPlayer().getUniqueId())) { return; } - // TODO: Migrate - EsItemStack item = new FoliaItemStack(e.getItemInHand().clone()); - EsPlayer p = new FoliaPlayer(e.getPlayer()); + EsItemStack item = e.getPlacedItem().clone(); + EsEquipmentSlot slot = e.getHand(); - // TODO: This is a terrible way of implementing this btw - Main.server.runTask(() -> { - p.setMainHand(item); - p.updateInventory(); // A bug makes this necessary - }); + Main.server.runTask(() -> e.getPlayer().getInventory().setItem(slot, item)); } } diff --git a/src/main/java/net/serble/estools/Commands/SafeTp.java b/src/main/java/net/serble/estools/Commands/SafeTp.java index aaf3c21..c8e8c42 100644 --- a/src/main/java/net/serble/estools/Commands/SafeTp.java +++ b/src/main/java/net/serble/estools/Commands/SafeTp.java @@ -3,20 +3,19 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.EsTeleportCause; +import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerTeleportEvent; +import net.serble.estools.ServerApi.Interfaces.EsEvent; -// TODO: Events command, needs migrating -public class SafeTp extends EsToolsCommand implements Listener { +public class SafeTp extends EsToolsCommand implements EsEventListener { public static boolean enabled = true; @Override public void onEnable() { enabled = Main.plugin.getConfig().getBoolean("safetp", true); - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); } @Override @@ -34,10 +33,14 @@ public boolean execute(EsCommandSender sender, String[] args) { return true; } - @EventHandler - public void teleport(final PlayerTeleportEvent e) { - if (enabled && equalsOr(e.getCause(), PlayerTeleportEvent.TeleportCause.COMMAND, - PlayerTeleportEvent.TeleportCause.PLUGIN, PlayerTeleportEvent.TeleportCause.UNKNOWN)) { + @Override + public void executeEvent(EsEvent event) { + if (!(event instanceof EsPlayerTeleportEvent)) { + return; + } + EsPlayerTeleportEvent e = (EsPlayerTeleportEvent) event; + + if (enabled && equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin, EsTeleportCause.Unknown)) { e.getPlayer().setFallDistance(0); } } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 2452dce..71d30b3 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -9,7 +9,9 @@ import net.serble.estools.Commands.Teleport.*; import net.serble.estools.Commands.Warps.*; import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; import net.serble.estools.ServerApi.ServerPlatform; @@ -25,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; @SuppressWarnings("UnusedReturnValue") public class Main { @@ -38,6 +42,7 @@ public class Main { public static EsServerSoftware server; public static EsLogger logger; private final Object context; + private static final List eventListeners = new ArrayList<>(); private static final int bStatsId = 21760; @@ -170,6 +175,18 @@ public void enable() { Give.enable(); Updater.checkForUpdate(); + + server.startEvents(); // Events will now trigger + } + + public static void callEvent(EsEvent event) { + for (EsEventListener listener : eventListeners) { + listener.executeEvent(event); + } + } + + public static void registerEvents(EsEventListener listener) { + eventListeners.add(listener); } // Setup Command Overloads diff --git a/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java b/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java new file mode 100644 index 0000000..7b5271c --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java @@ -0,0 +1,10 @@ +package net.serble.estools.ServerApi; + +public enum EsEquipmentSlot { + Hand, + OffHand, + Feet, + legs, + Chest, + Head +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsEventListener.java b/src/main/java/net/serble/estools/ServerApi/EsEventListener.java new file mode 100644 index 0000000..7cb0793 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsEventListener.java @@ -0,0 +1,7 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.ServerApi.Interfaces.EsEvent; + +public interface EsEventListener { + void executeEvent(EsEvent event); +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsTeleportCause.java b/src/main/java/net/serble/estools/ServerApi/EsTeleportCause.java new file mode 100644 index 0000000..875268b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsTeleportCause.java @@ -0,0 +1,15 @@ +package net.serble.estools.ServerApi; + +public enum EsTeleportCause { + EnderPearl, + Command, + Plugin, + NetherPortal, + EndPortal, + Spectate, + EndGateway, + ChorusFruit, + Dismount, + ExitBed, + Unknown +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsBlockPlaceEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsBlockPlaceEvent.java new file mode 100644 index 0000000..0a35056 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsBlockPlaceEvent.java @@ -0,0 +1,30 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.EsEquipmentSlot; +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsBlockPlaceEvent extends EsCancellableEvent { + private final EsPlayer player; + private final EsItemStack placedItem; + private final EsEquipmentSlot hand; + + public EsBlockPlaceEvent(EsPlayer p, EsItemStack item, EsEquipmentSlot ha) { + player = p; + placedItem = item; + hand = ha; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsItemStack getPlacedItem() { + return placedItem; + } + + public EsEquipmentSlot getHand() { + return hand; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsEntityDamageEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsEntityDamageEvent.java new file mode 100644 index 0000000..0db45e9 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsEntityDamageEvent.java @@ -0,0 +1,26 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsEntity; + +public class EsEntityDamageEvent extends EsCancellableEvent { + private final EsEntity entity; + private double damage; + + public EsEntityDamageEvent(EsEntity entity, double dmg) { + this.entity = entity; + damage = dmg; + } + + public EsEntity getEntity() { + return entity; + } + + public double getDamage() { + return damage; + } + + public void setDamage(double damage) { + this.damage = damage; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerDeathEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerDeathEvent.java new file mode 100644 index 0000000..6dd6b09 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerDeathEvent.java @@ -0,0 +1,16 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsPlayerDeathEvent implements EsEvent { + private final EsPlayer player; + + public EsPlayerDeathEvent(EsPlayer p) { + player = p; + } + + public EsPlayer getPlayer() { + return player; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerTeleportEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerTeleportEvent.java new file mode 100644 index 0000000..7fe9bcb --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerTeleportEvent.java @@ -0,0 +1,26 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsTeleportCause; +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsPlayerTeleportEvent extends EsCancellableEvent { + private final EsPlayer player; + private final EsTeleportCause cause; + private final EsLocation to; + + public EsPlayerTeleportEvent(EsPlayer p, EsTeleportCause c, EsLocation to) { + player = p; + cause = c; + this.to = to; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsTeleportCause getCause() { + return cause; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 57b16e2..2c67473 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -13,8 +13,6 @@ import java.util.Set; import java.util.UUID; -import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.toBukkitLocation; - public class BukkitEntity implements EsEntity { private final org.bukkit.entity.Entity bukkitEntity; @@ -131,4 +129,9 @@ public void addPassenger(EsEntity entity) { bukkitEntity.setPassenger(((BukkitEntity) entity).getBukkitEntity()); } } + + @Override + public void setFallDistance(float dis) { + bukkitEntity.setFallDistance(dis); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java new file mode 100644 index 0000000..cbc1e2b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -0,0 +1,88 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Events.EsBlockPlaceEvent; +import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; +import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; +import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityEvent; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import java.lang.reflect.InvocationTargetException; +import java.util.Objects; + +public class BukkitEventsListener implements Listener { + + private double getDamageFromEvent(EntityDamageEvent e) { + if (Main.minecraftVersion.getMinor() > 5) { + return e.getDamage(); + } + + try { + return (double)(int)EntityDamageEvent.class.getMethod("getDamage").invoke(e); + } catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + return 0d; + } + } + + private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SameParameterValue") double d) { + if (Main.minecraftVersion.getMinor() > 5) { + e.setDamage(d); + return; + } + + try { + //noinspection JavaReflectionMemberAccess, It's an int in older versions + EntityDamageEvent.class.getMethod("setDamage", int.class).invoke(e, (int) d); + } catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent e) { + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), new BukkitItemStack(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onTeleport(PlayerTeleportEvent e) { + EsPlayerTeleportEvent ee = new EsPlayerTeleportEvent( + new BukkitPlayer(e.getPlayer()), + BukkitHelper.fromBukkitTeleportCause(e.getCause()), + BukkitHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onPlayerDeath(PlayerDeathEvent e) { + try { + // In old versions getEntity() isn't in PlayerDeathEvent it is in EntityDeathEvent which + // returns a LivingEntity, so we have to use reflection to get it and then cast it to a Player + Player p = (Player) EntityEvent.class.getMethod("getEntity").invoke(e); + + EsPlayerDeathEvent ee = new EsPlayerDeathEvent(new BukkitPlayer(p)); + Main.callEvent(ee); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { + Main.logger.severe(ex.toString()); + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent e) { + EsEntityDamageEvent ee = new EsEntityDamageEvent(BukkitHelper.fromBukkitEntity(e.getEntity()), getDamageFromEvent(e)); + Main.callEvent(ee); + setDamageFromEvent(e, ee.getDamage()); + e.setCancelled(ee.isCancelled()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 4ce9c60..ad09dcb 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -1,11 +1,8 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.*; import net.serble.estools.Main; -import net.serble.estools.ServerApi.EsPersistentDataType; -import net.serble.estools.ServerApi.Position; -import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; @@ -18,6 +15,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.persistence.PersistentDataType; import org.bukkit.util.Vector; @@ -135,6 +134,64 @@ public static EsGameMode fromBukkitGameMode(GameMode mode) { throw new RuntimeException("Invalid GameMode"); } + public static PlayerTeleportEvent.TeleportCause toBukkitTeleportCause(EsTeleportCause esCause) { + switch (esCause) { + case EnderPearl: + return PlayerTeleportEvent.TeleportCause.ENDER_PEARL; + case Command: + return PlayerTeleportEvent.TeleportCause.COMMAND; + case Plugin: + return PlayerTeleportEvent.TeleportCause.PLUGIN; + case NetherPortal: + return PlayerTeleportEvent.TeleportCause.NETHER_PORTAL; + case EndPortal: + return PlayerTeleportEvent.TeleportCause.END_PORTAL; + case Spectate: + return PlayerTeleportEvent.TeleportCause.SPECTATE; + case EndGateway: + return PlayerTeleportEvent.TeleportCause.END_GATEWAY; + case ChorusFruit: + return PlayerTeleportEvent.TeleportCause.CHORUS_FRUIT; + case Dismount: + return PlayerTeleportEvent.TeleportCause.DISMOUNT; + case ExitBed: + return PlayerTeleportEvent.TeleportCause.EXIT_BED; + case Unknown: + return PlayerTeleportEvent.TeleportCause.UNKNOWN; + default: + throw new IllegalArgumentException("Unknown EsTeleportCause: " + esCause); + } + } + + public static EsTeleportCause fromBukkitTeleportCause(PlayerTeleportEvent.TeleportCause bukkitCause) { + switch (bukkitCause) { + case ENDER_PEARL: + return EsTeleportCause.EnderPearl; + case COMMAND: + return EsTeleportCause.Command; + case PLUGIN: + return EsTeleportCause.Plugin; + case NETHER_PORTAL: + return EsTeleportCause.NetherPortal; + case END_PORTAL: + return EsTeleportCause.EndPortal; + case SPECTATE: + return EsTeleportCause.Spectate; + case END_GATEWAY: + return EsTeleportCause.EndGateway; + case CHORUS_FRUIT: + return EsTeleportCause.ChorusFruit; + case DISMOUNT: + return EsTeleportCause.Dismount; + case EXIT_BED: + return EsTeleportCause.ExitBed; + case UNKNOWN: + return EsTeleportCause.Unknown; + default: + throw new IllegalArgumentException("Invalid TeleportCause"); + } + } + public static EsBlock fromBukkitBlock(Block block) { BlockState state = block.getState(); @@ -225,4 +282,42 @@ public static boolean isFolia() { return false; } } + + public static EquipmentSlot toBukkitEquipmentSlot(EsEquipmentSlot slot) { + switch (slot) { + case Feet: + return EquipmentSlot.FEET; + case Hand: + return EquipmentSlot.HAND; + case Head: + return EquipmentSlot.HEAD; + case legs: + return EquipmentSlot.LEGS; + case Chest: + return EquipmentSlot.CHEST; + case OffHand: + return EquipmentSlot.OFF_HAND; + } + + throw new RuntimeException("idfk"); + } + + public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { + switch (slot) { + case FEET: + return EsEquipmentSlot.Feet; + case HAND: + return EsEquipmentSlot.Hand; + case HEAD: + return EsEquipmentSlot.Head; + case LEGS: + return EsEquipmentSlot.legs; + case CHEST: + return EsEquipmentSlot.Chest; + case OFF_HAND: + return EsEquipmentSlot.OffHand; + default: + throw new IllegalArgumentException("Invalid EquipmentSlot"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index ba0cab9..28290ff 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; import org.bukkit.inventory.PlayerInventory; @@ -46,4 +47,9 @@ public EsItemStack getChestplate() { public EsItemStack getBoots() { return new BukkitItemStack(bukkitInv.getBoots()); } + + @Override + public void setItem(EsEquipmentSlot slot, EsItemStack item) { + bukkitInv.setItem(BukkitHelper.toBukkitEquipmentSlot(slot), ((BukkitItemStack) item).getBukkitItem()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 38674ce..c5dafee 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -254,4 +254,9 @@ public void runTask(Runnable task) { public EsLogger getLogger() { return new BukkitLogger(); } + + @Override + public void startEvents() { + Bukkit.getPluginManager().registerEvents(new BukkitEventsListener(), EsToolsBukkit.plugin); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java new file mode 100644 index 0000000..116454e --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -0,0 +1,89 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Events.EsBlockPlaceEvent; +import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; +import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; +import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityEvent; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import java.lang.reflect.InvocationTargetException; +import java.util.Objects; + +public class FoliaEventsListener implements Listener { + + private double getDamageFromEvent(EntityDamageEvent e) { + if (Main.minecraftVersion.getMinor() > 5) { + return e.getDamage(); + } + + try { + return (double)(int)EntityDamageEvent.class.getMethod("getDamage").invoke(e); + } catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + return 0d; + } + } + + private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SameParameterValue") double d) { + if (Main.minecraftVersion.getMinor() > 5) { + e.setDamage(d); + return; + } + + try { + //noinspection JavaReflectionMemberAccess, It's an int in older versions + EntityDamageEvent.class.getMethod("setDamage", int.class).invoke(e, (int) d); + } catch (Exception ex) { + Bukkit.getLogger().severe(ex.toString()); + } + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent e) { + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), new FoliaItemStack(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onTeleport(PlayerTeleportEvent e) { + EsPlayerTeleportEvent ee = new EsPlayerTeleportEvent( + new FoliaPlayer(e.getPlayer()), + FoliaHelper.fromBukkitTeleportCause(e.getCause()), + FoliaHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onPlayerDeath(PlayerDeathEvent e) { + try { + // In old versions getEntity() isn't in PlayerDeathEvent it is in EntityDeathEvent which + // returns a LivingEntity, so we have to use reflection to get it and then cast it to a Player + Player p = (Player) EntityEvent.class.getMethod("getEntity").invoke(e); + + EsPlayerDeathEvent ee = new EsPlayerDeathEvent(new FoliaPlayer(p)); + Main.callEvent(ee); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { + Main.logger.severe(ex.toString()); + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent e) { + EsEntityDamageEvent ee = new EsEntityDamageEvent(FoliaHelper.fromBukkitEntity(e.getEntity()), getDamageFromEvent(e)); + Main.callEvent(ee); + setDamageFromEvent(e, ee.getDamage()); + e.setCancelled(ee.isCancelled()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 3bd9905..6aaaa9d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -2,6 +2,7 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; public class FoliaItemStack extends BukkitItemStack { @@ -21,4 +22,9 @@ public FoliaItemStack(org.bukkit.inventory.ItemStack child) { public EsItemMeta getItemMeta() { return new FoliaItemMeta(bukkitItem.getItemMeta()); } + + @Override + public EsItemStack clone() { + return new FoliaItemStack(bukkitItem.clone()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java index 5e32d0a..8fc28c3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Folia; +import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; import org.bukkit.inventory.PlayerInventory; @@ -46,4 +47,9 @@ public EsItemStack getChestplate() { public EsItemStack getBoots() { return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getBoots()); } + + @Override + public void setItem(EsEquipmentSlot slot, EsItemStack item) { + bukkitInv.setItem(FoliaHelper.toBukkitEquipmentSlot(slot), ((FoliaItemStack) item).getBukkitItem()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 96f5cea..e620c33 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.Effects; +import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; @@ -253,4 +254,9 @@ public void runTask(Runnable task) { public EsLogger getLogger() { return new FoliaLogger(); } + + @Override + public void startEvents() { + Bukkit.getPluginManager().registerEvents(new FoliaEventsListener(), EsToolsBukkit.plugin); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java index fcc675d..8db17c6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java @@ -45,7 +45,7 @@ public void setTime(long time) { @Override public void setStorming(boolean val) { - FoliaHelper.runTaskOnNextTick(() -> bukkitWorld.setStorm(false)); + FoliaHelper.runTaskOnNextTick(() -> bukkitWorld.setStorm(val)); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCancellableEvent.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCancellableEvent.java new file mode 100644 index 0000000..f2c6003 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsCancellableEvent.java @@ -0,0 +1,13 @@ +package net.serble.estools.ServerApi.Interfaces; + +public abstract class EsCancellableEvent implements EsEvent { + private boolean cancelled = false; + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java index a1e3fec..3cc5b6d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEntity.java @@ -19,4 +19,5 @@ public interface EsEntity extends EsCommandSender { boolean hasScoreboardTag(String tag); void setOnFireTicks(int ticks); void addPassenger(EsEntity entity); + void setFallDistance(float dis); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEvent.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEvent.java new file mode 100644 index 0000000..f6f70eb --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsEvent.java @@ -0,0 +1,4 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsEvent { +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java index 203a926..8d6f95e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayerInventory.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.ServerApi.EsEquipmentSlot; + public interface EsPlayerInventory extends EsInventory { EsItemStack getOffHand(); EsItemStack getMainHand(); @@ -7,4 +9,5 @@ public interface EsPlayerInventory extends EsInventory { EsItemStack getLeggings(); EsItemStack getChestplate(); EsItemStack getBoots(); + void setItem(EsEquipmentSlot slot, EsItemStack item); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index dd6e68a..2231952 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -27,4 +27,5 @@ public interface EsServerSoftware { void runTaskLater(Runnable task, long ticks); void runTask(Runnable task); EsLogger getLogger(); + void startEvents(); } From 86167a597d01f51551d59b7586d2fb70380a3f96 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 26 May 2024 13:35:44 +1000 Subject: [PATCH 11/74] Implement all events --- .../net/serble/estools/Commands/Back.java | 10 +- .../net/serble/estools/Commands/Buddha.java | 6 +- .../net/serble/estools/Commands/CChest.java | 145 ++++++++-------- .../serble/estools/Commands/Give/Give.java | 1 + .../java/net/serble/estools/Commands/God.java | 8 +- .../serble/estools/ServerApi/EsClickType.java | 129 ++++++++++++++ .../estools/ServerApi/EsInventoryAction.java | 88 ++++++++++ .../Events/EsInventoryClickEvent.java | 62 +++++++ .../Events/EsInventoryCloseEvent.java | 23 +++ .../Events/EsInventoryDragEvent.java | 38 ++++ .../ServerApi/Events/EsPlayerKickEvent.java | 16 ++ .../ServerApi/Events/EsPlayerQuitEvent.java | 16 ++ .../Bukkit/BukkitEventsListener.java | 60 ++++++- .../Implementations/Bukkit/BukkitHelper.java | 162 ++++++++++++++++++ .../Bukkit/BukkitInventory.java | 20 +++ .../Bukkit/BukkitInventoryView.java | 35 ++++ .../Bukkit/BukkitItemStack.java | 13 +- .../Implementations/Bukkit/BukkitServer.java | 11 +- .../Folia/FoliaEventsListener.java | 62 ++++++- .../Implementations/Folia/FoliaHelper.java | 5 +- .../Implementations/Folia/FoliaInventory.java | 23 +++ .../Folia/FoliaInventoryView.java | 36 ++++ .../Implementations/Folia/FoliaItemStack.java | 5 + .../Implementations/Folia/FoliaServer.java | 27 ++- .../ServerApi/Interfaces/EsInventory.java | 14 ++ .../ServerApi/Interfaces/EsInventoryView.java | 8 + .../ServerApi/Interfaces/EsItemStack.java | 2 + .../Interfaces/EsServerSoftware.java | 3 +- 28 files changed, 921 insertions(+), 107 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsClickType.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsInventoryAction.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsInventoryClickEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsInventoryCloseEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsInventoryDragEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsPlayerKickEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsPlayerQuitEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventoryView.java diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index 5516f28..cd8aca9 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -12,8 +12,6 @@ import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; -import org.bukkit.event.entity.EntityEvent; import net.serble.estools.EsToolsCommand; @@ -52,12 +50,8 @@ public void executeEvent(EsEvent event) { return; } - try { - EsPlayer p = (EsPlayer)EntityEvent.class.getMethod("getEntity").invoke(e); - prevLocations.put(p.getUniqueId(), p.getLocation()); - } catch (Exception ex) { - Bukkit.getLogger().severe(ex.toString()); - } + EsPlayer p = e.getPlayer(); + prevLocations.put(p.getUniqueId(), p.getLocation()); return; } diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 0459676..0dea2af 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -2,14 +2,12 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; -import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -65,7 +63,7 @@ public boolean execute(EsCommandSender sender, String[] args) { if (taskId == -1) { save(); } else { - Bukkit.getScheduler().cancelTask(taskId); + Main.server.cancelTask(taskId); } send(sender, "&aBuddha mode &6disabled&a for &6%s", p.getName()); @@ -77,7 +75,7 @@ public boolean execute(EsCommandSender sender, String[] args) { if (timer >= 0) { timerStr = timer / 20d + " seconds"; - taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, () -> currentPlayers.remove(uid), timer); + taskId = Main.server.runTaskLater(() -> currentPlayers.remove(uid), timer); } currentPlayers.put(uid, taskId); diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 1ceab2a..110fd91 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -1,39 +1,28 @@ package net.serble.estools.Commands; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.ConfigManager; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaInventory; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; -import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import net.serble.estools.ServerApi.Interfaces.EsInventory; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; +import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.*; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; import java.util.*; -// TODO: This class has config and events -public class CChest extends EsToolsCommand implements Listener { +// TODO: This class has config +public class CChest extends EsToolsCommand implements EsEventListener { public static HashMap cChests = new HashMap<>(); @Override public void onEnable() { if (Main.minecraftVersion.getMinor() > 4) { - Bukkit.getServer().getPluginManager().registerEvents(this, EsToolsBukkit.plugin); + Main.registerEvents(this); } } @@ -77,28 +66,27 @@ public boolean execute(EsCommandSender sender, String[] args) { cChests.put(uid, inv); return true; } - - @EventHandler - public void inventoryClick(final InventoryClickEvent e) { - UUID uid = e.getWhoClicked().getUniqueId(); - ItemStack currentItem = e.getCurrentItem(); - + + private void onInventoryClick(final EsInventoryClickEvent e) { + UUID uid = e.getPlayer().getUniqueId(); + EsItemStack currentItem = e.getCurrentItem(); + // Check if inventory is cChest - if (!cChests.containsKey(uid) || !isSameInv(e.getInventory(), cChests.get(uid)) || e.getClickedInventory() == null) { + if (!cChests.containsKey(uid) || !e.getInventory().isEqualTo(cChests.get(uid)) || e.getClickedInventory() == null) { return; } - if (e.getAction().equals(InventoryAction.COLLECT_TO_CURSOR)) { + if (e.getAction().equals(EsInventoryAction.CollectToCursor)) { e.setCancelled(true); - ItemStack cursor = e.getCursor(); + EsItemStack cursor = e.getCursor(); if (cursor == null) { return; } - org.bukkit.inventory.Inventory pInv = e.getWhoClicked().getInventory(); - for (Map.Entry i : pInv.all(cursor.getType()).entrySet()) { - ItemStack item = i.getValue(); + EsInventory pInv = e.getPlayer().getInventory(); + for (Map.Entry i : pInv.all(cursor.getType()).entrySet()) { + EsItemStack item = i.getValue(); if (item == null) { continue; @@ -119,9 +107,9 @@ public void inventoryClick(final InventoryClickEvent e) { } // If player inventory - if (!e.getClickedInventory().equals(((FoliaInventory)cChests.get(uid)).getBukkitInventory())) { // TODO: Reliance + if (!e.getClickedInventory().isEqualTo(cChests.get(uid))) { // Shift click into cChest - if (equalsOr(e.getClick(), ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT) && currentItem != null) { + if (equalsOr(e.getClick(), EsClickType.ShiftLeft, EsClickType.ShiftRight) && currentItem != null) { e.setCancelled(true); e.getInventory().addItem(currentItem.clone()); @@ -131,23 +119,23 @@ public void inventoryClick(final InventoryClickEvent e) { } // Right click in cChest (clear item) - if (e.getClick().equals(ClickType.RIGHT) && currentItem != null) { + if (e.getClick().equals(EsClickType.Right) && currentItem != null) { e.setCancelled(true); e.getClickedInventory().setItem(e.getSlot(), null); return; } // Add to chest - if (equalsOr(e.getAction(), InventoryAction.PLACE_ALL, InventoryAction.PLACE_ONE, InventoryAction.PLACE_SOME)) { + if (equalsOr(e.getAction(), EsInventoryAction.PlaceAll, EsInventoryAction.PlaceOne, EsInventoryAction.PlaceSome)) { e.setCancelled(true); - ItemStack item = e.getCursor(); + EsItemStack item = e.getCursor(); if (item == null) { return; } - ItemStack finalItem = item.clone(); + EsItemStack finalItem = item.clone(); - if (equalsOr(e.getAction(), InventoryAction.PLACE_ONE, InventoryAction.PLACE_SOME)) { + if (equalsOr(e.getAction(), EsInventoryAction.PlaceOne, EsInventoryAction.PlaceSome)) { finalItem.setAmount(1); } @@ -156,39 +144,27 @@ public void inventoryClick(final InventoryClickEvent e) { } // Remove from cChest - if (equalsOr(e.getClick(), ClickType.LEFT, ClickType.DROP, ClickType.CONTROL_DROP, - ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT) && currentItem != null) { - ItemStack item = currentItem.clone(); + if (equalsOr(e.getClick(), EsClickType.Left, EsClickType.Drop, EsClickType.ControlDrop, + EsClickType.ShiftLeft, EsClickType.ShiftRight) && currentItem != null) { + EsItemStack item = currentItem.clone(); setItemTask(e.getInventory(), e.getSlot(), item); } } - private static void setItemTask(org.bukkit.inventory.Inventory inv, int slot, ItemStack item) { - Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, () -> inv.setItem(slot, item)); - } - - // TODO: Remove reliance on Bukkit - private static boolean isSameInv(Inventory a, EsInventory b) { - if (a == null && b == null) { - return true; - } - if (a == null || b == null) { - return false; - } - return a.equals(((FoliaInventory) b).getBukkitInventory()); + private static void setItemTask(EsInventory inv, int slot, EsItemStack item) { + Main.server.runTask(() -> inv.setItem(slot, item)); } // Cancel drag if inside cChest - @EventHandler - public void onInventoryDrag(final InventoryDragEvent e) { - EsInventory inv = cChests.get(e.getWhoClicked().getUniqueId()); - if (!isSameInv(e.getInventory(), inv)) { + private void onInventoryDrag(final EsInventoryDragEvent e) { + EsInventory inv = cChests.get(e.getPlayer().getUniqueId()); + if (!e.getInventory().isEqualTo(inv)) { return; } // Check if any of the slots dragged are in the cChest - for (int slot : e.getRawSlots()) { - if (isSameInv(Objects.requireNonNull(e.getView().getInventory(slot)), inv)) { + for (int slot : e.getChangedSlots()) { + if (e.getView().getInventory(slot).isEqualTo(inv)) { e.setCancelled(true); return; } @@ -213,7 +189,8 @@ public static EsInventory loadPlayer(EsPlayer p) { return inv; } - + + // TODO: Config public static void savePlayer(EsPlayer p) { UUID uid = p.getUniqueId(); @@ -226,24 +203,48 @@ public static void savePlayer(EsPlayer p) { ConfigManager.save("cchests/" + uid + ".yml", f); } - @EventHandler - public void onClose(InventoryCloseEvent e) { + private void onClose(EsInventoryCloseEvent e) { UUID uid = e.getPlayer().getUniqueId(); - if (isSameInv(e.getInventory(), cChests.get(uid))) { - savePlayer(new FoliaPlayer((Player) e.getPlayer())); // TODO: Fix + if (e.getInventory().isEqualTo(cChests.get(uid))) { + savePlayer(e.getPlayer()); } } - @EventHandler - public void onQuit(PlayerQuitEvent e) { - savePlayer(new FoliaPlayer(e.getPlayer())); + private void onQuit(EsPlayerQuitEvent e) { + savePlayer(e.getPlayer()); cChests.remove(e.getPlayer().getUniqueId()); } - - @EventHandler - public void onKick(PlayerKickEvent e) { - savePlayer(new FoliaPlayer(e.getPlayer())); + + private void onKick(EsPlayerKickEvent e) { + savePlayer(e.getPlayer()); cChests.remove(e.getPlayer().getUniqueId()); } + + @Override + public void executeEvent(EsEvent event) { + if (event instanceof EsPlayerQuitEvent) { + onQuit((EsPlayerQuitEvent) event); + return; + } + + if (event instanceof EsPlayerKickEvent) { + onKick((EsPlayerKickEvent) event); + return; + } + + if (event instanceof EsInventoryClickEvent) { + onInventoryClick((EsInventoryClickEvent) event); + return; + } + + if (event instanceof EsInventoryCloseEvent) { + onClose((EsInventoryCloseEvent) event); + return; + } + + if (event instanceof EsInventoryDragEvent) { + onInventoryDrag((EsInventoryDragEvent) event); + } + } } diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index bae3f8b..ccab7a2 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -15,6 +15,7 @@ import java.util.*; import java.util.Map.Entry; +// TODO: Config class public class Give implements EsToolsTabCompleter { private static HashMap materialNames; diff --git a/src/main/java/net/serble/estools/Commands/God.java b/src/main/java/net/serble/estools/Commands/God.java index 69310be..92ea072 100644 --- a/src/main/java/net/serble/estools/Commands/God.java +++ b/src/main/java/net/serble/estools/Commands/God.java @@ -2,18 +2,14 @@ import net.serble.estools.ConfigManager; import net.serble.estools.EntityCommand; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; -import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.event.EventHandler; -import org.bukkit.event.entity.EntityDamageEvent; import java.util.*; @@ -68,7 +64,7 @@ public boolean execute(EsCommandSender sender, String[] args) { if (taskId == -1) { save(); } else { - Bukkit.getScheduler().cancelTask(taskId); + Main.server.cancelTask(taskId); } send(sender, "&aGod mode &6disabled&a for &6%s", p.getName()); @@ -80,7 +76,7 @@ public boolean execute(EsCommandSender sender, String[] args) { if (timer >= 0) { timerStr = timer / 20d + " seconds"; - taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, () -> currentPlayers.remove(uid), timer); + taskId = Main.server.runTaskLater(() -> currentPlayers.remove(uid), timer); } currentPlayers.put(uid, taskId); diff --git a/src/main/java/net/serble/estools/ServerApi/EsClickType.java b/src/main/java/net/serble/estools/ServerApi/EsClickType.java new file mode 100644 index 0000000..6f048d7 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsClickType.java @@ -0,0 +1,129 @@ +package net.serble.estools.ServerApi; + +import org.jetbrains.annotations.ApiStatus; + +/** + * How the action was triggered, not what it did, see {@link net.serble.estools.ServerApi.EsInventoryAction} + * for the action. + */ +public enum EsClickType { + + /** + * The left (or primary) mouse button. + */ + Left, + /** + * Holding shift while pressing the left mouse button. + */ + ShiftLeft, + /** + * The right mouse button. + */ + Right, + /** + * Holding shift while pressing the right mouse button. + */ + ShiftRight, + /** + * Clicking the left mouse button on the grey area around the inventory. + */ + WindowBorderLeft, + /** + * Clicking the right mouse button on the grey area around the inventory. + */ + WindowBorderRight, + /** + * The middle mouse button, or a "scrollwheel click". + */ + Middle, + /** + * One of the number keys 1-9, correspond to slots on the hotbar. + */ + NumberKey, + /** + * Pressing the left mouse button twice in quick succession. + */ + DoubleClick, + /** + * The "Drop" key (defaults to Q). + */ + Drop, + /** + * Holding Ctrl while pressing the "Drop" key (defaults to Q). + */ + ControlDrop, + /** + * Any action done with the Creative inventory open. + */ + Creative, + /** + * The "swap item with offhand" key (defaults to F). + */ + SwapOffhand, + /** + * Should only be called if the implementation is not updated for a Minecraft version. + * Getting this value is a sign of a bug. + */ + @ApiStatus.Experimental + Unknown, + ; + + /** + * Gets whether this ClickType represents the pressing of a key on a + * keyboard. + * + * @return true if this ClickType represents the pressing of a key + */ + public boolean isKeyboardClick() { + return (this == EsClickType.NumberKey) || (this == EsClickType.Drop) || (this == EsClickType.ControlDrop) || (this == EsClickType.SwapOffhand); + } + + /** + * Gets whether this ClickType represents the pressing of a mouse button + * + * @return true if this ClickType represents the pressing of a mouse button + */ + public boolean isMouseClick() { + return (this == EsClickType.DoubleClick) || (this == EsClickType.Left) || (this == EsClickType.Right) || (this == EsClickType.Middle) + || (this == EsClickType.WindowBorderLeft) || (this == EsClickType.ShiftLeft) || (this == EsClickType.ShiftRight) || (this == EsClickType.WindowBorderRight); + } + + /** + * Gets whether this ClickType represents an action that can only be + * performed by a Player in creative mode. + * + * @return true if this action requires Creative mode + */ + public boolean isCreativeAction() { + // Why use middle click? + return (this == EsClickType.Middle) || (this == EsClickType.Creative); + } + + /** + * Gets whether this ClickType represents a right click. + * + * @return true if this ClickType represents a right click + */ + public boolean isRightClick() { + return (this == EsClickType.Right) || (this == EsClickType.ShiftRight); + } + + /** + * Gets whether this ClickType represents a left click. + * + * @return true if this ClickType represents a left click + */ + public boolean isLeftClick() { + return (this == EsClickType.Left) || (this == EsClickType.ShiftLeft) || (this == EsClickType.DoubleClick) || (this == EsClickType.Creative); + } + + /** + * Gets whether this ClickType indicates that the shift key was pressed + * down when the click was made. + * + * @return true if the action uses Shift. + */ + public boolean isShiftClick() { + return (this == EsClickType.ShiftLeft) || (this == EsClickType.ShiftRight); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsInventoryAction.java b/src/main/java/net/serble/estools/ServerApi/EsInventoryAction.java new file mode 100644 index 0000000..7450df0 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsInventoryAction.java @@ -0,0 +1,88 @@ +package net.serble.estools.ServerApi; + +/** + * The action that was triggered by the player's input + */ +public enum EsInventoryAction { + /** + * Nothing happened, I'm not sure why you would ever call the event for this, but it's here. + */ + Nothing, + /** + * All the items on the clicked slot are moved to the cursor. + */ + PickupAll, + /** + * Some of the items on the clicked slot are moved to the cursor. + */ + PickupSome, + /** + * Half of the items on the clicked slot are moved to the cursor. + */ + PickupHalf, + /** + * One of the items on the clicked slot are moved to the cursor. + */ + PickupOne, + /** + * All the items on the cursor are moved to the clicked slot. + */ + PlaceAll, + /** + * Some of the items from the cursor are moved to the clicked slot + * (usually up to the max stack size). + */ + PlaceSome, + /** + * A single item from the cursor is moved to the clicked slot. + */ + PlaceOne, + /** + * The clicked item and the cursor are exchanged. + */ + SwapWithCursor, + /** + * The entire cursor item is dropped. + */ + DropAllCursor, + /** + * One item is dropped from the cursor. + */ + DropOneCursor, + /** + * The entire clicked slot is dropped. + */ + DropAllSlot, + /** + * One item is dropped from the clicked slot. + */ + DropOneSlot, + /** + * The item is moved to the opposite inventory if a space is found. + */ + MoveToOtherInventory, + /** + * The clicked item is moved to the hotbar, and the item currently there + * is re-added to the player's inventory. + * The hotbar includes the player's offhand. + */ + HotbarMoveAndReadd, + /** + * The clicked slot and the picked hotbar slot are swapped. + * The hotbar includes the player's offhand. + */ + HotbarSwap, + /** + * A max-size stack of the clicked item is put on the cursor. + */ + CloneStack, + /** + * The inventory is searched for the same material, and they are put on + * the cursor up to the max stack size. + */ + CollectToCursor, + /** + * An unrecognized ClickType. + */ + Unknown +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryClickEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryClickEvent.java new file mode 100644 index 0000000..02f662b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryClickEvent.java @@ -0,0 +1,62 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsInventoryClickEvent extends EsCancellableEvent { + private final EsInventory clickedInv; + private final EsInventory inv; + private final EsItemStack currentItem; + private final EsInventoryAction action; + private final EsClickType click; + private final EsPlayer player; + private final EsItemStack cursor; + private final int slot; + + public EsInventoryClickEvent(EsPlayer player, int slot, EsItemStack cursor, EsInventory inv, EsInventory clickedInv, EsItemStack currentItem, EsInventoryAction action, EsClickType click) { + this.clickedInv = clickedInv; + this.inv = inv; + this.currentItem = currentItem; + this.action = action; + this.click = click; + this.player = player; + this.cursor = cursor; + this.slot = slot; + } + + public EsInventory getClickedInventory() { + return clickedInv; + } + + public EsItemStack getCurrentItem() { + return currentItem; + } + + public EsInventoryAction getAction() { + return action; + } + + public EsClickType getClick() { + return click; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsInventory getInventory() { + return inv; + } + + public EsItemStack getCursor() { + return cursor; + } + + public int getSlot() { + return slot; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryCloseEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryCloseEvent.java new file mode 100644 index 0000000..710dae0 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryCloseEvent.java @@ -0,0 +1,23 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsInventoryCloseEvent implements EsEvent { + private final EsPlayer player; + private final EsInventory inv; + + public EsInventoryCloseEvent(EsPlayer player, EsInventory inv) { + this.player = player; + this.inv = inv; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsInventory getInventory() { + return inv; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryDragEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryDragEvent.java new file mode 100644 index 0000000..a126ac2 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsInventoryDragEvent.java @@ -0,0 +1,38 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +import java.util.Set; + +public class EsInventoryDragEvent extends EsCancellableEvent { + private final EsPlayer player; + private final EsInventory inv; + private final Set changedSlots; + private final EsInventoryView view; + + public EsInventoryDragEvent(EsPlayer player, EsInventory inv, Set changedSlots, EsInventoryView view) { + this.player = player; + this.inv = inv; + this.changedSlots = changedSlots; + this.view = view; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsInventory getInventory() { + return inv; + } + + public Set getChangedSlots() { + return changedSlots; + } + + public EsInventoryView getView() { + return view; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerKickEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerKickEvent.java new file mode 100644 index 0000000..5e957a0 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerKickEvent.java @@ -0,0 +1,16 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsPlayerKickEvent implements EsEvent { + private final EsPlayer player; + + public EsPlayerKickEvent(EsPlayer player) { + this.player = player; + } + + public EsPlayer getPlayer() { + return player; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerQuitEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerQuitEvent.java new file mode 100644 index 0000000..a588eb6 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerQuitEvent.java @@ -0,0 +1,16 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsPlayerQuitEvent implements EsEvent { + private final EsPlayer player; + + public EsPlayerQuitEvent(EsPlayer player) { + this.player = player; + } + + public EsPlayer getPlayer() { + return player; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index cbc1e2b..f729766 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -1,10 +1,13 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Events.EsBlockPlaceEvent; -import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; -import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; -import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -13,10 +16,15 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import java.lang.reflect.InvocationTargetException; import java.util.Objects; +import java.util.Set; public class BukkitEventsListener implements Listener { @@ -85,4 +93,48 @@ public void onEntityDamage(EntityDamageEvent e) { setDamageFromEvent(e, ee.getDamage()); e.setCancelled(ee.isCancelled()); } + + @EventHandler + public void onInvClick(InventoryClickEvent e) { + EsInventory clInv = new BukkitInventory(e.getClickedInventory()); + EsInventory inv = new BukkitInventory(e.getInventory()); + EsClickType ct = BukkitHelper.fromBukkitClickType(e.getClick()); + EsInventoryAction ac = BukkitHelper.fromBukkitInventoryAction(e.getAction()); + EsItemStack ci = new BukkitItemStack(e.getCurrentItem()); + EsPlayer cl = new BukkitPlayer((Player) e.getWhoClicked()); + EsItemStack cu = new BukkitItemStack(e.getCursor()); + int sl = e.getSlot(); + EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + Main.callEvent(new EsPlayerQuitEvent(new BukkitPlayer(e.getPlayer()))); + } + + @EventHandler + public void onKick(PlayerQuitEvent e) { + Main.callEvent(new EsPlayerQuitEvent(new BukkitPlayer(e.getPlayer()))); + } + + @EventHandler + public void onInvClose(InventoryCloseEvent e) { + EsInventory inv = new BukkitInventory(e.getInventory()); + EsPlayer pl = new BukkitPlayer((Player) e.getPlayer()); + EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); + Main.callEvent(ee); + } + + @EventHandler + public void onDrag(InventoryDragEvent e) { + EsInventory inv = new BukkitInventory(e.getInventory()); + EsPlayer pl = new BukkitPlayer((Player) e.getWhoClicked()); + Set cs = e.getRawSlots(); + EsInventoryView view = new BukkitInventoryView(e.getView()); + EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index ad09dcb..f783ffd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -15,6 +15,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.persistence.PersistentDataType; @@ -320,4 +322,164 @@ public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { throw new IllegalArgumentException("Invalid EquipmentSlot"); } } + + public static EsClickType fromBukkitClickType(ClickType click) { + switch (click) { + case CREATIVE: + return EsClickType.Creative; + case DROP: + return EsClickType.Drop; + case LEFT: + return EsClickType.Left; + case RIGHT: + return EsClickType.Right; + case SHIFT_LEFT: + return EsClickType.ShiftLeft; + case SHIFT_RIGHT: + return EsClickType.ShiftRight; + case MIDDLE: + return EsClickType.Middle; + case UNKNOWN: + return EsClickType.Unknown; + case NUMBER_KEY: + return EsClickType.NumberKey; + case CONTROL_DROP: + return EsClickType.ControlDrop; + case DOUBLE_CLICK: + return EsClickType.DoubleClick; + case SWAP_OFFHAND: + return EsClickType.SwapOffhand; + case WINDOW_BORDER_LEFT: + return EsClickType.WindowBorderLeft; + case WINDOW_BORDER_RIGHT: + return EsClickType.WindowBorderRight; + default: + throw new RuntimeException("Invalid click type"); + } + } + + public static ClickType toBukkitClickType(EsClickType esClick) { + switch (esClick) { + case Creative: + return ClickType.CREATIVE; + case Drop: + return ClickType.DROP; + case Left: + return ClickType.LEFT; + case Right: + return ClickType.RIGHT; + case ShiftLeft: + return ClickType.SHIFT_LEFT; + case ShiftRight: + return ClickType.SHIFT_RIGHT; + case Middle: + return ClickType.MIDDLE; + case Unknown: + return ClickType.UNKNOWN; + case NumberKey: + return ClickType.NUMBER_KEY; + case ControlDrop: + return ClickType.CONTROL_DROP; + case DoubleClick: + return ClickType.DOUBLE_CLICK; + case SwapOffhand: + return ClickType.SWAP_OFFHAND; + case WindowBorderLeft: + return ClickType.WINDOW_BORDER_LEFT; + case WindowBorderRight: + return ClickType.WINDOW_BORDER_RIGHT; + default: + throw new IllegalArgumentException("Invalid EsClickType"); + } + } + + public static EsInventoryAction fromBukkitInventoryAction(InventoryAction action) { + switch (action) { + case NOTHING: + return EsInventoryAction.Nothing; + case PICKUP_ALL: + return EsInventoryAction.PickupAll; + case PICKUP_SOME: + return EsInventoryAction.PickupSome; + case PICKUP_HALF: + return EsInventoryAction.PickupHalf; + case PICKUP_ONE: + return EsInventoryAction.PickupOne; + case PLACE_ALL: + return EsInventoryAction.PlaceAll; + case PLACE_SOME: + return EsInventoryAction.PlaceSome; + case PLACE_ONE: + return EsInventoryAction.PlaceOne; + case SWAP_WITH_CURSOR: + return EsInventoryAction.SwapWithCursor; + case DROP_ALL_CURSOR: + return EsInventoryAction.DropAllCursor; + case DROP_ONE_CURSOR: + return EsInventoryAction.DropOneCursor; + case DROP_ALL_SLOT: + return EsInventoryAction.DropAllSlot; + case DROP_ONE_SLOT: + return EsInventoryAction.DropOneSlot; + case MOVE_TO_OTHER_INVENTORY: + return EsInventoryAction.MoveToOtherInventory; + case HOTBAR_MOVE_AND_READD: + return EsInventoryAction.HotbarMoveAndReadd; + case HOTBAR_SWAP: + return EsInventoryAction.HotbarSwap; + case CLONE_STACK: + return EsInventoryAction.CloneStack; + case COLLECT_TO_CURSOR: + return EsInventoryAction.CollectToCursor; + case UNKNOWN: + return EsInventoryAction.Unknown; + default: + throw new IllegalArgumentException("Invalid InventoryAction"); + } + } + + public static InventoryAction toBukkitInventoryAction(EsInventoryAction esAction) { + switch (esAction) { + case Nothing: + return InventoryAction.NOTHING; + case PickupAll: + return InventoryAction.PICKUP_ALL; + case PickupSome: + return InventoryAction.PICKUP_SOME; + case PickupHalf: + return InventoryAction.PICKUP_HALF; + case PickupOne: + return InventoryAction.PICKUP_ONE; + case PlaceAll: + return InventoryAction.PLACE_ALL; + case PlaceSome: + return InventoryAction.PLACE_SOME; + case PlaceOne: + return InventoryAction.PLACE_ONE; + case SwapWithCursor: + return InventoryAction.SWAP_WITH_CURSOR; + case DropAllCursor: + return InventoryAction.DROP_ALL_CURSOR; + case DropOneCursor: + return InventoryAction.DROP_ONE_CURSOR; + case DropAllSlot: + return InventoryAction.DROP_ALL_SLOT; + case DropOneSlot: + return InventoryAction.DROP_ONE_SLOT; + case MoveToOtherInventory: + return InventoryAction.MOVE_TO_OTHER_INVENTORY; + case HotbarMoveAndReadd: + return InventoryAction.HOTBAR_MOVE_AND_READD; + case HotbarSwap: + return InventoryAction.HOTBAR_SWAP; + case CloneStack: + return InventoryAction.CLONE_STACK; + case CollectToCursor: + return InventoryAction.COLLECT_TO_CURSOR; + case Unknown: + return InventoryAction.UNKNOWN; + default: + throw new IllegalArgumentException("Invalid EsInventoryAction"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index 2148952..29eb67a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -2,9 +2,12 @@ import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import org.bukkit.Material; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import java.util.*; + public class BukkitInventory implements EsInventory { private final org.bukkit.inventory.Inventory bukkitInv; @@ -62,4 +65,21 @@ public void clear() { public Inventory getBukkitInventory() { return bukkitInv; } + + @Override + public boolean isEqualTo(EsInventory inv) { + return ((BukkitInventory) inv).getBukkitInventory().equals(bukkitInv); + } + + @Override + public Map all(String material) { + Material mat = Material.valueOf(material); + Map out = new HashMap<>(); + + for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { + out.put(entry.getKey(), new BukkitItemStack(entry.getValue())); + } + + return out; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java new file mode 100644 index 0000000..df5c282 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java @@ -0,0 +1,35 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; + +public class BukkitInventoryView implements EsInventoryView { + private final InventoryView bukkitView; + + public BukkitInventoryView(InventoryView child) { + bukkitView = child; + } + + @Override + public EsInventory getTopInventory() { + return new BukkitInventory(bukkitView.getTopInventory()); + } + + @Override + public EsInventory getBottomInventory() { + return new BukkitInventory(bukkitView.getBottomInventory()); + } + + @Override + public EsInventory getInventory(int slot) { + return new BukkitInventory(bukkitView.getInventory(slot)); + } + + @Override + public EsPlayer getPlayer() { + return new BukkitPlayer((Player) bukkitView.getPlayer()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index 7ba38cd..5cb6710 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -4,6 +4,7 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; @@ -11,7 +12,7 @@ public class BukkitItemStack implements EsItemStack { private final org.bukkit.inventory.ItemStack bukkitItem; public BukkitItemStack(String mat, int amount) { - bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); + bukkitItem = new ItemStack(Material.valueOf(mat), amount); } public BukkitItemStack(org.bukkit.inventory.ItemStack child) { @@ -97,4 +98,14 @@ public int getDamage() { public EsItemStack clone() { return new BukkitItemStack(bukkitItem.clone()); } + + @Override + public boolean isSimilar(EsItemStack stack) { + return ((BukkitItemStack) stack).getBukkitItem().isSimilar(bukkitItem); + } + + @Override + public int getMaxStackSize() { + return bukkitItem.getMaxStackSize(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index c5dafee..96f847f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -16,6 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.util.*; @@ -241,8 +242,9 @@ public String getPluginName() { } @Override - public void runTaskLater(Runnable task, long ticks) { - Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); + public int runTaskLater(Runnable task, long ticks) { + BukkitTask bukkitTask = Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); + return bukkitTask.getTaskId(); } @Override @@ -250,6 +252,11 @@ public void runTask(Runnable task) { Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, task); } + @Override + public void cancelTask(int id) { + Bukkit.getScheduler().cancelTask(id); + } + @Override public EsLogger getLogger() { return new BukkitLogger(); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 116454e..cb9d420 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -1,11 +1,14 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Events.EsBlockPlaceEvent; -import net.serble.estools.ServerApi.Events.EsEntityDamageEvent; -import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; -import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.*; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -14,10 +17,15 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import java.lang.reflect.InvocationTargetException; import java.util.Objects; +import java.util.Set; public class FoliaEventsListener implements Listener { @@ -86,4 +94,48 @@ public void onEntityDamage(EntityDamageEvent e) { setDamageFromEvent(e, ee.getDamage()); e.setCancelled(ee.isCancelled()); } + + @EventHandler + public void onInvClick(InventoryClickEvent e) { + EsInventory clInv = new FoliaInventory(e.getClickedInventory()); + EsInventory inv = new FoliaInventory(e.getInventory()); + EsClickType ct = FoliaHelper.fromBukkitClickType(e.getClick()); + EsInventoryAction ac = FoliaHelper.fromBukkitInventoryAction(e.getAction()); + EsItemStack ci = new FoliaItemStack(e.getCurrentItem()); + EsPlayer cl = new FoliaPlayer((Player) e.getWhoClicked()); + EsItemStack cu = new FoliaItemStack(e.getCursor()); + int sl = e.getSlot(); + EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + Main.callEvent(new EsPlayerQuitEvent(new FoliaPlayer(e.getPlayer()))); + } + + @EventHandler + public void onKick(PlayerQuitEvent e) { + Main.callEvent(new EsPlayerQuitEvent(new FoliaPlayer(e.getPlayer()))); + } + + @EventHandler + public void onInvClose(InventoryCloseEvent e) { + EsInventory inv = new FoliaInventory(e.getInventory()); + EsPlayer pl = new FoliaPlayer((Player) e.getPlayer()); + EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); + Main.callEvent(ee); + } + + @EventHandler + public void onDrag(InventoryDragEvent e) { + EsInventory inv = new FoliaInventory(e.getInventory()); + EsPlayer pl = new FoliaPlayer((Player) e.getWhoClicked()); + Set cs = e.getRawSlots(); + EsInventoryView view = new FoliaInventoryView(e.getView()); + EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index b28a4c4..2053739 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -3,6 +3,7 @@ import io.papermc.paper.threadedregions.scheduler.EntityScheduler; import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; import io.papermc.paper.threadedregions.scheduler.RegionScheduler; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsLocation; @@ -86,8 +87,8 @@ private static EntityScheduler getEntityScheduler(Entity entity) { return grs; } - public static void runTaskLater(Runnable task, long ticks) { - getGlobalScheduler().runDelayed(EsToolsBukkit.plugin, (t) -> task.run(), ticks); + public static ScheduledTask runTaskLater(Runnable task, long ticks) { + return getGlobalScheduler().runDelayed(EsToolsBukkit.plugin, (t) -> task.run(), ticks); } public static void runTaskOnNextTick(Runnable task) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index f6eb9cd..fa787c0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -1,8 +1,14 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import org.bukkit.Material; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.util.HashMap; +import java.util.Map; public class FoliaInventory extends BukkitInventory { private final Inventory bukkitInv; @@ -30,4 +36,21 @@ public EsItemStack[] getContents() { public EsItemStack getItem(int slot) { return new FoliaItemStack(bukkitInv.getItem(slot)); } + + @Override + public boolean isEqualTo(EsInventory inv) { + return ((FoliaInventory) inv).getBukkitInventory().equals(bukkitInv); + } + + @Override + public Map all(String material) { + Material mat = Material.valueOf(material); + Map out = new HashMap<>(); + + for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { + out.put(entry.getKey(), new FoliaItemStack(entry.getValue())); + } + + return out; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java new file mode 100644 index 0000000..d2d7468 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java @@ -0,0 +1,36 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryView; + +public class FoliaInventoryView extends BukkitInventoryView { + private final InventoryView bukkitView; + + public FoliaInventoryView(InventoryView child) { + super(child); + bukkitView = child; + } + + @Override + public EsInventory getTopInventory() { + return new FoliaInventory(bukkitView.getTopInventory()); + } + + @Override + public EsInventory getBottomInventory() { + return new FoliaInventory(bukkitView.getBottomInventory()); + } + + @Override + public EsInventory getInventory(int slot) { + return new FoliaInventory(bukkitView.getInventory(slot)); + } + + @Override + public EsPlayer getPlayer() { + return new FoliaPlayer((Player) bukkitView.getPlayer()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 6aaaa9d..8e72fce 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -27,4 +27,9 @@ public EsItemMeta getItemMeta() { public EsItemStack clone() { return new FoliaItemStack(bukkitItem.clone()); } + + @Override + public boolean isSimilar(EsItemStack stack) { + return ((FoliaItemStack) stack).getBukkitItem().isSimilar(bukkitItem); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index e620c33..85cb310 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Folia; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.serble.estools.Effects; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; @@ -22,6 +23,7 @@ public class FoliaServer implements EsServerSoftware { private final JavaPlugin plugin; + private final Map tasks = new HashMap<>(); public FoliaServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; @@ -241,8 +243,15 @@ public String getPluginName() { } @Override - public void runTaskLater(Runnable task, long ticks) { - FoliaHelper.runTaskLater(task, ticks); + public int runTaskLater(Runnable task, long ticks) { + ScheduledTask foliaTask = FoliaHelper.runTaskLater(task, ticks); + int id = foliaTask.hashCode(); + if (tasks.containsKey(id)) { + throw new RuntimeException("Hash collision?"); + } + + tasks.put(id, foliaTask); + return id; } @Override @@ -250,6 +259,20 @@ public void runTask(Runnable task) { FoliaHelper.runTaskOnNextTick(task); } + @Override + public void cancelTask(int id) { + if (!tasks.containsKey(id)) { + throw new RuntimeException("That task doesn't exist"); + } + + ScheduledTask task = tasks.get(id); + if (task.isCancelled() || task.getExecutionState() == ScheduledTask.ExecutionState.FINISHED) { + return; + } + + task.cancel(); + } + @Override public EsLogger getLogger() { return new FoliaLogger(); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java index 114ba9c..939ff5f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; +import java.util.Map; + public interface EsInventory { void setItem(int slot, EsItemStack item); EsItemStack getItem(int slot); @@ -7,4 +9,16 @@ public interface EsInventory { void setContents(EsItemStack[] items); EsItemStack[] getContents(); void clear(); + boolean isEqualTo(EsInventory inv); + + /** + * Gets a list of slots and the items in those slots that are of a specific material. + * + * @param material + * The material to look for. + * + * @return + * A list of slot to item mappings. + */ + Map all(String material); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventoryView.java new file mode 100644 index 0000000..d2a2fd6 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventoryView.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Interfaces; + +public interface EsInventoryView { + EsInventory getTopInventory(); + EsInventory getBottomInventory(); + EsInventory getInventory(int slot); + EsPlayer getPlayer(); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 2f2d5df..ca08b10 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -12,4 +12,6 @@ public interface EsItemStack { void setDamage(int val); int getDamage(); EsItemStack clone(); + boolean isSimilar(EsItemStack stack); + int getMaxStackSize(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 2231952..644bab9 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -24,8 +24,9 @@ public interface EsServerSoftware { EsCommandSender getConsoleSender(); SemanticVersion getPluginVersion(); String getPluginName(); - void runTaskLater(Runnable task, long ticks); + int runTaskLater(Runnable task, long ticks); void runTask(Runnable task); + void cancelTask(int id); EsLogger getLogger(); void startEvents(); } From 62878ba58fb3dfd7eceb70428dabbd7bdce28b3a Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 26 May 2024 14:01:04 +1000 Subject: [PATCH 12/74] Fix resource overlap warns --- pom.xml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pom.xml b/pom.xml index 77c8069..3a7263c 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,23 @@ net.serble.estools + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + META-INF/MANIFEST.MF + + + + + + com.google.errorprone:error_prone_annotations + com.google.code.gson:gson + + From 3a2db3d9aaf3ca9908732490286cb922ca92876f Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 26 May 2024 18:08:19 +1000 Subject: [PATCH 13/74] Abstract commands and fix many commands --- TestedVersions.txt | 23 +---- abstraction.md | 4 +- pom.xml | 13 +++ .../net/serble/estools/Commands/Back.java | 10 +- .../net/serble/estools/Commands/Ench.java | 13 ++- .../net/serble/estools/Commands/EsTools.java | 6 ++ .../serble/estools/Commands/Give/Give.java | 3 +- .../net/serble/estools/Commands/Music.java | 3 +- .../net/serble/estools/Commands/SafeTp.java | 6 ++ .../net/serble/estools/EsToolsCommand.java | 24 +---- .../serble/estools/EsToolsTabCompleter.java | 8 +- src/main/java/net/serble/estools/Main.java | 84 +++++++++-------- .../estools/ServerApi/EsSoundCategory.java | 14 +++ .../Bukkit/BukkitEventsListener.java | 11 ++- .../Implementations/Bukkit/BukkitHelper.java | 36 ++++++-- .../Bukkit/BukkitInventory.java | 6 +- .../Bukkit/BukkitItemStack.java | 2 + .../Implementations/Bukkit/BukkitPlayer.java | 12 ++- .../Implementations/Bukkit/BukkitServer.java | 39 +++++++- .../Bukkit/BukkitTabCompleteGenerator.java} | 8 +- .../Folia/FoliaEventsListener.java | 11 ++- .../Implementations/Folia/FoliaHelper.java | 9 +- .../Implementations/Folia/FoliaInventory.java | 42 ++++++++- .../Implementations/Folia/FoliaItemStack.java | 91 +++++++++++++++++-- .../Implementations/Folia/FoliaPlayer.java | 12 ++- .../Implementations/Folia/FoliaServer.java | 56 +++++++++++- .../ServerApi/Interfaces/EsPlayer.java | 4 +- .../Interfaces/EsServerSoftware.java | 5 + 28 files changed, 409 insertions(+), 146 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsSoundCategory.java rename src/main/java/net/serble/estools/{TabCompleteGenerator.java => ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java} (76%) diff --git a/TestedVersions.txt b/TestedVersions.txt index 2804932..ef4b1f3 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -1,20 +1,5 @@ +Folia: +1.20.4 Failing + +Bukkit: 1.20.4 -1.19.4 -1.17.1 -1.16.1 -1.15.2 -1.14.4 -1.13.2 -1.12.2 -1.11.2 -1.10.2 -1.9.4 -1.8.8 -1.7.10 -1.6.4 -1.5.2 -1.4.7 -1.3.2 -1.2.5 -1.1.0 -1.0.0 \ No newline at end of file diff --git a/abstraction.md b/abstraction.md index 6fa75c4..56e0527 100644 --- a/abstraction.md +++ b/abstraction.md @@ -1,6 +1,4 @@ # TODO -- Events - Config - Fix testing -- Fix /safetp -- Fix weather commands \ No newline at end of file +- Fix teleport event not working on folia \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3a7263c..e29cb11 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,10 @@ org.bstats net.serble.estools + + co.aikar.taskchain + net.serble.estools.taskchain + @@ -91,6 +95,10 @@ papermc https://repo.papermc.io/repository/maven-public/ + + aikar + https://repo.aikar.co/content/groups/aikar/ + @@ -129,5 +137,10 @@ 1.20.4-R0.1-SNAPSHOT provided + + co.aikar + taskchain-bukkit + 3.7.2 + diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index cd8aca9..8acb2a5 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -14,6 +14,7 @@ import net.serble.estools.ServerApi.Interfaces.EsPlayer; import net.serble.estools.EsToolsCommand; +import net.serble.estools.ServerApi.ServerPlatform; public class Back extends EsToolsCommand implements EsEventListener { private static final HashMap prevLocations = new HashMap<>(); @@ -28,6 +29,10 @@ public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } + + if (Main.platform == ServerPlatform.Folia) { + send(sender, "&cCurrently due to Folia issues /back only works for death locations"); + } EsPlayer p = (EsPlayer) sender; if (prevLocations.containsKey(p.getUniqueId())) { @@ -45,11 +50,6 @@ public void executeEvent(EsEvent event) { if (event instanceof EsPlayerDeathEvent) { EsPlayerDeathEvent e = (EsPlayerDeathEvent) event; - if (Main.minecraftVersion.getMinor() > 1) { - prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); - return; - } - EsPlayer p = e.getPlayer(); prevLocations.put(p.getUniqueId(), p.getLocation()); return; diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index c07d6cb..2272c42 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -39,6 +40,11 @@ public boolean execute(EsCommandSender sender, String[] args) { } is = ((EsPlayer) sender).getMainHand(); + + if (is == null || Objects.equals(is.getType(), "AIR")) { + send(sender, "&cYou must be holding an item to enchant it"); + return false; + } } else { EsPlayer p = getPlayer(sender, args[2]); @@ -47,9 +53,14 @@ public boolean execute(EsCommandSender sender, String[] args) { } is = p.getMainHand(); + + if (is == null || Objects.equals(is.getType(), "AIR")) { + send(sender, "&c" + p.getName() + " isn't holding an item"); + return false; + } } - if (Main.server.doesEnchantmentExist(args[0].toLowerCase()) && is != null) { + if (Main.server.doesEnchantmentExist(args[0].toLowerCase())) { is.addEnchantment(args[0].toLowerCase(), level); send(sender, "&aEnchantment &6%s&a at level &6%s&a was added!", args[0].toLowerCase(), level); } diff --git a/src/main/java/net/serble/estools/Commands/EsTools.java b/src/main/java/net/serble/estools/Commands/EsTools.java index 77ab5ed..2e60e9e 100644 --- a/src/main/java/net/serble/estools/Commands/EsTools.java +++ b/src/main/java/net/serble/estools/Commands/EsTools.java @@ -6,6 +6,7 @@ import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.ServerPlatform; import net.serble.estools.Tester; import net.serble.estools.Updater; @@ -72,6 +73,11 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } + if (Main.platform == ServerPlatform.Folia) { // TODO: Fix + send(sender, "&cDue to scheduler limitations, /estools test is not currently supported on Folia"); + return false; + } + EsPlayer p = (EsPlayer) sender; Tester tester = Tester.runningTests.get(p.getUniqueId()); diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index ccab7a2..46f71fa 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -6,7 +6,6 @@ import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.command.Command; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -31,7 +30,7 @@ public static EsItemStack getItem(String name, int amount) { } @Override - public List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args) { + public List onTabComplete(EsCommandSender sender, String[] args) { List tab = new ArrayList<>(); if (args.length == 1) { diff --git a/src/main/java/net/serble/estools/Commands/Music.java b/src/main/java/net/serble/estools/Commands/Music.java index 226887e..e0c9cb6 100644 --- a/src/main/java/net/serble/estools/Commands/Music.java +++ b/src/main/java/net/serble/estools/Commands/Music.java @@ -2,6 +2,7 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsSoundCategory; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -74,7 +75,7 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - p.playSound(sound, p.getLocation().add(0, 1000, 0), 100000, 1); + p.playSound(sound, EsSoundCategory.Records, p.getLocation().add(0, 1000, 0), 100000, 1); // Substring away the MUSIC_DISK_ or the RECORD_ depending on the version // then reformat to be all lowercase except for the first character diff --git a/src/main/java/net/serble/estools/Commands/SafeTp.java b/src/main/java/net/serble/estools/Commands/SafeTp.java index c8e8c42..95ed1f7 100644 --- a/src/main/java/net/serble/estools/Commands/SafeTp.java +++ b/src/main/java/net/serble/estools/Commands/SafeTp.java @@ -8,6 +8,7 @@ import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.ServerPlatform; public class SafeTp extends EsToolsCommand implements EsEventListener { public static boolean enabled = true; @@ -20,6 +21,11 @@ public void onEnable() { @Override public boolean execute(EsCommandSender sender, String[] args) { + if (Main.platform == ServerPlatform.Folia) { + send(sender, "&cCurrently due to Folia issues SafeTP is not supported"); + return false; + } + if (enabled) { enabled = false; send(sender, "&aTeleporting now &6&lWILL&a make you take fall damage!"); diff --git a/src/main/java/net/serble/estools/EsToolsCommand.java b/src/main/java/net/serble/estools/EsToolsCommand.java index 4ee091b..9caaa54 100644 --- a/src/main/java/net/serble/estools/EsToolsCommand.java +++ b/src/main/java/net/serble/estools/EsToolsCommand.java @@ -1,25 +1,17 @@ package net.serble.estools; import net.serble.estools.ServerApi.EsLocation; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -// TODO: Remove these -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - import java.util.*; -public abstract class EsToolsCommand implements CommandExecutor, EsToolsTabCompleter { +public abstract class EsToolsCommand implements EsToolsTabCompleter { public void onEnable() { } @Override - public List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args) { + public List onTabComplete(EsCommandSender sender, String[] args) { if (Main.minecraftVersion.getMinor() < 7) { return new ArrayList<>(); } @@ -28,11 +20,6 @@ public List onTabComplete(EsCommandSender sender, Command command, Strin return fixTabComplete(tabComplete(sender, args, lArg), lArg); } - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { - return execute(FoliaHelper.fromBukkitCommandSender(sender), args); // TODO: This is dependent on Folia - } - public static List fixTabComplete(List inList, String arg) { final String argL = arg.toLowerCase(); inList.removeIf(n -> !n.toLowerCase().startsWith(argL)); @@ -61,12 +48,7 @@ public static void send(EsCommandSender s, String m, Object... a) { } public static String translate(String m, Object... a) { - if (Main.minecraftVersion.getMinor() > 1) { - return ChatColor.translateAlternateColorCodes('&', String.format(m, a)); - } - - // translateAlternateColorCodes doesn't exist in 1.1 and 1.0 - // do it manually + // This works for every Minecraft :) return String.format(m, a).replace('&', '§'); } diff --git a/src/main/java/net/serble/estools/EsToolsTabCompleter.java b/src/main/java/net/serble/estools/EsToolsTabCompleter.java index bcc779e..3347f05 100644 --- a/src/main/java/net/serble/estools/EsToolsTabCompleter.java +++ b/src/main/java/net/serble/estools/EsToolsTabCompleter.java @@ -1,15 +1,9 @@ package net.serble.estools; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import org.bukkit.command.Command; -import org.bukkit.command.PluginCommand; import java.util.List; public interface EsToolsTabCompleter { - default void register(PluginCommand cmd) { - cmd.setTabCompleter(TabCompleteGenerator.generate(this)); - } - - List onTabComplete(EsCommandSender sender, Command command, String alias, String[] args); + List onTabComplete(EsCommandSender sender, String[] args); } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 71d30b3..ce07895 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -11,6 +11,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; @@ -18,8 +19,7 @@ import net.serble.estools.Signs.SignMain; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; -import org.bukkit.command.PluginCommand; -import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.FileConfiguration; // TODO: The only remaining bukkit import in Main.java import net.serble.estools.Commands.*; @@ -28,7 +28,9 @@ import java.io.InputStream; import java.nio.file.Files; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @SuppressWarnings("UnusedReturnValue") public class Main { @@ -43,6 +45,7 @@ public class Main { public static EsLogger logger; private final Object context; private static final List eventListeners = new ArrayList<>(); + private static final Map commands = new HashMap<>(); private static final int bStatsId = 21760; @@ -185,74 +188,69 @@ public static void callEvent(EsEvent event) { } } + public static boolean executeCommand(EsCommandSender sender, String cmd, String[] args) { + if (!commands.containsKey(cmd)) { + logger.severe("&cThe command: '" + cmd + "', does not exist"); + return false; + } + + return commands.get(cmd).execute(sender, args); + } + public static void registerEvents(EsEventListener listener) { eventListeners.add(listener); } // Setup Command Overloads - public PluginCommand sc(String name, EsToolsCommand ce) { - PluginCommand cmd = EsToolsBukkit.plugin.getCommand(name); - assert cmd != null; - cmd.setExecutor(ce); + public void sc(String name, EsToolsCommand ce) { + server.registerCommand(name, ce); + commands.put(name, ce); ce.onEnable(); - - if (tabCompleteEnabled && cmd.getTabCompleter() == null) { - ce.register(cmd); - } - return cmd; } - public PluginCommand sc(String name, EsToolsCommand ce, int minVer) { - if (minecraftVersion.getMinor() >= minVer) return sc(name, ce); - else return sc(name, new WrongVersion(minVer)); + public void sc(String name, EsToolsCommand ce, int minVer) { + if (minecraftVersion.getMinor() >= minVer) { + sc(name, ce); + return; + } + sc(name, new WrongVersion(minVer)); } - public PluginCommand sc(String name, EsToolsCommand ce, EsToolsTabCompleter tc) { - PluginCommand cmd = sc(name, ce); + public void sc(String name, EsToolsCommand ce, EsToolsTabCompleter tc) { + sc(name, ce); if (tabCompleteEnabled) { - tc.register(cmd); + server.setTabCompleter(name, tc); } - return cmd; } - public PluginCommand sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter tc, int minMajor, int minMinor) { + public void sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter tc, int minMajor, int minMinor) { String versionName = minMajor + "." + minMinor; if (minecraftVersion.getMinor() > minMajor || (minecraftVersion.getMinor() == minMajor && minecraftVersion.getPatch() >= minMinor)) { if (tc == null) { - return sc(name, perm, ce); + sc(name, perm, ce); + return; } - return sc(name, perm, ce, tc); - } else return sc(name, new WrongVersion(versionName), new WrongVersion(versionName)); + sc(name, perm, ce, tc); + } else sc(name, new WrongVersion(versionName), new WrongVersion(versionName)); } - public PluginCommand sc(String name, String perm, EsToolsCommand ce) { - PluginCommand cmd = sc(name, ce); - cmd.setPermission("estools." + perm); - - if (minecraftVersion.getMinor() > 0) { // Permission errors weren't a thing in 1.0 - //noinspection deprecation, is still useful in pre 1.13 and technically is useful in rare situations post 1.13 - cmd.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); - } - - if (tabCompleteEnabled && cmd.getTabCompleter() == null) { // Give every command tab complete if they haven't already registered it - ce.register(cmd); - } - return cmd; + public void sc(String name, String perm, EsToolsCommand ce) { + sc(name, ce); + server.setCommandPermission(name, "estools." + perm); } - public PluginCommand sc(String name, String perm, EsToolsCommand ce, int minVer) { - if (minecraftVersion.getMinor() >= minVer) return sc(name, perm, ce); - else return sc(name, perm, new WrongVersion(minVer)); + public void sc(String name, String perm, EsToolsCommand ce, int minVer) { + if (minecraftVersion.getMinor() >= minVer) sc(name, perm, ce); + else sc(name, perm, new WrongVersion(minVer)); } - public PluginCommand sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter tc) { - PluginCommand cmd = sc(name, perm, ce); + public void sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter tc) { + sc(name, perm, ce); if (tabCompleteEnabled) { - tc.register(cmd); + server.setTabCompleter(name, tc); } - return cmd; } // Overriding to create more predictable behaviour between versions @@ -261,6 +259,7 @@ public FileConfiguration getConfig() { } // Overriding because method doesn't exist in very early versions + @SuppressWarnings("unused") // I might use it in the future public void saveResource(String res, boolean replace) { InputStream resource = EsToolsBukkit.plugin.getResource(res); File file = new File(EsToolsBukkit.plugin.getDataFolder(), res); @@ -276,7 +275,6 @@ public void saveResource(String res, boolean replace) { Files.copy(resource, file.toPath()); logger.info("Copied " + res + " to " + file.toPath()); } catch (IOException e) { - e.printStackTrace(); logger.severe("Failed to save resource: " + e); } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsSoundCategory.java b/src/main/java/net/serble/estools/ServerApi/EsSoundCategory.java new file mode 100644 index 0000000..cafc14e --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsSoundCategory.java @@ -0,0 +1,14 @@ +package net.serble.estools.ServerApi; + +public enum EsSoundCategory { + Master, + Music, + Records, + Weather, + Blocks, + Hostile, + Neutral, + Players, + Ambient, + Voice +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index f729766..eb5c706 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -9,6 +9,9 @@ import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -21,12 +24,13 @@ import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; import java.util.Objects; import java.util.Set; -public class BukkitEventsListener implements Listener { +public class BukkitEventsListener implements Listener, CommandExecutor { private double getDamageFromEvent(EntityDamageEvent e) { if (Main.minecraftVersion.getMinor() > 5) { @@ -137,4 +141,9 @@ public void onDrag(InventoryDragEvent e) { Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + return Main.executeCommand(BukkitHelper.fromBukkitCommandSender(sender), command.getName(), args); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index f783ffd..591de7e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -22,6 +22,7 @@ import org.bukkit.persistence.PersistentDataType; import org.bukkit.util.Vector; +@SuppressWarnings("unused") public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { Location bLoc = new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); @@ -196,12 +197,7 @@ public static EsTeleportCause fromBukkitTeleportCause(PlayerTeleportEvent.Telepo public static EsBlock fromBukkitBlock(Block block) { BlockState state = block.getState(); - - if (state instanceof Sign) { - return new BukkitSign((Sign) state); - } - - return new BukkitBlock(state); + return fromBukkitBlock(state); } @SuppressWarnings("deprecation") @@ -222,6 +218,7 @@ public static Enchantment getBukkitEnchantment(String name) { return Enchantment.getByName(BukkitEnchantmentsHelper.getByName(name)); } + @SuppressWarnings("rawtypes") public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType type) { switch (type) { case Byte: @@ -482,4 +479,31 @@ public static InventoryAction toBukkitInventoryAction(EsInventoryAction esAction throw new IllegalArgumentException("Invalid EsInventoryAction"); } } + + public static SoundCategory toBukkitSoundCategory(EsSoundCategory esSoundCategory) { + switch (esSoundCategory) { + case Master: + return SoundCategory.MASTER; + case Music: + return SoundCategory.MUSIC; + case Records: + return SoundCategory.RECORDS; + case Weather: + return SoundCategory.WEATHER; + case Blocks: + return SoundCategory.BLOCKS; + case Hostile: + return SoundCategory.HOSTILE; + case Neutral: + return SoundCategory.NEUTRAL; + case Players: + return SoundCategory.PLAYERS; + case Ambient: + return SoundCategory.AMBIENT; + case Voice: + return SoundCategory.VOICE; + default: + throw new IllegalArgumentException("Invalid EsSoundCategory"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index 29eb67a..bda5d69 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -17,7 +17,8 @@ public BukkitInventory(Inventory inv) { @Override public void setItem(int slot, EsItemStack item) { - bukkitInv.setItem(slot, ((BukkitItemStack) item).getBukkitItem()); + ItemStack stack = item == null ? null : ((BukkitItemStack) item).getBukkitItem(); + bukkitInv.setItem(slot, stack); } @Override @@ -68,6 +69,9 @@ public Inventory getBukkitInventory() { @Override public boolean isEqualTo(EsInventory inv) { + if (inv == null) { + return false; + } return ((BukkitInventory) inv).getBukkitInventory().equals(bukkitInv); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index 5cb6710..dd10f6f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -71,6 +71,7 @@ public void setDamage(int val) { if (meta instanceof Damageable) { ((Damageable) meta).setDamage(val); } + bukkitItem.setItemMeta(meta); } else { if (val > Short.MAX_VALUE) { val = Short.MAX_VALUE; @@ -94,6 +95,7 @@ public int getDamage() { return 0; } + @SuppressWarnings("MethodDoesntCallSuperMethod") // I don't care, it doesn't work like that @Override public EsItemStack clone() { return new BukkitItemStack(bukkitItem.clone()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index c30e84d..30ccf04 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -3,11 +3,13 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSoundCategory; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; import java.util.HashSet; @@ -47,7 +49,11 @@ public void setSaturation(double val) { @Override public EsItemStack getMainHand() { - return new BukkitItemStack(bukkitGetMainHand()); + ItemStack mainHand = bukkitGetMainHand(); + if (mainHand == null) { + return null; + } + return new BukkitItemStack(mainHand); } @Override @@ -111,8 +117,8 @@ public boolean isFlying() { } @Override - public void playSound(String sound, EsLocation loc, int volume, int pitch) { - bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + public void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { + bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), BukkitHelper.toBukkitSoundCategory(category), volume, pitch); } @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 96f847f..0c3bb5f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -1,12 +1,11 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.Effects; +import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.Main; -import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; +import org.bukkit.command.PluginCommand; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -23,9 +22,11 @@ public class BukkitServer implements EsServerSoftware { private final JavaPlugin plugin; + private final BukkitEventsListener listener; public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; + listener = new BukkitEventsListener(); } @Override @@ -264,6 +265,36 @@ public EsLogger getLogger() { @Override public void startEvents() { - Bukkit.getPluginManager().registerEvents(new BukkitEventsListener(), EsToolsBukkit.plugin); + Bukkit.getPluginManager().registerEvents(listener, EsToolsBukkit.plugin); + } + + @Override + public void registerCommand(String cmd, EsToolsTabCompleter tab) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + if (command.getTabCompleter() == null) { + command.setExecutor(listener); + } + if (Main.tabCompleteEnabled) { + command.setTabCompleter(BukkitTabCompleteGenerator.generate(tab)); + } + } + + @Override + public void setTabCompleter(String cmd, EsToolsTabCompleter tab) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + if (Main.tabCompleteEnabled) { + command.setTabCompleter(BukkitTabCompleteGenerator.generate(tab)); + } + } + + @Override + public void setCommandPermission(String cmd, String perm) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + command.setPermission(perm); + + if (Main.minecraftVersion.getMinor() > 0) { + //noinspection deprecation, is still useful in pre 1.13 and technically is useful in rare situations post 1.13 + command.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); + } } } diff --git a/src/main/java/net/serble/estools/TabCompleteGenerator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java similarity index 76% rename from src/main/java/net/serble/estools/TabCompleteGenerator.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java index ddae793..1df0288 100644 --- a/src/main/java/net/serble/estools/TabCompleteGenerator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java @@ -1,6 +1,6 @@ -package net.serble.estools; +package net.serble.estools.ServerApi.Implementations.Bukkit; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.EsToolsTabCompleter; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -9,14 +9,14 @@ import java.util.List; -public class TabCompleteGenerator { +public class BukkitTabCompleteGenerator { public static TabCompleter generate(EsToolsTabCompleter tabCompleter) { return new TabCompleter() { @Nullable @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) { - return tabCompleter.onTabComplete(BukkitHelper.fromBukkitCommandSender(sender), cmd, label, args); + return tabCompleter.onTabComplete(BukkitHelper.fromBukkitCommandSender(sender), args); } }; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index cb9d420..8761c74 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -10,6 +10,9 @@ import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -22,12 +25,13 @@ import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; import java.util.Objects; import java.util.Set; -public class FoliaEventsListener implements Listener { +public class FoliaEventsListener implements Listener, CommandExecutor { private double getDamageFromEvent(EntityDamageEvent e) { if (Main.minecraftVersion.getMinor() > 5) { @@ -138,4 +142,9 @@ public void onDrag(InventoryDragEvent e) { Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + return Main.executeCommand(BukkitHelper.fromBukkitCommandSender(sender), command.getName(), args); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 2053739..2d2e3f2 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -24,9 +24,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +@SuppressWarnings("unused") public class FoliaHelper extends BukkitHelper { - private static GlobalRegionScheduler getGlobalScheduler() { + public static GlobalRegionScheduler getGlobalScheduler() { GlobalRegionScheduler grs; try { Object serverInstance = EsToolsBukkit.plugin.getServer(); @@ -112,11 +113,7 @@ public static void runFromEntity(Entity entity, Runnable task) { } public static void runSync(Runnable task) { - //getGlobalScheduler().execute(EsToolsBukkit.plugin, task); - //Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, task); - //runTaskLater(task, 1); - //getGlobalScheduler().run(EsToolsBukkit.plugin, (t) -> task.run()); - Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, task, 1); + FoliaServer.newChain().sync(task::run).sync(() -> Bukkit.getLogger().info("Main thread: " + Thread.currentThread().getName())).execute(); } public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index fa787c0..1995772 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -1,6 +1,5 @@ package net.serble.estools.ServerApi.Implementations.Folia; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventory; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; @@ -10,17 +9,20 @@ import java.util.HashMap; import java.util.Map; -public class FoliaInventory extends BukkitInventory { +public class FoliaInventory implements EsInventory { private final Inventory bukkitInv; public FoliaInventory(Inventory inv) { - super(inv); bukkitInv = inv; } + public Inventory getBukkitInventory() { + return bukkitInv; + } + @Override public EsItemStack[] getContents() { - org.bukkit.inventory.ItemStack[] bukkitItems = bukkitInv.getContents(); + ItemStack[] bukkitItems = bukkitInv.getContents(); EsItemStack[] items = new EsItemStack[bukkitItems.length]; for (int i = 0; i < bukkitItems.length; i++) { if (bukkitItems[i] == null) { @@ -39,6 +41,9 @@ public EsItemStack getItem(int slot) { @Override public boolean isEqualTo(EsInventory inv) { + if (inv == null) { + return false; + } return ((FoliaInventory) inv).getBukkitInventory().equals(bukkitInv); } @@ -53,4 +58,33 @@ public Map all(String material) { return out; } + + @Override + public void setContents(EsItemStack[] items) { + ItemStack[] bukkitItems = new ItemStack[items.length]; + for (int i = 0; i < items.length; i++) { + if (items[i] == null) { + bukkitItems[i] = null; + continue; + } + bukkitItems[i] = ((FoliaItemStack) items[i]).getBukkitItem(); + } + bukkitInv.setContents(bukkitItems); + } + + @Override + public void setItem(int slot, EsItemStack item) { + ItemStack stack = item == null ? null : ((FoliaItemStack) item).getBukkitItem(); + bukkitInv.setItem(slot, stack); + } + + @Override + public void addItem(EsItemStack stack) { + bukkitInv.addItem(((FoliaItemStack) stack).getBukkitItem()); + } + + @Override + public void clear() { + bukkitInv.clear(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 8e72fce..7238d23 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -1,28 +1,92 @@ package net.serble.estools.ServerApi.Implementations.Folia; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; -public class FoliaItemStack extends BukkitItemStack { - private final org.bukkit.inventory.ItemStack bukkitItem; +public class FoliaItemStack implements EsItemStack { + private final ItemStack bukkitItem; public FoliaItemStack(String mat, int amount) { - super(mat, amount); bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); } public FoliaItemStack(org.bukkit.inventory.ItemStack child) { - super(child); bukkitItem = child; } + public ItemStack getBukkitItem() { + return bukkitItem; + } + + @Override + public int getAmount() { + return bukkitItem.getAmount(); + } + + @Override + public void setAmount(int val) { + bukkitItem.setAmount(val); + } + + @Override + public String getType() { + return bukkitItem.getType().name(); + } + + @Override + public void setType(String val) { + bukkitItem.setType(Material.valueOf(val)); + } + @Override public EsItemMeta getItemMeta() { return new FoliaItemMeta(bukkitItem.getItemMeta()); } + @Override + public void setItemMeta(EsItemMeta meta) { + bukkitItem.setItemMeta(((FoliaItemMeta) meta).getBukkitMeta()); + } + + @SuppressWarnings("deprecation") + @Override + public void setDamage(int val) { + if (Main.minecraftVersion.getMinor() > 12) { + ItemMeta meta = bukkitItem.getItemMeta(); + if (meta instanceof Damageable) { + ((Damageable) meta).setDamage(val); + } + bukkitItem.setItemMeta(meta); + } else { + if (val > Short.MAX_VALUE) { + val = Short.MAX_VALUE; + } + + bukkitItem.setDurability((short) val); + } + } + + @SuppressWarnings("deprecation") + @Override + public int getDamage() { + if (Main.minecraftVersion.getMinor() > 12) { + return bukkitItem.getDurability(); + } + + ItemMeta meta = bukkitItem.getItemMeta(); + if (meta instanceof Damageable) { + return ((Damageable) meta).getDamage(); + } + return 0; + } + + @SuppressWarnings("MethodDoesntCallSuperMethod") // I don't care, it doesn't work like that @Override public EsItemStack clone() { return new FoliaItemStack(bukkitItem.clone()); @@ -30,6 +94,21 @@ public EsItemStack clone() { @Override public boolean isSimilar(EsItemStack stack) { - return ((FoliaItemStack) stack).getBukkitItem().isSimilar(bukkitItem); + return ((FoliaItemStack) stack).bukkitItem.isSimilar(bukkitItem); + } + + @Override + public void addEnchantment(String enchantment, int level) { + bukkitItem.addUnsafeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment), level); + } + + @Override + public void removeEnchantment(String enchantment) { + bukkitItem.removeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment)); + } + + @Override + public int getMaxStackSize() { + return bukkitItem.getMaxStackSize(); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java index d07d9c2..529d862 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -3,11 +3,13 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSoundCategory; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; import java.util.HashSet; @@ -48,7 +50,11 @@ public void setSaturation(double val) { @Override public EsItemStack getMainHand() { - return new FoliaItemStack(bukkitGetMainHand()); + ItemStack mainHand = bukkitGetMainHand(); + if (mainHand == null) { + return null; + } + return new FoliaItemStack(mainHand); } @Override @@ -112,8 +118,8 @@ public boolean isFlying() { } @Override - public void playSound(String sound, EsLocation loc, int volume, int pitch) { - bukkitPlayer.playSound(FoliaHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + public void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { + bukkitPlayer.playSound(FoliaHelper.toBukkitLocation(loc), Sound.valueOf(sound), FoliaHelper.toBukkitSoundCategory(category), volume, pitch); } @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 85cb310..81de784 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -1,13 +1,16 @@ package net.serble.estools.ServerApi.Implementations.Folia; +import co.aikar.taskchain.BukkitTaskChainFactory; +import co.aikar.taskchain.TaskChain; +import co.aikar.taskchain.TaskChainFactory; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; -import net.serble.estools.Effects; +import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.Main; -import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitTabCompleteGenerator; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; +import org.bukkit.command.PluginCommand; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -23,10 +26,18 @@ public class FoliaServer implements EsServerSoftware { private final JavaPlugin plugin; + private final FoliaEventsListener listener; private final Map tasks = new HashMap<>(); + private static TaskChainFactory taskChainFactory; public FoliaServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; + taskChainFactory = BukkitTaskChainFactory.create(plugin); + listener = new FoliaEventsListener(); + } + + public static TaskChain newChain() { + return taskChainFactory.newChain(); } @Override @@ -224,6 +235,15 @@ public File getDataFolder() { @Override public void dispatchCommand(EsCommandSender sender, String cmd) { +// CompletableFuture> completeTask = new CompletableFuture>() +// .whenComplete((c, exception) -> { +// Main.logger.info("WE DID THE WHEN COMPLETE"); +// Bukkit.dispatchCommand(c.getKey(), c.getValue()); +// }); +// +// FoliaHelper.getGlobalScheduler().runDelayed(plugin, task -> { +// completeTask.complete(new AbstractMap.SimpleImmutableEntry<>(((FoliaPlayer) sender).getBukkitPlayer(), cmd)); +// }, 1L); FoliaHelper.runSync(() -> Bukkit.dispatchCommand(FoliaHelper.toBukkitCommandSender(sender), cmd)); } @@ -280,6 +300,34 @@ public EsLogger getLogger() { @Override public void startEvents() { - Bukkit.getPluginManager().registerEvents(new FoliaEventsListener(), EsToolsBukkit.plugin); + Bukkit.getPluginManager().registerEvents(listener, EsToolsBukkit.plugin); + } + + @Override + public void registerCommand(String cmd, EsToolsTabCompleter tab) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + command.setExecutor(listener); + if (Main.tabCompleteEnabled) { + command.setTabCompleter(BukkitTabCompleteGenerator.generate(tab)); + } + } + + @Override + public void setTabCompleter(String cmd, EsToolsTabCompleter tab) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + if (Main.tabCompleteEnabled) { + command.setTabCompleter(BukkitTabCompleteGenerator.generate(tab)); + } + } + + @Override + public void setCommandPermission(String cmd, String perm) { + PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); + command.setPermission(perm); + + if (Main.minecraftVersion.getMinor() > 0) { + //noinspection deprecation, is still useful in pre 1.13 and technically is useful in rare situations post 1.13 + command.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); + } } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 06588ca..1812753 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -2,7 +2,9 @@ import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSoundCategory; +@SuppressWarnings("unused") public interface EsPlayer extends EsLivingEntity { int getFoodLevel(); void setFoodLevel(int val); @@ -21,6 +23,6 @@ public interface EsPlayer extends EsLivingEntity { boolean getAllowFlight(); void setAllowFlight(boolean val); boolean isFlying(); - void playSound(String sound, EsLocation loc, int volume, int pitch); + void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch); void updateInventory(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 644bab9..6b786f9 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.EsToolsTabCompleter; import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotType; @@ -7,6 +8,7 @@ import java.util.Collection; import java.util.UUID; +@SuppressWarnings("unused") public interface EsServerSoftware { EsPlayer getPlayer(String name); EsEntity getEntity(UUID uuid); @@ -29,4 +31,7 @@ public interface EsServerSoftware { void cancelTask(int id); EsLogger getLogger(); void startEvents(); + void registerCommand(String cmd, EsToolsTabCompleter tab); + void setTabCompleter(String cmd, EsToolsTabCompleter tab); + void setCommandPermission(String cmd, String perm); } From 19163372eeb41622f23ceb48a84eb4151c35d084 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 26 May 2024 21:34:30 +1000 Subject: [PATCH 14/74] New config system, various fixes, vault signs --- TestedVersions.txt | 2 +- abstraction.md | 3 +- pom.xml | 10 ++ .../net/serble/estools/Commands/Buddha.java | 18 ++- .../net/serble/estools/Commands/CChest.java | 23 +--- .../serble/estools/Commands/Give/Give.java | 99 +++------------ .../java/net/serble/estools/Commands/God.java | 17 +-- .../net/serble/estools/Commands/SafeTp.java | 7 +- .../serble/estools/Commands/Warps/Warp.java | 12 +- .../estools/Commands/Warps/WarpLocation.java | 44 ++++--- .../estools/Commands/Warps/WarpManager.java | 55 ++------- .../serble/estools/Config/ConfigManager.java | 87 ++++++++++++++ .../Schemas/GeneralConfig/EsToolsConfig.java | 32 +++++ .../Schemas/GeneralConfig/UpdaterConfig.java | 41 +++++++ .../Config/Schemas/Give/GiveConfig.java | 43 +++++++ .../Config/Schemas/Give/GiveSettings.java | 23 ++++ .../net/serble/estools/ConfigManager.java | 113 ------------------ .../net/serble/estools/EntityCommand.java | 6 +- .../estools/Entrypoints/EsToolsBukkit.java | 3 + src/main/java/net/serble/estools/Main.java | 55 ++------- .../serble/estools/ServerApi/EsAction.java | 32 +++++ .../serble/estools/ServerApi/EsLocation.java | 32 ++++- .../Events/EsPlayerInteractEvent.java | 31 +++++ .../ServerApi/Events/EsSignChangeEvent.java | 30 +++++ .../Implementations/Bukkit/BukkitBlock.java | 5 + .../Bukkit/BukkitConfigMigrator.java | 8 ++ .../Bukkit/BukkitEventsListener.java | 30 ++++- .../Implementations/Bukkit/BukkitHelper.java | 18 +++ .../Implementations/Bukkit/BukkitServer.java | 29 ++++- .../Implementations/Bukkit/BukkitSign.java | 12 ++ .../Folia/FoliaEventsListener.java | 17 ++- .../Implementations/Folia/FoliaServer.java | 29 ++++- .../Implementations/Folia/FoliaSign.java | 14 ++- .../estools/ServerApi/Interfaces/EsBlock.java | 2 + .../Interfaces/EsServerSoftware.java | 10 ++ .../estools/ServerApi/Interfaces/EsSign.java | 2 + .../serble/estools/ServerApi/Position.java | 16 +++ .../estools/ServerApi/ServerPlatform.java | 14 ++- .../net/serble/estools/Signs/SignMain.java | 46 +++---- src/main/java/net/serble/estools/Updater.java | 23 ++-- src/main/resources/config.yml | 10 +- 41 files changed, 688 insertions(+), 415 deletions(-) create mode 100644 src/main/java/net/serble/estools/Config/ConfigManager.java create mode 100644 src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/EsToolsConfig.java create mode 100644 src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/UpdaterConfig.java create mode 100644 src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java create mode 100644 src/main/java/net/serble/estools/Config/Schemas/Give/GiveSettings.java delete mode 100644 src/main/java/net/serble/estools/ConfigManager.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsAction.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Events/EsSignChangeEvent.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java diff --git a/TestedVersions.txt b/TestedVersions.txt index ef4b1f3..3ad1ccb 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -1,5 +1,5 @@ Folia: -1.20.4 Failing +1.20.4 Failing (/estools test) Bukkit: 1.20.4 diff --git a/abstraction.md b/abstraction.md index 56e0527..c62ed5f 100644 --- a/abstraction.md +++ b/abstraction.md @@ -1,4 +1,3 @@ # TODO -- Config -- Fix testing +- Fix testing on folia - Fix teleport event not working on folia \ No newline at end of file diff --git a/pom.xml b/pom.xml index e29cb11..bcd32aa 100644 --- a/pom.xml +++ b/pom.xml @@ -99,6 +99,11 @@ aikar https://repo.aikar.co/content/groups/aikar/ + + Sonatype-public + SnakeYAML repository + http://oss.sonatype.org/content/groups/public/ + @@ -142,5 +147,10 @@ taskchain-bukkit 3.7.2 + + org.yaml + snakeyaml + 2.2-SNAPSHOT + diff --git a/src/main/java/net/serble/estools/Commands/Buddha.java b/src/main/java/net/serble/estools/Commands/Buddha.java index 0dea2af..8e8d6fa 100644 --- a/src/main/java/net/serble/estools/Commands/Buddha.java +++ b/src/main/java/net/serble/estools/Commands/Buddha.java @@ -1,6 +1,6 @@ package net.serble.estools.Commands; -import net.serble.estools.ConfigManager; +import net.serble.estools.Config.ConfigManager; import net.serble.estools.EntityCommand; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEventListener; @@ -8,22 +8,21 @@ import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import java.util.*; public class Buddha extends EntityCommand implements EsEventListener { private static final HashMap currentPlayers = new HashMap<>(); private static final String usage = genUsage("/buddha [entity] [time]"); + private static final String configFile = "buddhas.yml"; - @Override + @SuppressWarnings("unchecked") // I'm right, trust me + @Override public void onEnable() { Main.registerEvents(this); - FileConfiguration f = ConfigManager.load("gods.yml"); - List buddhas = f.getStringList("buddhas"); - buddhas.forEach(w -> currentPlayers.put(UUID.fromString(w), -1)); + List f = (List) ConfigManager.load(configFile, ArrayList.class); + f.forEach(w -> currentPlayers.put(UUID.fromString(w), -1)); } @Override @@ -90,8 +89,6 @@ public boolean execute(EsCommandSender sender, String[] args) { } private static void save() { - FileConfiguration f = new YamlConfiguration(); - List buddhas = new ArrayList<>(); for (Map.Entry kv : currentPlayers.entrySet()) { // only save if there isn't a time limit! @@ -100,8 +97,7 @@ private static void save() { } } - f.set("buddhas", buddhas); - ConfigManager.save("gods.yml", f); + ConfigManager.save(configFile, buddhas); } @Override diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 110fd91..1641f61 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -1,7 +1,7 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import net.serble.estools.ConfigManager; +import net.serble.estools.Config.ConfigManager; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsEventListener; @@ -9,12 +9,10 @@ import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Interfaces.*; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import java.util.*; -// TODO: This class has config +// TODO: Saving and loading data, can't instantiate EsInventory public class CChest extends EsToolsCommand implements EsEventListener { public static HashMap cChests = new HashMap<>(); @@ -174,14 +172,8 @@ private void onInventoryDrag(final EsInventoryDragEvent e) { public static EsInventory loadPlayer(EsPlayer p) { UUID uid = p.getUniqueId(); - FileConfiguration f = ConfigManager.load("cchests/" + uid + ".yml"); - - if (!f.contains("items")) { - return null; - } - - @SuppressWarnings("unchecked") - EsItemStack[] content = ((ArrayList) Objects.requireNonNull(f.get("items"))).toArray(new EsItemStack[0]); + EsInventory f = ConfigManager.load("cchests/" + uid + ".yml", EsInventory.class); + EsItemStack[] content = f.getContents(); EsInventory inv = Main.server.createInventory(null, 54, translate("&1Creative Chest")); inv.setContents(content); @@ -190,17 +182,14 @@ public static EsInventory loadPlayer(EsPlayer p) { return inv; } - // TODO: Config public static void savePlayer(EsPlayer p) { UUID uid = p.getUniqueId(); if (!cChests.containsKey(uid)) { return; } - - FileConfiguration f = new YamlConfiguration(); - f.set("items", cChests.get(uid).getContents()); - ConfigManager.save("cchests/" + uid + ".yml", f); + + ConfigManager.save("cchests/" + uid + ".yml", cChests.get(uid)); } private void onClose(EsInventoryCloseEvent e) { diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index 46f71fa..330bd10 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -1,20 +1,14 @@ package net.serble.estools.Commands.Give; import net.serble.estools.*; -import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.Config.ConfigManager; +import net.serble.estools.Config.Schemas.Give.GiveConfig; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import java.io.*; -import java.nio.file.Files; import java.util.*; import java.util.Map.Entry; -// TODO: Config class public class Give implements EsToolsTabCompleter { private static HashMap materialNames; @@ -45,103 +39,40 @@ public List onTabComplete(EsCommandSender sender, String[] args) { return EsToolsCommand.fixTabComplete(tab, args[args.length - 1]); } - - @SuppressWarnings("DataFlowIssue") // Not possible + public static void enable() { // initialise hashmaps //noinspection Convert2Diamond materialNames = new HashMap(); // Load config - FileConfiguration f = ConfigManager.load("give.yml"); + GiveConfig config = ConfigManager.load("give.yml", GiveConfig.class); // check if it has settings - HashMap materials = new HashMap<>(); - - if ( f.contains("settings") && - f.contains("settings.addWithoutUnderscores") && - f.contains("settings.removeWithUnderscores")) { // if it has these values then load files - - materials = loadItems(f); - } - - if (materials.isEmpty()) { // if it hasn't loaded or if it cant load anything - Bukkit.getLogger().info("Creating new give.yml"); - - try { - copyDefaultGiveYML(); - f = ConfigManager.load(new File(EsToolsBukkit.plugin.getDataFolder(), "give.yml")); - materials = loadItems(f); - } catch (Exception ignored) { } - } + Map materials = config.getItems(); // Load normal items - boolean addWithoutUnderscores = (boolean) f.get("settings.addWithoutUnderscores"); - boolean removeWithUnderscores = (boolean) f.get("settings.removeWithUnderscores"); - - for (Material mat : Material.values()) { - if (Main.minecraftVersion.getMinor() >= 12 && !mat.isItem()) continue; - - String name = mat.toString().toUpperCase(); - - if (name.contains("_") && addWithoutUnderscores) { - materialNames.put(name.replace("_",""), mat.name()); + for (String mat : Main.server.getMaterials(true)) { + if (mat == null) { + continue; + } + String name = mat.toUpperCase(); + if (name.contains("_") && config.getSettings().isAddWithoutUnderscores()) { + materialNames.put(name.replace("_",""), mat); - if (removeWithUnderscores) { + if (config.getSettings().isRemoveWithUnderscores()) { continue; } } - materialNames.put(name, mat.name()); + materialNames.put(name, mat); } // Load custom items for (Entry s : materials.entrySet()) { - Material mat = Material.getMaterial(s.getValue().toUpperCase()); - - if (mat != null) { - materialNames.put(s.getKey().toUpperCase(), mat.name()); - } - } - } - - private static HashMap loadItems(FileConfiguration f) { - HashMap ms = new HashMap<>(); - - if (f.contains("items")) { - ConfigurationSection itemsSection = f.getConfigurationSection("items"); - assert itemsSection != null; - - itemsSection.getKeys(false).forEach(key -> { - String content = (String) f.get("items." + key); - ms.put(key, content); - }); + materialNames.put(s.getKey().toUpperCase(), s.getValue().toUpperCase()); // Assume it exists, we trust our users = ) } - - return ms; } - - private static void copyDefaultGiveYML() throws IOException { - // The class loader that loaded the class - ClassLoader classLoader = Give.class.getClassLoader(); - InputStream inputStream = classLoader.getResourceAsStream("give.yml"); - - // the stream holding the file content - if (inputStream == null) { - return; - } - - byte[] buffer = new byte[inputStream.available()]; - //noinspection ResultOfMethodCallIgnored - inputStream.read(buffer); - - File targetFile = new File(EsToolsBukkit.plugin.getDataFolder(), "give.yml"); - OutputStream outStream = Files.newOutputStream(targetFile.toPath()); - outStream.write(buffer); - - inputStream.close(); - outStream.close(); - } public static EsItemStack parseArgs(String[] args) { if (args.length == 0) { diff --git a/src/main/java/net/serble/estools/Commands/God.java b/src/main/java/net/serble/estools/Commands/God.java index 92ea072..42ffa60 100644 --- a/src/main/java/net/serble/estools/Commands/God.java +++ b/src/main/java/net/serble/estools/Commands/God.java @@ -1,6 +1,6 @@ package net.serble.estools.Commands; -import net.serble.estools.ConfigManager; +import net.serble.estools.Config.ConfigManager; import net.serble.estools.EntityCommand; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEventListener; @@ -8,23 +8,21 @@ import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import java.util.*; -// TODO: Event command needs migrating public class God extends EntityCommand implements EsEventListener { private static final HashMap currentPlayers = new HashMap<>(); private static final String usage = genUsage("/god [entity] [time]"); + private static final String configFile = "gods.yml"; + @SuppressWarnings("unchecked") // I'm right, trust me @Override public void onEnable() { Main.registerEvents(this); - FileConfiguration f = ConfigManager.load("gods.yml"); - List godList = f.getStringList("gods"); - godList.forEach(w -> currentPlayers.put(UUID.fromString(w), -1)); + List f = (List) ConfigManager.load(configFile, ArrayList.class); + f.forEach(w -> currentPlayers.put(UUID.fromString(w), -1)); } @Override @@ -91,8 +89,6 @@ public boolean execute(EsCommandSender sender, String[] args) { } private static void save() { - FileConfiguration f = new YamlConfiguration(); - List gods = new ArrayList<>(); for (Map.Entry kv : currentPlayers.entrySet()) { // only save if there isn't a time limit! @@ -101,8 +97,7 @@ private static void save() { } } - f.set("gods", gods); - ConfigManager.save("gods.yml", f); + ConfigManager.save(configFile, gods); } @Override diff --git a/src/main/java/net/serble/estools/Commands/SafeTp.java b/src/main/java/net/serble/estools/Commands/SafeTp.java index 95ed1f7..20f305d 100644 --- a/src/main/java/net/serble/estools/Commands/SafeTp.java +++ b/src/main/java/net/serble/estools/Commands/SafeTp.java @@ -1,6 +1,5 @@ package net.serble.estools.Commands; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEventListener; @@ -15,7 +14,7 @@ public class SafeTp extends EsToolsCommand implements EsEventListener { @Override public void onEnable() { - enabled = Main.plugin.getConfig().getBoolean("safetp", true); + enabled = Main.plugin.getConfig().isSafeTp(); Main.registerEvents(this); } @@ -34,8 +33,8 @@ public boolean execute(EsCommandSender sender, String[] args) { send(sender, "&aTeleporting now &6&lWILL NOT&a make you take fall damage!"); } - Main.plugin.getConfig().set("safetp", enabled); - EsToolsBukkit.plugin.saveConfig(); // TODO: How does this work? + Main.plugin.getConfig().setSafeTp(enabled); + Main.plugin.saveConfig(); return true; } diff --git a/src/main/java/net/serble/estools/Commands/Warps/Warp.java b/src/main/java/net/serble/estools/Commands/Warps/Warp.java index 354871c..311ff75 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/Warp.java +++ b/src/main/java/net/serble/estools/Commands/Warps/Warp.java @@ -36,7 +36,7 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - p.teleport(warp.location); + p.teleport(warp.getLocation()); send(p, "&aTeleported to warp &6%s&a.", warpName); return true; } @@ -48,10 +48,10 @@ private static boolean canUseWarp(EsPlayer p, WarpLocation warp) { boolean hasManage = p.hasPermission("estools.warps.manage"); boolean hasDefault = p.hasPermission("estools.warps.default"); - String warpPermission = "estools.warp." + warp.name; + String warpPermission = "estools.warp." + warp.getName(); // you can only use global warps if you are in the same world, or have manage permission - if (!warp.global && !hasManage && !p.getWorld().equals(warp.location.getWorld())) { + if (!warp.isGlobal() && !hasManage && !p.getWorld().equals(warp.getLocation().getWorld())) { return false; } @@ -68,10 +68,10 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA if (args.length == 1) { for (WarpLocation warp : WarpManager.warps.values()) { - boolean inSameWorld = p.getWorld().equals(warp.location.getWorld()); + boolean inSameWorld = p.getWorld().equals(warp.getLocation().getWorld()); - if (canUseWarp(p, warp) && (warp.global || inSameWorld)) { - tab.add(warp.name); + if (canUseWarp(p, warp) && (warp.isGlobal() || inSameWorld)) { + tab.add(warp.getName()); } } } diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java index 97fb07f..8768997 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpLocation.java @@ -1,37 +1,33 @@ package net.serble.estools.Commands.Warps; import net.serble.estools.ServerApi.EsLocation; -import org.bukkit.configuration.serialization.ConfigurationSerializable; -import org.bukkit.configuration.serialization.SerializableAs; -import java.util.Map; -import java.util.HashMap; +public class WarpLocation { + private EsLocation location; + private String name; + private boolean global; -@SerializableAs("WarpLocation") -public class WarpLocation implements ConfigurationSerializable { - public EsLocation location; - public String name; - public boolean global; - - @Override - public Map serialize() { - Map result = new HashMap<>(); + public String getName() { + return name; + } - result.put("location", location); - result.put("name", name); - result.put("global", global); + public EsLocation getLocation() { + return location; + } - return result; + public boolean isGlobal() { + return global; } - @SuppressWarnings("unused") - public static WarpLocation deserialize(Map args) { - WarpLocation warp = new WarpLocation(); + public void setLocation(EsLocation location) { + this.location = location; + } - warp.location = (EsLocation)args.get("location"); - warp.name = (String)args.get("name"); - warp.global = (boolean)args.get("global"); + public void setGlobal(boolean global) { + this.global = global; + } - return warp; + public void setName(String name) { + this.name = name; } } diff --git a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java index 5484e1e..b9e329e 100644 --- a/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java +++ b/src/main/java/net/serble/estools/Commands/Warps/WarpManager.java @@ -1,17 +1,15 @@ package net.serble.estools.Commands.Warps; import net.serble.estools.EsToolsCommand; -import net.serble.estools.ConfigManager; +import net.serble.estools.Config.ConfigManager; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.configuration.serialization.ConfigurationSerialization; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; // this command is /warps public class WarpManager extends EsToolsCommand { @@ -22,45 +20,16 @@ public class WarpManager extends EsToolsCommand { private static final String consoleUsage = genUsage("/warps list"); - public static final HashMap warps = new HashMap<>(); + public static Map warps = new HashMap<>(); + @SuppressWarnings("unchecked") @Override public void onEnable() { - ConfigurationSerialization.registerClass(WarpLocation.class, "WarpLocation"); - - FileConfiguration f = ConfigManager.load("warps.yml"); - - List warpList = f.getList("warps"); - if (warpList == null) { - return; - } - - warpList.sort((w1, w2) -> { - if (w1 instanceof WarpLocation && w2 instanceof WarpLocation) { - WarpLocation warp1 = (WarpLocation)w1; - WarpLocation warp2 = (WarpLocation)w1; - - return warp1.name.compareTo(warp2.name); - } - - return 0; - }); - - warpList.forEach(w -> { - if (w instanceof WarpLocation) { - WarpLocation warp = (WarpLocation)w; - warps.put(warp.name, warp); - } - }); + warps = ConfigManager.load("warps.yml", HashMap.class, WarpLocation.class); } private static void saveWarps() { - FileConfiguration f = new YamlConfiguration(); - - List warpList = new ArrayList<>(warps.values()); - f.set("warps", warpList); - - ConfigManager.save("warps.yml", f); + ConfigManager.save("warps.yml", warps); } public boolean execute(EsCommandSender sender, String[] args) { @@ -85,8 +54,8 @@ public boolean execute(EsCommandSender sender, String[] args) { for (WarpLocation warp : warps.values()) { warpList.append(translate("\n&6%s &aat &6%s &ain &6%s &ais &6%s", - warp.name, locationToString(warp.location), - warp.location.getWorld().getName(), warp.global ? "global" : "local")); + warp.getName(), locationToString(warp.getLocation()), + warp.getLocation().getWorld().getName(), warp.isGlobal() ? "global" : "local")); } send(sender, warpList.toString()); @@ -137,7 +106,7 @@ warp.name, locationToString(warp.location), switch (args[0].toLowerCase()) { case "add": { if (warp != null) { - send(sender, "&cWarp does not exist, please use add to create it."); + send(sender, "&cWarp already exists."); return false; } @@ -209,9 +178,9 @@ else if (args.length < 6) { // if you supplied only some of the coordinates, wha } WarpLocation warp = new WarpLocation(); - warp.location = loc; - warp.global = global; - warp.name = warpName; + warp.setLocation(loc); + warp.setGlobal(global); + warp.setName(warpName); warps.put(warpName, warp); saveWarps(); diff --git a/src/main/java/net/serble/estools/Config/ConfigManager.java b/src/main/java/net/serble/estools/Config/ConfigManager.java new file mode 100644 index 0000000..21d7251 --- /dev/null +++ b/src/main/java/net/serble/estools/Config/ConfigManager.java @@ -0,0 +1,87 @@ +package net.serble.estools.Config; + +import java.io.*; +import java.util.Arrays; + +import net.serble.estools.Main; +import org.yaml.snakeyaml.LoaderOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.inspector.TagInspector; + +public class ConfigManager { + + @SuppressWarnings("rawtypes") + public static T load(String fileName, Class base, Class... allowedClasses) { + String[] cs = new String[allowedClasses.length]; + for (int i = 0; i < cs.length; i++) { + cs[i] = allowedClasses[i].getName(); + } + File file = new File(Main.server.getDataFolder(), fileName); + return load(file, base, cs); + } + + public static T load(String fileName, Class base, String... allowedClasses) { + File file = new File(Main.server.getDataFolder(), fileName); + return load(file, base, allowedClasses); + } + + // Exists so the above 2 don't make ambig reference when array is not supplied + public static T load(String fileName, Class base) { + File file = new File(Main.server.getDataFolder(), fileName); + return load(file, base); + } + + public static T load(File configFile, Class base, String... allowedClasses) { + if (!configFile.exists()) { + // Save a default version + try { + Object instance = base.newInstance(); + save(configFile, instance); + } catch (IllegalAccessException | InstantiationException e) { + Main.logger.severe("Failed to save default config, this is a bug"); + throw new RuntimeException("Bug with saving default config: " + e); + } + } + + LoaderOptions options = new LoaderOptions(); + TagInspector taginspector = tag -> + tag.getClassName().equals(base.getName()) || + Arrays.stream(allowedClasses).anyMatch(c -> c.equals(tag.getClassName())); + options.setTagInspector(taginspector); + Yaml yaml = new Yaml(new Constructor(base, options)); + try { + return yaml.load(new FileInputStream(configFile)); + } catch (FileNotFoundException e) { + Main.logger.severe("Impossible situation reached, files that we created don't exist"); + throw new RuntimeException("Impossible"); + } + } + + public static void save(String file, Object obj) { + File configFile = new File(Main.server.getDataFolder(), file); + save(configFile, obj); + } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public static void save(File configFile, Object obj) { + if (!configFile.exists()) { + configFile.getParentFile().mkdirs(); + try { + configFile.createNewFile(); + } catch (IOException e) { + Main.logger.warning("Failed to create config file: " + e); + } + } + + Yaml yaml = new Yaml(); + String contents = yaml.dump(obj); + try { + FileWriter writer = new FileWriter(configFile); + writer.write(contents); + writer.close(); + } catch (IOException e) { + Main.logger.severe("Failed to save config file: " + e); + } + } +} diff --git a/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/EsToolsConfig.java b/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/EsToolsConfig.java new file mode 100644 index 0000000..205eb83 --- /dev/null +++ b/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/EsToolsConfig.java @@ -0,0 +1,32 @@ +package net.serble.estools.Config.Schemas.GeneralConfig; + +@SuppressWarnings("unused") // SnakeYAML needs the setters +public class EsToolsConfig { + private boolean safeTp = false; + private boolean metrics = true; + private UpdaterConfig updater = new UpdaterConfig(); + + public boolean isSafeTp() { + return safeTp; + } + + public void setSafeTp(boolean safeTp) { + this.safeTp = safeTp; + } + + public boolean isMetrics() { + return metrics; + } + + public void setMetrics(boolean metrics) { + this.metrics = metrics; + } + + public UpdaterConfig getUpdater() { + return updater; + } + + public void setUpdater(UpdaterConfig updater) { + this.updater = updater; + } +} diff --git a/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/UpdaterConfig.java b/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/UpdaterConfig.java new file mode 100644 index 0000000..5097183 --- /dev/null +++ b/src/main/java/net/serble/estools/Config/Schemas/GeneralConfig/UpdaterConfig.java @@ -0,0 +1,41 @@ +package net.serble.estools.Config.Schemas.GeneralConfig; + +@SuppressWarnings("unused") // SnakeYAML needs the setters +public class UpdaterConfig { + private String githubReleasesUrl = "https://api.github.com/repos/CoPokBl/EsTools/releases/latest"; + private boolean autoUpdate = false; + private boolean warnOnOutdated = false; + private boolean logOnOutdated = true; + + public boolean isAutoUpdate() { + return autoUpdate; + } + + public boolean isLogOnOutdated() { + return logOnOutdated; + } + + public boolean isWarnOnOutdated() { + return warnOnOutdated; + } + + public void setAutoUpdate(boolean autoUpdate) { + this.autoUpdate = autoUpdate; + } + + public String getGithubReleasesUrl() { + return githubReleasesUrl; + } + + public void setGithubReleasesUrl(String githubReleasesUrl) { + this.githubReleasesUrl = githubReleasesUrl; + } + + public void setLogOnOutdated(boolean logOnOutdated) { + this.logOnOutdated = logOnOutdated; + } + + public void setWarnOnOutdated(boolean warnOnOutdated) { + this.warnOnOutdated = warnOnOutdated; + } +} diff --git a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java new file mode 100644 index 0000000..da31169 --- /dev/null +++ b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java @@ -0,0 +1,43 @@ +package net.serble.estools.Config.Schemas.Give; + +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("unused") // SnakeYAML needs the setters +public class GiveConfig { + private GiveSettings settings = new GiveSettings(); + private Map items = new HashMap() {{ + put("boat", "oak_boat"); + put("pearl", "ender_pearl"); + put("sign", "oak_sign"); + put("button", "stone_button"); + put("sapling", "oak_sapling"); + put("log", "oak_log"); + put("door", "oak_door"); + put("fence", "oak_fence"); + put("fence_gate", "oak_fence_gate"); + put("leaves", "oak_leaves"); + put("planks", "oak_planks"); + put("pressure_plate", "stone_pressure_plate"); + put("trapdoor", "oak_trapdoor"); + put("steak", "cooked_beef"); + put("water", "water_bucket"); + put("lava", "lava_bucket"); + }}; + + public GiveSettings getSettings() { + return settings; + } + + public void setSettings(GiveSettings settings) { + this.settings = settings; + } + + public Map getItems() { + return items; + } + + public void setItems(Map items) { + this.items = items; + } +} diff --git a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveSettings.java b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveSettings.java new file mode 100644 index 0000000..54b3bee --- /dev/null +++ b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveSettings.java @@ -0,0 +1,23 @@ +package net.serble.estools.Config.Schemas.Give; + +@SuppressWarnings("unused") // SnakeYAML needs the setters +public class GiveSettings { + private boolean addWithoutUnderscores = true; + private boolean removeWithUnderscores = false; + + public boolean isAddWithoutUnderscores() { + return addWithoutUnderscores; + } + + public boolean isRemoveWithUnderscores() { + return removeWithUnderscores; + } + + public void setAddWithoutUnderscores(boolean addWithoutUnderscores) { + this.addWithoutUnderscores = addWithoutUnderscores; + } + + public void setRemoveWithUnderscores(boolean removeWithUnderscores) { + this.removeWithUnderscores = removeWithUnderscores; + } +} diff --git a/src/main/java/net/serble/estools/ConfigManager.java b/src/main/java/net/serble/estools/ConfigManager.java deleted file mode 100644 index e908b05..0000000 --- a/src/main/java/net/serble/estools/ConfigManager.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.serble.estools; - -import java.io.*; -import java.util.Set; - -import org.bukkit.Bukkit; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; - -public class ConfigManager { - - public static final Main plugin = Main.plugin; - - public static FileConfiguration load(String fileName) { - return load(new File(Main.server.getDataFolder(), fileName)); - } - - @SuppressWarnings("ResultOfMethodCallIgnored") - public static FileConfiguration load(File configFile) { - if (!configFile.exists()) { - configFile.getParentFile().mkdirs(); - try { - configFile.createNewFile(); - } catch (IOException e) { - Bukkit.getLogger().warning("Failed to create config file: " + e); - } - } - - FileConfiguration config = new YamlConfiguration(); - try { - config.load(configFile); - } catch (IOException | InvalidConfigurationException | IllegalArgumentException e) { - Bukkit.getLogger().severe("Failed to read config file: " + e); - } - - return config; - } - - @SuppressWarnings("ResultOfMethodCallIgnored") - public static void save(String file, FileConfiguration config) { - File configFile = new File(Main.server.getDataFolder(), file); - if (!configFile.exists()) { - configFile.getParentFile().mkdirs(); - try { - configFile.createNewFile(); - } catch (IOException e) { - Bukkit.getLogger().warning("Failed to create config file: " + e); - } - } - - try { - config.save(configFile); - } catch (IOException e) { - Bukkit.getLogger().severe("Failed to save config file: " + e); - } - } - - // Make sure to save the file after you run this, we don't do that automatically - public static boolean patchDefaults(FileConfiguration config, FileConfiguration defaults) { - return patchDefaults("", config, defaults); - } - - // Patch a section, needed because we must recursively traverse the key hierarchy - private static boolean patchDefaults(String path, FileConfiguration config, FileConfiguration defaults) { - Main.asrt(path != null); - Main.asrt(config != null); - Main.asrt(defaults != null); - - boolean changedAnything = false; - - ConfigurationSection section = defaults.getConfigurationSection(path); - if (section == null) { - if (!config.contains(path)) { - config.set(path, defaults.get(path)); - changedAnything = true; - } - } else { - Set keys = section.getKeys(false); - for (String key : keys) { - String updatedPath = path.isEmpty() ? key : path + "." + key; - changedAnything = patchDefaults(updatedPath, config, defaults) || changedAnything; - } - } - - return changedAnything; - } - - public static boolean patchDefaults(FileConfiguration config, InputStream defaultsRes) { - InputStreamReader reader = new InputStreamReader(defaultsRes); - FileConfiguration defaults = new YamlConfiguration(); - String fileContents; - try { - StringBuilder contentsBuilder = new StringBuilder(); - BufferedReader bufferedReader = new BufferedReader(reader); - - String line; - while ((line = bufferedReader.readLine()) != null) { - contentsBuilder.append(line).append("\n"); - } - - bufferedReader.close(); - fileContents = contentsBuilder.toString(); - defaults.loadFromString(fileContents); - } catch (IOException | InvalidConfigurationException e) { - Bukkit.getLogger().severe("Could not load default config file, there is a good chance this is bug with EsTools"); - return false; - } - - return patchDefaults(config, defaults); - } -} diff --git a/src/main/java/net/serble/estools/EntityCommand.java b/src/main/java/net/serble/estools/EntityCommand.java index 928e0a9..4d0c756 100644 --- a/src/main/java/net/serble/estools/EntityCommand.java +++ b/src/main/java/net/serble/estools/EntityCommand.java @@ -71,15 +71,15 @@ public static T getTarget(final EsEntity entity, final double threshold = 1; for (final T other : entities) { final Position n = other.getLocation() - .subtract(entity.getLocation()); + .subtract(entity.getLocation()).toPosition(); if (entity.getLocation().getDirection().normalise().crossProduct(n) .lengthSquared() < threshold && n.normalise().dot( entity.getLocation().getDirection().normalise()) >= 0) { if (target == null || target.getLocation().distanceSquared( - entity.getLocation()) > other.getLocation() - .distanceSquared(entity.getLocation())) + entity.getLocation().toPosition()) > other.getLocation() + .distanceSquared(entity.getLocation().toPosition())) target = other; } } diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java index 588f525..36f2327 100644 --- a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -11,6 +11,9 @@ public class EsToolsBukkit extends JavaPlugin { @Override public void onEnable() { + // This is needed for SnakeYAML to work on Spigot, see https://www.spigotmc.org/threads/cannot-use-shaded-snakeyaml-to-construct-class-object.136707/#post-4329381 + Thread.currentThread().setContextClassLoader(this.getClassLoader()); + plugin = this; ServerPlatform platform = isFolia() ? ServerPlatform.Folia : ServerPlatform.Bukkit; diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index ce07895..a4c6306 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -8,6 +8,8 @@ import net.serble.estools.Commands.PowerPick.*; import net.serble.estools.Commands.Teleport.*; import net.serble.estools.Commands.Warps.*; +import net.serble.estools.Config.ConfigManager; +import net.serble.estools.Config.Schemas.GeneralConfig.EsToolsConfig; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; @@ -19,14 +21,9 @@ import net.serble.estools.Signs.SignMain; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; -import org.bukkit.configuration.file.FileConfiguration; // TODO: The only remaining bukkit import in Main.java import net.serble.estools.Commands.*; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -38,7 +35,7 @@ public class Main { public static ServerPlatform platform; public static SemanticVersion minecraftVersion; public static boolean tabCompleteEnabled = true; - private static FileConfiguration config; // Get with overriden getConfig() method + private static EsToolsConfig config; // Get with overriden getConfig() method public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; public static EsServerSoftware server; @@ -59,9 +56,8 @@ public void enable() { server = platform.getServerInstance(context); logger = server.getLogger(); - logger.info("Starting EsTools on platform: " + platform.name()); - minecraftVersion = server.getVersion(); + logger.info("Starting EsTools on platform: " + platform.name() + " (MC: " + minecraftVersion.getString() + ")"); Effects.load(); @@ -73,20 +69,12 @@ public void enable() { } } - // Create config if not exists, saveDefaultConfig() doesn't exist in 1.0 - File configFile = new File(EsToolsBukkit.plugin.getDataFolder(), "config.yml"); - if (!configFile.exists()) { - saveResource("config.yml", false); - } - config = ConfigManager.load("config.yml"); - - // Add keys that don't exist from the default config - if (ConfigManager.patchDefaults(getConfig(), EsToolsBukkit.plugin.getResource("config.yml"))) { - ConfigManager.save("config.yml", getConfig()); // Only save if something changed - } + // Load the config + config = ConfigManager.load("config.yml", EsToolsConfig.class); + logger.info("Loaded github releases url: " + config.getUpdater().getGithubReleasesUrl()); // Metrics - if (getConfig().getBoolean("metrics", true)) { + if (config.isMetrics() && platform.supportsMetrics()) { Metrics metrics = new Metrics(EsToolsBukkit.plugin, bStatsId); metrics.addCustomChart(new SimplePie("vault_enabled", () -> String.valueOf(Vault.economy != null))); logger.info("Started bStat metrics"); @@ -254,31 +242,10 @@ public void sc(String name, String perm, EsToolsCommand ce, EsToolsTabCompleter } // Overriding to create more predictable behaviour between versions - public FileConfiguration getConfig() { + public EsToolsConfig getConfig() { return config; } - // Overriding because method doesn't exist in very early versions - @SuppressWarnings("unused") // I might use it in the future - public void saveResource(String res, boolean replace) { - InputStream resource = EsToolsBukkit.plugin.getResource(res); - File file = new File(EsToolsBukkit.plugin.getDataFolder(), res); - - if (file.exists()) { - logger.info("Tried to copy resource but it already exists: " + res); - return; - } - - try { - Files.createDirectories(file.getParentFile().toPath()); - asrt(resource != null); - Files.copy(resource, file.toPath()); - logger.info("Copied " + res + " to " + file.toPath()); - } catch (IOException e) { - logger.severe("Failed to save resource: " + e); - } - } - public static void asrt(boolean condition) { asrt(condition, "Condition is false"); } @@ -296,5 +263,9 @@ public static void asrt(boolean condition, String msg) { logger.severe("Assertion failed: " + msg); throw new AssertionError(msg); } + + public void saveConfig() { + ConfigManager.save("config.yml", config); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsAction.java b/src/main/java/net/serble/estools/ServerApi/EsAction.java new file mode 100644 index 0000000..fcec21a --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsAction.java @@ -0,0 +1,32 @@ +package net.serble.estools.ServerApi; + +public enum EsAction { + /** + * Left-clicking a block + */ + LeftClickBlock, + /** + * Right-clicking a block + */ + RightClickBlock, + /** + * Left-clicking the air + */ + LeftClickAir, + /** + * Right-clicking the air + */ + RightClickAir, + /** + * Stepping onto or into a block (Ass-pressure) + *

+ * Examples: + *

    + *
  • Jumping on soil + *
  • Standing on pressure plate + *
  • Triggering redstone ore + *
  • Triggering tripwire + *
+ */ + Physical +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsLocation.java b/src/main/java/net/serble/estools/ServerApi/EsLocation.java index b500d33..8fb42e4 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsLocation.java +++ b/src/main/java/net/serble/estools/ServerApi/EsLocation.java @@ -1,36 +1,60 @@ package net.serble.estools.ServerApi; +import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsWorld; +@SuppressWarnings("unused") // SnakeYAML needs the setters public class EsLocation extends Position { - private final EsWorld world; + private EsWorld world; + private String worldName; private Position direction; private double yaw; private double pitch; public EsLocation(EsWorld world, double x, double y, double z) { - super(x, y, z); this.world = world; + this.worldName = world.getName(); + setX(x); + setY(y); + setZ(z); } public EsLocation(EsWorld world, Position dir, double x, double y, double z) { - super(x, y, z); this.world = world; + this.worldName = world.getName(); direction = dir; + setX(x); + setY(y); + setZ(z); } public EsLocation(EsWorld world, Position dir, double x, double y, double z, double yaw, double pitch) { - super(x, y, z); this.world = world; + this.worldName = world.getName(); direction = dir; this.yaw = yaw; this.pitch = pitch; + setX(x); + setY(y); + setZ(z); } + /** You probably shouldn't use this, it's here for SnakeYAML */ + public EsLocation() { } + public EsWorld getWorld() { return world; } + public String getWorldName() { + return worldName; + } + + public void setWorldName(String worldName) { // Sets world based on name, for SnakeYAML to correctly use locations + this.worldName = worldName; + world = Main.server.getWorld(worldName); + } + public double distanceTo(EsLocation other) { return super.distanceTo(other); } diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java new file mode 100644 index 0000000..1cc76bd --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java @@ -0,0 +1,31 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.EsAction; +import net.serble.estools.ServerApi.Interfaces.EsBlock; +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.jetbrains.annotations.Nullable; + +public class EsPlayerInteractEvent extends EsCancellableEvent { + private final EsPlayer player; + private final @Nullable EsBlock clickedBlock; + private final EsAction action; + + public EsPlayerInteractEvent(EsPlayer player, @Nullable EsBlock clickedBlock, EsAction action) { + this.player = player; + this.clickedBlock = clickedBlock; + this.action = action; + } + + public EsPlayer getPlayer() { + return player; + } + + public EsAction getAction() { + return action; + } + + public @Nullable EsBlock getClickedBlock() { + return clickedBlock; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsSignChangeEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsSignChangeEvent.java new file mode 100644 index 0000000..373b28d --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsSignChangeEvent.java @@ -0,0 +1,30 @@ +package net.serble.estools.ServerApi.Events; + +import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; + +public class EsSignChangeEvent extends EsCancellableEvent { + private final EsPlayer player; + private final String[] lines; + + public EsSignChangeEvent(EsPlayer player, String[] lines) { + this.player = player; + this.lines = lines; + } + + public EsPlayer getPlayer() { + return player; + } + + public String[] getLines() { + return lines; + } + + public String getLine(int line) { + return lines[line]; + } + + public void setLine(int line, String text) { + lines[line] = text; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java index db715a4..9dc3ddf 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitBlock.java @@ -34,4 +34,9 @@ public int getY() { public int getZ() { return getBukkitBlock().getZ(); } + + @Override + public String getType() { + return bukkitState.getType().name(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java new file mode 100644 index 0000000..fd34b2b --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import org.bukkit.configuration.file.FileConfiguration; + +/** Tool for migrating from legacy Bukkit configs to v5 compatible configs */ +public class BukkitConfigMigrator { + private static FileConfiguration config; +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index eb5c706..aba8b75 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -1,13 +1,11 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsAction; import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; -import net.serble.estools.ServerApi.Interfaces.EsInventory; -import net.serble.estools.ServerApi.Interfaces.EsInventoryView; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -16,12 +14,14 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.jetbrains.annotations.NotNull; @@ -142,6 +142,28 @@ public void onDrag(InventoryDragEvent e) { e.setCancelled(ee.isCancelled()); } + @EventHandler + public void onInteract(PlayerInteractEvent e) { + EsBlock cb = e.getClickedBlock() == null ? null : new BukkitBlock(e.getClickedBlock().getState()); + EsPlayer p = new BukkitPlayer(e.getPlayer()); + EsAction ac = BukkitHelper.fromBukkitAction(e.getAction()); + EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onSignChange(SignChangeEvent e) { + EsPlayer p = new BukkitPlayer(e.getPlayer()); + String[] lines = e.getLines(); + EsSignChangeEvent ee = new EsSignChangeEvent(p, lines); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + for (int i = 0; i < lines.length; i++) { + e.setLine(i, ee.getLine(i)); + } + } + @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { return Main.executeCommand(BukkitHelper.fromBukkitCommandSender(sender), command.getName(), args); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 591de7e..95462a0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -15,6 +15,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.block.Action; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; @@ -506,4 +507,21 @@ public static SoundCategory toBukkitSoundCategory(EsSoundCategory esSoundCategor throw new IllegalArgumentException("Invalid EsSoundCategory"); } } + + public static EsAction fromBukkitAction(Action action) { + switch (action) { + case LEFT_CLICK_BLOCK: + return EsAction.LeftClickBlock; + case RIGHT_CLICK_BLOCK: + return EsAction.RightClickBlock; + case LEFT_CLICK_AIR: + return EsAction.LeftClickAir; + case RIGHT_CLICK_AIR: + return EsAction.RightClickAir; + case PHYSICAL: + return EsAction.Physical; + default: + throw new IllegalArgumentException("Invalid Action"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 0c3bb5f..d8d7ea4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -85,7 +85,6 @@ public SemanticVersion getVersion() { // Parse the minecraft version from the B } } - Bukkit.getLogger().info("Version detected as: 1." + minor + '.' + patch + " from: " + versionS); return new SemanticVersion(1, minor, patch); } @@ -297,4 +296,32 @@ public void setCommandPermission(String cmd, String perm) { command.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); } } + + @Override + public void broadcast(String msg, String perm) { + Bukkit.broadcast(msg, perm); + } + + @Override + public void broadcast(String msg) { + Bukkit.broadcastMessage(msg); + } + + @Override + public String[] getMaterials(boolean onlyItems) { + Material[] materials = Material.values(); + String[] strMaterials = new String[materials.length]; + for (int i = 0; i < materials.length; i++) { + if (onlyItems && Main.minecraftVersion.getMinor() >= 12 && !materials[i].isItem()) { + continue; + } + strMaterials[i] = materials[i].name(); + } + return strMaterials; + } + + @Override + public EsWorld getWorld(String name) { + return new BukkitWorld(Bukkit.getWorld(name)); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java index b4f88d9..3d94530 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSign.java @@ -40,4 +40,16 @@ public void setLine(int line, String text) { public EsSignSide getTargetSide(EsPlayer player) { return new BukkitSignSide(bukkitSign.getTargetSide(((BukkitPlayer) player).getBukkitPlayer())); } + + @SuppressWarnings("deprecation") + @Override + public String getLine(int line) { + return bukkitSign.getLine(line); + } + + @SuppressWarnings("deprecation") + @Override + public String[] getLines() { + return bukkitSign.getLines(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 8761c74..72a4aaf 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -1,14 +1,12 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsAction; import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Implementations.Bukkit.*; -import net.serble.estools.ServerApi.Interfaces.EsInventory; -import net.serble.estools.ServerApi.Interfaces.EsInventoryView; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -23,6 +21,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.jetbrains.annotations.NotNull; @@ -143,6 +142,16 @@ public void onDrag(InventoryDragEvent e) { e.setCancelled(ee.isCancelled()); } + @EventHandler + public void onInteract(PlayerInteractEvent e) { + EsBlock cb = e.getClickedBlock() == null ? null : new FoliaBlock(e.getClickedBlock().getState()); + EsPlayer p = new FoliaPlayer(e.getPlayer()); + EsAction ac = FoliaHelper.fromBukkitAction(e.getAction()); + EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { return Main.executeCommand(BukkitHelper.fromBukkitCommandSender(sender), command.getName(), args); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 81de784..6b2242f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -96,7 +96,6 @@ public SemanticVersion getVersion() { // Parse the minecraft version from the B } } - Main.logger.info("Version detected as: 1." + minor + '.' + patch + " from: " + versionS); return new SemanticVersion(1, minor, patch); } @@ -219,6 +218,19 @@ public String[] getSounds() { return strSounds; } + @Override + public String[] getMaterials(boolean onlyItems) { + Material[] materials = Material.values(); + String[] strMaterials = new String[materials.length]; + for (int i = 0; i < materials.length; i++) { + if (onlyItems && Main.minecraftVersion.getMinor() >= 12 && !materials[i].isItem()) { + continue; + } + strMaterials[i] = materials[i].name(); + } + return strMaterials; + } + @Override public boolean doesEnchantmentExist(String name) { try { @@ -330,4 +342,19 @@ public void setCommandPermission(String cmd, String perm) { command.setPermissionMessage(EsToolsCommand.translate("&cYou do not have permission to run this command.")); } } + + @Override + public void broadcast(String msg, String perm) { + Bukkit.broadcast(msg, perm); + } + + @Override + public void broadcast(String msg) { + Bukkit.broadcastMessage(msg); + } + + @Override + public EsWorld getWorld(String name) { + return new FoliaWorld(Bukkit.getWorld(name)); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java index 8c0a10b..4eddf2d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java @@ -5,7 +5,7 @@ import net.serble.estools.ServerApi.Interfaces.EsSignSide; import org.bukkit.block.Sign; -public class FoliaSign extends net.serble.estools.ServerApi.Implementations.Folia.FoliaBlock implements EsSign { +public class FoliaSign extends FoliaBlock implements EsSign { private final Sign bukkitSign; public FoliaSign(Sign block) { @@ -36,6 +36,18 @@ public void setLine(int line, String text) { bukkitSign.setLine(line, text); } + @SuppressWarnings("deprecation") + @Override + public String getLine(int line) { + return bukkitSign.getLine(line); + } + + @SuppressWarnings("deprecation") + @Override + public String[] getLines() { + return bukkitSign.getLines(); + } + @Override public EsSignSide getTargetSide(EsPlayer player) { return new net.serble.estools.ServerApi.Implementations.Folia.FoliaSignSide(bukkitSign.getTargetSide(((net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer) player).getBukkitPlayer())); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java index e7ffeed..c8ffc30 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsBlock.java @@ -1,8 +1,10 @@ package net.serble.estools.ServerApi.Interfaces; +@SuppressWarnings("unused") public interface EsBlock { boolean breakNaturally(); int getX(); int getY(); int getZ(); + String getType(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 6b786f9..969bd57 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -20,6 +20,13 @@ public interface EsServerSoftware { String[] getPotionEffectTypes(); String[] getEnchantments(); String[] getSounds(); + + /** + * @param onlyItems + * Whether to only include materials that are classified as items + * */ + String[] getMaterials(boolean onlyItems); + boolean doesEnchantmentExist(String name); File getDataFolder(); void dispatchCommand(EsCommandSender sender, String cmd); @@ -34,4 +41,7 @@ public interface EsServerSoftware { void registerCommand(String cmd, EsToolsTabCompleter tab); void setTabCompleter(String cmd, EsToolsTabCompleter tab); void setCommandPermission(String cmd, String perm); + void broadcast(String msg, String perm); + void broadcast(String msg); + EsWorld getWorld(String name); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java index b1db1e0..2ce470c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsSign.java @@ -9,6 +9,8 @@ public interface EsSign extends EsBlock { /** Only for pre 1.20 where signs are singlesided */ void setLine(int line, String text); + String getLine(int line); + String[] getLines(); /** Gets the side targeted by the player, only for 1.20+ */ EsSignSide getTargetSide(EsPlayer player); diff --git a/src/main/java/net/serble/estools/ServerApi/Position.java b/src/main/java/net/serble/estools/ServerApi/Position.java index 59b2c4e..3e40e7c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Position.java +++ b/src/main/java/net/serble/estools/ServerApi/Position.java @@ -27,6 +27,18 @@ public double getZ() { return z; } + public void setX(double x) { + this.x = x; + } + + public void setY(double y) { + this.y = y; + } + + public void setZ(double z) { + this.z = z; + } + public double distanceTo(Position other) { return Math.sqrt(distanceSquared(other)); } @@ -59,4 +71,8 @@ public double length() { public double dot(Position other) { return x * other.x + y * other.y + z * other.z; } + + public Position toPosition() { + return this; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java index 43cf0e8..f166766 100644 --- a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java +++ b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java @@ -5,8 +5,18 @@ import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; public enum ServerPlatform { - Bukkit, // Includes all derivatives - Folia; + Bukkit(true), // Includes all derivatives + Folia(true); + + final boolean hasMetrics; + + ServerPlatform(boolean bStats) { + hasMetrics = bStats; + } + + public boolean supportsMetrics() { + return hasMetrics; + } public EsServerSoftware getServerInstance(Object context) { switch (this) { diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index 987fd81..9d990b2 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -1,20 +1,17 @@ package net.serble.estools.Signs; -import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.EsToolsCommand; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; -import org.bukkit.Bukkit; -import org.bukkit.block.Sign; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.SignChangeEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsAction; +import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.Events.EsPlayerInteractEvent; +import net.serble.estools.ServerApi.Events.EsSignChangeEvent; +import net.serble.estools.ServerApi.Interfaces.EsEvent; +import net.serble.estools.ServerApi.Interfaces.EsSign; import java.util.HashMap; -// TODO: Migrate away from bukkit events -public class SignMain implements Listener { +public class SignMain implements EsEventListener { private static final HashMap signs = new HashMap<>(); private static final HashMap signConversions = new HashMap<>(); @@ -27,7 +24,7 @@ public static void init() { addSign(new Repair(), "[repair]", EsToolsCommand.translate("&1[Repair]")); addSign(new Sell(), "[sell]", EsToolsCommand.translate("&1[Sell]")); - Bukkit.getServer().getPluginManager().registerEvents(new SignMain(), EsToolsBukkit.plugin); + Main.registerEvents(new SignMain()); } public static void addSign(SignType signType, String conversion, String sign) { @@ -35,23 +32,20 @@ public static void addSign(SignType signType, String conversion, String sign) { signs.put(sign, signType); } - @SuppressWarnings("deprecation") - @EventHandler - public void interact(PlayerInteractEvent e) { - if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getClickedBlock().getType().name().endsWith("SIGN")) { - Sign state = (Sign)e.getClickedBlock().getState(); + public void interact(EsPlayerInteractEvent e) { + if (e.getAction().equals(EsAction.RightClickBlock) && e.getClickedBlock() instanceof EsSign) { + EsSign state = (EsSign) e.getClickedBlock(); SignType signType = signs.get(state.getLine(0)); if (signType != null) { e.setCancelled(true); - signType.run(new FoliaPlayer(e.getPlayer()), state.getLines()); + signType.run(e.getPlayer(), state.getLines()); } } } - @EventHandler - public void changeSign(SignChangeEvent e) { + public void changeSign(EsSignChangeEvent e) { if (!e.getPlayer().hasPermission("estools.signs")) { return; } @@ -62,4 +56,16 @@ public void changeSign(SignChangeEvent e) { e.setLine(0, str); } } + + @Override + public void executeEvent(EsEvent event) { + if (event instanceof EsPlayerInteractEvent) { + interact((EsPlayerInteractEvent) event); + return; + } + + if (event instanceof EsSignChangeEvent) { + changeSign((EsSignChangeEvent) event); + } + } } diff --git a/src/main/java/net/serble/estools/Updater.java b/src/main/java/net/serble/estools/Updater.java index 284a134..b092afc 100644 --- a/src/main/java/net/serble/estools/Updater.java +++ b/src/main/java/net/serble/estools/Updater.java @@ -3,7 +3,6 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; -import org.bukkit.Bukkit; import java.io.*; import java.net.HttpURLConnection; @@ -15,13 +14,13 @@ public class Updater { private static void checkForUpdateBlocking() { try { - URL url = new URL(Objects.requireNonNull(Main.plugin.getConfig().getString("updater.github-release-url"))); + URL url = new URL(Objects.requireNonNull(Main.plugin.getConfig().getUpdater().getGithubReleasesUrl())); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.connect(); if (con.getResponseCode() != 200) { - Bukkit.getLogger().severe("[EsTools Updater] GitHub gave unsuccessful response code: " + con.getResponseCode()); + Main.logger.severe("[EsTools Updater] GitHub gave unsuccessful response code: " + con.getResponseCode()); return; } @@ -58,22 +57,22 @@ private static void checkForUpdateBlocking() { Main.newVersion = onlineVersion; // Announcements - if (Main.plugin.getConfig().getBoolean("updater.warn-on-outdated", false)) { - Bukkit.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString()), "estools.update"); + if (Main.plugin.getConfig().getUpdater().isWarnOnOutdated()) { + Main.server.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString()), "estools.update"); } - if (Main.plugin.getConfig().getBoolean("updater.log-on-outdated", false)) { - Bukkit.getLogger().info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString())); + if (Main.plugin.getConfig().getUpdater().isLogOnOutdated()) { + Main.logger.info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString())); } - if (!Main.plugin.getConfig().getBoolean("updater.auto-update", false)) { + if (!Main.plugin.getConfig().getUpdater().isAutoUpdate()) { return; } downloadNewUpdate(); - Bukkit.getLogger().info("[EsTools Updater] Downloaded latest plugin version"); + Main.logger.info("[EsTools Updater] Downloaded latest plugin version"); Main.newVersionReady = true; } catch (IOException e) { - Bukkit.getLogger().severe("[EsTools Updater] Failed to check for updates, invalid releases URL configured"); + Main.logger.severe("[EsTools Updater] Failed to check for updates, invalid releases URL configured"); } } @@ -90,7 +89,7 @@ public static void downloadNewUpdate(EsCommandSender reportTo) { downloadFile(waitingDownloadUrl); Main.newVersionReady = true; } catch (IOException e) { - Bukkit.getLogger().severe("Failed to update: " + e); + Main.logger.severe("Failed to update: " + e); return; } @@ -116,7 +115,7 @@ public static void downloadFile(String fileURL) InputStream inputStream = httpConn.getInputStream(); String saveFilePath = Main.plugin.getClass().getProtectionDomain() .getCodeSource().getLocation().getFile().replace("%20", " "); - Bukkit.getLogger().info("Save file path: " + saveFilePath); + Main.logger.info("Save file path: " + saveFilePath); FileOutputStream outputStream = new FileOutputStream(saveFilePath); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9f0f405..4add2bc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ # Go to https://estools.serble.net/configuration.html for information on configuring the plugin # Stops players taking fall damage when they teleport -safetp: true +safeTp: true # Toggle bStats metrics metrics: true @@ -10,14 +10,14 @@ metrics: true updater: # The GitHub API path to query for the latest version, change if you want to use a fork - github-release-url: "https://api.github.com/repos/CoPokBl/EsTools/releases/latest" + githubReleasesUrl: "https://api.github.com/repos/CoPokBl/EsTools/releases/latest" # Whether to automatically install updates if they are available - auto-update: false + autoUpdate: false # Whether to notify operators when they join the server if the plugin is outdated # Leave this off if you enable auto-update, this will prompt user to type /estools update - warn-on-outdated: false + warnOnOutdated: false # Log to the console when the plugin is out of date - log-on-outdated: true + logOnOutdated: true From ed4944e8ea3bbd1465da608507d6af6736a2951f Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Mon, 27 May 2024 09:07:40 +1000 Subject: [PATCH 15/74] Remove /music todo --- src/main/java/net/serble/estools/Commands/Music.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/Music.java b/src/main/java/net/serble/estools/Commands/Music.java index e0c9cb6..5e58e0a 100644 --- a/src/main/java/net/serble/estools/Commands/Music.java +++ b/src/main/java/net/serble/estools/Commands/Music.java @@ -11,7 +11,6 @@ import java.util.List; import java.util.Random; -// TODO: Incomplete public class Music extends EsToolsCommand { private static final String usage = genUsage("/music [song]"); private static final List musics = new ArrayList<>(); From 77b19fe8f499cfb489f1f9a1bcfc49fdcd47a798 Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Mon, 27 May 2024 14:50:42 +1000 Subject: [PATCH 16/74] Attempt fix cchests config --- .../net/serble/estools/Commands/CChest.java | 23 ++++++++++++++++--- .../serble/estools/Config/ConfigManager.java | 3 ++- .../Bukkit/BukkitInventory.java | 7 ++++++ .../Bukkit/BukkitItemStack.java | 13 +++++++++-- .../Bukkit/BukkitPlayerInventory.java | 4 ++++ .../Bukkit/BukkitSerialisableItemStack.java | 8 +++++++ .../Implementations/Bukkit/BukkitServer.java | 12 ++++++++++ .../Implementations/Folia/FoliaItemStack.java | 9 ++++++++ .../Implementations/Folia/FoliaServer.java | 12 ++++++++++ .../ServerApi/Interfaces/EsItemStack.java | 1 + .../Interfaces/EsServerSoftware.java | 4 ++++ 11 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index 1641f61..e053938 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -172,8 +172,18 @@ private void onInventoryDrag(final EsInventoryDragEvent e) { public static EsInventory loadPlayer(EsPlayer p) { UUID uid = p.getUniqueId(); - EsInventory f = ConfigManager.load("cchests/" + uid + ".yml", EsInventory.class); - EsItemStack[] content = f.getContents(); + @SuppressWarnings("unchecked") + List f = (List) ConfigManager.load( + "cchests/" + uid + ".yml", + ArrayList.class); + EsItemStack[] content = new EsItemStack[f.size()]; + for (int i = 0; i < content.length; i++) { + if (f.get(i) == null) { + content[i] = null; + continue; + } + content[i] = Main.server.createItemStack(f.get(i)); + } EsInventory inv = Main.server.createInventory(null, 54, translate("&1Creative Chest")); inv.setContents(content); @@ -189,7 +199,14 @@ public static void savePlayer(EsPlayer p) { return; } - ConfigManager.save("cchests/" + uid + ".yml", cChests.get(uid)); + EsInventory inv = cChests.get(uid); + Object[] contents = new Object[inv.getContents().length]; + for (int i = 0; i < inv.getContents().length; i++) { + EsItemStack stack = inv.getContents()[i]; + contents[i] = stack == null ? null : stack.getInternalObject(); + } + + ConfigManager.save("cchests/" + uid + ".yml", contents); } private void onClose(EsInventoryCloseEvent e) { diff --git a/src/main/java/net/serble/estools/Config/ConfigManager.java b/src/main/java/net/serble/estools/Config/ConfigManager.java index 21d7251..32c29ff 100644 --- a/src/main/java/net/serble/estools/Config/ConfigManager.java +++ b/src/main/java/net/serble/estools/Config/ConfigManager.java @@ -47,7 +47,8 @@ public static T load(File configFile, Class base, String... all LoaderOptions options = new LoaderOptions(); TagInspector taginspector = tag -> tag.getClassName().equals(base.getName()) || - Arrays.stream(allowedClasses).anyMatch(c -> c.equals(tag.getClassName())); + Arrays.stream(allowedClasses).anyMatch(c -> c.equals(tag.getClassName())) || + Arrays.stream(Main.server.getRelevantInternalTypes()).anyMatch(c -> tag.getClassName().endsWith(c)); options.setTagInspector(taginspector); Yaml yaml = new Yaml(new Constructor(base, options)); try { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index bda5d69..4709592 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -13,6 +13,13 @@ public class BukkitInventory implements EsInventory { public BukkitInventory(Inventory inv) { bukkitInv = inv; + if (inv == null) { + throw new NullPointerException("inv"); + } + } + + public BukkitInventory() { + bukkitInv = null; } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index dd10f6f..31c14ba 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -9,7 +9,7 @@ import org.bukkit.inventory.meta.ItemMeta; public class BukkitItemStack implements EsItemStack { - private final org.bukkit.inventory.ItemStack bukkitItem; + private final ItemStack bukkitItem; public BukkitItemStack(String mat, int amount) { bukkitItem = new ItemStack(Material.valueOf(mat), amount); @@ -19,10 +19,14 @@ public BukkitItemStack(org.bukkit.inventory.ItemStack child) { bukkitItem = child; } - public org.bukkit.inventory.ItemStack getBukkitItem() { + public ItemStack getBukkitItem() { return bukkitItem; } + public BukkitItemStack(Object internalObject) { + bukkitItem = (ItemStack) internalObject; + } + @Override public String getType() { return bukkitItem.getType().name(); @@ -110,4 +114,9 @@ public boolean isSimilar(EsItemStack stack) { public int getMaxStackSize() { return bukkitItem.getMaxStackSize(); } + + @Override + public Object getInternalObject() { + return bukkitItem; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index 28290ff..8589093 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -13,6 +13,10 @@ public BukkitPlayerInventory(PlayerInventory inv) { bukkitInv = inv; } + public BukkitPlayerInventory() { + bukkitInv = null; + } + @Override public PlayerInventory getBukkitInventory() { return bukkitInv; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java new file mode 100644 index 0000000..e30f725 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java @@ -0,0 +1,8 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import org.bukkit.inventory.ItemStack; + +public class BukkitSerialisableItemStack extends ItemStack { + + +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index d8d7ea4..bce3a5c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -324,4 +324,16 @@ public String[] getMaterials(boolean onlyItems) { public EsWorld getWorld(String name) { return new BukkitWorld(Bukkit.getWorld(name)); } + + @Override + public EsItemStack createItemStack(Object internalObject) { + return new BukkitItemStack(internalObject); + } + + @Override + public String[] getRelevantInternalTypes() { + return new String[] { + "CraftItemStack" // CChest config files + }; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 7238d23..90abb58 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -16,6 +16,10 @@ public FoliaItemStack(String mat, int amount) { bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); } + public FoliaItemStack(Object internalObject) { + bukkitItem = (ItemStack) internalObject; + } + public FoliaItemStack(org.bukkit.inventory.ItemStack child) { bukkitItem = child; } @@ -111,4 +115,9 @@ public void removeEnchantment(String enchantment) { public int getMaxStackSize() { return bukkitItem.getMaxStackSize(); } + + @Override + public Object getInternalObject() { + return bukkitItem; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 6b2242f..6b575c4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -357,4 +357,16 @@ public void broadcast(String msg) { public EsWorld getWorld(String name) { return new FoliaWorld(Bukkit.getWorld(name)); } + + @Override + public EsItemStack createItemStack(Object internalObject) { + return new FoliaItemStack(internalObject); + } + + @Override + public String[] getRelevantInternalTypes() { + return new String[] { + "CraftItemStack" // CChest config files + }; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index ca08b10..337f683 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -14,4 +14,5 @@ public interface EsItemStack { EsItemStack clone(); boolean isSimilar(EsItemStack stack); int getMaxStackSize(); + Object getInternalObject(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 969bd57..9052d38 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -44,4 +44,8 @@ public interface EsServerSoftware { void broadcast(String msg, String perm); void broadcast(String msg); EsWorld getWorld(String name); + EsItemStack createItemStack(Object internalObject); + + /** Get a list of classes that SnakeYAML needs to accept for proper config serialisation */ + String[] getRelevantInternalTypes(); } From 7646e6736318dd41f71000f52d639f59510f35c1 Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Tue, 28 May 2024 14:36:20 +1000 Subject: [PATCH 17/74] Attempt reflection janky fix --- pom.xml | 11 ++++ .../serble/estools/Config/ConfigManager.java | 20 ++++++ .../Bukkit/BukkitSerialisableItemStack.java | 63 ++++++++++++++++--- 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index bcd32aa..8a15737 100644 --- a/pom.xml +++ b/pom.xml @@ -152,5 +152,16 @@ snakeyaml 2.2-SNAPSHOT + + + org.javassist + javassist + 3.30.2-GA + + + org.reflections + reflections + 0.9.11 + diff --git a/src/main/java/net/serble/estools/Config/ConfigManager.java b/src/main/java/net/serble/estools/Config/ConfigManager.java index 32c29ff..343a9cd 100644 --- a/src/main/java/net/serble/estools/Config/ConfigManager.java +++ b/src/main/java/net/serble/estools/Config/ConfigManager.java @@ -2,8 +2,12 @@ import java.io.*; import java.util.Arrays; +import java.util.Set; +import javassist.*; import net.serble.estools.Main; +import org.reflections.Reflections; +import org.reflections.scanners.SubTypesScanner; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -44,6 +48,22 @@ public static T load(File configFile, Class base, String... all } } + // Patch object so it can be constructed, this is hacky but it should work + try { + Reflections reflections = new Reflections("", new SubTypesScanner(false)); + Set> allClasses = + reflections.getSubTypesOf(Object.class); + + ClassPool pool = ClassPool.getDefault(); + CtClass cc = pool.get("path.to.your.class"); + + for (CtConstructor constructor: cc.getDeclaredConstructors()) { + constructor.setModifiers(Modifier.PUBLIC); + } + } catch (NotFoundException e) { + Main.logger.severe("Failed to patch loading class, configs may not work properly"); + } + LoaderOptions options = new LoaderOptions(); TagInspector taginspector = tag -> tag.getClassName().equals(base.getName()) || diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java index e30f725..8bf53d6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java @@ -1,8 +1,55 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; - -import org.bukkit.inventory.ItemStack; - -public class BukkitSerialisableItemStack extends ItemStack { - - -} +//package net.serble.estools.ServerApi.Implementations.Bukkit; +// +//import org.yaml.snakeyaml.DumperOptions; +//import org.yaml.snakeyaml.LoaderOptions; +//import org.yaml.snakeyaml.constructor.Construct; +//import org.yaml.snakeyaml.constructor.Constructor; +//import org.yaml.snakeyaml.introspector.Property; +//import org.yaml.snakeyaml.nodes.Node; +//import org.yaml.snakeyaml.nodes.NodeTuple; +//import org.yaml.snakeyaml.nodes.Tag; +//import org.yaml.snakeyaml.representer.Representer; +// +//import java.lang.reflect.Modifier; +// +//class BukkitSerialisableItemStack extends Constructor { +// +// public BukkitSerialisableItemStack(Class theClass) { +// super(new LoaderOptions()); +// } +// +// +// @Override +// protected Class getClassForNode(Node node) { +// Class clazz = super.getClassForNode(node); +// if (clazz.getName().endsWith("CraftItemStack") && clazz.getDeclaredConstructors()[0].getModifiers() == Modifier.PROTECTED) { +// return YourExtendedClass.class; // YourExtendedClass is a class that extends YourClass and has a public default constructor. +// } +// +// return clazz; +// +// } +// +//} +// +// +// +//class CustomRepresenter extends Representer { +// public CustomRepresenter() { +// super(new DumperOptions()); +// this.getPropertyUtils().setSkipMissingProperties(true); +// } +// +// +// +// @Override +// protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) { +// if (propertyValue == null) { +// return null; // skip null properties +// } +// else { +// return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); +// } +// } +// +//} \ No newline at end of file From 59232118171330a898c5f23f8372ad1e14c96b3e Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Thu, 30 May 2024 13:01:44 +1000 Subject: [PATCH 18/74] Full normal non janky CChests saving --- pom.xml | 11 --- .../net/serble/estools/Commands/CChest.java | 17 ++-- .../serble/estools/Config/ConfigManager.java | 20 ---- .../ServerApi/EsSerialisableItemStack.java | 94 +++++++++++++++++++ .../Bukkit/BukkitEnchantmentsHelper.java | 10 ++ .../Implementations/Bukkit/BukkitHelper.java | 10 ++ .../Bukkit/BukkitItemStack.java | 14 +++ .../Bukkit/BukkitSerialisableItemStack.java | 55 ----------- .../Implementations/Folia/FoliaItemStack.java | 14 +++ .../ServerApi/Interfaces/EsItemStack.java | 3 + 10 files changed, 152 insertions(+), 96 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java delete mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java diff --git a/pom.xml b/pom.xml index 8a15737..bcd32aa 100644 --- a/pom.xml +++ b/pom.xml @@ -152,16 +152,5 @@ snakeyaml 2.2-SNAPSHOT - - - org.javassist - javassist - 3.30.2-GA - - - org.reflections - reflections - 0.9.11 - diff --git a/src/main/java/net/serble/estools/Commands/CChest.java b/src/main/java/net/serble/estools/Commands/CChest.java index e053938..f29b923 100644 --- a/src/main/java/net/serble/estools/Commands/CChest.java +++ b/src/main/java/net/serble/estools/Commands/CChest.java @@ -3,16 +3,12 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Config.ConfigManager; import net.serble.estools.Main; -import net.serble.estools.ServerApi.EsClickType; -import net.serble.estools.ServerApi.EsEventListener; -import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.*; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Interfaces.*; import java.util.*; -// TODO: Saving and loading data, can't instantiate EsInventory public class CChest extends EsToolsCommand implements EsEventListener { public static HashMap cChests = new HashMap<>(); @@ -173,16 +169,17 @@ public static EsInventory loadPlayer(EsPlayer p) { UUID uid = p.getUniqueId(); @SuppressWarnings("unchecked") - List f = (List) ConfigManager.load( + List f = (List) ConfigManager.load( "cchests/" + uid + ".yml", - ArrayList.class); + ArrayList.class, + EsSerialisableItemStack.class); EsItemStack[] content = new EsItemStack[f.size()]; for (int i = 0; i < content.length; i++) { if (f.get(i) == null) { content[i] = null; continue; } - content[i] = Main.server.createItemStack(f.get(i)); + content[i] = f.get(i).toItemStack(); } EsInventory inv = Main.server.createInventory(null, 54, translate("&1Creative Chest")); @@ -200,10 +197,10 @@ public static void savePlayer(EsPlayer p) { } EsInventory inv = cChests.get(uid); - Object[] contents = new Object[inv.getContents().length]; + EsSerialisableItemStack[] contents = new EsSerialisableItemStack[inv.getContents().length]; for (int i = 0; i < inv.getContents().length; i++) { EsItemStack stack = inv.getContents()[i]; - contents[i] = stack == null ? null : stack.getInternalObject(); + contents[i] = stack == null ? null : EsSerialisableItemStack.generate(stack); } ConfigManager.save("cchests/" + uid + ".yml", contents); diff --git a/src/main/java/net/serble/estools/Config/ConfigManager.java b/src/main/java/net/serble/estools/Config/ConfigManager.java index 343a9cd..32c29ff 100644 --- a/src/main/java/net/serble/estools/Config/ConfigManager.java +++ b/src/main/java/net/serble/estools/Config/ConfigManager.java @@ -2,12 +2,8 @@ import java.io.*; import java.util.Arrays; -import java.util.Set; -import javassist.*; import net.serble.estools.Main; -import org.reflections.Reflections; -import org.reflections.scanners.SubTypesScanner; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -48,22 +44,6 @@ public static T load(File configFile, Class base, String... all } } - // Patch object so it can be constructed, this is hacky but it should work - try { - Reflections reflections = new Reflections("", new SubTypesScanner(false)); - Set> allClasses = - reflections.getSubTypesOf(Object.class); - - ClassPool pool = ClassPool.getDefault(); - CtClass cc = pool.get("path.to.your.class"); - - for (CtConstructor constructor: cc.getDeclaredConstructors()) { - constructor.setModifiers(Modifier.PUBLIC); - } - } catch (NotFoundException e) { - Main.logger.severe("Failed to patch loading class, configs may not work properly"); - } - LoaderOptions options = new LoaderOptions(); TagInspector taginspector = tag -> tag.getClassName().equals(base.getName()) || diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java new file mode 100644 index 0000000..62f87f3 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -0,0 +1,94 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; + +import java.util.List; +import java.util.Map; + + +// TODO: NBT +public class EsSerialisableItemStack { + private String material; + private int amount; + private String customName; + private List lore; + private Map enchantments; + private EsItemFlag[] flags; + + /** Public constructor for serialiser */ + public EsSerialisableItemStack() { } + + public static EsSerialisableItemStack generate(EsItemStack stack) { + EsSerialisableItemStack result = new EsSerialisableItemStack(); + result.setMaterial(stack.getType()); + result.setAmount(stack.getAmount()); + result.setLore(stack.getItemMeta().getLore()); + result.setCustomName(stack.getItemMeta().getDisplayName()); + result.setEnchantments(stack.getEnchantments()); + result.setFlags(stack.getItemMeta().getItemFlags().toArray(new EsItemFlag[0])); + return result; + } + + public EsItemStack toItemStack() { + EsItemStack stack = Main.server.createItemStack(material, amount); + EsItemMeta meta = stack.getItemMeta(); + meta.setLore(lore); + meta.setDisplayName(customName); + meta.addItemFlags(flags); + stack.setItemMeta(meta); + for (Map.Entry ench : enchantments.entrySet()) { + stack.addEnchantment(ench.getKey(), ench.getValue()); + } + return stack; + } + + public int getAmount() { + return amount; + } + + public String getMaterial() { + return material; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public void setMaterial(String material) { + this.material = material; + } + + public String getCustomName() { + return customName; + } + + public List getLore() { + return lore; + } + + public void setCustomName(String customName) { + this.customName = customName; + } + + public void setLore(List lore) { + this.lore = lore; + } + + public Map getEnchantments() { + return enchantments; + } + + public void setEnchantments(Map enchantments) { + this.enchantments = enchantments; + } + + public EsItemFlag[] getFlags() { + return flags; + } + + public void setFlags(EsItemFlag[] flags) { + this.flags = flags; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java index 691a747..cf05f79 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java @@ -17,6 +17,16 @@ public static String getByName(String name) { return ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH)); } + public static String getNameFromValue(String val) { + for (Map.Entry entry : ENCHANTMENTS.entrySet()) { + if (val.toLowerCase().equals(entry.getValue())) { + return entry.getKey(); + } + } + + return null; + } + public static Set> entrySet() { return ENCHANTMENTS.entrySet(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 95462a0..acd59f5 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -219,6 +219,16 @@ public static Enchantment getBukkitEnchantment(String name) { return Enchantment.getByName(BukkitEnchantmentsHelper.getByName(name)); } + public static String fromBukkitEnchantment(Enchantment ench) { + if (Main.minecraftVersion.getMinor() >= 13) { + return ench.getKey().getKey(); + } + + // We have to use deprecated method for pre 1.13 + //noinspection deprecation + return BukkitEnchantmentsHelper.getNameFromValue(ench.getName()); + } + @SuppressWarnings("rawtypes") public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType type) { switch (type) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index 31c14ba..c03f2fa 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -4,10 +4,14 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; +import java.util.HashMap; +import java.util.Map; + public class BukkitItemStack implements EsItemStack { private final ItemStack bukkitItem; @@ -119,4 +123,14 @@ public int getMaxStackSize() { public Object getInternalObject() { return bukkitItem; } + + @Override + public Map getEnchantments() { + Map enchs = new HashMap<>(); + for (Map.Entry ench : bukkitItem.getEnchantments().entrySet()) { + enchs.put(BukkitHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); + } + + return enchs; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java deleted file mode 100644 index 8bf53d6..0000000 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitSerialisableItemStack.java +++ /dev/null @@ -1,55 +0,0 @@ -//package net.serble.estools.ServerApi.Implementations.Bukkit; -// -//import org.yaml.snakeyaml.DumperOptions; -//import org.yaml.snakeyaml.LoaderOptions; -//import org.yaml.snakeyaml.constructor.Construct; -//import org.yaml.snakeyaml.constructor.Constructor; -//import org.yaml.snakeyaml.introspector.Property; -//import org.yaml.snakeyaml.nodes.Node; -//import org.yaml.snakeyaml.nodes.NodeTuple; -//import org.yaml.snakeyaml.nodes.Tag; -//import org.yaml.snakeyaml.representer.Representer; -// -//import java.lang.reflect.Modifier; -// -//class BukkitSerialisableItemStack extends Constructor { -// -// public BukkitSerialisableItemStack(Class theClass) { -// super(new LoaderOptions()); -// } -// -// -// @Override -// protected Class getClassForNode(Node node) { -// Class clazz = super.getClassForNode(node); -// if (clazz.getName().endsWith("CraftItemStack") && clazz.getDeclaredConstructors()[0].getModifiers() == Modifier.PROTECTED) { -// return YourExtendedClass.class; // YourExtendedClass is a class that extends YourClass and has a public default constructor. -// } -// -// return clazz; -// -// } -// -//} -// -// -// -//class CustomRepresenter extends Representer { -// public CustomRepresenter() { -// super(new DumperOptions()); -// this.getPropertyUtils().setSkipMissingProperties(true); -// } -// -// -// -// @Override -// protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) { -// if (propertyValue == null) { -// return null; // skip null properties -// } -// else { -// return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); -// } -// } -// -//} \ No newline at end of file diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 90abb58..ab2f45a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -5,10 +5,14 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; +import java.util.HashMap; +import java.util.Map; + public class FoliaItemStack implements EsItemStack { private final ItemStack bukkitItem; @@ -120,4 +124,14 @@ public int getMaxStackSize() { public Object getInternalObject() { return bukkitItem; } + + @Override + public Map getEnchantments() { + Map enchs = new HashMap<>(); + for (Map.Entry ench : bukkitItem.getEnchantments().entrySet()) { + enchs.put(FoliaHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); + } + + return enchs; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 337f683..9ab1991 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; +import java.util.Map; + public interface EsItemStack { String getType(); void setType(String val); @@ -15,4 +17,5 @@ public interface EsItemStack { boolean isSimilar(EsItemStack stack); int getMaxStackSize(); Object getInternalObject(); + Map getEnchantments(); } From f4c8b4a2f16468a89184e2dc2cde0801e8e0c03b Mon Sep 17 00:00:00 2001 From: copokbl Date: Thu, 30 May 2024 22:25:29 +1000 Subject: [PATCH 19/74] Start working on BukkitConfigMigrator --- src/main/java/net/serble/estools/Main.java | 8 +- .../Bukkit/BukkitConfigMigrator.java | 169 +++++++++++++++++- 2 files changed, 173 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index a4c6306..c1cfe0d 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -13,6 +13,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitConfigMigrator; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; @@ -62,6 +63,9 @@ public void enable() { Effects.load(); if (platform == ServerPlatform.Bukkit) { + // We have to support old configs + BukkitConfigMigrator.checkPerformMigration(); + try { Vault.setupEconomy(); } catch (Exception e) { @@ -71,7 +75,6 @@ public void enable() { // Load the config config = ConfigManager.load("config.yml", EsToolsConfig.class); - logger.info("Loaded github releases url: " + config.getUpdater().getGithubReleasesUrl()); // Metrics if (config.isMetrics() && platform.supportsMetrics()) { @@ -246,7 +249,8 @@ public EsToolsConfig getConfig() { return config; } - public static void asrt(boolean condition) { + @SuppressWarnings("unused") + public static void asrt(boolean condition) { asrt(condition, "Condition is false"); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java index fd34b2b..0ea0914 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -1,8 +1,173 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Config.ConfigManager; +import net.serble.estools.Config.Schemas.GeneralConfig.EsToolsConfig; +import net.serble.estools.Config.Schemas.GeneralConfig.UpdaterConfig; +import net.serble.estools.Config.Schemas.Give.GiveConfig; +import net.serble.estools.Config.Schemas.Give.GiveSettings; +import net.serble.estools.Main; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; -/** Tool for migrating from legacy Bukkit configs to v5 compatible configs */ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Tool for migrating from legacy Bukkit configs to v5 compatible configs. + * Once migration is complete an empty file ".configsmigrated" is created. +*/ public class BukkitConfigMigrator { - private static FileConfiguration config; + private static final String migratedFile = ".configsmigrated"; + private static final String mainConfig = "config.yml"; + private static final String giveConfig = "give.yml"; + private static final String godsConfig = "gods.yml"; + private static final String buddhaConfig = "buddhas.yml"; + + public static void checkPerformMigration() { + File migratedAlreadyFile = new File(Main.server.getDataFolder(), migratedFile); + + // If we have already migrated, or we have no data to migrate then return + if (migratedAlreadyFile.exists()) { + return; + } + + // If there is no data at all then we don't need to migrate so mark as migrated + if (!Main.server.getDataFolder().exists()) { + try { + boolean ignored = migratedAlreadyFile.getParentFile().mkdirs(); + boolean ignored2 = migratedAlreadyFile.createNewFile(); + } catch (IOException e) { + Main.logger.severe("[EsTools] Failed to create file specifying that configs are migrated"); + } + } + + Main.logger.warning("[EsTools] It seems you are using an outdated config, migrating files to v5"); + + migrateMainConfig(); + migrateGiveConfig(); + migrateGodsConfig(); + + Main.logger.info("[EsTools] Successfully migrated all existing config files"); + try { + boolean ignored = migratedAlreadyFile.createNewFile(); + } catch (IOException e) { + Main.logger.severe("[EsTools] Failed to create file specifying that configs are migrated"); + } + } + + private static void migrateMainConfig() { // config.yml + File file = new File(Main.server.getDataFolder(), mainConfig); + if (!file.exists()) { + return; + } + + try { // We will now try to get all the values, defaulting to their current defaults + FileConfiguration config = new YamlConfiguration(); + config.load(file); + + EsToolsConfig newConfig = new EsToolsConfig(); + newConfig.setSafeTp(config.getBoolean("safetp", newConfig.isSafeTp())); + newConfig.setMetrics(config.getBoolean("metrics", newConfig.isMetrics())); + + UpdaterConfig updaterConfig = newConfig.getUpdater(); + updaterConfig.setGithubReleasesUrl(config.getString("updater.github-release-url", updaterConfig.getGithubReleasesUrl())); + updaterConfig.setAutoUpdate(config.getBoolean("updater.auto-update", updaterConfig.isAutoUpdate())); + updaterConfig.setWarnOnOutdated(config.getBoolean("updater.warn-on-outdated", updaterConfig.isWarnOnOutdated())); + updaterConfig.setLogOnOutdated(config.getBoolean("updater.log-on-outdated", updaterConfig.isLogOnOutdated())); + + // Move file to file.old + File oldFile = new File(Main.server.getDataFolder(), mainConfig + ".old"); + if (!file.renameTo(oldFile)) { + throw new FileNotFoundException("Failed to rename old config file"); + } + + // Save new config + ConfigManager.save(file, newConfig); + + Main.logger.info("[EsTools] Successfully migrated " + mainConfig); + } catch (IOException | InvalidConfigurationException e) { + Main.logger.severe("[EsTools] Failed to migrate main config, it might be invalid"); + Main.logger.severe("[EsTools] You may need to delete config.yml for it to function again"); + Main.logger.severe("[EsTools] Your current setting ARE NOT BEING RESPECTED"); + } + } + + private static void migrateGiveConfig() { // config.yml + File file = new File(Main.server.getDataFolder(), giveConfig); + if (!file.exists()) { + return; + } + + try { // We will now try to get all the values, defaulting to their current defaults + FileConfiguration config = new YamlConfiguration(); + config.load(file); + + GiveConfig newConfig = new GiveConfig(); + GiveSettings settings = newConfig.getSettings(); + settings.setAddWithoutUnderscores(config.getBoolean("settings.addWithoutUnderscores", settings.isAddWithoutUnderscores())); + settings.setRemoveWithUnderscores(config.getBoolean("settings.removeWithUnderscores", settings.isRemoveWithUnderscores())); + + ConfigurationSection itemsSec = config.getConfigurationSection("items"); + if (itemsSec != null) { + Map items = new HashMap<>(); + for (String key : itemsSec.getKeys(false)) { + items.put(key, itemsSec.getString(key)); + } + + newConfig.setItems(items); + } + + // Move file to file.old + File oldFile = new File(Main.server.getDataFolder(), giveConfig + ".old"); + if (!file.renameTo(oldFile)) { + throw new FileNotFoundException("Failed to rename old config file"); + } + + // Save new config + ConfigManager.save(file, newConfig); + + Main.logger.info("[EsTools] Successfully migrated " + giveConfig); + } catch (IOException | InvalidConfigurationException e) { + Main.logger.severe("[EsTools] Failed to migrate config, it might be invalid"); + Main.logger.severe("[EsTools] You may need to delete " + giveConfig + " for it to function again"); + Main.logger.severe("[EsTools] Your current setting ARE NOT BEING RESPECTED"); + } + } + + private static void migrateGodsConfig() { // config.yml + File file = new File(Main.server.getDataFolder(), godsConfig); + if (!file.exists()) { + return; + } + + try { // We will now try to get all the values, defaulting to their current defaults + FileConfiguration config = new YamlConfiguration(); + config.load(file); + + List gods = config.getStringList("gods"); + List buddhas = config.getStringList("buddhas"); + + // Move file to file.old + File oldFile = new File(Main.server.getDataFolder(), godsConfig + ".old"); + if (!file.renameTo(oldFile)) { + throw new FileNotFoundException("Failed to rename old config file"); + } + + // Save gods.yml and buddhas.yml + ConfigManager.save(file, gods); + ConfigManager.save(buddhaConfig, buddhas); + + Main.logger.info("[EsTools] Successfully migrated " + godsConfig); + } catch (IOException | InvalidConfigurationException e) { + Main.logger.severe("[EsTools] Failed to migrate config, it might be invalid"); + Main.logger.severe("[EsTools] You may need to delete " + godsConfig + " for it to function again"); + Main.logger.severe("[EsTools] Your current setting ARE NOT BEING RESPECTED"); + } + } } From 70b234a8b3358776e62aa11ced4479503b92076d Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Fri, 31 May 2024 11:21:25 +1000 Subject: [PATCH 20/74] Finish config migrator --- .../ServerApi/EsSerialisableItemStack.java | 2 +- .../Bukkit/BukkitConfigMigrator.java | 118 +++++++++++++++++- .../Bukkit/OldWarpLocation.java | 38 ++++++ 3 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index 62f87f3..53083a3 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -8,7 +8,7 @@ import java.util.Map; -// TODO: NBT +// TODO: NBT, Potions public class EsSerialisableItemStack { private String material; private int amount; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java index 0ea0914..05c9f53 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -1,22 +1,24 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Commands.Warps.WarpLocation; import net.serble.estools.Config.ConfigManager; import net.serble.estools.Config.Schemas.GeneralConfig.EsToolsConfig; import net.serble.estools.Config.Schemas.GeneralConfig.UpdaterConfig; import net.serble.estools.Config.Schemas.Give.GiveConfig; import net.serble.estools.Config.Schemas.Give.GiveSettings; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsSerialisableItemStack; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.serialization.ConfigurationSerialization; +import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Tool for migrating from legacy Bukkit configs to v5 compatible configs. @@ -28,6 +30,8 @@ public class BukkitConfigMigrator { private static final String giveConfig = "give.yml"; private static final String godsConfig = "gods.yml"; private static final String buddhaConfig = "buddhas.yml"; + private static final String warpsConfig = "warps.yml"; + private static final String cchestsFolder = "cchests"; public static void checkPerformMigration() { File migratedAlreadyFile = new File(Main.server.getDataFolder(), migratedFile); @@ -52,6 +56,8 @@ public static void checkPerformMigration() { migrateMainConfig(); migrateGiveConfig(); migrateGodsConfig(); + migrateWarpsConfig(); + migrateCChestsConfig(); Main.logger.info("[EsTools] Successfully migrated all existing config files"); try { @@ -170,4 +176,110 @@ private static void migrateGodsConfig() { // config.yml Main.logger.severe("[EsTools] Your current setting ARE NOT BEING RESPECTED"); } } + + private static void migrateWarpsConfig() { // config.yml + File file = new File(Main.server.getDataFolder(), warpsConfig); + if (!file.exists()) { + return; + } + + try { // We will now try to get all the values, defaulting to their current defaults + // Register the old WarpLocation class, so it can be parsed + ConfigurationSerialization.registerClass(OldWarpLocation.class, "WarpLocation"); + FileConfiguration config = new YamlConfiguration(); + config.load(file); + + Map newConfig = new HashMap<>(); + + List warpList = config.getList("warps"); + if (warpList == null) { + return; + } + + warpList.sort((w1, w2) -> { + if (w1 instanceof OldWarpLocation && w2 instanceof OldWarpLocation) { + OldWarpLocation warp1 = (OldWarpLocation)w1; + OldWarpLocation warp2 = (OldWarpLocation)w1; + + return warp1.name.compareTo(warp2.name); + } + + return 0; + }); + + warpList.forEach(w -> { + if (w instanceof OldWarpLocation) { + OldWarpLocation warp = (OldWarpLocation)w; + WarpLocation loc = new WarpLocation(); + loc.setGlobal(warp.global); + loc.setName(warp.name); + loc.setLocation(BukkitHelper.fromBukkitLocation(warp.location)); + newConfig.put(warp.name, loc); + } + }); + + // Move file to file.old + File oldFile = new File(Main.server.getDataFolder(), warpsConfig + ".old"); + if (!file.renameTo(oldFile)) { + throw new FileNotFoundException("Failed to rename old config file"); + } + + // Save new config + ConfigManager.save(file, newConfig); + + Main.logger.info("[EsTools] Successfully migrated " + warpsConfig); + } catch (IOException | InvalidConfigurationException e) { + Main.logger.severe("[EsTools] Failed to migrate config, it might be invalid"); + Main.logger.severe("[EsTools] You may need to delete " + warpsConfig + " for it to function again"); + Main.logger.severe("[EsTools] Your current setting ARE NOT BEING RESPECTED"); + } + } + + private static void migrateCChestsConfig() { // config.yml + File folder = new File(Main.server.getDataFolder(), cchestsFolder); + + File[] savedPlayers = folder.listFiles(); + + if (savedPlayers == null) { + return; + } + + Main.logger.warning("[EsTools] Now migrating CChest config files, this may take a while"); + + for (File file : savedPlayers) { + try { // We will now try to get all the values, defaulting to their current defaults + FileConfiguration config = new YamlConfiguration(); + config.load(file); + + if (!config.contains("items")) { + continue; + } + + @SuppressWarnings("unchecked") + ItemStack[] content = ((ArrayList) Objects.requireNonNull(config.get("items"))).toArray(new ItemStack[0]); + + EsSerialisableItemStack[] newConfig = new EsSerialisableItemStack[content.length]; + for (int i = 0; i < content.length; i++) { + if (content[i] == null) { + newConfig[i] = null; + continue; + } + newConfig[i] = EsSerialisableItemStack.generate(Main.server.createItemStack(content[i])); + } + + // Move file to file.old + File oldFile = new File(file.getName() + ".old"); + if (!file.renameTo(oldFile)) { + throw new FileNotFoundException("Failed to rename old config file"); + } + + // Save new config + ConfigManager.save(file, newConfig); + + Main.logger.info("[EsTools] Successfully migrated CChest: " + file.getName()); + } catch (IOException | InvalidConfigurationException e) { + Main.logger.severe("[EsTools] Failed to migrate CChest config, data may be lost"); + } + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java new file mode 100644 index 0000000..8d6f9c4 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java @@ -0,0 +1,38 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import org.bukkit.Location; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.SerializableAs; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +@SerializableAs("WarpLocation") +public class OldWarpLocation implements ConfigurationSerializable { + public Location location; + public String name; + public boolean global; + + @Override + public @NotNull Map serialize() { + Map result = new HashMap<>(); + + result.put("location", location); + result.put("name", name); + result.put("global", global); + + return result; + } + + @SuppressWarnings("unused") + public static OldWarpLocation deserialize(Map args) { + OldWarpLocation warp = new OldWarpLocation(); + + warp.location = (Location)args.get("location"); + warp.name = (String)args.get("name"); + warp.global = (boolean)args.get("global"); + + return warp; + } +} From 77efd749da827fbb3d2cc6cca2781ea7dbe7ec6b Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Mon, 3 Jun 2024 11:18:04 +1000 Subject: [PATCH 21/74] Potions saving to YML (untested) --- .../java/net/serble/estools/MetaHandler.java | 1 + .../estools/ServerApi/EsPotionEffect.java | 42 +++++++++++ .../ServerApi/EsSerialisableItemStack.java | 39 +++++++++- .../Implementations/Bukkit/BukkitHelper.java | 56 ++++++++++++++ .../Implementations/Bukkit/BukkitPotion.java | 73 ++++++++++++++++++ .../Implementations/Bukkit/BukkitServer.java | 29 ++++++-- .../Implementations/Folia/FoliaHelper.java | 20 +++++ .../Implementations/Folia/FoliaPotion.java | 74 +++++++++++++++++++ .../Implementations/Folia/FoliaServer.java | 25 ++++++- .../ServerApi/Interfaces/EsPotion.java | 10 +++ .../Interfaces/EsServerSoftware.java | 3 +- 11 files changed, 361 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Interfaces/EsPotion.java diff --git a/src/main/java/net/serble/estools/MetaHandler.java b/src/main/java/net/serble/estools/MetaHandler.java index 7b016d5..de4dd44 100644 --- a/src/main/java/net/serble/estools/MetaHandler.java +++ b/src/main/java/net/serble/estools/MetaHandler.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Objects; +// TODO: This is Bukkit specific (But still works on other platforms), migrate commands away from using it // This class exists because Minecraft < 1.4 doesn't have ItemMeta. It just encapsulates all ItemMeta methods. public class MetaHandler { diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java b/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java new file mode 100644 index 0000000..24e7d73 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java @@ -0,0 +1,42 @@ +package net.serble.estools.ServerApi; + +@SuppressWarnings("unused") // Needs the methods for serialiser +public class EsPotionEffect { + private String type; + private int amp; + private int duration; + + public EsPotionEffect(String type, int amp, int duration) { + this.type = type; + this.amp = amp; + this.duration = duration; + } + + public EsPotionEffect() { // Serialiser needs this + + } + + public int getAmp() { + return amp; + } + + public int getDuration() { + return duration; + } + + public String getType() { + return type; + } + + public void setAmp(int amp) { + this.amp = amp; + } + + public void setDuration(int duration) { + this.duration = duration; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index 53083a3..07d9036 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -3,12 +3,14 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPotion; import java.util.List; import java.util.Map; -// TODO: NBT, Potions +// TODO: NBT +@SuppressWarnings("unused") // Needs the methods for YAML serialiser public class EsSerialisableItemStack { private String material; private int amount; @@ -16,6 +18,8 @@ public class EsSerialisableItemStack { private List lore; private Map enchantments; private EsItemFlag[] flags; + private EsPotionEffect[] potionEffects; + private EsPotType potType; /** Public constructor for serialiser */ public EsSerialisableItemStack() { } @@ -28,11 +32,19 @@ public static EsSerialisableItemStack generate(EsItemStack stack) { result.setCustomName(stack.getItemMeta().getDisplayName()); result.setEnchantments(stack.getEnchantments()); result.setFlags(stack.getItemMeta().getItemFlags().toArray(new EsItemFlag[0])); + + if (stack instanceof EsPotion) { + EsPotion pot = (EsPotion) stack; + result.setPotionEffects(pot.getEffects()); + result.setPotType(pot.getPotionType()); + } return result; } public EsItemStack toItemStack() { - EsItemStack stack = Main.server.createItemStack(material, amount); + EsItemStack stack = potionEffects == null ? + Main.server.createItemStack(material, amount) : + Main.server.createPotion(potType); EsItemMeta meta = stack.getItemMeta(); meta.setLore(lore); meta.setDisplayName(customName); @@ -41,6 +53,13 @@ public EsItemStack toItemStack() { for (Map.Entry ench : enchantments.entrySet()) { stack.addEnchantment(ench.getKey(), ench.getValue()); } + + if (potionEffects != null) { + EsPotion pot = (EsPotion) stack; + for (EsPotionEffect effect : potionEffects) { + pot.addEffect(effect); + } + } return stack; } @@ -91,4 +110,20 @@ public EsItemFlag[] getFlags() { public void setFlags(EsItemFlag[] flags) { this.flags = flags; } + + public EsPotionEffect[] getPotionEffects() { + return potionEffects; + } + + public void setPotionEffects(EsPotionEffect[] potionEffects) { + this.potionEffects = potionEffects; + } + + public EsPotType getPotType() { + return potType; + } + + public void setPotType(EsPotType potType) { + this.potType = potType; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index acd59f5..e5d759f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -20,9 +20,16 @@ import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; import org.bukkit.util.Vector; +import java.util.Objects; + @SuppressWarnings("unused") public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { @@ -284,6 +291,55 @@ public static EsBlock fromBukkitBlock(BlockState block) { return new BukkitBlock(block); } + public static EsItemStack fromBukkitItem(ItemStack item) { + if (Main.minecraftVersion.getMinor() >= 9) { + if (item.getItemMeta() instanceof PotionMeta) { + return new BukkitPotion(item); + } + + return new BukkitItemStack(item); + } else if (Main.minecraftVersion.getMinor() >= 4) { + if (item.getType().name().endsWith("POTION")) { + return new BukkitItemStack(item); + } + + return new BukkitItemStack(item); + } else { + throw new RuntimeException("Potions aren't support in this Minecraft version"); + } + } + + public static EsPotType fromBukkitPotType(Material type) { + return fromBukkitPotType(type.name()); + } + + public static EsPotType fromBukkitPotType(String type) { + switch (type) { + case "SPLASH_POTION": + return EsPotType.splash; + case "POTION": + return EsPotType.drink; + case "LINGERING_POTION": + return EsPotType.lingering; + } + + return null; + } + + public static PotionEffect toBukkitPotionEffect(EsPotionEffect effect) { + return new PotionEffect(toBukkitPotionEffectType(effect.getType()), effect.getDuration(), effect.getAmp()); + } + + // TODO: Backwards compat? + public static PotionEffectType toBukkitPotionEffectType(String type) { + return Objects.requireNonNull(Registry.EFFECT.match(type)); + } + + // TODO: Backwards compat? + public static PotionType toBukkitPotionType(String type) { + return Objects.requireNonNull(Registry.POTION.match(type)); + } + public static boolean isFolia() { try { Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java new file mode 100644 index 0000000..dca431a --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java @@ -0,0 +1,73 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Interfaces.EsPotion; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; // TODO: pre 1.9 errors because no PotionMeta and pre 1.4 errors +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionEffect; + +@SuppressWarnings("deprecation") // This whole class depends on the deprecated Potion class +public class BukkitPotion extends BukkitItemStack implements EsPotion { + private Potion bukkitPotion; + private PotionMeta bukkitMeta; + private boolean old; + + public BukkitPotion(ItemStack is) { + super(is); + calcOld(); + + if (old) { + bukkitMeta = (PotionMeta) is.getItemMeta(); + } else { + bukkitPotion = Potion.fromItemStack(is); + } + } + + private void calcOld() { + if (Main.minecraftVersion.getMinor() >= 9) { + old = false; + } else if (Main.minecraftVersion.getMinor() >= 4) { + old = true; + } else { + throw new RuntimeException("Potion are not supported in this Minecraft version"); + } + } + + @Override + public EsPotionEffect[] getEffects() { + PotionEffect[] in; + if (old) { + in = bukkitPotion.getEffects().toArray(new PotionEffect[0]); + } else { + in = bukkitMeta.getCustomEffects().toArray(new PotionEffect[0]); + } + + EsPotionEffect[] out = new EsPotionEffect[in.length]; + for (int i = 0; i < out.length; i++) { + PotionEffect effect = in[i]; + out[i] = new EsPotionEffect(effect.getType().getName(), effect.getAmplifier(), effect.getDuration()); + } + return out; + } + + @Override + public EsPotType getPotionType() { + if (old) { + return BukkitHelper.fromBukkitPotType(bukkitPotion.toItemStack(0).getType()); + } else { + return BukkitHelper.fromBukkitPotType(getBukkitItem().getType()); + } + } + + @Override + public void addEffect(EsPotionEffect effect) { + if (old) { + bukkitPotion.getEffects().add(BukkitHelper.toBukkitPotionEffect(effect)); + } else { + bukkitMeta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index bce3a5c..d4b8482 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -13,6 +13,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitTask; @@ -120,7 +121,7 @@ public EsItemStack createItemStack(String material, int amount) { @SuppressWarnings("deprecation") @Override - public EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + public EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { if (Main.minecraftVersion.getMinor() >= 9) { String type = potType == EsPotType.drink ? "POTION" : @@ -136,9 +137,9 @@ public EsItemStack createPotion(EsPotType potType, String effect, int duration, PotionMeta meta = (PotionMeta) pot.getItemMeta(); assert meta != null; - meta.addCustomEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effType)), duration, amp-1), true); + meta.addCustomEffect(new PotionEffect(BukkitHelper.toBukkitPotionEffectType(effType), duration, amp-1), true); pot.setItemMeta(meta); - return new BukkitItemStack(pot); + return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { String effType; try { @@ -147,9 +148,27 @@ public EsItemStack createPotion(EsPotType potType, String effect, int duration, return null; } - org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); + Potion potion = new Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); potion.setSplash(potType == EsPotType.splash); - return new BukkitItemStack(potion.toItemStack(1)); + return new BukkitPotion(potion.toItemStack(1)); + } else { // This isn't possible to get to because this class won't load on 1.3 and below + return null; + } + } + + @SuppressWarnings("deprecation") + @Override + public EsPotion createPotion(EsPotType potType) { + if (Main.minecraftVersion.getMinor() >= 9) { + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), 1); + return new BukkitPotion(pot); + } else if (Main.minecraftVersion.getMinor() >= 4) { + Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); + potion.setSplash(potType == EsPotType.splash); + return new BukkitPotion(potion.toItemStack(1)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 2d2e3f2..bcbb6d8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -20,6 +20,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -182,6 +184,24 @@ public static EsBlock fromBukkitBlock(Block block) { return new net.serble.estools.ServerApi.Implementations.Folia.FoliaBlock(state); } + public static EsItemStack fromBukkitItem(ItemStack item) { + if (Main.minecraftVersion.getMinor() >= 9) { + if (item.getItemMeta() instanceof PotionMeta) { + return new FoliaPotion(item); + } + + return new FoliaItemStack(item); + } else if (Main.minecraftVersion.getMinor() >= 4) { + if (item.getType().name().endsWith("POTION")) { + return new FoliaPotion(item); + } + + return new FoliaItemStack(item); + } else { + throw new RuntimeException("Potions aren't support in this Minecraft version"); + } + } + @SuppressWarnings("deprecation") public static Enchantment getBukkitEnchantment(String name) { if (Main.minecraftVersion.getMinor() >= 13) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java new file mode 100644 index 0000000..d0fd6bc --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java @@ -0,0 +1,74 @@ +package net.serble.estools.ServerApi.Implementations.Folia; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.EsPotion; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionEffect; + +@SuppressWarnings("deprecation") // This whole class depends on the deprecated Potion class +public class FoliaPotion extends FoliaItemStack implements EsPotion { + private Potion bukkitPotion; + private PotionMeta bukkitMeta; + private boolean old; + + public FoliaPotion(ItemStack is) { + super(is); + calcOld(); + + if (old) { + bukkitMeta = (PotionMeta) is.getItemMeta(); + } else { + bukkitPotion = Potion.fromItemStack(is); + } + } + + private void calcOld() { + if (Main.minecraftVersion.getMinor() >= 9) { + old = false; + } else if (Main.minecraftVersion.getMinor() >= 4) { + old = true; + } else { + throw new RuntimeException("Potion are not supported in this Minecraft version"); + } + } + + @Override + public EsPotionEffect[] getEffects() { + PotionEffect[] in; + if (old) { + in = bukkitPotion.getEffects().toArray(new PotionEffect[0]); + } else { + in = bukkitMeta.getCustomEffects().toArray(new PotionEffect[0]); + } + + EsPotionEffect[] out = new EsPotionEffect[in.length]; + for (int i = 0; i < out.length; i++) { + PotionEffect effect = in[i]; + out[i] = new EsPotionEffect(effect.getType().getName(), effect.getAmplifier(), effect.getDuration()); + } + return out; + } + + @Override + public EsPotType getPotionType() { + if (old) { + return FoliaHelper.fromBukkitPotType(bukkitPotion.toItemStack(0).getType()); + } else { + return FoliaHelper.fromBukkitPotType(getBukkitItem().getType()); + } + } + + @Override + public void addEffect(EsPotionEffect effect) { + if (old) { + bukkitPotion.getEffects().add(BukkitHelper.toBukkitPotionEffect(effect)); + } else { + bukkitMeta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 6b575c4..dd6d592 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -131,7 +132,7 @@ public EsItemStack createItemStack(String material, int amount) { @SuppressWarnings("deprecation") @Override - public EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + public EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { if (Main.minecraftVersion.getMinor() >= 9) { String type = potType == EsPotType.drink ? "POTION" : @@ -149,7 +150,7 @@ public EsItemStack createPotion(EsPotType potType, String effect, int duration, assert meta != null; meta.addCustomEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effType)), duration, amp-1), true); pot.setItemMeta(meta); - return new FoliaItemStack(pot); + return new FoliaPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { String effType; try { @@ -160,7 +161,25 @@ public EsItemStack createPotion(EsPotType potType, String effect, int duration, org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); potion.setSplash(potType == EsPotType.splash); - return new FoliaItemStack(potion.toItemStack(1)); + return new FoliaPotion(potion.toItemStack(1)); + } else { // This isn't possible to get to because this class won't load on 1.3 and below + return null; + } + } + + @SuppressWarnings("deprecation") + @Override + public EsPotion createPotion(EsPotType potType) { + if (Main.minecraftVersion.getMinor() >= 9) { + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), 1); + return new FoliaPotion(pot); + } else if (Main.minecraftVersion.getMinor() >= 4) { + Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); + potion.setSplash(potType == EsPotType.splash); + return new FoliaPotion(potion.toItemStack(1)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPotion.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPotion.java new file mode 100644 index 0000000..414a1f5 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPotion.java @@ -0,0 +1,10 @@ +package net.serble.estools.ServerApi.Interfaces; + +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.EsPotionEffect; + +public interface EsPotion extends EsItemStack { + EsPotionEffect[] getEffects(); + EsPotType getPotionType(); + void addEffect(EsPotionEffect effect); +} diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java index 9052d38..2caf280 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java @@ -15,7 +15,8 @@ public interface EsServerSoftware { SemanticVersion getVersion(); Collection getOnlinePlayers(); EsItemStack createItemStack(String material, int amount); - EsItemStack createPotion(EsPotType potType, String effect, int duration, int amp, int amount); + EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount); + EsPotion createPotion(EsPotType potType); EsInventory createInventory(EsPlayer owner, int size, String title); String[] getPotionEffectTypes(); String[] getEnchantments(); From b74915f6ed418a0efceb32a18d3a902ce7ec80a4 Mon Sep 17 00:00:00 2001 From: copokbl Date: Wed, 5 Jun 2024 17:51:00 +1000 Subject: [PATCH 22/74] Work towards fixing Potion saving in CChests --- .../Bukkit/BukkitEventsListener.java | 6 +++--- .../Implementations/Bukkit/BukkitHelper.java | 10 +++++++--- .../Bukkit/BukkitInventory.java | 6 +++--- .../Bukkit/BukkitItemStack.java | 6 +----- .../Implementations/Bukkit/BukkitPlayer.java | 2 +- .../Bukkit/BukkitPlayerInventory.java | 12 +++++------ .../Implementations/Bukkit/BukkitPotion.java | 20 +++++++++++++++---- .../Implementations/Bukkit/BukkitServer.java | 2 +- .../Folia/FoliaEventsListener.java | 6 +++--- .../Implementations/Folia/FoliaInventory.java | 6 +++--- .../Implementations/Folia/FoliaItemStack.java | 6 +----- .../Implementations/Folia/FoliaPlayer.java | 2 +- .../Folia/FoliaPlayerInventory.java | 12 +++++------ .../Implementations/Folia/FoliaPotion.java | 11 +++++++++- .../Implementations/Folia/FoliaServer.java | 2 +- 15 files changed, 63 insertions(+), 46 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index aba8b75..68d4be0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -61,7 +61,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { - EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), new BukkitItemStack(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -104,9 +104,9 @@ public void onInvClick(InventoryClickEvent e) { EsInventory inv = new BukkitInventory(e.getInventory()); EsClickType ct = BukkitHelper.fromBukkitClickType(e.getClick()); EsInventoryAction ac = BukkitHelper.fromBukkitInventoryAction(e.getAction()); - EsItemStack ci = new BukkitItemStack(e.getCurrentItem()); + EsItemStack ci = BukkitHelper.fromBukkitItem(e.getCurrentItem()); EsPlayer cl = new BukkitPlayer((Player) e.getWhoClicked()); - EsItemStack cu = new BukkitItemStack(e.getCursor()); + EsItemStack cu = BukkitHelper.fromBukkitItem(e.getCursor()); int sl = e.getSlot(); EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); Main.callEvent(ee); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index e5d759f..3b80e9f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -292,15 +292,19 @@ public static EsBlock fromBukkitBlock(BlockState block) { } public static EsItemStack fromBukkitItem(ItemStack item) { + if (item == null) { + return null; + } + if (Main.minecraftVersion.getMinor() >= 9) { - if (item.getItemMeta() instanceof PotionMeta) { + if (item.getItemMeta() != null && item.getItemMeta() instanceof PotionMeta) { return new BukkitPotion(item); } - + return new BukkitItemStack(item); } else if (Main.minecraftVersion.getMinor() >= 4) { if (item.getType().name().endsWith("POTION")) { - return new BukkitItemStack(item); + return new BukkitPotion(item); } return new BukkitItemStack(item); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index 4709592..3cb84a0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -30,7 +30,7 @@ public void setItem(int slot, EsItemStack item) { @Override public EsItemStack getItem(int slot) { - return new BukkitItemStack(bukkitInv.getItem(slot)); + return BukkitHelper.fromBukkitItem(bukkitInv.getItem(slot)); } @Override @@ -60,7 +60,7 @@ public EsItemStack[] getContents() { items[i] = null; continue; } - items[i] = new BukkitItemStack(bukkitItems[i]); + items[i] = BukkitHelper.fromBukkitItem(bukkitItems[i]); } return items; } @@ -88,7 +88,7 @@ public Map all(String material) { Map out = new HashMap<>(); for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { - out.put(entry.getKey(), new BukkitItemStack(entry.getValue())); + out.put(entry.getKey(), BukkitHelper.fromBukkitItem(entry.getValue())); } return out; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index c03f2fa..d40490e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -19,7 +19,7 @@ public BukkitItemStack(String mat, int amount) { bukkitItem = new ItemStack(Material.valueOf(mat), amount); } - public BukkitItemStack(org.bukkit.inventory.ItemStack child) { + public BukkitItemStack(ItemStack child) { bukkitItem = child; } @@ -27,10 +27,6 @@ public ItemStack getBukkitItem() { return bukkitItem; } - public BukkitItemStack(Object internalObject) { - bukkitItem = (ItemStack) internalObject; - } - @Override public String getType() { return bukkitItem.getType().name(); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 30ccf04..e9cc683 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -53,7 +53,7 @@ public EsItemStack getMainHand() { if (mainHand == null) { return null; } - return new BukkitItemStack(mainHand); + return BukkitHelper.fromBukkitItem(mainHand); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index 8589093..3acbaf0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -24,32 +24,32 @@ public PlayerInventory getBukkitInventory() { @Override public EsItemStack getOffHand() { - return new BukkitItemStack(bukkitInv.getItemInOffHand()); + return BukkitHelper.fromBukkitItem(bukkitInv.getItemInOffHand()); } @Override public EsItemStack getMainHand() { - return new BukkitItemStack(bukkitInv.getItemInMainHand()); + return BukkitHelper.fromBukkitItem(bukkitInv.getItemInMainHand()); } @Override public EsItemStack getHelmet() { - return new BukkitItemStack(bukkitInv.getHelmet()); + return BukkitHelper.fromBukkitItem(bukkitInv.getHelmet()); } @Override public EsItemStack getLeggings() { - return new BukkitItemStack(bukkitInv.getLeggings()); + return BukkitHelper.fromBukkitItem(bukkitInv.getLeggings()); } @Override public EsItemStack getChestplate() { - return new BukkitItemStack(bukkitInv.getChestplate()); + return BukkitHelper.fromBukkitItem(bukkitInv.getChestplate()); } @Override public EsItemStack getBoots() { - return new BukkitItemStack(bukkitInv.getBoots()); + return BukkitHelper.fromBukkitItem(bukkitInv.getBoots()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java index dca431a..b9ddce5 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java @@ -8,10 +8,17 @@ import org.bukkit.inventory.meta.PotionMeta; // TODO: pre 1.9 errors because no PotionMeta and pre 1.4 errors import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionType; + +import java.util.ArrayList; +import java.util.List; @SuppressWarnings("deprecation") // This whole class depends on the deprecated Potion class public class BukkitPotion extends BukkitItemStack implements EsPotion { + /** Only used when old is true */ private Potion bukkitPotion; + + /** Only used when old is false */ private PotionMeta bukkitMeta; private boolean old; @@ -20,9 +27,9 @@ public BukkitPotion(ItemStack is) { calcOld(); if (old) { - bukkitMeta = (PotionMeta) is.getItemMeta(); - } else { bukkitPotion = Potion.fromItemStack(is); + } else { + bukkitMeta = (PotionMeta) is.getItemMeta(); } } @@ -42,7 +49,12 @@ public EsPotionEffect[] getEffects() { if (old) { in = bukkitPotion.getEffects().toArray(new PotionEffect[0]); } else { - in = bukkitMeta.getCustomEffects().toArray(new PotionEffect[0]); + PotionType baseType = bukkitMeta.getBasePotionType(); + List baseEffects = new ArrayList<>(baseType.getPotionEffects()); // What's the difference between base and custom effects? + if (bukkitMeta.hasCustomEffects()) { // The docs say to do this + baseEffects.addAll(bukkitMeta.getCustomEffects()); + } + in = baseEffects.toArray(new PotionEffect[0]); } EsPotionEffect[] out = new EsPotionEffect[in.length]; @@ -63,7 +75,7 @@ public EsPotType getPotionType() { } @Override - public void addEffect(EsPotionEffect effect) { + public void addEffect(EsPotionEffect effect) { // TODO: This just doesn't work, same with the Folia implementation if (old) { bukkitPotion.getEffects().add(BukkitHelper.toBukkitPotionEffect(effect)); } else { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index d4b8482..ee5cd57 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -346,7 +346,7 @@ public EsWorld getWorld(String name) { @Override public EsItemStack createItemStack(Object internalObject) { - return new BukkitItemStack(internalObject); + return BukkitHelper.fromBukkitItem((ItemStack) internalObject); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 72a4aaf..965243c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -61,7 +61,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { - EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), new FoliaItemStack(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), FoliaHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -104,9 +104,9 @@ public void onInvClick(InventoryClickEvent e) { EsInventory inv = new FoliaInventory(e.getInventory()); EsClickType ct = FoliaHelper.fromBukkitClickType(e.getClick()); EsInventoryAction ac = FoliaHelper.fromBukkitInventoryAction(e.getAction()); - EsItemStack ci = new FoliaItemStack(e.getCurrentItem()); + EsItemStack ci = FoliaHelper.fromBukkitItem(e.getCurrentItem()); EsPlayer cl = new FoliaPlayer((Player) e.getWhoClicked()); - EsItemStack cu = new FoliaItemStack(e.getCursor()); + EsItemStack cu = FoliaHelper.fromBukkitItem(e.getCursor()); int sl = e.getSlot(); EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); Main.callEvent(ee); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index 1995772..b8e11a2 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -29,14 +29,14 @@ public EsItemStack[] getContents() { items[i] = null; continue; } - items[i] = new FoliaItemStack(bukkitItems[i]); + items[i] = FoliaHelper.fromBukkitItem(bukkitItems[i]); } return items; } @Override public EsItemStack getItem(int slot) { - return new FoliaItemStack(bukkitInv.getItem(slot)); + return FoliaHelper.fromBukkitItem(bukkitInv.getItem(slot)); } @Override @@ -53,7 +53,7 @@ public Map all(String material) { Map out = new HashMap<>(); for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { - out.put(entry.getKey(), new FoliaItemStack(entry.getValue())); + out.put(entry.getKey(), FoliaHelper.fromBukkitItem(entry.getValue())); } return out; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index ab2f45a..1dfd7da 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -20,11 +20,7 @@ public FoliaItemStack(String mat, int amount) { bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); } - public FoliaItemStack(Object internalObject) { - bukkitItem = (ItemStack) internalObject; - } - - public FoliaItemStack(org.bukkit.inventory.ItemStack child) { + public FoliaItemStack(ItemStack child) { bukkitItem = child; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java index 529d862..1556780 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -54,7 +54,7 @@ public EsItemStack getMainHand() { if (mainHand == null) { return null; } - return new FoliaItemStack(mainHand); + return FoliaHelper.fromBukkitItem(mainHand); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java index 8fc28c3..5dc3d11 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayerInventory.java @@ -20,32 +20,32 @@ public PlayerInventory getBukkitInventory() { @Override public EsItemStack getOffHand() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getItemInOffHand()); + return FoliaHelper.fromBukkitItem(bukkitInv.getItemInOffHand()); } @Override public EsItemStack getMainHand() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getItemInMainHand()); + return FoliaHelper.fromBukkitItem(bukkitInv.getItemInMainHand()); } @Override public EsItemStack getHelmet() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getHelmet()); + return FoliaHelper.fromBukkitItem(bukkitInv.getHelmet()); } @Override public EsItemStack getLeggings() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getLeggings()); + return FoliaHelper.fromBukkitItem(bukkitInv.getLeggings()); } @Override public EsItemStack getChestplate() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getChestplate()); + return FoliaHelper.fromBukkitItem(bukkitInv.getChestplate()); } @Override public EsItemStack getBoots() { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaItemStack(bukkitInv.getBoots()); + return FoliaHelper.fromBukkitItem(bukkitInv.getBoots()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java index d0fd6bc..f9630ee 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java @@ -9,6 +9,10 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionType; + +import java.util.ArrayList; +import java.util.List; @SuppressWarnings("deprecation") // This whole class depends on the deprecated Potion class public class FoliaPotion extends FoliaItemStack implements EsPotion { @@ -43,7 +47,12 @@ public EsPotionEffect[] getEffects() { if (old) { in = bukkitPotion.getEffects().toArray(new PotionEffect[0]); } else { - in = bukkitMeta.getCustomEffects().toArray(new PotionEffect[0]); + PotionType baseType = bukkitMeta.getBasePotionType(); + List baseEffects = new ArrayList<>(baseType.getPotionEffects()); // What's the difference between base and custom effects? + if (bukkitMeta.hasCustomEffects()) { // The docs say to do this + baseEffects.addAll(bukkitMeta.getCustomEffects()); + } + in = baseEffects.toArray(new PotionEffect[0]); } EsPotionEffect[] out = new EsPotionEffect[in.length]; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index dd6d592..07ab32b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -379,7 +379,7 @@ public EsWorld getWorld(String name) { @Override public EsItemStack createItemStack(Object internalObject) { - return new FoliaItemStack(internalObject); + return FoliaHelper.fromBukkitItem((ItemStack) internalObject); } @Override From cc66a634d881e6aa9823207e8502e6595048e19b Mon Sep 17 00:00:00 2001 From: copokbl Date: Wed, 5 Jun 2024 18:57:07 +1000 Subject: [PATCH 23/74] Stop events messing with the current cancellation state and messing up other plugins --- .../Implementations/Bukkit/BukkitEventsListener.java | 7 +++++++ .../Implementations/Folia/FoliaEventsListener.java | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 68d4be0..757d8ed 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -62,6 +62,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -72,6 +73,7 @@ public void onTeleport(PlayerTeleportEvent e) { new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitTeleportCause(e.getCause()), BukkitHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -93,6 +95,7 @@ public void onPlayerDeath(PlayerDeathEvent e) { @EventHandler public void onEntityDamage(EntityDamageEvent e) { EsEntityDamageEvent ee = new EsEntityDamageEvent(BukkitHelper.fromBukkitEntity(e.getEntity()), getDamageFromEvent(e)); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); setDamageFromEvent(e, ee.getDamage()); e.setCancelled(ee.isCancelled()); @@ -109,6 +112,7 @@ public void onInvClick(InventoryClickEvent e) { EsItemStack cu = BukkitHelper.fromBukkitItem(e.getCursor()); int sl = e.getSlot(); EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -138,6 +142,7 @@ public void onDrag(InventoryDragEvent e) { Set cs = e.getRawSlots(); EsInventoryView view = new BukkitInventoryView(e.getView()); EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -148,6 +153,7 @@ public void onInteract(PlayerInteractEvent e) { EsPlayer p = new BukkitPlayer(e.getPlayer()); EsAction ac = BukkitHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -157,6 +163,7 @@ public void onSignChange(SignChangeEvent e) { EsPlayer p = new BukkitPlayer(e.getPlayer()); String[] lines = e.getLines(); EsSignChangeEvent ee = new EsSignChangeEvent(p, lines); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); for (int i = 0; i < lines.length; i++) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 965243c..5ba2327 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -62,6 +62,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), FoliaHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -72,6 +73,7 @@ public void onTeleport(PlayerTeleportEvent e) { new FoliaPlayer(e.getPlayer()), FoliaHelper.fromBukkitTeleportCause(e.getCause()), FoliaHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -93,6 +95,7 @@ public void onPlayerDeath(PlayerDeathEvent e) { @EventHandler public void onEntityDamage(EntityDamageEvent e) { EsEntityDamageEvent ee = new EsEntityDamageEvent(FoliaHelper.fromBukkitEntity(e.getEntity()), getDamageFromEvent(e)); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); setDamageFromEvent(e, ee.getDamage()); e.setCancelled(ee.isCancelled()); @@ -109,6 +112,7 @@ public void onInvClick(InventoryClickEvent e) { EsItemStack cu = FoliaHelper.fromBukkitItem(e.getCursor()); int sl = e.getSlot(); EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -138,6 +142,7 @@ public void onDrag(InventoryDragEvent e) { Set cs = e.getRawSlots(); EsInventoryView view = new FoliaInventoryView(e.getView()); EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } @@ -148,6 +153,7 @@ public void onInteract(PlayerInteractEvent e) { EsPlayer p = new FoliaPlayer(e.getPlayer()); EsAction ac = FoliaHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); + ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); } From f631b913689c351e8be8c74d0febb6f21bece60d Mon Sep 17 00:00:00 2001 From: Calcilore Date: Sat, 15 Jun 2024 22:43:40 +1000 Subject: [PATCH 24/74] Made EsSerialisableItemStack work with any NBT data/meta, and fixed bug where playerInteractEvent was cancelled when it shouldn't be. --- .../estools/ServerApi/EsEventResult.java | 7 ++ .../ServerApi/EsSerialisableItemStack.java | 71 ++++++------------- .../Events/EsPlayerInteractEvent.java | 23 +++++- .../Bukkit/BukkitEventsListener.java | 15 ++-- .../Implementations/Bukkit/BukkitHelper.java | 36 ++++++++++ .../Bukkit/BukkitItemStack.java | 22 ++++++ .../Folia/FoliaEventsListener.java | 17 +++-- .../Implementations/Folia/FoliaHelper.java | 9 +++ .../Implementations/Folia/FoliaItemStack.java | 22 ++++++ .../ServerApi/Interfaces/EsItemStack.java | 2 + .../net/serble/estools/Signs/SignMain.java | 3 +- 11 files changed, 160 insertions(+), 67 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsEventResult.java diff --git a/src/main/java/net/serble/estools/ServerApi/EsEventResult.java b/src/main/java/net/serble/estools/ServerApi/EsEventResult.java new file mode 100644 index 0000000..5c9e797 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsEventResult.java @@ -0,0 +1,7 @@ +package net.serble.estools.ServerApi; + +public enum EsEventResult { + DENY, + DEFAULT, + ALLOW +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index 07d9036..93a718d 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -1,11 +1,9 @@ package net.serble.estools.ServerApi; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPotion; -import java.util.List; import java.util.Map; @@ -14,11 +12,9 @@ public class EsSerialisableItemStack { private String material; private int amount; - private String customName; - private List lore; + private String itemMeta; private Map enchantments; - private EsItemFlag[] flags; - private EsPotionEffect[] potionEffects; + private boolean isPotion; private EsPotType potType; /** Public constructor for serialiser */ @@ -28,38 +24,27 @@ public static EsSerialisableItemStack generate(EsItemStack stack) { EsSerialisableItemStack result = new EsSerialisableItemStack(); result.setMaterial(stack.getType()); result.setAmount(stack.getAmount()); - result.setLore(stack.getItemMeta().getLore()); - result.setCustomName(stack.getItemMeta().getDisplayName()); result.setEnchantments(stack.getEnchantments()); - result.setFlags(stack.getItemMeta().getItemFlags().toArray(new EsItemFlag[0])); + result.setItemMeta(stack.exportItemMeta()); if (stack instanceof EsPotion) { EsPotion pot = (EsPotion) stack; - result.setPotionEffects(pot.getEffects()); result.setPotType(pot.getPotionType()); + result.setIsPotion(true); + } else { + result.setIsPotion(false); } + return result; } public EsItemStack toItemStack() { - EsItemStack stack = potionEffects == null ? - Main.server.createItemStack(material, amount) : - Main.server.createPotion(potType); - EsItemMeta meta = stack.getItemMeta(); - meta.setLore(lore); - meta.setDisplayName(customName); - meta.addItemFlags(flags); - stack.setItemMeta(meta); - for (Map.Entry ench : enchantments.entrySet()) { - stack.addEnchantment(ench.getKey(), ench.getValue()); - } + EsItemStack stack = isPotion ? + Main.server.createPotion(potType) : + Main.server.createItemStack(material, 1); - if (potionEffects != null) { - EsPotion pot = (EsPotion) stack; - for (EsPotionEffect effect : potionEffects) { - pot.addEffect(effect); - } - } + stack.setAmount(amount); + stack.importItemMeta(itemMeta); return stack; } @@ -79,22 +64,6 @@ public void setMaterial(String material) { this.material = material; } - public String getCustomName() { - return customName; - } - - public List getLore() { - return lore; - } - - public void setCustomName(String customName) { - this.customName = customName; - } - - public void setLore(List lore) { - this.lore = lore; - } - public Map getEnchantments() { return enchantments; } @@ -103,20 +72,20 @@ public void setEnchantments(Map enchantments) { this.enchantments = enchantments; } - public EsItemFlag[] getFlags() { - return flags; + public String getItemMeta() { + return itemMeta; } - public void setFlags(EsItemFlag[] flags) { - this.flags = flags; + public void setItemMeta(String itemMeta) { + this.itemMeta = itemMeta; } - public EsPotionEffect[] getPotionEffects() { - return potionEffects; + public boolean isPotion() { + return isPotion; } - public void setPotionEffects(EsPotionEffect[] potionEffects) { - this.potionEffects = potionEffects; + public void setIsPotion(boolean isPotion) { + this.isPotion = isPotion; } public EsPotType getPotType() { diff --git a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java index 1cc76bd..796ac4a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java +++ b/src/main/java/net/serble/estools/ServerApi/Events/EsPlayerInteractEvent.java @@ -1,15 +1,18 @@ package net.serble.estools.ServerApi.Events; import net.serble.estools.ServerApi.EsAction; +import net.serble.estools.ServerApi.EsEventResult; import net.serble.estools.ServerApi.Interfaces.EsBlock; -import net.serble.estools.ServerApi.Interfaces.EsCancellableEvent; +import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.jetbrains.annotations.Nullable; -public class EsPlayerInteractEvent extends EsCancellableEvent { +public class EsPlayerInteractEvent implements EsEvent { private final EsPlayer player; private final @Nullable EsBlock clickedBlock; private final EsAction action; + private EsEventResult useInteractedBlock; + private EsEventResult useItemInHand; public EsPlayerInteractEvent(EsPlayer player, @Nullable EsBlock clickedBlock, EsAction action) { this.player = player; @@ -28,4 +31,20 @@ public EsAction getAction() { public @Nullable EsBlock getClickedBlock() { return clickedBlock; } + + public EsEventResult getUseItemInHand() { + return useItemInHand; + } + + public void setUseItemInHand(EsEventResult useItemInHand) { + this.useItemInHand = useItemInHand; + } + + public EsEventResult getUseInteractedBlock() { + return useInteractedBlock; + } + + public void setUseInteractedBlock(EsEventResult useInteractedBlock) { + this.useInteractedBlock = useInteractedBlock; + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 757d8ed..87a3a17 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -103,8 +103,8 @@ public void onEntityDamage(EntityDamageEvent e) { @EventHandler public void onInvClick(InventoryClickEvent e) { - EsInventory clInv = new BukkitInventory(e.getClickedInventory()); - EsInventory inv = new BukkitInventory(e.getInventory()); + EsInventory clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); EsClickType ct = BukkitHelper.fromBukkitClickType(e.getClick()); EsInventoryAction ac = BukkitHelper.fromBukkitInventoryAction(e.getAction()); EsItemStack ci = BukkitHelper.fromBukkitItem(e.getCurrentItem()); @@ -129,7 +129,7 @@ public void onKick(PlayerQuitEvent e) { @EventHandler public void onInvClose(InventoryCloseEvent e) { - EsInventory inv = new BukkitInventory(e.getInventory()); + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); EsPlayer pl = new BukkitPlayer((Player) e.getPlayer()); EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); Main.callEvent(ee); @@ -137,7 +137,7 @@ public void onInvClose(InventoryCloseEvent e) { @EventHandler public void onDrag(InventoryDragEvent e) { - EsInventory inv = new BukkitInventory(e.getInventory()); + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); EsPlayer pl = new BukkitPlayer((Player) e.getWhoClicked()); Set cs = e.getRawSlots(); EsInventoryView view = new BukkitInventoryView(e.getView()); @@ -153,9 +153,12 @@ public void onInteract(PlayerInteractEvent e) { EsPlayer p = new BukkitPlayer(e.getPlayer()); EsAction ac = BukkitHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); - ee.setCancelled(e.isCancelled()); + + ee.setUseInteractedBlock(BukkitHelper.fromBukkitEventResult(e.useInteractedBlock())); + ee.setUseItemInHand(BukkitHelper.fromBukkitEventResult(e.useItemInHand())); Main.callEvent(ee); - e.setCancelled(ee.isCancelled()); + e.setUseInteractedBlock(BukkitHelper.toBukkitEventResult(ee.getUseInteractedBlock())); + e.setUseItemInHand(BukkitHelper.toBukkitEventResult(ee.getUseItemInHand())); } @EventHandler diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 3b80e9f..dbd2b5e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -15,11 +15,13 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.block.Action; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.persistence.PersistentDataType; @@ -594,4 +596,38 @@ public static EsAction fromBukkitAction(Action action) { throw new IllegalArgumentException("Invalid Action"); } } + + public static EsInventory fromBukkitInventory(Inventory inv) { + if (inv == null) { + return null; + } + + return new BukkitInventory(inv); + } + + public static EsEventResult fromBukkitEventResult(Event.Result result) { + switch (result) { + case ALLOW: + return EsEventResult.ALLOW; + case DENY: + return EsEventResult.DENY; + case DEFAULT: + return EsEventResult.DEFAULT; + default: + throw new IllegalArgumentException("Invalid Event.Result"); + } + } + + public static Event.Result toBukkitEventResult(EsEventResult result) { + switch (result) { + case ALLOW: + return Event.Result.ALLOW; + case DENY: + return Event.Result.DENY; + case DEFAULT: + return Event.Result.DEFAULT; + default: + throw new IllegalArgumentException("Invalid EsEventResult"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index d40490e..94962b6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -4,6 +4,8 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; @@ -62,6 +64,26 @@ public EsItemMeta getItemMeta() { return new BukkitItemMeta(bukkitItem.getItemMeta()); } + @Override + public String exportItemMeta() { + YamlConfiguration config = new YamlConfiguration(); + config.set("item", bukkitItem.getItemMeta()); + return config.saveToString(); + } + + @Override + public void importItemMeta(String meta) { + YamlConfiguration config = new YamlConfiguration(); + try { + config.loadFromString(meta); + } catch (InvalidConfigurationException e) { + throw new RuntimeException(e); + } + + ItemMeta itemMeta = (ItemMeta)config.get("item"); + bukkitItem.setItemMeta(itemMeta); + } + @Override public void setItemMeta(EsItemMeta meta) { bukkitItem.setItemMeta(((BukkitItemMeta) meta).getBukkitMeta()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 5ba2327..21ac9d5 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -103,8 +103,8 @@ public void onEntityDamage(EntityDamageEvent e) { @EventHandler public void onInvClick(InventoryClickEvent e) { - EsInventory clInv = new FoliaInventory(e.getClickedInventory()); - EsInventory inv = new FoliaInventory(e.getInventory()); + EsInventory clInv = FoliaHelper.fromBukkitInventory(e.getClickedInventory()); + EsInventory inv = FoliaHelper.fromBukkitInventory(e.getInventory()); EsClickType ct = FoliaHelper.fromBukkitClickType(e.getClick()); EsInventoryAction ac = FoliaHelper.fromBukkitInventoryAction(e.getAction()); EsItemStack ci = FoliaHelper.fromBukkitItem(e.getCurrentItem()); @@ -129,7 +129,7 @@ public void onKick(PlayerQuitEvent e) { @EventHandler public void onInvClose(InventoryCloseEvent e) { - EsInventory inv = new FoliaInventory(e.getInventory()); + EsInventory inv = FoliaHelper.fromBukkitInventory(e.getInventory()); EsPlayer pl = new FoliaPlayer((Player) e.getPlayer()); EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); Main.callEvent(ee); @@ -137,7 +137,7 @@ public void onInvClose(InventoryCloseEvent e) { @EventHandler public void onDrag(InventoryDragEvent e) { - EsInventory inv = new FoliaInventory(e.getInventory()); + EsInventory inv = FoliaHelper.fromBukkitInventory(e.getInventory()); EsPlayer pl = new FoliaPlayer((Player) e.getWhoClicked()); Set cs = e.getRawSlots(); EsInventoryView view = new FoliaInventoryView(e.getView()); @@ -153,13 +153,16 @@ public void onInteract(PlayerInteractEvent e) { EsPlayer p = new FoliaPlayer(e.getPlayer()); EsAction ac = FoliaHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); - ee.setCancelled(e.isCancelled()); + + ee.setUseInteractedBlock(FoliaHelper.fromBukkitEventResult(e.useInteractedBlock())); + ee.setUseItemInHand(FoliaHelper.fromBukkitEventResult(e.useItemInHand())); Main.callEvent(ee); - e.setCancelled(ee.isCancelled()); + e.setUseInteractedBlock(FoliaHelper.toBukkitEventResult(ee.getUseInteractedBlock())); + e.setUseItemInHand(FoliaHelper.toBukkitEventResult(ee.getUseItemInHand())); } @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - return Main.executeCommand(BukkitHelper.fromBukkitCommandSender(sender), command.getName(), args); + return Main.executeCommand(FoliaHelper.fromBukkitCommandSender(sender), command.getName(), args); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index bcbb6d8..0527a09 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -20,6 +20,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -236,4 +237,12 @@ public static boolean isFolia() { return false; } } + + public static EsInventory fromBukkitInventory(Inventory inv) { + if (inv == null) { + return null; + } + + return new FoliaInventory(inv); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 1dfd7da..9bcce70 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -5,6 +5,8 @@ import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; @@ -53,6 +55,26 @@ public EsItemMeta getItemMeta() { return new FoliaItemMeta(bukkitItem.getItemMeta()); } + @Override + public String exportItemMeta() { + YamlConfiguration config = new YamlConfiguration(); + config.set("item", bukkitItem.getItemMeta()); + return config.saveToString(); + } + + @Override + public void importItemMeta(String meta) { + YamlConfiguration config = new YamlConfiguration(); + try { + config.loadFromString(meta); + } catch (InvalidConfigurationException e) { + throw new RuntimeException(e); + } + + ItemMeta itemMeta = (ItemMeta)config.get("item"); + bukkitItem.setItemMeta(itemMeta); + } + @Override public void setItemMeta(EsItemMeta meta) { bukkitItem.setItemMeta(((FoliaItemMeta) meta).getBukkitMeta()); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 9ab1991..15f9f68 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -10,6 +10,8 @@ public interface EsItemStack { void addEnchantment(String enchantment, int level); void removeEnchantment(String enchantment); EsItemMeta getItemMeta(); + String exportItemMeta(); + void importItemMeta(String meta); void setItemMeta(EsItemMeta meta); void setDamage(int val); int getDamage(); diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index 9d990b2..8889a56 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -4,6 +4,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsAction; import net.serble.estools.ServerApi.EsEventListener; +import net.serble.estools.ServerApi.EsEventResult; import net.serble.estools.ServerApi.Events.EsPlayerInteractEvent; import net.serble.estools.ServerApi.Events.EsSignChangeEvent; import net.serble.estools.ServerApi.Interfaces.EsEvent; @@ -39,7 +40,7 @@ public void interact(EsPlayerInteractEvent e) { SignType signType = signs.get(state.getLine(0)); if (signType != null) { - e.setCancelled(true); + e.setUseInteractedBlock(EsEventResult.DENY); signType.run(e.getPlayer(), state.getLines()); } } From 41cd4e06ebc5f8dbc88092ee2789af3cf1bdc3a4 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sat, 15 Jun 2024 23:34:24 +1000 Subject: [PATCH 25/74] Relocations, fix EsSerialisableItemStack potions and folia bugs --- pom.xml | 6 +++++- .../serble/estools/ServerApi/EsSerialisableItemStack.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitHelper.java | 4 ++++ .../ServerApi/Implementations/Folia/FoliaHelper.java | 8 ++++++++ .../ServerApi/Implementations/Folia/FoliaServer.java | 3 ++- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index bcd32aa..900dba9 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,11 @@ co.aikar.taskchain - net.serble.estools.taskchain + net.serble.taskchain + + + org.yaml.snakeyaml + net.serble.snakeyaml diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index 93a718d..b656b0b 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -80,7 +80,7 @@ public void setItemMeta(String itemMeta) { this.itemMeta = itemMeta; } - public boolean isPotion() { + public boolean getIsPotion() { return isPotion; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index dbd2b5e..f6b33e6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -62,6 +62,10 @@ public static Vector toVector(Position pos) { } public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { + if (sender == null) { + return null; + } + if (sender instanceof Player) { return new BukkitPlayer((Player) sender); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 0527a09..e2653f8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -120,6 +120,10 @@ public static void runSync(Runnable task) { } public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { + if (sender == null) { + return null; + } + if (sender instanceof Player) { return new net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer((Player) sender); } @@ -186,6 +190,10 @@ public static EsBlock fromBukkitBlock(Block block) { } public static EsItemStack fromBukkitItem(ItemStack item) { + if (item == null) { + return null; + } + if (Main.minecraftVersion.getMinor() >= 9) { if (item.getItemMeta() instanceof PotionMeta) { return new FoliaPotion(item); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 07ab32b..0f6cd4e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -385,7 +385,8 @@ public EsItemStack createItemStack(Object internalObject) { @Override public String[] getRelevantInternalTypes() { return new String[] { - "CraftItemStack" // CChest config files + "CraftItemStack", // CChest config files + "net.serble.estools.ServerApi.EsSerialisableItemStack" }; } } From aff8e2fba872be405ca8df23f388b1a92a9b29a2 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Sun, 16 Jun 2024 01:23:49 +1000 Subject: [PATCH 26/74] Compatability improvemetns --- .../net/serble/estools/Commands/Mount.java | 5 +++ src/main/java/net/serble/estools/Main.java | 2 +- .../Bukkit/BukkitConfigMigrator.java | 35 +++++++++++++++++-- .../Implementations/Bukkit/BukkitHelper.java | 5 +++ .../Bukkit/BukkitItemMeta.java | 23 ++++++------ .../Bukkit/BukkitLivingEntity.java | 5 ++- .../Implementations/Bukkit/BukkitServer.java | 9 +++-- .../Implementations/Bukkit/BukkitWorld.java | 2 +- 8 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/Mount.java b/src/main/java/net/serble/estools/Commands/Mount.java index 592a6b3..45f1a7a 100644 --- a/src/main/java/net/serble/estools/Commands/Mount.java +++ b/src/main/java/net/serble/estools/Commands/Mount.java @@ -37,6 +37,11 @@ public boolean execute(EsCommandSender sender, String[] args) { } for (EsEntity entity : riders) { + if (entity.getUniqueId() == target.getUniqueId()) { + send(sender, "&cSorry, but you cant ride yourself! (the world ends)"); + return false; + } + target.addPassenger(entity); target = entity; } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index c1cfe0d..bb9ce45 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -111,7 +111,7 @@ public void enable() { sc("setstack", "setstack", new SetStack()); sc("ci", "clearinv", new ClearInv()); sc("day", "time", new Day()); - sc("moon", "time", new Night()); + sc("night", "time", new Night()); sc("noon", "time", new Noon()); sc("midnight", "time", new Midnight()); sc("sun", "weather", new Sun()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java index 05c9f53..99aefe4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -73,6 +73,11 @@ private static void migrateMainConfig() { // config.yml return; } + if (file.length() == 0) { + file.delete(); + return; + } + try { // We will now try to get all the values, defaulting to their current defaults FileConfiguration config = new YamlConfiguration(); config.load(file); @@ -110,6 +115,11 @@ private static void migrateGiveConfig() { // config.yml return; } + if (file.length() == 0) { + file.delete(); + return; + } + try { // We will now try to get all the values, defaulting to their current defaults FileConfiguration config = new YamlConfiguration(); config.load(file); @@ -152,6 +162,11 @@ private static void migrateGodsConfig() { // config.yml return; } + if (file.length() == 0) { + file.delete(); + return; + } + try { // We will now try to get all the values, defaulting to their current defaults FileConfiguration config = new YamlConfiguration(); config.load(file); @@ -183,6 +198,11 @@ private static void migrateWarpsConfig() { // config.yml return; } + if (file.length() == 0) { + file.delete(); + return; + } + try { // We will now try to get all the values, defaulting to their current defaults // Register the old WarpLocation class, so it can be parsed ConfigurationSerialization.registerClass(OldWarpLocation.class, "WarpLocation"); @@ -238,12 +258,21 @@ private static void migrateWarpsConfig() { // config.yml private static void migrateCChestsConfig() { // config.yml File folder = new File(Main.server.getDataFolder(), cchestsFolder); - File[] savedPlayers = folder.listFiles(); - - if (savedPlayers == null) { + File[] files = folder.listFiles(); + if (files == null) { return; } + List savedPlayers = new ArrayList<>(Arrays.asList(files)); + savedPlayers.removeIf(file -> { + if (file.length() == 0) { + file.delete(); + return true; + } + + return false; + }); + Main.logger.warning("[EsTools] Now migrating CChest config files, this may take a while"); for (File file : savedPlayers) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index f6b33e6..6faaa55 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Effects; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; import net.serble.estools.Main; @@ -342,6 +343,10 @@ public static PotionEffect toBukkitPotionEffect(EsPotionEffect effect) { // TODO: Backwards compat? public static PotionEffectType toBukkitPotionEffectType(String type) { + if (Main.minecraftVersion.getMinor() < 20) { + return PotionEffectType.getByName(Effects.getByName(type)); + } + return Objects.requireNonNull(Registry.EFFECT.match(type)); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java index 1cbd59b..a6b9f3b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -40,22 +40,23 @@ public Set getItemFlags() { @Override public void addItemFlags(EsItemFlag... flags) { - ItemFlag[] bukkitFlags = new ItemFlag[flags.length]; - for (int i = 0; i < flags.length; i++) { - EsItemFlag flag = flags[i]; - bukkitFlags[i] = ItemFlag.valueOf(flag.name()); - } - bukkitMeta.addItemFlags(bukkitFlags); + bukkitMeta.addItemFlags(convertFlags(flags)); } @Override public void removeItemFlags(EsItemFlag... flags) { - ItemFlag[] bukkitFlags = new ItemFlag[flags.length]; - for (int i = 0; i < flags.length; i++) { - EsItemFlag flag = flags[i]; - bukkitFlags[i] = ItemFlag.valueOf(flag.name()); + bukkitMeta.removeItemFlags(convertFlags(flags)); + } + + private ItemFlag[] convertFlags(EsItemFlag[] flags) { + List bukkitFlags = new ArrayList<>(flags.length); + for (EsItemFlag flag : flags) { + try { + bukkitFlags.add(ItemFlag.valueOf(flag.name())); + } catch (IllegalArgumentException ignored) {} // doesnt exist, dont add } - bukkitMeta.removeItemFlags(bukkitFlags); + + return bukkitFlags.toArray(new ItemFlag[0]); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index a60aceb..f5be26d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -4,7 +4,6 @@ import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; -import org.bukkit.Registry; import org.bukkit.attribute.Attribute; import org.bukkit.entity.LivingEntity; import org.bukkit.potion.PotionEffect; @@ -99,12 +98,12 @@ public void setHealth(double val) { @Override public void addPotionEffect(String effect, int duration, int amplifier) { - bukkitEntity.addPotionEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect)), duration, amplifier)); + bukkitEntity.addPotionEffect(new PotionEffect(BukkitHelper.toBukkitPotionEffectType(effect), duration, amplifier)); } @Override public void removePotionEffect(String effect) { - bukkitEntity.removePotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect))); + bukkitEntity.removePotionEffect(BukkitHelper.toBukkitPotionEffectType(effect)); } @SuppressWarnings("deprecation") // Gotta love backwards compatibility diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index ee5cd57..0471c0f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -195,13 +195,12 @@ public String[] getPotionEffectTypes() { @Override public String[] getEnchantments() { if (Main.minecraftVersion.getMinor() > 12) { - String[] out = new String[(int) Registry.ENCHANTMENT.stream().count()]; - int i = 0; + List out = new ArrayList<>(); for (Enchantment e : Registry.ENCHANTMENT) { - out[i] = e.getKey().getKey(); - i++; + out.add(e.getKey().getKey()); } - return out; + + return out.toArray(new String[0]); } // Pre 1.13, we need to use the helper to get all the keys diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index aefece8..bb71f79 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -47,7 +47,7 @@ public void setTime(long time) { @Override public void setStorming(boolean val) { - bukkitWorld.setStorm(false); + bukkitWorld.setStorm(val); } @Override From f2cc2de109ac3f86a85213272318e0bbc062bfc8 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 16 Jun 2024 14:46:38 +1000 Subject: [PATCH 27/74] Fix /editsign target block broken null --- .../ServerApi/Implementations/Bukkit/BukkitHelper.java | 4 ++++ .../ServerApi/Implementations/Bukkit/BukkitPlayer.java | 7 +++++-- .../ServerApi/Implementations/Folia/FoliaHelper.java | 4 ++++ .../ServerApi/Implementations/Folia/FoliaPlayer.java | 8 ++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 6faaa55..1cf2893 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -291,6 +291,10 @@ public static NamespacedKey getNamespacedKey(String keyString) { } public static EsBlock fromBukkitBlock(BlockState block) { + if (block == null) { + return null; + } + if (block instanceof Sign) { return new BukkitSign((Sign) block); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index e9cc683..81bb2d8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable; import java.util.HashSet; -import java.util.Objects; public class BukkitPlayer extends BukkitLivingEntity implements EsPlayer { private final org.bukkit.entity.Player bukkitPlayer; @@ -98,7 +97,11 @@ public EsGameMode getGameMode() { @Override public @Nullable EsBlock getTargetBlock() { - return BukkitHelper.fromBukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); + Block target = bukkitGetTargetBlock(); + if (target == null) { + return null; + } + return BukkitHelper.fromBukkitBlock(target.getState()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index e2653f8..cd5229f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -230,6 +230,10 @@ public static Enchantment getBukkitEnchantment(String name) { } public static EsBlock fromBukkitBlock(BlockState block) { + if (block == null) { + return null; + } + if (block instanceof Sign) { return new FoliaSign((Sign) block); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java index 1556780..fe8fe41 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable; import java.util.HashSet; -import java.util.Objects; // Can't extend BukkitEntity because it needs to extend FoliaLivingEntity for it to work public class FoliaPlayer extends FoliaLivingEntity implements EsPlayer { @@ -99,7 +98,12 @@ public EsGameMode getGameMode() { @Override public @Nullable EsBlock getTargetBlock() { - return FoliaHelper.fromBukkitBlock(Objects.requireNonNull(bukkitGetTargetBlock()).getState()); + Block target = bukkitGetTargetBlock(); + if (target == null) { + return null; + } + + return FoliaHelper.fromBukkitBlock(target.getState()); } @Override From f2884ef234dd95864ce76456f4f6b34b27b96ab4 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 16 Jun 2024 21:47:53 +1000 Subject: [PATCH 28/74] Testing, fix registry old version issues --- TestedVersions.txt | 5 +++++ .../serble/estools/Config/Schemas/Give/GiveConfig.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitHelper.java | 10 ++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/TestedVersions.txt b/TestedVersions.txt index 3ad1ccb..af31681 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -3,3 +3,8 @@ Folia: Bukkit: 1.20.4 +1.19.4 +1.18.2 +1.17.1 +1.16.1 +1.14.4 diff --git a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java index da31169..e6742be 100644 --- a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java +++ b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java @@ -9,7 +9,7 @@ public class GiveConfig { private Map items = new HashMap() {{ put("boat", "oak_boat"); put("pearl", "ender_pearl"); - put("sign", "oak_sign"); + put("sign", "oak_sign"); // TODO: Keys like this don't work in old versions, they need to not be used on old put("button", "stone_button"); put("sapling", "oak_sapling"); put("log", "oak_log"); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 1cf2893..d254b29 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -217,7 +217,7 @@ public static EsBlock fromBukkitBlock(Block block) { @SuppressWarnings("deprecation") public static Enchantment getBukkitEnchantment(String name) { - if (Main.minecraftVersion.getMinor() >= 13) { + if (Main.minecraftVersion.getMinor() >= 14) { Enchantment ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); if (ench == null) { // Dump info and throw @@ -275,7 +275,7 @@ public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType @SuppressWarnings("UnstableApiUsage") public static NamespacedKey getNamespacedKey(String keyString) { - if (Main.minecraftVersion.getMinor() >= 16) { + if (Main.minecraftVersion.getMinor() >= 17) { return NamespacedKey.fromString(keyString, EsToolsBukkit.plugin); } @@ -345,7 +345,6 @@ public static PotionEffect toBukkitPotionEffect(EsPotionEffect effect) { return new PotionEffect(toBukkitPotionEffectType(effect.getType()), effect.getDuration(), effect.getAmp()); } - // TODO: Backwards compat? public static PotionEffectType toBukkitPotionEffectType(String type) { if (Main.minecraftVersion.getMinor() < 20) { return PotionEffectType.getByName(Effects.getByName(type)); @@ -354,8 +353,11 @@ public static PotionEffectType toBukkitPotionEffectType(String type) { return Objects.requireNonNull(Registry.EFFECT.match(type)); } - // TODO: Backwards compat? public static PotionType toBukkitPotionType(String type) { + if (Main.minecraftVersion.getMinor() < 20) { + return PotionType.valueOf(Effects.getPotionByName(type)); + } + return Objects.requireNonNull(Registry.POTION.match(type)); } From 8a17da9c9e285bfb81d41d3174823f24e430716b Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 16 Jun 2024 22:41:50 +1000 Subject: [PATCH 29/74] Fix give bug where conflict and default config override actual because it no exist in latesdt but does in old because old has sign and oak_sign is no --- .../java/net/serble/estools/Commands/Give/Give.java | 11 +++++++++-- .../estools/Config/Schemas/Give/GiveConfig.java | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index 330bd10..ca30ab7 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -52,7 +52,8 @@ public static void enable() { Map materials = config.getItems(); // Load normal items - for (String mat : Main.server.getMaterials(true)) { + String[] builtinMats = Main.server.getMaterials(true); + for (String mat : builtinMats) { if (mat == null) { continue; } @@ -70,7 +71,13 @@ public static void enable() { // Load custom items for (Entry s : materials.entrySet()) { - materialNames.put(s.getKey().toUpperCase(), s.getValue().toUpperCase()); // Assume it exists, we trust our users = ) + if (s == null) { + continue; + } + if (Arrays.stream(builtinMats).noneMatch(b -> b != null && b.equalsIgnoreCase(s.getValue()))) { + continue; // Don't trust users, if it doesn't exist then skip + } + materialNames.put(s.getKey().toUpperCase(), s.getValue().toUpperCase()); } } diff --git a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java index e6742be..da31169 100644 --- a/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java +++ b/src/main/java/net/serble/estools/Config/Schemas/Give/GiveConfig.java @@ -9,7 +9,7 @@ public class GiveConfig { private Map items = new HashMap() {{ put("boat", "oak_boat"); put("pearl", "ender_pearl"); - put("sign", "oak_sign"); // TODO: Keys like this don't work in old versions, they need to not be used on old + put("sign", "oak_sign"); put("button", "stone_button"); put("sapling", "oak_sapling"); put("log", "oak_log"); From 410ed0f3116a65532cb37e2d1bc589b29a305ef7 Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Mon, 17 Jun 2024 10:26:03 +1000 Subject: [PATCH 30/74] Fix when EquipmentSlots weren't used in PlayerInventory (1.12 done) --- .../estools/ServerApi/EsEquipmentSlot.java | 2 +- .../Implementations/Bukkit/BukkitHelper.java | 4 +-- .../Bukkit/BukkitPlayerInventory.java | 35 +++++++++++++++++++ .../Implementations/Bukkit/BukkitServer.java | 11 +++--- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java b/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java index 7b5271c..46492a7 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java +++ b/src/main/java/net/serble/estools/ServerApi/EsEquipmentSlot.java @@ -4,7 +4,7 @@ public enum EsEquipmentSlot { Hand, OffHand, Feet, - legs, + Legs, Chest, Head } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index d254b29..3ac7331 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -378,7 +378,7 @@ public static EquipmentSlot toBukkitEquipmentSlot(EsEquipmentSlot slot) { return EquipmentSlot.HAND; case Head: return EquipmentSlot.HEAD; - case legs: + case Legs: return EquipmentSlot.LEGS; case Chest: return EquipmentSlot.CHEST; @@ -398,7 +398,7 @@ public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { case HEAD: return EsEquipmentSlot.Head; case LEGS: - return EsEquipmentSlot.legs; + return EsEquipmentSlot.Legs; case CHEST: return EsEquipmentSlot.Chest; case OFF_HAND: diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index 3acbaf0..e70b8bd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -1,8 +1,10 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.PlayerInventory; public class BukkitPlayerInventory extends BukkitInventory implements EsPlayerInventory { @@ -54,6 +56,39 @@ public EsItemStack getBoots() { @Override public void setItem(EsEquipmentSlot slot, EsItemStack item) { + if (Main.minecraftVersion.getMinor() <= 12) { + int slotId; + switch (slot) { + case Head: + slotId = 5; + break; + + case Chest: + slotId = 6; + break; + + case Legs: + slotId = 7; + break; + + case Feet: + slotId = 8; + break; + + case Hand: + slotId = bukkitInv.getHeldItemSlot(); + break; + + case OffHand: + slotId = 40; + break; + + default: + throw new IllegalStateException("Unexpected value: " + slot); + } + ((Inventory) bukkitInv).setItem(slotId, ((BukkitItemStack) item).getBukkitItem()); + return; + } bukkitInv.setItem(BukkitHelper.toBukkitEquipmentSlot(slot), ((BukkitItemStack) item).getBukkitItem()); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 0471c0f..e13ca7e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -185,11 +185,14 @@ public EsInventory createInventory(EsPlayer owner, int size, String title) { public String[] getPotionEffectTypes() { // We need to use the deprecated .values() method because Registry doesn't exist in old versions PotionEffectType[] effectTypes = PotionEffectType.values(); - String[] out = new String[effectTypes.length]; - for (int i = 0; i < effectTypes.length; i++) { - out[i] = effectTypes[i].getName(); // Same reason as above for deprecated method + List out = new ArrayList<>(); + for (PotionEffectType effectType : effectTypes) { + if (effectType == null) { + continue; + } + out.add(effectType.getName()); // Same reason as above for deprecated method } - return out; + return out.toArray(new String[0]); } @Override From 8594683d206a25bfc07ea828b1ddab8d3a0ff66e Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Mon, 17 Jun 2024 12:25:00 +1000 Subject: [PATCH 31/74] Fix sounds not existing in old versions and fix old enchants not being compared and calculated correctly and fix tester values and test 1.10.2 --- TestedVersions.txt | 3 +++ src/main/java/net/serble/estools/Commands/Back.java | 6 +----- .../java/net/serble/estools/Commands/SetUnbreakable.java | 7 +++---- .../Implementations/Bukkit/BukkitEnchantmentsHelper.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitPlayer.java | 4 ++++ src/main/java/net/serble/estools/Tester.java | 4 ++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/TestedVersions.txt b/TestedVersions.txt index af31681..604b4a7 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -8,3 +8,6 @@ Bukkit: 1.17.1 1.16.1 1.14.4 +1.12.2 +1.11.2 +1.10.2 \ No newline at end of file diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index 8acb2a5..ee1747a 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -6,7 +6,6 @@ import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; -import net.serble.estools.ServerApi.EsTeleportCause; import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -57,10 +56,7 @@ public void executeEvent(EsEvent event) { if (event instanceof EsPlayerTeleportEvent) { EsPlayerTeleportEvent e = (EsPlayerTeleportEvent) event; - - if (equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin)) { - prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); - } + prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); } } } diff --git a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java index 732f104..1baf258 100644 --- a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java +++ b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java @@ -7,7 +7,6 @@ import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import java.util.Arrays; import java.util.Objects; public class SetUnbreakable extends EsToolsCommand { @@ -35,10 +34,10 @@ public boolean execute(EsCommandSender sender, String[] args) { message = "&aSet item to &6Unbreakable!"; } } else { - if (Arrays.stream(Main.server.getEnchantments()).anyMatch(c -> c.equalsIgnoreCase("DURABILITY"))) { - item.removeEnchantment("DURABILITY"); + if (item.getEnchantments().keySet().stream().anyMatch(c -> c != null && c.equalsIgnoreCase("unbreaking"))) { + item.removeEnchantment("unbreaking"); } else { - item.addEnchantment("DURABILITY", 32767); + item.addEnchantment("unbreaking", 32767); // ID is unbreaking because it is translated message = "&aSet item to &6Unbreakable!"; } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java index cf05f79..b0a34ca 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java @@ -19,7 +19,7 @@ public static String getByName(String name) { public static String getNameFromValue(String val) { for (Map.Entry entry : ENCHANTMENTS.entrySet()) { - if (val.toLowerCase().equals(entry.getValue())) { + if (val.equalsIgnoreCase(entry.getValue())) { return entry.getKey(); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 81bb2d8..3bb286e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -121,6 +121,10 @@ public boolean isFlying() { @Override public void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { + if (Main.minecraftVersion.getMinor() < 11) { + bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + return; + } bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), BukkitHelper.toBukkitSoundCategory(category), volume, pitch); } diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index bb0f1b3..76e192b 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -37,10 +37,10 @@ public class Tester { new Tuple<>("tpall", 2), new Tuple<>("warps add test", 2), new Tuple<>("safetp", 2), - new Tuple<>("tp ~50 ~5000 ~50", 1), + new Tuple<>("tp ~50 ~4000 ~50", 1), new Tuple<>("warp test", 2), new Tuple<>("warps remove test", 2), - new Tuple<>("tp ~50 ~5000 ~50", 1), + new Tuple<>("tp ~50 ~4000 ~50", 1), new Tuple<>("back", 1), new Tuple<>("cchest", -1), new Tuple<>("ci", 1), From 4dd563eeeedbc09837c8150e79901bd4870a51af Mon Sep 17 00:00:00 2001 From: copokbl Date: Mon, 17 Jun 2024 18:00:52 +1000 Subject: [PATCH 32/74] Equipment slots and brainless potion implement --- .../Implementations/Bukkit/BukkitEventsListener.java | 7 ++++++- .../ServerApi/Implementations/Bukkit/BukkitServer.java | 9 +-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 87a3a17..3c0e439 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -3,6 +3,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsAction; import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Interfaces.*; @@ -61,7 +62,11 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { - EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + EsEquipmentSlot slot = EsEquipmentSlot.Hand; + if (Main.minecraftVersion.getMinor() > 8) { + slot = BukkitHelper.fromBukkitEquipmentSlot(e.getHand()); + } + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitItem(e.getItemInHand()), slot); ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index e13ca7e..5db7d7c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -141,14 +141,7 @@ public EsPotion createPotion(EsPotType potType, String effect, int duration, int pot.setItemMeta(meta); return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { - String effType; - try { - effType = Effects.getPotionByName(effect); - } catch (IllegalArgumentException e) { - return null; - } - - Potion potion = new Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); + Potion potion = new Potion(BukkitHelper.toBukkitPotionType(effect), amp); potion.setSplash(potType == EsPotType.splash); return new BukkitPotion(potion.toItemStack(1)); } else { // This isn't possible to get to because this class won't load on 1.3 and below From 1e1bf6896b36c2dcc5355112e9303e7f4df21ede Mon Sep 17 00:00:00 2001 From: copokbl Date: Mon, 17 Jun 2024 20:39:13 +1000 Subject: [PATCH 33/74] Fix stuff for 1.7.10 --- pom.xml | 9 +++- .../net/serble/estools/Commands/Back.java | 5 +++ .../Bukkit/BukkitEventsListener.java | 3 +- .../Implementations/Bukkit/BukkitHelper.java | 42 ++---------------- .../Bukkit/BukkitInventory.java | 5 +++ .../Bukkit/BukkitInventoryView.java | 7 +++ .../Implementations/Bukkit/BukkitPlayer.java | 15 +++++++ .../Bukkit/BukkitPlayerInventory.java | 3 +- .../Implementations/Bukkit/BukkitWorld.java | 21 +++++++-- .../BukkitEquipmentSlotHelper.java | 44 +++++++++++++++++++ .../Folia/FoliaEventsListener.java | 3 +- .../Implementations/Folia/FoliaHelper.java | 40 +++++++++++++++++ .../Implementations/Folia/FoliaInventory.java | 5 +++ .../ServerApi/Interfaces/EsInventory.java | 1 + .../ServerApi/Interfaces/EsPlayer.java | 3 ++ 15 files changed, 158 insertions(+), 48 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java diff --git a/pom.xml b/pom.xml index 900dba9..b6b4a7a 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,10 @@ org.yaml.snakeyaml net.serble.snakeyaml + + com.google.gson + net.serble.gson + @@ -59,8 +63,8 @@ - com.google.errorprone:error_prone_annotations - com.google.code.gson:gson + + @@ -133,6 +137,7 @@ com.google.code.gson gson 2.11.0 + compile io.papermc diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index ee1747a..d3962ef 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -6,6 +6,7 @@ import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsTeleportCause; import net.serble.estools.ServerApi.Events.EsPlayerDeathEvent; import net.serble.estools.ServerApi.Events.EsPlayerTeleportEvent; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -56,6 +57,10 @@ public void executeEvent(EsEvent event) { if (event instanceof EsPlayerTeleportEvent) { EsPlayerTeleportEvent e = (EsPlayerTeleportEvent) event; + if (Main.minecraftVersion.getMinor() > 9 && + !equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin)) { + return; + } prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 3c0e439..38ac5e8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -6,6 +6,7 @@ import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation.BukkitEquipmentSlotHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -64,7 +65,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara public void onBlockPlace(BlockPlaceEvent e) { EsEquipmentSlot slot = EsEquipmentSlot.Hand; if (Main.minecraftVersion.getMinor() > 8) { - slot = BukkitHelper.fromBukkitEquipmentSlot(e.getHand()); + slot = BukkitEquipmentSlotHelper.fromBukkitEquipmentSlot(e.getHand()); } EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new BukkitPlayer(e.getPlayer()), BukkitHelper.fromBukkitItem(e.getItemInHand()), slot); ee.setCancelled(e.isCancelled()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java index 3ac7331..5574421 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java @@ -21,7 +21,6 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -31,6 +30,9 @@ import org.bukkit.potion.PotionType; import org.bukkit.util.Vector; +// DO NOT IMPORT THE FOLLOWING BECAUSE THEY BREAK OLDER VERSIONS BECAUSE THEY DON'T EXIST +// org.bukkit.inventory.EquipmentSlot + import java.util.Objects; @SuppressWarnings("unused") @@ -370,44 +372,6 @@ public static boolean isFolia() { } } - public static EquipmentSlot toBukkitEquipmentSlot(EsEquipmentSlot slot) { - switch (slot) { - case Feet: - return EquipmentSlot.FEET; - case Hand: - return EquipmentSlot.HAND; - case Head: - return EquipmentSlot.HEAD; - case Legs: - return EquipmentSlot.LEGS; - case Chest: - return EquipmentSlot.CHEST; - case OffHand: - return EquipmentSlot.OFF_HAND; - } - - throw new RuntimeException("idfk"); - } - - public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { - switch (slot) { - case FEET: - return EsEquipmentSlot.Feet; - case HAND: - return EsEquipmentSlot.Hand; - case HEAD: - return EsEquipmentSlot.Head; - case LEGS: - return EsEquipmentSlot.Legs; - case CHEST: - return EsEquipmentSlot.Chest; - case OFF_HAND: - return EsEquipmentSlot.OffHand; - default: - throw new IllegalArgumentException("Invalid EquipmentSlot"); - } - } - public static EsClickType fromBukkitClickType(ClickType click) { switch (click) { case CREATIVE: diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index 3cb84a0..0c4d8e3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -82,6 +82,11 @@ public boolean isEqualTo(EsInventory inv) { return ((BukkitInventory) inv).getBukkitInventory().equals(bukkitInv); } + @Override + public int getSize() { + return bukkitInv.getSize(); + } + @Override public Map all(String material) { Material mat = Material.valueOf(material); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java index df5c282..792ef44 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsInventoryView; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -25,6 +26,12 @@ public EsInventory getBottomInventory() { @Override public EsInventory getInventory(int slot) { + if (Main.minecraftVersion.getMinor() <= 7) { // .getInventory(slot) doesn't exist + if (slot >= getTopInventory().getSize()) { // Bottom inv + return getBottomInventory(); + } + return getTopInventory(); + } return new BukkitInventory(bukkitView.getInventory(slot)); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 3bb286e..6cadef8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -168,4 +168,19 @@ private void setMainHandBukkit(org.bukkit.entity.Player p, org.bukkit.inventory. p.getInventory().setItemInHand(is); } } + + @Override + public boolean hasPermission(String node) { + return bukkitPlayer.hasPermission(node); + } + + @Override + public void sendMessage(String... args) { + bukkitPlayer.sendMessage(args); + } + + @Override + public boolean isPermissionSet(String node) { + return bukkitPlayer.isPermissionSet(node); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index e70b8bd..74c6d7f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -2,6 +2,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; +import net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation.BukkitEquipmentSlotHelper; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; import org.bukkit.inventory.Inventory; @@ -89,6 +90,6 @@ public void setItem(EsEquipmentSlot slot, EsItemStack item) { ((Inventory) bukkitInv).setItem(slotId, ((BukkitItemStack) item).getBukkitItem()); return; } - bukkitInv.setItem(BukkitHelper.toBukkitEquipmentSlot(slot), ((BukkitItemStack) item).getBukkitItem()); + bukkitInv.setItem(BukkitEquipmentSlotHelper.toBukkitEquipmentSlot(slot), ((BukkitItemStack) item).getBukkitItem()); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index bb71f79..3606a6e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -1,12 +1,15 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Main; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; +import org.bukkit.entity.Entity; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; public class BukkitWorld implements EsWorld { private final org.bukkit.World bukkitWorld; @@ -30,11 +33,23 @@ public List getEntities() { return entities; } + private boolean isWithin(EsLocation loc, double xoff, double yoff, double zoff, Entity entity) { + return Math.abs(loc.getX() - entity.getLocation().getX()) <= xoff && + Math.abs(loc.getY() - entity.getLocation().getY()) <= yoff && + Math.abs(loc.getZ() - entity.getLocation().getZ()) <= zoff; + } + @Override public List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff) { - Collection bEntities = bukkitWorld.getNearbyEntities(BukkitHelper.toBukkitLocation(loc), xoff, yoff, zoff); - List entities = new ArrayList<>(); - for (org.bukkit.entity.Entity bEntity : bEntities) { + Collection bEntities; + if (Main.minecraftVersion.getMinor() > 7) { + bEntities = bukkitWorld.getNearbyEntities(BukkitHelper.toBukkitLocation(loc), xoff, yoff, zoff); + } else { + Collection allEntities = bukkitWorld.getEntities(); + bEntities = allEntities.stream().filter(e -> isWithin(loc, xoff, yoff, zoff, e)).collect(Collectors.toList()); + } + List entities = new ArrayList<>(bEntities.size()); + for (Entity bEntity : bEntities) { entities.add(BukkitHelper.fromBukkitEntity(bEntity)); } return entities; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java new file mode 100644 index 0000000..4ca2e29 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java @@ -0,0 +1,44 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation; + +import net.serble.estools.ServerApi.EsEquipmentSlot; + +public class BukkitEquipmentSlotHelper { + + public static org.bukkit.inventory.EquipmentSlot toBukkitEquipmentSlot(EsEquipmentSlot slot) { + switch (slot) { + case Feet: + return org.bukkit.inventory.EquipmentSlot.FEET; + case Hand: + return org.bukkit.inventory.EquipmentSlot.HAND; + case Head: + return org.bukkit.inventory.EquipmentSlot.HEAD; + case Legs: + return org.bukkit.inventory.EquipmentSlot.LEGS; + case Chest: + return org.bukkit.inventory.EquipmentSlot.CHEST; + case OffHand: + return org.bukkit.inventory.EquipmentSlot.OFF_HAND; + } + + throw new RuntimeException("idfk"); + } + + public static EsEquipmentSlot fromBukkitEquipmentSlot(org.bukkit.inventory.EquipmentSlot slot) { + switch (slot) { + case FEET: + return EsEquipmentSlot.Feet; + case HAND: + return EsEquipmentSlot.Hand; + case HEAD: + return EsEquipmentSlot.Head; + case LEGS: + return EsEquipmentSlot.Legs; + case CHEST: + return EsEquipmentSlot.Chest; + case OFF_HAND: + return EsEquipmentSlot.OffHand; + default: + throw new IllegalArgumentException("Invalid EquipmentSlot"); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 21ac9d5..41f2771 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -5,7 +5,6 @@ import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; -import net.serble.estools.ServerApi.Implementations.Bukkit.*; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -61,7 +60,7 @@ private void setDamageFromEvent(EntityDamageEvent e, @SuppressWarnings("SamePara @EventHandler public void onBlockPlace(BlockPlaceEvent e) { - EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), FoliaHelper.fromBukkitItem(e.getItemInHand()), BukkitHelper.fromBukkitEquipmentSlot(e.getHand())); + EsBlockPlaceEvent ee = new EsBlockPlaceEvent(new FoliaPlayer(e.getPlayer()), FoliaHelper.fromBukkitItem(e.getItemInHand()), FoliaHelper.fromBukkitEquipmentSlot(e.getHand())); ee.setCancelled(e.isCancelled()); Main.callEvent(ee); e.setCancelled(ee.isCancelled()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index cd5229f..af700c5 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -6,6 +6,7 @@ import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; @@ -20,6 +21,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -257,4 +259,42 @@ public static EsInventory fromBukkitInventory(Inventory inv) { return new FoliaInventory(inv); } + + public static EquipmentSlot toBukkitEquipmentSlot(EsEquipmentSlot slot) { + switch (slot) { + case Feet: + return EquipmentSlot.FEET; + case Hand: + return EquipmentSlot.HAND; + case Head: + return EquipmentSlot.HEAD; + case Legs: + return EquipmentSlot.LEGS; + case Chest: + return EquipmentSlot.CHEST; + case OffHand: + return EquipmentSlot.OFF_HAND; + } + + throw new RuntimeException("idfk"); + } + + public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { + switch (slot) { + case FEET: + return EsEquipmentSlot.Feet; + case HAND: + return EsEquipmentSlot.Hand; + case HEAD: + return EsEquipmentSlot.Head; + case LEGS: + return EsEquipmentSlot.Legs; + case CHEST: + return EsEquipmentSlot.Chest; + case OFF_HAND: + return EsEquipmentSlot.OffHand; + default: + throw new IllegalArgumentException("Invalid EquipmentSlot"); + } + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index b8e11a2..e043409 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -47,6 +47,11 @@ public boolean isEqualTo(EsInventory inv) { return ((FoliaInventory) inv).getBukkitInventory().equals(bukkitInv); } + @Override + public int getSize() { + return bukkitInv.getSize(); + } + @Override public Map all(String material) { Material mat = Material.valueOf(material); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java index 939ff5f..4102693 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java @@ -10,6 +10,7 @@ public interface EsInventory { EsItemStack[] getContents(); void clear(); boolean isEqualTo(EsInventory inv); + int getSize(); /** * Gets a list of slots and the items in those slots that are of a specific material. diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index 1812753..c3c1713 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -25,4 +25,7 @@ public interface EsPlayer extends EsLivingEntity { boolean isFlying(); void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch); void updateInventory(); + boolean hasPermission(String node); + boolean isPermissionSet(String node); + void sendMessage(String... args); } From dafc410eba571c6bd62f1f3a05126bcd74e57787 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Tue, 18 Jun 2024 21:56:41 +1000 Subject: [PATCH 34/74] Moved away for using Strings for Materials, Enchantments and Effects and now we use Dedicated types for it. Will move Sounds away from String later. --- .../java/net/serble/estools/Commands/Eff.java | 17 ++- .../net/serble/estools/Commands/Ench.java | 28 ++-- .../net/serble/estools/Commands/GetInfo.java | 17 ++- .../serble/estools/Commands/Give/Give.java | 23 +-- .../net/serble/estools/Commands/Potion.java | 32 +++-- .../estools/Commands/PowerPick/PowerAxe.java | 8 +- .../estools/Commands/PowerPick/PowerHoe.java | 8 +- .../estools/Commands/PowerPick/PowerPick.java | 8 +- .../Commands/PowerPick/PowerShovel.java | 10 +- .../Commands/PowerPick/PowerSword.java | 8 +- .../estools/Commands/PowerPick/PowerTool.java | 5 +- .../estools/Commands/SetUnbreakable.java | 11 +- src/main/java/net/serble/estools/Effects.java | 83 ----------- src/main/java/net/serble/estools/Main.java | 8 +- .../java/net/serble/estools/MetaHandler.java | 12 -- .../net/serble/estools/SemanticVersion.java | 16 +++ .../estools/ServerApi/EsEnchantment.java | 49 +++++++ .../serble/estools/ServerApi/EsMaterial.java | 49 +++++++ .../estools/ServerApi/EsPotionEffect.java | 8 +- .../estools/ServerApi/EsPotionEffectType.java | 49 +++++++ .../ServerApi/EsSerialisableItemStack.java | 12 +- .../Bukkit/BukkitConfigMigrator.java | 12 +- .../Bukkit/BukkitEnchantmentsHelper.java | 77 ---------- .../Implementations/Bukkit/BukkitEntity.java | 1 + .../Bukkit/BukkitEventsListener.java | 3 +- .../Bukkit/BukkitInventory.java | 7 +- .../Bukkit/BukkitItemStack.java | 35 +++-- .../Bukkit/BukkitLivingEntity.java | 20 +-- .../Bukkit/BukkitPersistentDataContainer.java | 2 +- .../Implementations/Bukkit/BukkitPlayer.java | 1 + .../Bukkit/BukkitPlayerInventory.java | 3 +- .../Implementations/Bukkit/BukkitPotion.java | 4 +- .../Implementations/Bukkit/BukkitServer.java | 129 +++++++---------- .../Bukkit/BukkitTabCompleteGenerator.java | 1 + .../Implementations/Bukkit/BukkitWorld.java | 1 + .../Bukkit/Helper/BukkitEffectHelper.java | 105 ++++++++++++++ .../Helper/BukkitEnchantmentHelper.java | 77 ++++++++++ .../BukkitEquipmentSlotHelper.java | 2 +- .../Bukkit/{ => Helper}/BukkitHelper.java | 58 ++------ ...elper.java => FoliaEnchantmentHelper.java} | 4 +- .../Implementations/Folia/FoliaHelper.java | 21 +-- .../Implementations/Folia/FoliaInventory.java | 6 +- .../Implementations/Folia/FoliaItemStack.java | 36 ++--- .../Folia/FoliaLivingEntity.java | 20 +-- .../Implementations/Folia/FoliaPotion.java | 8 +- .../Implementations/Folia/FoliaServer.java | 135 +++++++----------- .../ServerApi/Interfaces/EsInventory.java | 4 +- .../ServerApi/Interfaces/EsItemStack.java | 13 +- .../ServerApi/Interfaces/EsLivingEntity.java | 9 +- .../{EsServerSoftware.java => EsServer.java} | 19 +-- .../estools/ServerApi/ServerPlatform.java | 4 +- src/main/java/net/serble/estools/Tester.java | 3 +- src/main/java/net/serble/estools/Utils.java | 20 +++ 53 files changed, 736 insertions(+), 565 deletions(-) delete mode 100644 src/main/java/net/serble/estools/Effects.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsEnchantment.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsMaterial.java create mode 100644 src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java delete mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{Encapsulation => Helper}/BukkitEquipmentSlotHelper.java (95%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{ => Helper}/BukkitHelper.java (90%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Folia/{FoliaEnchantmentsHelper.java => FoliaEnchantmentHelper.java} (62%) rename src/main/java/net/serble/estools/ServerApi/Interfaces/{EsServerSoftware.java => EsServer.java} (76%) create mode 100644 src/main/java/net/serble/estools/Utils.java diff --git a/src/main/java/net/serble/estools/Commands/Eff.java b/src/main/java/net/serble/estools/Commands/Eff.java index a7ed8ac..c0fc81d 100644 --- a/src/main/java/net/serble/estools/Commands/Eff.java +++ b/src/main/java/net/serble/estools/Commands/Eff.java @@ -1,6 +1,7 @@ package net.serble.estools.Commands; -import net.serble.estools.Effects; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.EsPotionEffectType; import net.serble.estools.Main; import net.serble.estools.MultiPlayerCommand; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -8,7 +9,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; public class Eff extends MultiPlayerCommand { private static final String usage = genUsage("/eff [amplifier] [duration] [players]"); @@ -20,9 +20,8 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - String effect = Effects.getByName(args[0]); - - if (effect == null) { + EsPotionEffectType effectType = EsPotionEffectType.fromKey(args[0]); + if (effectType == null) { send(sender, "&cEffect not found!"); return false; } @@ -76,11 +75,11 @@ public boolean execute(EsCommandSender sender, String[] args) { } for (EsPlayer p : players) { - p.removePotionEffect(effect); - p.addPotionEffect(effect, duration, amplifier); + p.removePotionEffect(effectType); + p.addPotionEffect(new EsPotionEffect(effectType, amplifier, duration)); } - send(sender, "&aAdded effect &6%s&a at level &6%s&a for &6%s", Effects.getName(effect), amplifier + 1, durationStr); + send(sender, "&aAdded effect &6%s&a at level &6%s&a for &6%s", effectType.getKey(), amplifier + 1, durationStr); return true; } @@ -108,7 +107,7 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA switch (args.length) { case 1: - for (Map.Entry e : Effects.entrySet()) { + for (EsPotionEffectType e : Main.server.getPotionEffectTypes()) { tab.add(e.getKey().toLowerCase()); } break; diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index 2272c42..0544d22 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -1,11 +1,11 @@ package net.serble.estools.Commands; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Objects; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -21,8 +21,12 @@ public boolean execute(EsCommandSender sender, String[] args) { send(sender, usage); return false; } - - EsItemStack is; + + EsEnchantment enchantment = EsEnchantment.fromKey(args[0]); + if (enchantment == null) { + send(sender, "&cEnchantment does not exist!"); + return false; + } int level = 1; if (args.length > 1) { @@ -33,7 +37,8 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } } - + + EsItemStack is; if (args.length <= 2) { if (isNotPlayer(sender, usage)) { return false; @@ -59,14 +64,9 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } } - - if (Main.server.doesEnchantmentExist(args[0].toLowerCase())) { - is.addEnchantment(args[0].toLowerCase(), level); - send(sender, "&aEnchantment &6%s&a at level &6%s&a was added!", args[0].toLowerCase(), level); - } - else { - send(sender, usage); - } + + is.addEnchantment(enchantment, level); + send(sender, "&aEnchantment &6%s&a at level &6%s&a was added!", args[0].toLowerCase(), level); return true; } @@ -76,7 +76,9 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA switch (args.length) { case 1: - tab.addAll(Arrays.asList(Main.server.getEnchantments())); + for (EsEnchantment enchantment : Main.server.getEnchantments()) { + tab.add(enchantment.getKey()); + } break; case 3: diff --git a/src/main/java/net/serble/estools/Commands/GetInfo.java b/src/main/java/net/serble/estools/Commands/GetInfo.java index f744eb6..8ac1a78 100644 --- a/src/main/java/net/serble/estools/Commands/GetInfo.java +++ b/src/main/java/net/serble/estools/Commands/GetInfo.java @@ -1,6 +1,6 @@ package net.serble.estools.Commands; -import net.serble.estools.Effects; +import net.serble.estools.ServerApi.EsPotionEffect; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -10,7 +10,9 @@ import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class GetInfo extends EntityCommand { private static final String usage = genUsage("/getinfo "); @@ -72,17 +74,14 @@ public boolean execute(EsCommandSender sender, String[] args) { StringBuilder potionEffects = new StringBuilder(); { - String[] pos = le.getActivePotionEffects().toArray(new String[0]); + List potions = le.getActivePotionEffects(); - if (pos.length == 0) { + if (potions.isEmpty()) { potionEffects.append("None"); } else { - for (int i = 0; i < pos.length - 1; i++) { - String po = pos[i]; - potionEffects.append(Effects.getName(po)).append(", "); - } - - potionEffects.append(Effects.getName(pos[pos.length - 1])); + potionEffects.append(potions.stream() + .map(pot -> String.format("%s at %s for %s seconds", pot.getType(), pot.getAmp(), pot.getDuration()/20)) + .collect(Collectors.joining(", "))); } } diff --git a/src/main/java/net/serble/estools/Commands/Give/Give.java b/src/main/java/net/serble/estools/Commands/Give/Give.java index ca30ab7..f87d807 100644 --- a/src/main/java/net/serble/estools/Commands/Give/Give.java +++ b/src/main/java/net/serble/estools/Commands/Give/Give.java @@ -3,6 +3,7 @@ import net.serble.estools.*; import net.serble.estools.Config.ConfigManager; import net.serble.estools.Config.Schemas.Give.GiveConfig; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,11 +11,11 @@ import java.util.Map.Entry; public class Give implements EsToolsTabCompleter { - private static HashMap materialNames; + private static HashMap materialNames; public static EsItemStack getItem(String name, int amount) { - name = name.toUpperCase(); - String mat = materialNames.get(name); + name = name.toLowerCase(); + EsMaterial mat = materialNames.get(name); if (mat == null) { return null; @@ -43,7 +44,7 @@ public List onTabComplete(EsCommandSender sender, String[] args) { public static void enable() { // initialise hashmaps //noinspection Convert2Diamond - materialNames = new HashMap(); + materialNames = new HashMap(); // Load config GiveConfig config = ConfigManager.load("give.yml", GiveConfig.class); @@ -52,12 +53,13 @@ public static void enable() { Map materials = config.getItems(); // Load normal items - String[] builtinMats = Main.server.getMaterials(true); - for (String mat : builtinMats) { + Set builtinMats = Main.server.getMaterials(true); + for (EsMaterial mat : builtinMats) { if (mat == null) { continue; } - String name = mat.toUpperCase(); + + String name = mat.getKey().toLowerCase(); if (name.contains("_") && config.getSettings().isAddWithoutUnderscores()) { materialNames.put(name.replace("_",""), mat); @@ -74,10 +76,13 @@ public static void enable() { if (s == null) { continue; } - if (Arrays.stream(builtinMats).noneMatch(b -> b != null && b.equalsIgnoreCase(s.getValue()))) { + + EsMaterial material = EsMaterial.fromKey(s.getValue().toLowerCase()); + if (material == null) { continue; // Don't trust users, if it doesn't exist then skip } - materialNames.put(s.getKey().toUpperCase(), s.getValue().toUpperCase()); + + materialNames.put(s.getKey().toLowerCase(), material); } } diff --git a/src/main/java/net/serble/estools/Commands/Potion.java b/src/main/java/net/serble/estools/Commands/Potion.java index 8173550..dd69c17 100644 --- a/src/main/java/net/serble/estools/Commands/Potion.java +++ b/src/main/java/net/serble/estools/Commands/Potion.java @@ -1,9 +1,9 @@ package net.serble.estools.Commands; -import net.serble.estools.Effects; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.EsPotionEffectType; import net.serble.estools.EntityCommand; import net.serble.estools.Main; -import net.serble.estools.MetaHandler; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; public class Potion extends EntityCommand { private static final String usage = genUsage("/potion [amplifier] [duration] [amount] [drink/splash/lingering] [player]"); @@ -23,19 +22,26 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } + EsPotionEffectType type = EsPotionEffectType.fromKey(args[0]); + if (type == null) { + send(sender, "Potion type does not exist!"); + return false; + } + int amount = 1; if (args.length >= 4) { amount = tryParseInt(args[3], 1); } - int amp = 1; + int amp = 0; if (args.length >= 2) { - amp = tryParseInt(args[1], 1); + // amp 0 is level 1, so subtract 1 from player input + amp = tryParseInt(args[1], 1) - 1; } int duration = 60*20*5; if (args.length >= 3) { - duration = tryParseInt(args[2], 60*5)*20; + duration = (int) (tryParseDouble(args[2], 60d*5d)*20d); } EsPotType potType = EsPotType.drink; @@ -43,7 +49,12 @@ public boolean execute(EsCommandSender sender, String[] args) { try { potType = EsPotType.valueOf(args[4].toLowerCase()); } catch (IllegalArgumentException ignored) { - send(sender, "&cInvalid potion type, must be drink, splash, or lingering (if on 1.9+)"); + if (Main.minecraftVersion.getMinor() >= 9) { + send(sender, "&cInvalid potion type, must be drink, splash, or lingering"); + } else { + send(sender, "&cInvalid potion type, must be drink or splash"); + } + return false; } @@ -75,8 +86,9 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - EsItemStack pot = MetaHandler.getPotion(sender, potType, args[0], duration, amp, amount); + EsItemStack pot = Main.server.createPotion(potType, new EsPotionEffect(type, amp, duration), amount); if (pot == null) { + send(sender, "&cInvalid potion effect!"); return false; } @@ -98,12 +110,12 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA switch (args.length) { case 1: if (Main.minecraftVersion.getMinor() <= 8) { - for (Map.Entry e : Effects.getPotionList()) { + for (EsPotionEffectType e : Main.server.getOldPotionTypes()) { tab.add(e.getKey().toLowerCase()); } } else { - for (Map.Entry e : Effects.entrySet()) { + for (EsPotionEffectType e : Main.server.getPotionEffectTypes()) { tab.add(e.getKey().toLowerCase()); } } diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java index 67383b4..36bdda2 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerAxe.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,13 +12,13 @@ public class PowerAxe extends EsToolsCommand { public static void init() { if (Main.minecraftVersion.getMinor() > 15) { - powerItem = Main.server.createItemStack("NETHERITE_AXE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("NETHERITE_AXE"), 1); } else { - powerItem = Main.server.createItemStack("DIAMOND_AXE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("DIAMOND_AXE"), 1); } - PowerTool.setupItem(powerItem, "efficiency"); + PowerTool.setupItem(powerItem, EsEnchantment.createUnchecked("efficiency")); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java index 84bb752..1038a13 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerHoe.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,13 +12,13 @@ public class PowerHoe extends EsToolsCommand { public static void init() { if (Main.minecraftVersion.getMinor() > 15) { - powerItem = Main.server.createItemStack("NETHERITE_HOE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("NETHERITE_HOE"), 1); } else { - powerItem = Main.server.createItemStack("DIAMOND_HOE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("DIAMOND_HOE"), 1); } - PowerTool.setupItem(powerItem, "efficiency"); + PowerTool.setupItem(powerItem, EsEnchantment.createUnchecked("efficiency")); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java index 4deef7c..dd85779 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerPick.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,13 +12,13 @@ public class PowerPick extends EsToolsCommand { public static void init() { if (Main.minecraftVersion.getMinor() > 15) { - powerItem = Main.server.createItemStack("NETHERITE_PICKAXE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("NETHERITE_PICKAXE"), 1); } else { - powerItem = Main.server.createItemStack("DIAMOND_PICKAXE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("DIAMOND_PICKAXE"), 1); } - PowerTool.setupItem(powerItem, "efficiency"); + PowerTool.setupItem(powerItem, EsEnchantment.createUnchecked("efficiency")); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java index ea8913b..ef908be 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerShovel.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,14 +12,14 @@ public class PowerShovel extends EsToolsCommand { public static void init() { if (Main.minecraftVersion.getMinor() > 15) { - powerItem = Main.server.createItemStack("NETHERITE_SHOVEL", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("NETHERITE_SHOVEL"), 1); } else if (Main.minecraftVersion.getMinor() > 12) { - powerItem = Main.server.createItemStack("DIAMOND_SHOVEL", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("DIAMOND_SHOVEL"), 1); } else { - powerItem = Main.server.createItemStack("DIAMOND_SPADE", 1); + powerItem = Main.server.createItemStack(EsMaterial.fromKey("DIAMOND_SPADE"), 1); } - PowerTool.setupItem(powerItem, "efficiency"); + PowerTool.setupItem(powerItem, EsEnchantment.createUnchecked("efficiency")); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java index bf5ff82..b0c5982 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerSword.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -10,12 +12,12 @@ public class PowerSword extends EsToolsCommand { public static void init() { if (Main.minecraftVersion.getMinor() > 12) { - powerPick = Main.server.createItemStack("SALMON", 1); + powerPick = Main.server.createItemStack(EsMaterial.fromKey("SALMON"), 1); } else { - powerPick = Main.server.createItemStack("RAW_FISH", 1); + powerPick = Main.server.createItemStack(EsMaterial.fromKey("RAW_FISH"), 1); } - PowerTool.setupItem(powerPick, "sharpness"); + PowerTool.setupItem(powerPick, EsEnchantment.createUnchecked("sharpness")); } @Override diff --git a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java index b722a6e..b49f7d2 100644 --- a/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java +++ b/src/main/java/net/serble/estools/Commands/PowerPick/PowerTool.java @@ -2,6 +2,7 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -16,7 +17,7 @@ public static void init() { PowerSword.init(); } - public static void setupItem(EsItemStack item, String enchantment) { + public static void setupItem(EsItemStack item, EsEnchantment enchantment) { item.addEnchantment(enchantment, 32767); if (Main.minecraftVersion.getMinor() > 10) { @@ -24,7 +25,7 @@ public static void setupItem(EsItemStack item, String enchantment) { im.setUnbreakable(true); item.setItemMeta(im); } else { - item.addEnchantment("unbreaking", 32767); + item.addEnchantment(EsEnchantment.createUnchecked("unbreaking"), 32767); } } diff --git a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java index 1baf258..9f49c2d 100644 --- a/src/main/java/net/serble/estools/Commands/SetUnbreakable.java +++ b/src/main/java/net/serble/estools/Commands/SetUnbreakable.java @@ -2,6 +2,8 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -18,7 +20,7 @@ public boolean execute(EsCommandSender sender, String[] args) { } EsItemStack item = ((EsPlayer) sender).getMainHand(); - if (item == null || Objects.equals(item.getType(), "AIR") || (Main.minecraftVersion.getMinor() > 10 && item.getItemMeta() == null)) { + if (item == null || Objects.equals(item.getType(), EsMaterial.createUnchecked("AIR")) || (Main.minecraftVersion.getMinor() > 10 && item.getItemMeta() == null)) { send(sender, "&cMust be a damageable item"); return false; } @@ -34,10 +36,11 @@ public boolean execute(EsCommandSender sender, String[] args) { message = "&aSet item to &6Unbreakable!"; } } else { - if (item.getEnchantments().keySet().stream().anyMatch(c -> c != null && c.equalsIgnoreCase("unbreaking"))) { - item.removeEnchantment("unbreaking"); + if (item.getEnchantments().keySet().stream() + .anyMatch(c -> c != null && c.getKey().equalsIgnoreCase("unbreaking"))) { + item.removeEnchantment(EsEnchantment.createUnchecked("unbreaking")); } else { - item.addEnchantment("unbreaking", 32767); // ID is unbreaking because it is translated + item.addEnchantment(EsEnchantment.createUnchecked("unbreaking"), 32767); message = "&aSet item to &6Unbreakable!"; } } diff --git a/src/main/java/net/serble/estools/Effects.java b/src/main/java/net/serble/estools/Effects.java deleted file mode 100644 index 2f84efd..0000000 --- a/src/main/java/net/serble/estools/Effects.java +++ /dev/null @@ -1,83 +0,0 @@ -package net.serble.estools; - -import java.util.*; - -public class Effects { - private static final Map EFFECTS = new HashMap<>(); - private static final Map POTIONS = new HashMap<>(); - - public static String getByName(String name) { - return EFFECTS.get(name); - } - - public static String getPotionByName(String name) { - return POTIONS.get(name.toLowerCase(Locale.ENGLISH)); - } - - public static String getName(String effect) { - for (Map.Entry a : EFFECTS.entrySet()) { - if (a.getValue().equals(effect)) { - return a.getKey(); - } - } - - return "invalid"; - } - - public static Set> entrySet() { - return EFFECTS.entrySet(); - } - - public static Set> getPotionList() { - return POTIONS.entrySet(); - } - - public static void load() { - if (Main.minecraftVersion.getMinor() <= 8) { // 1.8 only potions be funky - POTIONS.put("regeneration", "REGEN"); - POTIONS.put("speed", "SPEED"); - POTIONS.put("fire_resistance", "FIRE_RESISTANCE"); - POTIONS.put("poison", "POISON"); - POTIONS.put("instant_health", "INSTANT_HEAL"); - POTIONS.put("weakness", "WEAKNESS"); - POTIONS.put("strength", "STRENGTH"); - POTIONS.put("slowness", "SLOWNESS"); - POTIONS.put("instant_damage", "INSTANT_DAMAGE"); - - if (Main.minecraftVersion.getMinor() >= 4 && Main.minecraftVersion.getMinor() >= 2) { // Night vision was added in 1.4.2 - POTIONS.put("night_vision", "NIGHT_VISION"); - POTIONS.put("invisibility", "INVISIBILITY"); - } - - if (Main.minecraftVersion.getMinor() >= 7 && Main.minecraftVersion.getMinor() >= 2) { // 1.7.2 - POTIONS.put("water_breathing", "WATER"); - } - - if (Main.minecraftVersion.getMinor() >= 8) { // 1.8 - POTIONS.put("jump_boost", "JUMP"); - } - } - - HashMap nameReplacers = new HashMap() {{ - put("confusion", "nausea"); - put("damage_resistance", "resistance"); - put("fast_digging", "haste"); - put("harm", "instant_damage"); - put("heal", "instant_health"); - put("increase_damage", "strength"); - put("slow", "slowness"); - put("slow_digging", "mining_fatigue"); - put("jump", "jump_boost"); - }}; - - for (String s : Main.server.getPotionEffectTypes()) { - String name = s.toLowerCase(); - - if (nameReplacers.containsKey(name)) { - name = nameReplacers.get(name); - } - - EFFECTS.put(name, s.toLowerCase()); - } - } -} diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index bb9ce45..d198f01 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -14,10 +14,11 @@ import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitConfigMigrator; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; -import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; +import net.serble.estools.ServerApi.Interfaces.EsServer; import net.serble.estools.ServerApi.ServerPlatform; import net.serble.estools.Signs.SignMain; import org.bstats.bukkit.Metrics; @@ -39,7 +40,7 @@ public class Main { private static EsToolsConfig config; // Get with overriden getConfig() method public static SemanticVersion newVersion = null; // The version available to download public static boolean newVersionReady = false; - public static EsServerSoftware server; + public static EsServer server; public static EsLogger logger; private final Object context; private static final List eventListeners = new ArrayList<>(); @@ -58,9 +59,10 @@ public void enable() { logger = server.getLogger(); minecraftVersion = server.getVersion(); + server.initialise(); logger.info("Starting EsTools on platform: " + platform.name() + " (MC: " + minecraftVersion.getString() + ")"); - Effects.load(); + BukkitEffectHelper.load(); if (platform == ServerPlatform.Bukkit) { // We have to support old configs diff --git a/src/main/java/net/serble/estools/MetaHandler.java b/src/main/java/net/serble/estools/MetaHandler.java index de4dd44..8bff903 100644 --- a/src/main/java/net/serble/estools/MetaHandler.java +++ b/src/main/java/net/serble/estools/MetaHandler.java @@ -1,7 +1,5 @@ package net.serble.estools; -import net.serble.estools.ServerApi.EsPotType; -import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -12,16 +10,6 @@ // This class exists because Minecraft < 1.4 doesn't have ItemMeta. It just encapsulates all ItemMeta methods. public class MetaHandler { - public static EsItemStack getPotion(EsCommandSender sender, EsPotType potType, String effect, int duration, int amp, int amount) { - EsItemStack pot = Main.server.createPotion(potType, effect, duration, amp, amount); - if (pot == null) { - EsToolsCommand.send(sender, "&cInvalid potion effect"); - return null; - } - - return pot; - } - public static void renameItem(EsItemStack item, String name) { EsItemMeta meta = Objects.requireNonNull(item.getItemMeta()); meta.setDisplayName(name); diff --git a/src/main/java/net/serble/estools/SemanticVersion.java b/src/main/java/net/serble/estools/SemanticVersion.java index be01462..e2bd488 100644 --- a/src/main/java/net/serble/estools/SemanticVersion.java +++ b/src/main/java/net/serble/estools/SemanticVersion.java @@ -52,4 +52,20 @@ public boolean isLowerThan(SemanticVersion other) { return getPatch() < other.getPatch(); } + + public boolean isAtLeast(SemanticVersion other) { + if (getMajor() < other.getMajor()) { + return false; + } else if (getMajor() > other.getMajor()) { + return true; + } + + if (getMinor() < other.getMinor()) { + return false; + } else if (getMinor() > other.getMinor()) { + return true; + } + + return getPatch() >= other.getPatch(); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java new file mode 100644 index 0000000..43c2da5 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.Main; +import net.serble.estools.Utils; +import org.jetbrains.annotations.Nullable; + +public class EsEnchantment { + private String key; + + public static EsEnchantment createUnchecked(String key) { + EsEnchantment type = new EsEnchantment(); + type.key = key; + + return type; + } + + public static @Nullable EsEnchantment fromKey(String key) { + EsEnchantment type = EsEnchantment.createUnchecked(key.toLowerCase()); + + if (Main.server.getEnchantments().contains(type)) { + return type; + } + + return null; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EsEnchantment) { + return ((EsEnchantment) obj).getKey().equals(key); + } + + return false; + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return Utils.keyToDisplayName(key); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsMaterial.java b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java new file mode 100644 index 0000000..26d97fc --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.Main; +import net.serble.estools.Utils; +import org.jetbrains.annotations.Nullable; + +public class EsMaterial { + private String key; + + public static EsMaterial createUnchecked(String key) { + EsMaterial type = new EsMaterial(); + type.key = key; + + return type; + } + + public static @Nullable EsMaterial fromKey(String key) { + EsMaterial type = EsMaterial.createUnchecked(key.toLowerCase()); + + if (Main.server.getMaterials(false).contains(type)) { + return type; + } + + return null; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EsMaterial) { + return ((EsMaterial) obj).getKey().equals(key); + } + + return false; + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return Utils.keyToDisplayName(key); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java b/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java index 24e7d73..7a079a2 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java +++ b/src/main/java/net/serble/estools/ServerApi/EsPotionEffect.java @@ -2,11 +2,11 @@ @SuppressWarnings("unused") // Needs the methods for serialiser public class EsPotionEffect { - private String type; + private EsPotionEffectType type; private int amp; private int duration; - public EsPotionEffect(String type, int amp, int duration) { + public EsPotionEffect(EsPotionEffectType type, int amp, int duration) { this.type = type; this.amp = amp; this.duration = duration; @@ -24,7 +24,7 @@ public int getDuration() { return duration; } - public String getType() { + public EsPotionEffectType getType() { return type; } @@ -36,7 +36,7 @@ public void setDuration(int duration) { this.duration = duration; } - public void setType(String type) { + public void setType(EsPotionEffectType type) { this.type = type; } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java new file mode 100644 index 0000000..8fa3bd1 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.Main; +import net.serble.estools.Utils; +import org.jetbrains.annotations.Nullable; + +public class EsPotionEffectType { + private String key; + + public static EsPotionEffectType createUnchecked(String key) { + EsPotionEffectType type = new EsPotionEffectType(); + type.key = key; + + return type; + } + + public static @Nullable EsPotionEffectType fromKey(String key) { + EsPotionEffectType type = EsPotionEffectType.createUnchecked(key); + + if (Main.server.getPotionEffectTypes().contains(type)) { + return type; + } + + return null; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EsPotionEffectType) { + return ((EsPotionEffectType) obj).getKey().equals(key); + } + + return false; + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return Utils.keyToDisplayName(key); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index b656b0b..040eee2 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -10,10 +10,10 @@ // TODO: NBT @SuppressWarnings("unused") // Needs the methods for YAML serialiser public class EsSerialisableItemStack { - private String material; + private EsMaterial material; private int amount; private String itemMeta; - private Map enchantments; + private Map enchantments; private boolean isPotion; private EsPotType potType; @@ -52,7 +52,7 @@ public int getAmount() { return amount; } - public String getMaterial() { + public EsMaterial getMaterial() { return material; } @@ -60,15 +60,15 @@ public void setAmount(int amount) { this.amount = amount; } - public void setMaterial(String material) { + public void setMaterial(EsMaterial material) { this.material = material; } - public Map getEnchantments() { + public Map getEnchantments() { return enchantments; } - public void setEnchantments(Map enchantments) { + public void setEnchantments(Map enchantments) { this.enchantments = enchantments; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java index 99aefe4..8e2ffc0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -8,6 +8,7 @@ import net.serble.estools.Config.Schemas.Give.GiveSettings; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsSerialisableItemStack; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; @@ -285,15 +286,16 @@ private static void migrateCChestsConfig() { // config.yml } @SuppressWarnings("unchecked") - ItemStack[] content = ((ArrayList) Objects.requireNonNull(config.get("items"))).toArray(new ItemStack[0]); + ArrayList content = (ArrayList) Objects.requireNonNull(config.get("items")); - EsSerialisableItemStack[] newConfig = new EsSerialisableItemStack[content.length]; - for (int i = 0; i < content.length; i++) { - if (content[i] == null) { + EsSerialisableItemStack[] newConfig = new EsSerialisableItemStack[content.size()]; + for (int i = 0; i < content.size(); i++) { + if (content.get(i) == null) { newConfig[i] = null; continue; } - newConfig[i] = EsSerialisableItemStack.generate(Main.server.createItemStack(content[i])); + + newConfig[i] = EsSerialisableItemStack.generate(new BukkitItemStack(content.get(i))); } // Move file to file.old diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java deleted file mode 100644 index b0a34ca..0000000 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEnchantmentsHelper.java +++ /dev/null @@ -1,77 +0,0 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; - -import net.serble.estools.Main; - -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -// This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. -// only enchantments before 1.13 need to be here because this class isn't used past 1.13 -public class BukkitEnchantmentsHelper { - private static final Map ENCHANTMENTS = new HashMap<>(); - - /** Converts the natural vanilla names to the Bukkit enum values */ - public static String getByName(String name) { - return ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH)); - } - - public static String getNameFromValue(String val) { - for (Map.Entry entry : ENCHANTMENTS.entrySet()) { - if (val.equalsIgnoreCase(entry.getValue())) { - return entry.getKey(); - } - } - - return null; - } - - public static Set> entrySet() { - return ENCHANTMENTS.entrySet(); - } - - static { AddEnchants(); } - - private static void AddEnchants() { - ENCHANTMENTS.put("sharpness", "DAMAGE_ALL"); - ENCHANTMENTS.put("bane_of_arthropods", "DAMAGE_ARTHROPODS"); - ENCHANTMENTS.put("smite", "DAMAGE_UNDEAD"); - ENCHANTMENTS.put("efficiency", "DIG_SPEED"); - ENCHANTMENTS.put("unbreaking", "DURABILITY"); - ENCHANTMENTS.put("fire_aspect", "FIRE_ASPECT"); - ENCHANTMENTS.put("knockback", "KNOCKBACK"); - ENCHANTMENTS.put("looting", "LOOT_BONUS_MOBS"); - ENCHANTMENTS.put("respiration", "OXYGEN"); - ENCHANTMENTS.put("protection", "PROTECTION_ENVIRONMENTAL"); - ENCHANTMENTS.put("blast_protection", "PROTECTION_EXPLOSIONS"); - ENCHANTMENTS.put("feather_falling", "PROTECTION_FALL"); - ENCHANTMENTS.put("projectile_protection", "PROTECTION_PROJECTILE"); - ENCHANTMENTS.put("silk_touch", "SILK_TOUCH"); - ENCHANTMENTS.put("aqua_affinity", "WATER_WORKER"); - ENCHANTMENTS.put("flame", "ARROW_FIRE"); - ENCHANTMENTS.put("power", "ARROW_DAMAGE"); - ENCHANTMENTS.put("punch", "ARROW_KNOCKBACK"); - ENCHANTMENTS.put("infinity", "ARROW_INFINITE"); - ENCHANTMENTS.put("fortune", "LOOT_BONUS_BLOCKS"); - - if (Main.minecraftVersion.getMinor() <= 3) return; - ENCHANTMENTS.put("thorns", "THORNS"); - - if (Main.minecraftVersion.getMinor() <= 6) return; - ENCHANTMENTS.put("luck", "LUCK"); - ENCHANTMENTS.put("lure", "LURE"); - - if (Main.minecraftVersion.getMinor() <= 7) return; - ENCHANTMENTS.put("depth_strider", "DEPTH_STRIDER"); - - if (Main.minecraftVersion.getMinor() <= 8) return; - ENCHANTMENTS.put("frost_walker", "FROST_WALKER"); - ENCHANTMENTS.put("mending", "MENDING"); - - if (Main.minecraftVersion.getMinor() <= 10) return; - ENCHANTMENTS.put("binding_curse", "BINDING_CURSE"); - ENCHANTMENTS.put("vanishing_curse", "VANISHING_CURSE"); - ENCHANTMENTS.put("sweeping_edge", "SWEEPING_EDGE"); - } -} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 2c67473..03dcf3b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -3,6 +3,7 @@ import io.papermc.lib.PaperLib; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsEntity; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 38ac5e8..0a91420 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -6,7 +6,8 @@ import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; -import net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index 0c4d8e3..ccbcfff 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.ServerApi.EsMaterial; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; @@ -88,10 +90,11 @@ public int getSize() { } @Override - public Map all(String material) { - Material mat = Material.valueOf(material); + public Map all(EsMaterial material) { + Material mat = BukkitHelper.toBukkitMaterial(material); Map out = new HashMap<>(); + assert bukkitInv != null; for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { out.put(entry.getKey(), BukkitHelper.fromBukkitItem(entry.getValue())); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index 94962b6..de2d7bf 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -1,9 +1,12 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import org.bukkit.Material; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; @@ -13,12 +16,13 @@ import java.util.HashMap; import java.util.Map; +import java.util.Set; public class BukkitItemStack implements EsItemStack { private final ItemStack bukkitItem; - public BukkitItemStack(String mat, int amount) { - bukkitItem = new ItemStack(Material.valueOf(mat), amount); + public BukkitItemStack(EsMaterial mat, int amount) { + bukkitItem = new ItemStack(BukkitHelper.toBukkitMaterial(mat), amount); } public BukkitItemStack(ItemStack child) { @@ -30,13 +34,13 @@ public ItemStack getBukkitItem() { } @Override - public String getType() { - return bukkitItem.getType().name(); + public EsMaterial getType() { + return BukkitHelper.fromBukkitMaterial(bukkitItem.getType()); } @Override - public void setType(String val) { - bukkitItem.setType(Material.valueOf(val)); + public void setType(EsMaterial val) { + bukkitItem.setType(BukkitHelper.toBukkitMaterial(val)); } @Override @@ -50,13 +54,13 @@ public void setAmount(int val) { } @Override - public void addEnchantment(String enchantment, int level) { - bukkitItem.addUnsafeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment), level); + public void addEnchantment(EsEnchantment enchantment, int level) { + bukkitItem.addUnsafeEnchantment(BukkitEnchantmentHelper.toBukkitEnchantment(enchantment), level); } @Override - public void removeEnchantment(String enchantment) { - bukkitItem.removeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment)); + public void removeEnchantment(EsEnchantment enchantment) { + bukkitItem.removeEnchantment(BukkitEnchantmentHelper.toBukkitEnchantment(enchantment)); } @Override @@ -143,10 +147,11 @@ public Object getInternalObject() { } @Override - public Map getEnchantments() { - Map enchs = new HashMap<>(); - for (Map.Entry ench : bukkitItem.getEnchantments().entrySet()) { - enchs.put(BukkitHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); + public Map getEnchantments() { + Set> bEnchs = bukkitItem.getEnchantments().entrySet(); + Map enchs = new HashMap<>(bEnchs.size()); + for (Map.Entry ench : bEnchs) { + enchs.put(BukkitEnchantmentHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); } return enchs; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index f5be26d..9b7196a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -1,6 +1,10 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.EsPotionEffectType; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; @@ -97,23 +101,23 @@ public void setHealth(double val) { } @Override - public void addPotionEffect(String effect, int duration, int amplifier) { - bukkitEntity.addPotionEffect(new PotionEffect(BukkitHelper.toBukkitPotionEffectType(effect), duration, amplifier)); + public void addPotionEffect(EsPotionEffect effect) { + bukkitEntity.addPotionEffect(BukkitHelper.toBukkitPotionEffect(effect)); } @Override - public void removePotionEffect(String effect) { - bukkitEntity.removePotionEffect(BukkitHelper.toBukkitPotionEffectType(effect)); + public void removePotionEffect(EsPotionEffectType effect) { + bukkitEntity.removePotionEffect(BukkitEffectHelper.toBukkitEffectType(effect)); } - @SuppressWarnings("deprecation") // Gotta love backwards compatibility @Override - public List getActivePotionEffects() { + public List getActivePotionEffects() { Collection bukkitEffects = bukkitEntity.getActivePotionEffects(); - List out = new ArrayList<>(); + List out = new ArrayList<>(bukkitEffects.size()); for (PotionEffect eff : bukkitEffects) { - out.add(eff.getType().getName()); + out.add(BukkitHelper.fromBukkitPotionEffect(eff)); } + return out; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java index 7ea6ddb..ef2790e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java @@ -7,7 +7,7 @@ import java.util.Objects; -import static net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper.*; +import static net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper.*; @SuppressWarnings("unchecked") // Trust me bro public class BukkitPersistentDataContainer implements EsPersistentDataContainer { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 6cadef8..95e370d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -4,6 +4,7 @@ import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.EsSoundCategory; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.Sound; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index 74c6d7f..bd857ce 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -2,7 +2,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; -import net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; import org.bukkit.inventory.Inventory; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java index b9ddce5..9e93158 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java @@ -3,6 +3,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; // TODO: pre 1.9 errors because no PotionMeta and pre 1.4 errors @@ -60,7 +62,7 @@ public EsPotionEffect[] getEffects() { EsPotionEffect[] out = new EsPotionEffect[in.length]; for (int i = 0; i < out.length; i++) { PotionEffect effect = in[i]; - out[i] = new EsPotionEffect(effect.getType().getName(), effect.getAmplifier(), effect.getDuration()); + out[i] = new EsPotionEffect(BukkitEffectHelper.fromBukkitEffectType(effect.getType()), effect.getAmplifier(), effect.getDuration()); } return out; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 5db7d7c..2125a0a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -2,11 +2,13 @@ import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; @@ -14,22 +16,43 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.util.*; -public class BukkitServer implements EsServerSoftware { +public class BukkitServer implements EsServer { private final JavaPlugin plugin; private final BukkitEventsListener listener; + private static final Set materials = new HashSet<>(); + private static final Set itemMaterials = new HashSet<>(); public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; listener = new BukkitEventsListener(); } + @Override + public void initialise() { + for (Material mat : Material.values()) { + EsMaterial esMat; + if (Main.minecraftVersion.getMinor() > 12) { + esMat = EsMaterial.createUnchecked(mat.getKey().getKey().toLowerCase()); + + if (mat.isItem()) { + itemMaterials.add(esMat); + } + } else { + esMat = EsMaterial.createUnchecked(mat.name().toLowerCase()); + + itemMaterials.add(esMat); + } + + materials.add(esMat); + } + } + @Override public EsPlayer getPlayer(String name) { Player p = Bukkit.getPlayer(name); @@ -115,35 +138,35 @@ public Collection getOnlinePlayers() { } @Override - public EsItemStack createItemStack(String material, int amount) { + public EsItemStack createItemStack(EsMaterial material, int amount) { return new BukkitItemStack(material, amount); } @SuppressWarnings("deprecation") @Override - public EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amount) { if (Main.minecraftVersion.getMinor() >= 9) { String type = potType == EsPotType.drink ? "POTION" : potType.toString().toUpperCase() + "_POTION"; ItemStack pot = new ItemStack(Material.valueOf(type), amount); - String effType; - try { - effType = Effects.getByName(effect); - } catch (IllegalArgumentException e) { - return null; - } - PotionMeta meta = (PotionMeta) pot.getItemMeta(); assert meta != null; - meta.addCustomEffect(new PotionEffect(BukkitHelper.toBukkitPotionEffectType(effType), duration, amp-1), true); + meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); pot.setItemMeta(meta); + return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { - Potion potion = new Potion(BukkitHelper.toBukkitPotionType(effect), amp); + PotionType type = BukkitEffectHelper.getPotionFromEffectType(effect.getType()); + if (type == null) { // This can fail if the effect doesn't have a potion for it + return null; + } + + Potion potion = new Potion(type, effect.getAmp()); potion.setSplash(potType == EsPotType.splash); - return new BukkitPotion(potion.toItemStack(1)); + + return new BukkitPotion(potion.toItemStack(amount)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; } @@ -156,11 +179,14 @@ public EsPotion createPotion(EsPotType potType) { String type = potType == EsPotType.drink ? "POTION" : potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), 1); + return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); potion.setSplash(potType == EsPotType.splash); + return new BukkitPotion(potion.toItemStack(1)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; @@ -173,42 +199,19 @@ public EsInventory createInventory(EsPlayer owner, int size, String title) { return new BukkitInventory(Bukkit.createInventory(holder, size, title)); } - @SuppressWarnings("deprecation") @Override - public String[] getPotionEffectTypes() { - // We need to use the deprecated .values() method because Registry doesn't exist in old versions - PotionEffectType[] effectTypes = PotionEffectType.values(); - List out = new ArrayList<>(); - for (PotionEffectType effectType : effectTypes) { - if (effectType == null) { - continue; - } - out.add(effectType.getName()); // Same reason as above for deprecated method - } - return out.toArray(new String[0]); + public Set getPotionEffectTypes() { + return BukkitEffectHelper.getEffectList(); } @Override - public String[] getEnchantments() { - if (Main.minecraftVersion.getMinor() > 12) { - List out = new ArrayList<>(); - for (Enchantment e : Registry.ENCHANTMENT) { - out.add(e.getKey().getKey()); - } - - return out.toArray(new String[0]); - } - - // Pre 1.13, we need to use the helper to get all the keys - Set> enchSet = BukkitEnchantmentsHelper.entrySet(); - String[] enchs = new String[enchSet.size()]; - int i = 0; - for (Map.Entry enchEntry : enchSet) { - enchs[i] = enchEntry.getKey(); - i++; - } + public Set getOldPotionTypes() { + return BukkitEffectHelper.getPotionList(); + } - return enchs; + @Override + public Set getEnchantments() { + return BukkitEnchantmentHelper.getEnchantmentList(); } @Override @@ -221,15 +224,6 @@ public String[] getSounds() { return strSounds; } - @Override - public boolean doesEnchantmentExist(String name) { - try { - return BukkitHelper.getBukkitEnchantment(name) != null; - } catch (Exception e) { - return false; - } - } - @Override public File getDataFolder() { return plugin.getDataFolder(); @@ -322,16 +316,8 @@ public void broadcast(String msg) { } @Override - public String[] getMaterials(boolean onlyItems) { - Material[] materials = Material.values(); - String[] strMaterials = new String[materials.length]; - for (int i = 0; i < materials.length; i++) { - if (onlyItems && Main.minecraftVersion.getMinor() >= 12 && !materials[i].isItem()) { - continue; - } - strMaterials[i] = materials[i].name(); - } - return strMaterials; + public Set getMaterials(boolean onlyItems) { + return onlyItems ? itemMaterials : materials; } @Override @@ -339,15 +325,8 @@ public EsWorld getWorld(String name) { return new BukkitWorld(Bukkit.getWorld(name)); } - @Override - public EsItemStack createItemStack(Object internalObject) { - return BukkitHelper.fromBukkitItem((ItemStack) internalObject); - } - @Override public String[] getRelevantInternalTypes() { - return new String[] { - "CraftItemStack" // CChest config files - }; + return new String[] {}; } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java index 1df0288..aa55995 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.EsToolsTabCompleter; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index 3606a6e..bd5e694 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -2,6 +2,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.entity.Entity; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java new file mode 100644 index 0000000..3e2c990 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java @@ -0,0 +1,105 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; + +import net.serble.estools.Main; +import net.serble.estools.SemanticVersion; +import net.serble.estools.ServerApi.EsPotionEffectType; +import org.bukkit.Registry; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; + +import java.util.*; + +public class BukkitEffectHelper { + private static final Map OLD_POTIONS = new HashMap<>(); + + // Potion effect types, because getKey() doesnt exist pre 1.18 + private static final Map ES_TO_BUKKIT = new HashMap<>(); + private static final Map BUKKIT_TO_ES = new HashMap<>(); + + public static PotionEffectType toBukkitEffectType(EsPotionEffectType effect) { + return ES_TO_BUKKIT.get(effect); + } + + public static EsPotionEffectType fromBukkitEffectType(PotionEffectType effect) { + return BUKKIT_TO_ES.get(effect); + } + + public static PotionType getPotionFromEffectType(EsPotionEffectType type) { + return OLD_POTIONS.get(type); + } + + public static Set getEffectList() { + return ES_TO_BUKKIT.keySet(); + } + + public static Set getPotionList() { + return OLD_POTIONS.keySet(); + } + + public static void load() { + // in 1.8 and below, there were no custom potions, just some hardcoded ones. + // these are PotionTypes, and we need to generate them based on EsPotionEffectTypes + if (Main.minecraftVersion.getMinor() <= 8) { + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("regeneration"), PotionType.REGEN); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("speed"), PotionType.SPEED); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("fire_resistance"), PotionType.FIRE_RESISTANCE); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("poison"), PotionType.POISON); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("instant_health"), PotionType.INSTANT_HEAL); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("weakness"), PotionType.WEAKNESS); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("strength"), PotionType.STRENGTH); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("slowness"), PotionType.SLOWNESS); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("instant_damage"), PotionType.INSTANT_DAMAGE); + + if (Main.minecraftVersion.getMinor() >= 4 && Main.minecraftVersion.getMinor() >= 2) { // Night vision was added in 1.4.2 + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("night_vision"), PotionType.NIGHT_VISION); + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("invisibility"), PotionType.INVISIBILITY); + } + + if (Main.minecraftVersion.getMinor() >= 7 && Main.minecraftVersion.getMinor() >= 2) { // 1.7.2 + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("water_breathing"), PotionType.WATER); + } + + if (Main.minecraftVersion.getMinor() >= 8) { // 1.8 + OLD_POTIONS.put(EsPotionEffectType.createUnchecked("jump_boost"), PotionType.JUMP); + } + } + + final HashMap nameReplacers = new HashMap() {{ + put("confusion", "nausea"); + put("damage_resistance", "resistance"); + put("fast_digging", "haste"); + put("harm", "instant_damage"); + put("heal", "instant_health"); + put("increase_damage", "strength"); + put("slow", "slowness"); + put("slow_digging", "mining_fatigue"); + put("jump", "jump_boost"); + }}; + + // Registry.EFFECT was added in 1.20.3... + if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 20, 3))) { + for (PotionEffectType type : Registry.EFFECT) { + EsPotionEffectType esType = EsPotionEffectType.createUnchecked(type.getKey().getKey()); + ES_TO_BUKKIT.put(esType, type); + BUKKIT_TO_ES.put(type, esType); + } + } else { + for (PotionEffectType type : PotionEffectType.values()) { + String name; + if (Main.minecraftVersion.getMinor() >= 18) { + name = type.getKey().getKey(); + } else { + name = type.getName(); + + if (nameReplacers.containsKey(name)) { + name = nameReplacers.get(name); + } + } + + EsPotionEffectType esType = EsPotionEffectType.createUnchecked(name); + ES_TO_BUKKIT.put(esType, type); + BUKKIT_TO_ES.put(type, esType); + } + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java new file mode 100644 index 0000000..fef1a63 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java @@ -0,0 +1,77 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsEnchantment; +import org.bukkit.Registry; +import org.bukkit.enchantments.Enchantment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +// This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. +// only enchantments before 1.13 need to be here because this class isn't used past 1.13 +public class BukkitEnchantmentHelper { + private static final Map ES_TO_BUKKIT = new HashMap<>(); + private static final Map BUKKIT_TO_ES = new HashMap<>(); + + public static EsEnchantment fromBukkitEnchantment(Enchantment ench) { + return BUKKIT_TO_ES.get(ench); + } + + public static Enchantment toBukkitEnchantment(EsEnchantment ench) { + return ES_TO_BUKKIT.get(ench); + } + + public static Set getEnchantmentList() { + return ES_TO_BUKKIT.keySet(); + } + + static { + final HashMap nameReplacers = new HashMap() {{ + put("damage_all", "sharpness"); + put("damage_undead", "smite"); + put("damage_arthropods", "bane_of_arthropods"); + put("dig_speed", "efficiency"); + put("durability", "unbreaking"); + put("loot_bonus_mobs", "looting"); + put("oxygen", "respiration"); + put("protection_environmental", "protection"); + put("protection_explosions", "blast_protection"); + put("protection_fall", "feather_falling"); + put("protection_projectile", "projectile_protection"); + put("water_worker", "aqua_affinity"); + put("arrow_fire", "flame"); + put("arrow_damage", "power"); + put("arrow_knockback", "punch"); + put("arrow_infinite", "infinity"); + put("loot_bonus_blocks", "fortune"); + }}; + + // Registry.ENCHANTMENT was added in 1.14 + if (Main.minecraftVersion.getMinor() > 13) { + for (Enchantment ench : Registry.ENCHANTMENT) { + EsEnchantment esEnch = EsEnchantment.createUnchecked(ench.getKey().getKey()); + ES_TO_BUKKIT.put(esEnch, ench); + BUKKIT_TO_ES.put(ench, esEnch); + } + } else { + for (Enchantment ench : Enchantment.values()) { + String name; + if (Main.minecraftVersion.getMinor() >= 13) { + name = ench.getKey().getKey(); + } else { + name = ench.getName(); + + if (nameReplacers.containsKey(name)) { + name = nameReplacers.get(name); + } + } + + EsEnchantment esType = EsEnchantment.createUnchecked(name); + ES_TO_BUKKIT.put(esType, ench); + BUKKIT_TO_ES.put(ench, esType); + } + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java similarity index 95% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java index 4ca2e29..7c8db5d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Encapsulation/BukkitEquipmentSlotHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit.Encapsulation; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; import net.serble.estools.ServerApi.EsEquipmentSlot; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java similarity index 90% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java index 5574421..1e21c88 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java @@ -1,9 +1,9 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; -import net.serble.estools.Effects; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.*; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; @@ -12,7 +12,6 @@ import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -26,14 +25,11 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionType; import org.bukkit.util.Vector; // DO NOT IMPORT THE FOLLOWING BECAUSE THEY BREAK OLDER VERSIONS BECAUSE THEY DON'T EXIST // org.bukkit.inventory.EquipmentSlot -import java.util.Objects; @SuppressWarnings("unused") public class BukkitHelper { @@ -217,34 +213,6 @@ public static EsBlock fromBukkitBlock(Block block) { return fromBukkitBlock(state); } - @SuppressWarnings("deprecation") - public static Enchantment getBukkitEnchantment(String name) { - if (Main.minecraftVersion.getMinor() >= 14) { - Enchantment ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); - if (ench == null) { - // Dump info and throw - Bukkit.getLogger().severe("Failed to find enchantment: " + name); - for (Enchantment e : Registry.ENCHANTMENT) { - Bukkit.getLogger().severe("This exists: " + e.getName()); - } - throw new RuntimeException("Could not find enchantment: " + name); - } - } - - // We have to use deprecated method for pre 1.13 - return Enchantment.getByName(BukkitEnchantmentsHelper.getByName(name)); - } - - public static String fromBukkitEnchantment(Enchantment ench) { - if (Main.minecraftVersion.getMinor() >= 13) { - return ench.getKey().getKey(); - } - - // We have to use deprecated method for pre 1.13 - //noinspection deprecation - return BukkitEnchantmentsHelper.getNameFromValue(ench.getName()); - } - @SuppressWarnings("rawtypes") public static PersistentDataType toBukkitPersistentDataType(EsPersistentDataType type) { switch (type) { @@ -344,23 +312,27 @@ public static EsPotType fromBukkitPotType(String type) { } public static PotionEffect toBukkitPotionEffect(EsPotionEffect effect) { - return new PotionEffect(toBukkitPotionEffectType(effect.getType()), effect.getDuration(), effect.getAmp()); + return new PotionEffect(BukkitEffectHelper.toBukkitEffectType(effect.getType()), effect.getDuration(), effect.getAmp()); + } + + public static EsPotionEffect fromBukkitPotionEffect(PotionEffect effect) { + return new EsPotionEffect(BukkitEffectHelper.fromBukkitEffectType(effect.getType()), effect.getAmplifier(), effect.getDuration()); } - public static PotionEffectType toBukkitPotionEffectType(String type) { - if (Main.minecraftVersion.getMinor() < 20) { - return PotionEffectType.getByName(Effects.getByName(type)); + public static Material toBukkitMaterial(EsMaterial mat) { + if (Main.minecraftVersion.getMinor() > 12) { + return Registry.MATERIAL.get(NamespacedKey.minecraft(mat.getKey())); } - return Objects.requireNonNull(Registry.EFFECT.match(type)); + return Material.valueOf(mat.getKey().toUpperCase()); } - public static PotionType toBukkitPotionType(String type) { - if (Main.minecraftVersion.getMinor() < 20) { - return PotionType.valueOf(Effects.getPotionByName(type)); + public static EsMaterial fromBukkitMaterial(Material mat) { + if (Main.minecraftVersion.getMinor() > 12) { + return EsMaterial.createUnchecked(mat.getKey().getKey()); } - return Objects.requireNonNull(Registry.POTION.match(type)); + return EsMaterial.createUnchecked(mat.name()); } public static boolean isFolia() { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java similarity index 62% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java index 103d6d0..c0f10e1 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentsHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java @@ -1,7 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Folia; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitEnchantmentsHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; // This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. // only enchantments before 1.13 need to be here because this class isn't used past 1.13 -public class FoliaEnchantmentsHelper extends BukkitEnchantmentsHelper { } +public class FoliaEnchantmentHelper extends BukkitEnchantmentHelper { } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index af700c5..5e5004f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -8,7 +8,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsLocation; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; @@ -17,7 +17,6 @@ import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -213,24 +212,6 @@ public static EsItemStack fromBukkitItem(ItemStack item) { } } - @SuppressWarnings("deprecation") - public static Enchantment getBukkitEnchantment(String name) { - if (Main.minecraftVersion.getMinor() >= 13) { - Enchantment ench = Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)); - if (ench == null) { - // Dump info and throw - Bukkit.getLogger().severe("Failed to find enchantment: " + name); - for (Enchantment e : Registry.ENCHANTMENT) { - Bukkit.getLogger().severe("This exists: " + e.getName()); - } - throw new RuntimeException("Could not find enchantment: " + name); - } - } - - // We have to use deprecated method for pre 1.13 - return Enchantment.getByName(FoliaEnchantmentsHelper.getByName(name)); - } - public static EsBlock fromBukkitBlock(BlockState block) { if (block == null) { return null; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index e043409..28d9015 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Folia; +import net.serble.estools.ServerApi.EsMaterial; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; @@ -53,8 +55,8 @@ public int getSize() { } @Override - public Map all(String material) { - Material mat = Material.valueOf(material); + public Map all(EsMaterial material) { + Material mat = BukkitHelper.toBukkitMaterial(material); Map out = new HashMap<>(); for (Map.Entry entry : bukkitInv.all(mat).entrySet()) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 9bcce70..6156c1e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -1,10 +1,12 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import org.bukkit.Material; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; @@ -14,12 +16,13 @@ import java.util.HashMap; import java.util.Map; +import java.util.Set; public class FoliaItemStack implements EsItemStack { private final ItemStack bukkitItem; - public FoliaItemStack(String mat, int amount) { - bukkitItem = new org.bukkit.inventory.ItemStack(Material.valueOf(mat), amount); + public FoliaItemStack(EsMaterial mat, int amount) { + bukkitItem = new org.bukkit.inventory.ItemStack(BukkitHelper.toBukkitMaterial(mat), amount); } public FoliaItemStack(ItemStack child) { @@ -41,13 +44,13 @@ public void setAmount(int val) { } @Override - public String getType() { - return bukkitItem.getType().name(); + public EsMaterial getType() { + return BukkitHelper.fromBukkitMaterial(bukkitItem.getType()); } @Override - public void setType(String val) { - bukkitItem.setType(Material.valueOf(val)); + public void setType(EsMaterial val) { + bukkitItem.setType(BukkitHelper.toBukkitMaterial(val)); } @Override @@ -124,13 +127,13 @@ public boolean isSimilar(EsItemStack stack) { } @Override - public void addEnchantment(String enchantment, int level) { - bukkitItem.addUnsafeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment), level); + public void addEnchantment(EsEnchantment enchantment, int level) { + bukkitItem.addUnsafeEnchantment(FoliaEnchantmentHelper.toBukkitEnchantment(enchantment), level); } @Override - public void removeEnchantment(String enchantment) { - bukkitItem.removeEnchantment(BukkitHelper.getBukkitEnchantment(enchantment)); + public void removeEnchantment(EsEnchantment enchantment) { + bukkitItem.removeEnchantment(FoliaEnchantmentHelper.toBukkitEnchantment(enchantment)); } @Override @@ -144,10 +147,11 @@ public Object getInternalObject() { } @Override - public Map getEnchantments() { - Map enchs = new HashMap<>(); - for (Map.Entry ench : bukkitItem.getEnchantments().entrySet()) { - enchs.put(FoliaHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); + public Map getEnchantments() { + Set> bEnchs = bukkitItem.getEnchantments().entrySet(); + Map enchs = new HashMap<>(bEnchs.size()); + for (Map.Entry ench : bEnchs) { + enchs.put(BukkitEnchantmentHelper.fromBukkitEnchantment(ench.getKey()), ench.getValue()); } return enchs; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java index 13ad803..9d89a4d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java @@ -1,10 +1,12 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.EsPotionEffectType; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; -import org.bukkit.Registry; import org.bukkit.attribute.Attribute; import org.bukkit.entity.LivingEntity; import org.bukkit.potion.PotionEffect; @@ -99,23 +101,23 @@ public void setHealth(double val) { } @Override - public void addPotionEffect(String effect, int duration, int amplifier) { - bukkitEntity.addPotionEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect)), duration, amplifier)); + public void addPotionEffect(EsPotionEffect effect) { + bukkitEntity.addPotionEffect(FoliaHelper.toBukkitPotionEffect(effect)); } @Override - public void removePotionEffect(String effect) { - bukkitEntity.removePotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effect))); + public void removePotionEffect(EsPotionEffectType effect) { + bukkitEntity.removePotionEffect(BukkitEffectHelper.toBukkitEffectType(effect)); } - @SuppressWarnings("deprecation") // Gotta love backwards compatibility @Override - public List getActivePotionEffects() { + public List getActivePotionEffects() { Collection bukkitEffects = bukkitEntity.getActivePotionEffects(); - List out = new ArrayList<>(); + List out = new ArrayList<>(bukkitEffects.size()); for (PotionEffect eff : bukkitEffects) { - out.add(eff.getType().getName()); + out.add(FoliaHelper.fromBukkitPotionEffect(eff)); } + return out; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java index f9630ee..fa19e49 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java @@ -3,7 +3,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.EsPotionEffect; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; @@ -58,7 +58,7 @@ public EsPotionEffect[] getEffects() { EsPotionEffect[] out = new EsPotionEffect[in.length]; for (int i = 0; i < out.length; i++) { PotionEffect effect = in[i]; - out[i] = new EsPotionEffect(effect.getType().getName(), effect.getAmplifier(), effect.getDuration()); + out[i] = new EsPotionEffect(BukkitEffectHelper.fromBukkitEffectType(effect.getType()), effect.getAmplifier(), effect.getDuration()); } return out; } @@ -75,9 +75,9 @@ public EsPotType getPotionType() { @Override public void addEffect(EsPotionEffect effect) { if (old) { - bukkitPotion.getEffects().add(BukkitHelper.toBukkitPotionEffect(effect)); + bukkitPotion.getEffects().add(FoliaHelper.toBukkitPotionEffect(effect)); } else { - bukkitMeta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); + bukkitMeta.addCustomEffect(FoliaHelper.toBukkitPotionEffect(effect), true); } } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 0f6cd4e..d41650a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -6,12 +6,14 @@ import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPotion; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitTabCompleteGenerator; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; @@ -19,17 +21,18 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; import java.io.File; import java.util.*; -public class FoliaServer implements EsServerSoftware { +public class FoliaServer implements EsServer { private final JavaPlugin plugin; private final FoliaEventsListener listener; private final Map tasks = new HashMap<>(); private static TaskChainFactory taskChainFactory; + private static final Set materials = new HashSet<>(); + private static final Set itemMaterials = new HashSet<>(); public FoliaServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; @@ -37,6 +40,26 @@ public FoliaServer(Object pluginObj) { listener = new FoliaEventsListener(); } + @Override + public void initialise() { + for (Material mat : Material.values()) { + EsMaterial esMat; + if (Main.minecraftVersion.getMinor() > 12) { + esMat = EsMaterial.createUnchecked(mat.getKey().getKey().toLowerCase()); + + if (mat.isItem()) { + itemMaterials.add(esMat); + } + } else { + esMat = EsMaterial.createUnchecked(mat.name().toLowerCase()); + + itemMaterials.add(esMat); + } + + materials.add(esMat); + } + } + public static TaskChain newChain() { return taskChainFactory.newChain(); } @@ -126,42 +149,35 @@ public Collection getOnlinePlayers() { } @Override - public EsItemStack createItemStack(String material, int amount) { + public EsItemStack createItemStack(EsMaterial material, int amount) { return new FoliaItemStack(material, amount); } @SuppressWarnings("deprecation") @Override - public EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount) { + public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amount) { if (Main.minecraftVersion.getMinor() >= 9) { String type = potType == EsPotType.drink ? "POTION" : potType.toString().toUpperCase() + "_POTION"; ItemStack pot = new ItemStack(Material.valueOf(type), amount); - String effType; - try { - effType = Effects.getByName(effect); - } catch (IllegalArgumentException e) { - return null; - } - PotionMeta meta = (PotionMeta) pot.getItemMeta(); assert meta != null; - meta.addCustomEffect(new PotionEffect(Objects.requireNonNull(Registry.EFFECT.match(effType)), duration, amp-1), true); + meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); pot.setItemMeta(meta); - return new FoliaPotion(pot); + + return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { - String effType; - try { - effType = Effects.getPotionByName(effect); - } catch (IllegalArgumentException e) { + PotionType type = BukkitEffectHelper.getPotionFromEffectType(effect.getType()); + if (type == null) { // This can fail if the effect doesn't have a potion for it return null; } - org.bukkit.potion.Potion potion = new org.bukkit.potion.Potion(Objects.requireNonNull(Registry.POTION.match(effType)), amp); + Potion potion = new Potion(type, effect.getAmp()); potion.setSplash(potType == EsPotType.splash); - return new FoliaPotion(potion.toItemStack(1)); + + return new BukkitPotion(potion.toItemStack(amount)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; } @@ -174,12 +190,15 @@ public EsPotion createPotion(EsPotType potType) { String type = potType == EsPotType.drink ? "POTION" : potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), 1); - return new FoliaPotion(pot); + + return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); potion.setSplash(potType == EsPotType.splash); - return new FoliaPotion(potion.toItemStack(1)); + + return new BukkitPotion(potion.toItemStack(1)); } else { // This isn't possible to get to because this class won't load on 1.3 and below return null; } @@ -191,40 +210,19 @@ public EsInventory createInventory(EsPlayer owner, int size, String title) { return new FoliaInventory(Bukkit.createInventory(holder, size, title)); } - @SuppressWarnings("deprecation") @Override - public String[] getPotionEffectTypes() { - // We need to use the deprecated .values() method because Registry doesn't exist in old versions - PotionEffectType[] effectTypes = PotionEffectType.values(); - String[] out = new String[effectTypes.length]; - for (int i = 0; i < effectTypes.length; i++) { - out[i] = effectTypes[i].getName(); // Same reason as above for deprecated method - } - return out; + public Set getPotionEffectTypes() { + return BukkitEffectHelper.getEffectList(); } @Override - public String[] getEnchantments() { - if (Main.minecraftVersion.getMinor() > 12) { - String[] out = new String[(int) Registry.ENCHANTMENT.stream().count()]; - int i = 0; - for (Enchantment e : Registry.ENCHANTMENT) { - out[i] = e.getKey().getKey(); - i++; - } - return out; - } - - // Pre 1.13, we need to use the helper to get all the keys - Set> enchSet = FoliaEnchantmentsHelper.entrySet(); - String[] enchs = new String[enchSet.size()]; - int i = 0; - for (Map.Entry enchEntry : enchSet) { - enchs[i] = enchEntry.getKey(); - i++; - } + public Set getOldPotionTypes() { + return BukkitEffectHelper.getPotionList(); + } - return enchs; + @Override + public Set getEnchantments() { + return FoliaEnchantmentHelper.getEnchantmentList(); } @Override @@ -238,25 +236,8 @@ public String[] getSounds() { } @Override - public String[] getMaterials(boolean onlyItems) { - Material[] materials = Material.values(); - String[] strMaterials = new String[materials.length]; - for (int i = 0; i < materials.length; i++) { - if (onlyItems && Main.minecraftVersion.getMinor() >= 12 && !materials[i].isItem()) { - continue; - } - strMaterials[i] = materials[i].name(); - } - return strMaterials; - } - - @Override - public boolean doesEnchantmentExist(String name) { - try { - return FoliaHelper.getBukkitEnchantment(name) != null; - } catch (Exception e) { - return false; - } + public Set getMaterials(boolean onlyItems) { + return onlyItems ? itemMaterials : materials; } @Override @@ -377,16 +358,8 @@ public EsWorld getWorld(String name) { return new FoliaWorld(Bukkit.getWorld(name)); } - @Override - public EsItemStack createItemStack(Object internalObject) { - return FoliaHelper.fromBukkitItem((ItemStack) internalObject); - } - @Override public String[] getRelevantInternalTypes() { - return new String[] { - "CraftItemStack", // CChest config files - "net.serble.estools.ServerApi.EsSerialisableItemStack" - }; + return new String[] {}; } } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java index 4102693..d450d70 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsInventory.java @@ -1,5 +1,7 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.ServerApi.EsMaterial; + import java.util.Map; public interface EsInventory { @@ -21,5 +23,5 @@ public interface EsInventory { * @return * A list of slot to item mappings. */ - Map all(String material); + Map all(EsMaterial material); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java index 15f9f68..92707d6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsItemStack.java @@ -1,14 +1,17 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; + import java.util.Map; public interface EsItemStack { - String getType(); - void setType(String val); + EsMaterial getType(); + void setType(EsMaterial val); int getAmount(); void setAmount(int amount); - void addEnchantment(String enchantment, int level); - void removeEnchantment(String enchantment); + void addEnchantment(EsEnchantment enchantment, int level); + void removeEnchantment(EsEnchantment enchantment); EsItemMeta getItemMeta(); String exportItemMeta(); void importItemMeta(String meta); @@ -19,5 +22,5 @@ public interface EsItemStack { boolean isSimilar(EsItemStack stack); int getMaxStackSize(); Object getInternalObject(); - Map getEnchantments(); + Map getEnchantments(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java index d5f56e9..b665e31 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsLivingEntity.java @@ -1,5 +1,8 @@ package net.serble.estools.ServerApi.Interfaces; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.EsPotionEffectType; + import java.util.List; public interface EsLivingEntity extends EsEntity { @@ -8,8 +11,8 @@ public interface EsLivingEntity extends EsEntity { double getHealth(); void setHealth(double val); void sendMessage(String... msg); - void addPotionEffect(String effect, int duration, int amplifier); - void removePotionEffect(String effect); - List getActivePotionEffects(); + void addPotionEffect(EsPotionEffect effect); + void removePotionEffect(EsPotionEffectType effect); + List getActivePotionEffects(); EsWorld getWorld(); } diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java similarity index 76% rename from src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java rename to src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java index 2caf280..1296c46 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServerSoftware.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java @@ -2,33 +2,35 @@ import net.serble.estools.EsToolsTabCompleter; import net.serble.estools.SemanticVersion; -import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.*; import java.io.File; import java.util.Collection; +import java.util.Set; import java.util.UUID; @SuppressWarnings("unused") -public interface EsServerSoftware { +public interface EsServer { EsPlayer getPlayer(String name); EsEntity getEntity(UUID uuid); SemanticVersion getVersion(); Collection getOnlinePlayers(); - EsItemStack createItemStack(String material, int amount); - EsPotion createPotion(EsPotType potType, String effect, int duration, int amp, int amount); + EsItemStack createItemStack(EsMaterial material, int amount); + EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amount); EsPotion createPotion(EsPotType potType); EsInventory createInventory(EsPlayer owner, int size, String title); - String[] getPotionEffectTypes(); - String[] getEnchantments(); + Set getPotionEffectTypes(); + Set getOldPotionTypes(); + Set getEnchantments(); String[] getSounds(); + void initialise(); /** * @param onlyItems * Whether to only include materials that are classified as items * */ - String[] getMaterials(boolean onlyItems); + Set getMaterials(boolean onlyItems); - boolean doesEnchantmentExist(String name); File getDataFolder(); void dispatchCommand(EsCommandSender sender, String cmd); EsCommandSender getConsoleSender(); @@ -45,7 +47,6 @@ public interface EsServerSoftware { void broadcast(String msg, String perm); void broadcast(String msg); EsWorld getWorld(String name); - EsItemStack createItemStack(Object internalObject); /** Get a list of classes that SnakeYAML needs to accept for proper config serialisation */ String[] getRelevantInternalTypes(); diff --git a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java index f166766..754d9ee 100644 --- a/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java +++ b/src/main/java/net/serble/estools/ServerApi/ServerPlatform.java @@ -2,7 +2,7 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitServer; import net.serble.estools.ServerApi.Implementations.Folia.FoliaServer; -import net.serble.estools.ServerApi.Interfaces.EsServerSoftware; +import net.serble.estools.ServerApi.Interfaces.EsServer; public enum ServerPlatform { Bukkit(true), // Includes all derivatives @@ -18,7 +18,7 @@ public boolean supportsMetrics() { return hasMetrics; } - public EsServerSoftware getServerInstance(Object context) { + public EsServer getServerInstance(Object context) { switch (this) { case Bukkit: return new BukkitServer(context); diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index 76e192b..0b66b51 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -96,7 +96,8 @@ public class Tester { new Tuple<>("getpersistentdata estools:hello string", 2), new Tuple<>("removepersistentdata estools:hello", 2), new Tuple<>("getpersistentdata estools:hello string", 2), - new Tuple<>("suicide", 4) + new Tuple<>("buddha", -1), + new Tuple<>("suicide", 4), }; public Tester(EsPlayer p) { diff --git a/src/main/java/net/serble/estools/Utils.java b/src/main/java/net/serble/estools/Utils.java new file mode 100644 index 0000000..4afcacd --- /dev/null +++ b/src/main/java/net/serble/estools/Utils.java @@ -0,0 +1,20 @@ +package net.serble.estools; + +public class Utils { + public static String keyToDisplayName(String key) { + StringBuilder name = new StringBuilder(key); + name.setCharAt(0, Character.toUpperCase(name.charAt(0))); + + int index = name.indexOf("_"); + while (index != -1) { + name.setCharAt(index, ' '); + if (index < name.length()) { // Edge case where item id ends with '_' + name.setCharAt(index+1, Character.toUpperCase(name.charAt(index+1))); + } + + index = name.indexOf("_"); + } + + return name.toString(); + } +} From d3e815f8ce7b9331fc9924f14fbdd7cf305ace94 Mon Sep 17 00:00:00 2001 From: copokbl Date: Tue, 18 Jun 2024 22:07:02 +1000 Subject: [PATCH 35/74] Fixed name of Helpers package for bukkit --- src/main/java/net/serble/estools/Main.java | 2 +- .../Implementations/Bukkit/BukkitConfigMigrator.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitEntity.java | 2 +- .../Implementations/Bukkit/BukkitEventsListener.java | 4 ++-- .../ServerApi/Implementations/Bukkit/BukkitInventory.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitItemStack.java | 4 ++-- .../Implementations/Bukkit/BukkitLivingEntity.java | 4 ++-- .../Bukkit/BukkitPersistentDataContainer.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitPlayer.java | 2 +- .../Implementations/Bukkit/BukkitPlayerInventory.java | 4 ++-- .../ServerApi/Implementations/Bukkit/BukkitPotion.java | 4 ++-- .../ServerApi/Implementations/Bukkit/BukkitServer.java | 6 +++--- .../Implementations/Bukkit/BukkitTabCompleteGenerator.java | 2 +- .../ServerApi/Implementations/Bukkit/BukkitWorld.java | 2 +- .../Bukkit/{Helper => Helpers}/BukkitEffectHelper.java | 2 +- .../Bukkit/{Helper => Helpers}/BukkitEnchantmentHelper.java | 2 +- .../{Helper => Helpers}/BukkitEquipmentSlotHelper.java | 2 +- .../Bukkit/{Helper => Helpers}/BukkitHelper.java | 2 +- .../Implementations/Folia/FoliaEnchantmentHelper.java | 2 +- .../ServerApi/Implementations/Folia/FoliaHelper.java | 2 +- .../ServerApi/Implementations/Folia/FoliaInventory.java | 2 +- .../ServerApi/Implementations/Folia/FoliaItemStack.java | 4 ++-- .../ServerApi/Implementations/Folia/FoliaLivingEntity.java | 2 +- .../ServerApi/Implementations/Folia/FoliaPotion.java | 2 +- .../ServerApi/Implementations/Folia/FoliaServer.java | 4 ++-- 25 files changed, 34 insertions(+), 34 deletions(-) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{Helper => Helpers}/BukkitEffectHelper.java (99%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{Helper => Helpers}/BukkitEnchantmentHelper.java (99%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{Helper => Helpers}/BukkitEquipmentSlotHelper.java (99%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{Helper => Helpers}/BukkitHelper.java (99%) diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index d198f01..b3888d6 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -14,7 +14,7 @@ import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitConfigMigrator; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java index 8e2ffc0..b84e286 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java @@ -8,7 +8,7 @@ import net.serble.estools.Config.Schemas.Give.GiveSettings; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsSerialisableItemStack; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 03dcf3b..0dd0a4f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -3,7 +3,7 @@ import io.papermc.lib.PaperLib; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsEntity; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index 0a91420..aad0e70 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -6,8 +6,8 @@ import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEquipmentSlotHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java index ccbcfff..dba4330 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventory.java @@ -1,7 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.ServerApi.EsMaterial; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index de2d7bf..e2929e8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -3,8 +3,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEnchantment; import net.serble.estools.ServerApi.EsMaterial; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.configuration.InvalidConfigurationException; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index 9b7196a..0765357 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -3,8 +3,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotionEffect; import net.serble.estools.ServerApi.EsPotionEffectType; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java index ef2790e..d45726f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPersistentDataContainer.java @@ -7,7 +7,7 @@ import java.util.Objects; -import static net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper.*; +import static net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper.*; @SuppressWarnings("unchecked") // Trust me bro public class BukkitPersistentDataContainer implements EsPersistentDataContainer { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 95e370d..5d8db0d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -4,7 +4,7 @@ import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.EsSoundCategory; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.Sound; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index bd857ce..c4dbd8f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -2,8 +2,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEquipmentSlotHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEquipmentSlotHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayerInventory; import org.bukkit.inventory.Inventory; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java index 9e93158..79989fa 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotion.java @@ -3,8 +3,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.EsPotionEffect; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; // TODO: pre 1.9 errors because no PotionMeta and pre 1.4 errors diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 2125a0a..1e4e282 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -3,9 +3,9 @@ import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java index aa55995..b0d0d8e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitTabCompleteGenerator.java @@ -1,7 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.EsToolsTabCompleter; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index bd5e694..7e606cd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -2,7 +2,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsLocation; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.entity.Entity; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java similarity index 99% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java index 3e2c990..426c3ac 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEffectHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Main; import net.serble.estools.SemanticVersion; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java similarity index 99% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java index fef1a63..44dd7eb 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEnchantmentHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEnchantment; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEquipmentSlotHelper.java similarity index 99% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEquipmentSlotHelper.java index 7c8db5d..7f2ed8a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitEquipmentSlotHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEquipmentSlotHelper.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.ServerApi.EsEquipmentSlot; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java similarity index 99% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 1e21c88..9949c20 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helper/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit.Helper; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java index c0f10e1..cebc49f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java @@ -1,6 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Folia; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; // This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. // only enchantments before 1.13 need to be here because this class isn't used past 1.13 diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 5e5004f..9d78ff0 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -8,7 +8,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsLocation; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java index 28d9015..1c09eea 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventory.java @@ -1,7 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Folia; import net.serble.estools.ServerApi.EsMaterial; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.Material; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 6156c1e..1b543f3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -3,8 +3,8 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEnchantment; import net.serble.estools.ServerApi.EsMaterial; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEnchantmentHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.configuration.InvalidConfigurationException; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java index 9d89a4d..782d220 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaLivingEntity.java @@ -3,7 +3,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotionEffect; import net.serble.estools.ServerApi.EsPotionEffectType; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsLivingEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; import org.bukkit.Bukkit; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java index fa19e49..054af0b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPotion.java @@ -3,7 +3,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsPotType; import net.serble.estools.ServerApi.EsPotionEffect; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsPotion; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index d41650a..6ecd81b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -9,8 +9,8 @@ import net.serble.estools.ServerApi.*; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPotion; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitTabCompleteGenerator; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helper.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; From 64cc35f4ddcf2ad488eec76792547e697ba57b16 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Wed, 19 Jun 2024 15:32:17 +1000 Subject: [PATCH 36/74] Added EsSound for sound effects (some old sounds in old versions do not work), and made sure that all keys for similar types are lowercase. --- .../net/serble/estools/Commands/Music.java | 65 +- .../estools/ServerApi/EsEnchantment.java | 4 +- .../serble/estools/ServerApi/EsMaterial.java | 4 +- .../estools/ServerApi/EsPotionEffectType.java | 2 +- .../net/serble/estools/ServerApi/EsSound.java | 49 + .../Implementations/Bukkit/BukkitPlayer.java | 8 +- .../Implementations/Bukkit/BukkitServer.java | 23 +- .../Bukkit/Helpers/BukkitEffectHelper.java | 4 + .../Bukkit/Helpers/BukkitHelper.java | 17 + .../Helpers/BukkitSoundEnumConverter.java | 1053 +++++++++++++++++ .../Implementations/Folia/FoliaPlayer.java | 7 +- .../Implementations/Folia/FoliaServer.java | 27 +- .../ServerApi/Interfaces/EsPlayer.java | 3 +- .../ServerApi/Interfaces/EsServer.java | 2 +- 14 files changed, 1187 insertions(+), 81 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/EsSound.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java diff --git a/src/main/java/net/serble/estools/Commands/Music.java b/src/main/java/net/serble/estools/Commands/Music.java index 5e58e0a..6264788 100644 --- a/src/main/java/net/serble/estools/Commands/Music.java +++ b/src/main/java/net/serble/estools/Commands/Music.java @@ -2,45 +2,33 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsSound; import net.serble.estools.ServerApi.EsSoundCategory; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Random; public class Music extends EsToolsCommand { - private static final String usage = genUsage("/music [song]"); - private static final List musics = new ArrayList<>(); + private static final List randomDiscs = new ArrayList<>(); // discs that can be picked randomly private static final List tabComplete = new ArrayList<>(); private static final Random random = new Random(); @Override public void onEnable() { - if (Main.minecraftVersion.getMinor() > 12) { - for (String s : Main.server.getSounds()) { - if (s.startsWith("MUSIC_DISC")) { - musics.add(s); - tabComplete.add(s.toLowerCase().substring(11)); - } - } - - musics.remove("MUSIC_DISC_13"); - musics.remove("MUSIC_DISC_11"); - } else { - for (String s : Main.server.getSounds()) { - if (s.startsWith("RECORD")) { - tabComplete.add(s.toLowerCase().substring(7)); - - if (!s.equals("RECORD_11") && !s.equals("RECORD_13")) { - musics.add(s); - } - } + for (EsSound s : Main.server.getSounds()) { + if (s.getKey().startsWith("music_disc")) { + randomDiscs.add(s); + tabComplete.add(s.getKey().substring(11)); } } + randomDiscs.remove(EsSound.createUnchecked("music_disc.13")); + randomDiscs.remove(EsSound.createUnchecked("music_disc.11")); + randomDiscs.remove(EsSound.createUnchecked("music_disc.5")); + tabComplete.add("random"); } @@ -49,38 +37,29 @@ public boolean execute(EsCommandSender sender, String[] args) { if (isNotPlayer(sender)) { return false; } - + EsPlayer p = (EsPlayer) sender; - String sound; + EsSound sound; if (args.length > 0 && !args[0].equalsIgnoreCase("random")) { - try { - if (Main.minecraftVersion.getMinor() > 12) { - sound = "MUSIC_DISC_" + args[0].toUpperCase(); - } - else { - sound = "RECORD_" + args[0].toUpperCase(); - } - } catch (IllegalArgumentException e) { - send(sender, usage); + sound = EsSound.fromKey("music_disc." + args[0]); + + if (sound == null) { + send(sender, "&cThat music disc does not exist!"); return false; } } else { - sound = musics.get(random.nextInt(musics.size())); - } - - if (Arrays.stream(Main.server.getSounds()).noneMatch(s -> s.equalsIgnoreCase(sound))) { - send(sender, usage); - return false; + sound = randomDiscs.get(random.nextInt(randomDiscs.size())); } + // play very high up and with large volume so that the sound doesn't pan + // volume doesn't make the sound louder, it just increases its range. p.playSound(sound, EsSoundCategory.Records, p.getLocation().add(0, 1000, 0), 100000, 1); - // Substring away the MUSIC_DISK_ or the RECORD_ depending on the version - // then reformat to be all lowercase except for the first character - String name = sound.toLowerCase().substring(Main.minecraftVersion.getMinor() > 12 ? 11 : 7); + // Substring away the music_disc, then reformat to capitalise the first letter + String name = sound.getKey().toLowerCase().substring(11); name = String.valueOf(name.charAt(0)).toUpperCase() + name.substring(1); - + send(sender, "&aNow Playing: &6%s", name); return true; } diff --git a/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java index 43c2da5..e0c8967 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java +++ b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java @@ -9,13 +9,13 @@ public class EsEnchantment { public static EsEnchantment createUnchecked(String key) { EsEnchantment type = new EsEnchantment(); - type.key = key; + type.key = key.toLowerCase(); return type; } public static @Nullable EsEnchantment fromKey(String key) { - EsEnchantment type = EsEnchantment.createUnchecked(key.toLowerCase()); + EsEnchantment type = EsEnchantment.createUnchecked(key); if (Main.server.getEnchantments().contains(type)) { return type; diff --git a/src/main/java/net/serble/estools/ServerApi/EsMaterial.java b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java index 26d97fc..afbd8a0 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsMaterial.java +++ b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java @@ -9,13 +9,13 @@ public class EsMaterial { public static EsMaterial createUnchecked(String key) { EsMaterial type = new EsMaterial(); - type.key = key; + type.key = key.toLowerCase(); return type; } public static @Nullable EsMaterial fromKey(String key) { - EsMaterial type = EsMaterial.createUnchecked(key.toLowerCase()); + EsMaterial type = EsMaterial.createUnchecked(key); if (Main.server.getMaterials(false).contains(type)) { return type; diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java index 8fa3bd1..1e4b59c 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java +++ b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java @@ -9,7 +9,7 @@ public class EsPotionEffectType { public static EsPotionEffectType createUnchecked(String key) { EsPotionEffectType type = new EsPotionEffectType(); - type.key = key; + type.key = key.toLowerCase(); return type; } diff --git a/src/main/java/net/serble/estools/ServerApi/EsSound.java b/src/main/java/net/serble/estools/ServerApi/EsSound.java new file mode 100644 index 0000000..05f33fd --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/EsSound.java @@ -0,0 +1,49 @@ +package net.serble.estools.ServerApi; + +import net.serble.estools.Main; +import net.serble.estools.Utils; +import org.jetbrains.annotations.Nullable; + +public class EsSound { + private String key; + + public static EsSound createUnchecked(String key) { + EsSound type = new EsSound(); + type.key = key.toLowerCase(); + + return type; + } + + public static @Nullable EsSound fromKey(String key) { + EsSound type = EsSound.createUnchecked(key); + + if (Main.server.getSounds().contains(type)) { + return type; + } + + return null; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof EsSound) { + return ((EsSound) obj).getKey().equals(key); + } + + return false; + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public String toString() { + return Utils.keyToDisplayName(key); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 5d8db0d..d32ec30 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -3,11 +3,11 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSound; import net.serble.estools.ServerApi.EsSoundCategory; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; -import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; @@ -121,12 +121,12 @@ public boolean isFlying() { } @Override - public void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { + public void playSound(EsSound sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { if (Main.minecraftVersion.getMinor() < 11) { - bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), volume, pitch); + bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), BukkitHelper.toBukkitSound(sound), volume, pitch); return; } - bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), Sound.valueOf(sound), BukkitHelper.toBukkitSoundCategory(category), volume, pitch); + bukkitPlayer.playSound(BukkitHelper.toBukkitLocation(loc), BukkitHelper.toBukkitSound(sound), BukkitHelper.toBukkitSoundCategory(category), volume, pitch); } @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 1e4e282..a89e2c8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -6,6 +6,7 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitSoundEnumConverter; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; @@ -27,6 +28,7 @@ public class BukkitServer implements EsServer { private final BukkitEventsListener listener; private static final Set materials = new HashSet<>(); private static final Set itemMaterials = new HashSet<>(); + private static final Set sounds = new HashSet<>(); public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; @@ -51,6 +53,18 @@ public void initialise() { materials.add(esMat); } + + if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + for (Sound sound : Registry.SOUNDS) { + EsSound esSound = EsSound.createUnchecked(sound.getKey().getKey()); + sounds.add(esSound); + } + } else { + for (Sound sound : Sound.values()) { + EsSound esSound = BukkitSoundEnumConverter.convertEnumToKey(sound.name()); + sounds.add(esSound); + } + } } @Override @@ -215,13 +229,8 @@ public Set getEnchantments() { } @Override - public String[] getSounds() { - Sound[] sounds = Sound.values(); - String[] strSounds = new String[sounds.length]; - for (int i = 0; i < sounds.length; i++) { - strSounds[i] = sounds[i].name(); - } - return strSounds; + public Set getSounds() { + return sounds; } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java index 426c3ac..4d3fc85 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java @@ -85,6 +85,10 @@ public static void load() { } } else { for (PotionEffectType type : PotionEffectType.values()) { + if (type == null) { + continue; + } + String name; if (Main.minecraftVersion.getMinor() >= 18) { name = type.getKey().getKey(); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 9949c20..c66abab 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Entrypoints.EsToolsBukkit; +import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.*; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.*; @@ -52,6 +53,22 @@ public static EsLocation fromBukkitLocation(Location loc) { loc.getPitch()); } + public static Sound toBukkitSound(EsSound sound) { + if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + return Registry.SOUNDS.get(NamespacedKey.minecraft(sound.getKey())); + } else { + return Sound.valueOf(BukkitSoundEnumConverter.convertKeyToEnum(sound)); + } + } + + public static EsSound fromBukkitSound(Sound sound) { + if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + return EsSound.createUnchecked(sound.getKey().getKey()); + } else { + return BukkitSoundEnumConverter.convertEnumToKey(sound.name()); + } + } + public static Position fromVector(Vector vec) { return new Position(vec.getX(), vec.getY(), vec.getZ()); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java new file mode 100644 index 0000000..9b5f270 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java @@ -0,0 +1,1053 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsSound; + +/** + * This class converts the Sound.name() to an EsSound, EsSounds use the sound key and Sound.name() is a semi arbitrary + * name, since there is no way to differenciate between underscores and dots in Sound.name() names, + * I generated a switch statement that converts between them. + *

+ * This was generated from minecraft 1.16.5, newer versions can use Sound.getKey().getKey() to get this info. + *

+ * Unfortunately, some old keys will be different, but i made Records convert to the new types. + */ +public class BukkitSoundEnumConverter { + public static String convertKeyToEnum(EsSound sound) { + String key = sound.getKey(); + + // music discs used ot be called records + if (Main.minecraftVersion.getMinor() <= 12) { + switch (key) { + case "music_disc.11": return "RECORD_11"; + case "music_disc.13": return "RECORD_13"; + case "music_disc.blocks": return "RECORD_BLOCKS"; + case "music_disc.cat": return "RECORD_CAT"; + case "music_disc.chirp": return "RECORD_CHIRP"; + case "music_disc.far": return "RECORD_FAR"; + case "music_disc.mall": return "RECORD_MALL"; + case "music_disc.mellohi": return "RECORD_MELLOHI"; + case "music_disc.stal": return "RECORD_STAL"; + case "music_disc.strad": return "RECORD_STRAD"; + case "music_disc.wait": return "RECORD_WAIT"; + case "music_disc.ward": return "RECORD_WARD"; + } + } + + return key.toUpperCase().replace('.', '_'); + } + + public static EsSound convertEnumToKey(String eVal) { + switch (eVal) { + case "AMBIENT_BASALT_DELTAS_ADDITIONS": return EsSound.createUnchecked("ambient.basalt_deltas.additions"); + case "AMBIENT_BASALT_DELTAS_LOOP": return EsSound.createUnchecked("ambient.basalt_deltas.loop"); + case "AMBIENT_BASALT_DELTAS_MOOD": return EsSound.createUnchecked("ambient.basalt_deltas.mood"); + case "AMBIENT_CAVE": return EsSound.createUnchecked("ambient.cave"); + case "AMBIENT_CRIMSON_FOREST_ADDITIONS": return EsSound.createUnchecked("ambient.crimson_forest.additions"); + case "AMBIENT_CRIMSON_FOREST_LOOP": return EsSound.createUnchecked("ambient.crimson_forest.loop"); + case "AMBIENT_CRIMSON_FOREST_MOOD": return EsSound.createUnchecked("ambient.crimson_forest.mood"); + case "AMBIENT_NETHER_WASTES_ADDITIONS": return EsSound.createUnchecked("ambient.nether_wastes.additions"); + case "AMBIENT_NETHER_WASTES_LOOP": return EsSound.createUnchecked("ambient.nether_wastes.loop"); + case "AMBIENT_NETHER_WASTES_MOOD": return EsSound.createUnchecked("ambient.nether_wastes.mood"); + case "AMBIENT_SOUL_SAND_VALLEY_ADDITIONS": return EsSound.createUnchecked("ambient.soul_sand_valley.additions"); + case "AMBIENT_SOUL_SAND_VALLEY_LOOP": return EsSound.createUnchecked("ambient.soul_sand_valley.loop"); + case "AMBIENT_SOUL_SAND_VALLEY_MOOD": return EsSound.createUnchecked("ambient.soul_sand_valley.mood"); + case "AMBIENT_UNDERWATER_ENTER": return EsSound.createUnchecked("ambient.underwater.enter"); + case "AMBIENT_UNDERWATER_EXIT": return EsSound.createUnchecked("ambient.underwater.exit"); + case "AMBIENT_UNDERWATER_LOOP": return EsSound.createUnchecked("ambient.underwater.loop"); + case "AMBIENT_UNDERWATER_LOOP_ADDITIONS": return EsSound.createUnchecked("ambient.underwater.loop.additions"); + case "AMBIENT_UNDERWATER_LOOP_ADDITIONS_RARE": return EsSound.createUnchecked("ambient.underwater.loop.additions.rare"); + case "AMBIENT_UNDERWATER_LOOP_ADDITIONS_ULTRA_RARE": return EsSound.createUnchecked("ambient.underwater.loop.additions.ultra_rare"); + case "AMBIENT_WARPED_FOREST_ADDITIONS": return EsSound.createUnchecked("ambient.warped_forest.additions"); + case "AMBIENT_WARPED_FOREST_LOOP": return EsSound.createUnchecked("ambient.warped_forest.loop"); + case "AMBIENT_WARPED_FOREST_MOOD": return EsSound.createUnchecked("ambient.warped_forest.mood"); + case "BLOCK_ANCIENT_DEBRIS_BREAK": return EsSound.createUnchecked("block.ancient_debris.break"); + case "BLOCK_ANCIENT_DEBRIS_FALL": return EsSound.createUnchecked("block.ancient_debris.fall"); + case "BLOCK_ANCIENT_DEBRIS_HIT": return EsSound.createUnchecked("block.ancient_debris.hit"); + case "BLOCK_ANCIENT_DEBRIS_PLACE": return EsSound.createUnchecked("block.ancient_debris.place"); + case "BLOCK_ANCIENT_DEBRIS_STEP": return EsSound.createUnchecked("block.ancient_debris.step"); + case "BLOCK_ANVIL_BREAK": return EsSound.createUnchecked("block.anvil.break"); + case "BLOCK_ANVIL_DESTROY": return EsSound.createUnchecked("block.anvil.destroy"); + case "BLOCK_ANVIL_FALL": return EsSound.createUnchecked("block.anvil.fall"); + case "BLOCK_ANVIL_HIT": return EsSound.createUnchecked("block.anvil.hit"); + case "BLOCK_ANVIL_LAND": return EsSound.createUnchecked("block.anvil.land"); + case "BLOCK_ANVIL_PLACE": return EsSound.createUnchecked("block.anvil.place"); + case "BLOCK_ANVIL_STEP": return EsSound.createUnchecked("block.anvil.step"); + case "BLOCK_ANVIL_USE": return EsSound.createUnchecked("block.anvil.use"); + case "BLOCK_BAMBOO_BREAK": return EsSound.createUnchecked("block.bamboo.break"); + case "BLOCK_BAMBOO_FALL": return EsSound.createUnchecked("block.bamboo.fall"); + case "BLOCK_BAMBOO_HIT": return EsSound.createUnchecked("block.bamboo.hit"); + case "BLOCK_BAMBOO_PLACE": return EsSound.createUnchecked("block.bamboo.place"); + case "BLOCK_BAMBOO_SAPLING_BREAK": return EsSound.createUnchecked("block.bamboo_sapling.break"); + case "BLOCK_BAMBOO_SAPLING_HIT": return EsSound.createUnchecked("block.bamboo_sapling.hit"); + case "BLOCK_BAMBOO_SAPLING_PLACE": return EsSound.createUnchecked("block.bamboo_sapling.place"); + case "BLOCK_BAMBOO_STEP": return EsSound.createUnchecked("block.bamboo.step"); + case "BLOCK_BARREL_CLOSE": return EsSound.createUnchecked("block.barrel.close"); + case "BLOCK_BARREL_OPEN": return EsSound.createUnchecked("block.barrel.open"); + case "BLOCK_BASALT_BREAK": return EsSound.createUnchecked("block.basalt.break"); + case "BLOCK_BASALT_FALL": return EsSound.createUnchecked("block.basalt.fall"); + case "BLOCK_BASALT_HIT": return EsSound.createUnchecked("block.basalt.hit"); + case "BLOCK_BASALT_PLACE": return EsSound.createUnchecked("block.basalt.place"); + case "BLOCK_BASALT_STEP": return EsSound.createUnchecked("block.basalt.step"); + case "BLOCK_BEACON_ACTIVATE": return EsSound.createUnchecked("block.beacon.activate"); + case "BLOCK_BEACON_AMBIENT": return EsSound.createUnchecked("block.beacon.ambient"); + case "BLOCK_BEACON_DEACTIVATE": return EsSound.createUnchecked("block.beacon.deactivate"); + case "BLOCK_BEACON_POWER_SELECT": return EsSound.createUnchecked("block.beacon.power_select"); + case "BLOCK_BEEHIVE_DRIP": return EsSound.createUnchecked("block.beehive.drip"); + case "BLOCK_BEEHIVE_ENTER": return EsSound.createUnchecked("block.beehive.enter"); + case "BLOCK_BEEHIVE_EXIT": return EsSound.createUnchecked("block.beehive.exit"); + case "BLOCK_BEEHIVE_SHEAR": return EsSound.createUnchecked("block.beehive.shear"); + case "BLOCK_BEEHIVE_WORK": return EsSound.createUnchecked("block.beehive.work"); + case "BLOCK_BELL_RESONATE": return EsSound.createUnchecked("block.bell.resonate"); + case "BLOCK_BELL_USE": return EsSound.createUnchecked("block.bell.use"); + case "BLOCK_BLASTFURNACE_FIRE_CRACKLE": return EsSound.createUnchecked("block.blastfurnace.fire_crackle"); + case "BLOCK_BONE_BLOCK_BREAK": return EsSound.createUnchecked("block.bone_block.break"); + case "BLOCK_BONE_BLOCK_FALL": return EsSound.createUnchecked("block.bone_block.fall"); + case "BLOCK_BONE_BLOCK_HIT": return EsSound.createUnchecked("block.bone_block.hit"); + case "BLOCK_BONE_BLOCK_PLACE": return EsSound.createUnchecked("block.bone_block.place"); + case "BLOCK_BONE_BLOCK_STEP": return EsSound.createUnchecked("block.bone_block.step"); + case "BLOCK_BREWING_STAND_BREW": return EsSound.createUnchecked("block.brewing_stand.brew"); + case "BLOCK_BUBBLE_COLUMN_BUBBLE_POP": return EsSound.createUnchecked("block.bubble_column.bubble_pop"); + case "BLOCK_BUBBLE_COLUMN_UPWARDS_AMBIENT": return EsSound.createUnchecked("block.bubble_column.upwards_ambient"); + case "BLOCK_BUBBLE_COLUMN_UPWARDS_INSIDE": return EsSound.createUnchecked("block.bubble_column.upwards_inside"); + case "BLOCK_BUBBLE_COLUMN_WHIRLPOOL_AMBIENT": return EsSound.createUnchecked("block.bubble_column.whirlpool_ambient"); + case "BLOCK_BUBBLE_COLUMN_WHIRLPOOL_INSIDE": return EsSound.createUnchecked("block.bubble_column.whirlpool_inside"); + case "BLOCK_CAMPFIRE_CRACKLE": return EsSound.createUnchecked("block.campfire.crackle"); + case "BLOCK_CHAIN_BREAK": return EsSound.createUnchecked("block.chain.break"); + case "BLOCK_CHAIN_FALL": return EsSound.createUnchecked("block.chain.fall"); + case "BLOCK_CHAIN_HIT": return EsSound.createUnchecked("block.chain.hit"); + case "BLOCK_CHAIN_PLACE": return EsSound.createUnchecked("block.chain.place"); + case "BLOCK_CHAIN_STEP": return EsSound.createUnchecked("block.chain.step"); + case "BLOCK_CHEST_CLOSE": return EsSound.createUnchecked("block.chest.close"); + case "BLOCK_CHEST_LOCKED": return EsSound.createUnchecked("block.chest.locked"); + case "BLOCK_CHEST_OPEN": return EsSound.createUnchecked("block.chest.open"); + case "BLOCK_CHORUS_FLOWER_DEATH": return EsSound.createUnchecked("block.chorus_flower.death"); + case "BLOCK_CHORUS_FLOWER_GROW": return EsSound.createUnchecked("block.chorus_flower.grow"); + case "BLOCK_COMPARATOR_CLICK": return EsSound.createUnchecked("block.comparator.click"); + case "BLOCK_COMPOSTER_EMPTY": return EsSound.createUnchecked("block.composter.empty"); + case "BLOCK_COMPOSTER_FILL": return EsSound.createUnchecked("block.composter.fill"); + case "BLOCK_COMPOSTER_FILL_SUCCESS": return EsSound.createUnchecked("block.composter.fill_success"); + case "BLOCK_COMPOSTER_READY": return EsSound.createUnchecked("block.composter.ready"); + case "BLOCK_CONDUIT_ACTIVATE": return EsSound.createUnchecked("block.conduit.activate"); + case "BLOCK_CONDUIT_AMBIENT": return EsSound.createUnchecked("block.conduit.ambient"); + case "BLOCK_CONDUIT_AMBIENT_SHORT": return EsSound.createUnchecked("block.conduit.ambient.short"); + case "BLOCK_CONDUIT_ATTACK_TARGET": return EsSound.createUnchecked("block.conduit.attack.target"); + case "BLOCK_CONDUIT_DEACTIVATE": return EsSound.createUnchecked("block.conduit.deactivate"); + case "BLOCK_CORAL_BLOCK_BREAK": return EsSound.createUnchecked("block.coral_block.break"); + case "BLOCK_CORAL_BLOCK_FALL": return EsSound.createUnchecked("block.coral_block.fall"); + case "BLOCK_CORAL_BLOCK_HIT": return EsSound.createUnchecked("block.coral_block.hit"); + case "BLOCK_CORAL_BLOCK_PLACE": return EsSound.createUnchecked("block.coral_block.place"); + case "BLOCK_CORAL_BLOCK_STEP": return EsSound.createUnchecked("block.coral_block.step"); + case "BLOCK_CROP_BREAK": return EsSound.createUnchecked("block.crop.break"); + case "BLOCK_DISPENSER_DISPENSE": return EsSound.createUnchecked("block.dispenser.dispense"); + case "BLOCK_DISPENSER_FAIL": return EsSound.createUnchecked("block.dispenser.fail"); + case "BLOCK_DISPENSER_LAUNCH": return EsSound.createUnchecked("block.dispenser.launch"); + case "BLOCK_ENCHANTMENT_TABLE_USE": return EsSound.createUnchecked("block.enchantment_table.use"); + case "BLOCK_ENDER_CHEST_CLOSE": return EsSound.createUnchecked("block.ender_chest.close"); + case "BLOCK_ENDER_CHEST_OPEN": return EsSound.createUnchecked("block.ender_chest.open"); + case "BLOCK_END_GATEWAY_SPAWN": return EsSound.createUnchecked("block.end_gateway.spawn"); + case "BLOCK_END_PORTAL_FRAME_FILL": return EsSound.createUnchecked("block.end_portal_frame.fill"); + case "BLOCK_END_PORTAL_SPAWN": return EsSound.createUnchecked("block.end_portal.spawn"); + case "BLOCK_FENCE_GATE_CLOSE": return EsSound.createUnchecked("block.fence_gate.close"); + case "BLOCK_FENCE_GATE_OPEN": return EsSound.createUnchecked("block.fence_gate.open"); + case "BLOCK_FIRE_AMBIENT": return EsSound.createUnchecked("block.fire.ambient"); + case "BLOCK_FIRE_EXTINGUISH": return EsSound.createUnchecked("block.fire.extinguish"); + case "BLOCK_FUNGUS_BREAK": return EsSound.createUnchecked("block.fungus.break"); + case "BLOCK_FUNGUS_FALL": return EsSound.createUnchecked("block.fungus.fall"); + case "BLOCK_FUNGUS_HIT": return EsSound.createUnchecked("block.fungus.hit"); + case "BLOCK_FUNGUS_PLACE": return EsSound.createUnchecked("block.fungus.place"); + case "BLOCK_FUNGUS_STEP": return EsSound.createUnchecked("block.fungus.step"); + case "BLOCK_FURNACE_FIRE_CRACKLE": return EsSound.createUnchecked("block.furnace.fire_crackle"); + case "BLOCK_GILDED_BLACKSTONE_BREAK": return EsSound.createUnchecked("block.gilded_blackstone.break"); + case "BLOCK_GILDED_BLACKSTONE_FALL": return EsSound.createUnchecked("block.gilded_blackstone.fall"); + case "BLOCK_GILDED_BLACKSTONE_HIT": return EsSound.createUnchecked("block.gilded_blackstone.hit"); + case "BLOCK_GILDED_BLACKSTONE_PLACE": return EsSound.createUnchecked("block.gilded_blackstone.place"); + case "BLOCK_GILDED_BLACKSTONE_STEP": return EsSound.createUnchecked("block.gilded_blackstone.step"); + case "BLOCK_GLASS_BREAK": return EsSound.createUnchecked("block.glass.break"); + case "BLOCK_GLASS_FALL": return EsSound.createUnchecked("block.glass.fall"); + case "BLOCK_GLASS_HIT": return EsSound.createUnchecked("block.glass.hit"); + case "BLOCK_GLASS_PLACE": return EsSound.createUnchecked("block.glass.place"); + case "BLOCK_GLASS_STEP": return EsSound.createUnchecked("block.glass.step"); + case "BLOCK_GRASS_BREAK": return EsSound.createUnchecked("block.grass.break"); + case "BLOCK_GRASS_FALL": return EsSound.createUnchecked("block.grass.fall"); + case "BLOCK_GRASS_HIT": return EsSound.createUnchecked("block.grass.hit"); + case "BLOCK_GRASS_PLACE": return EsSound.createUnchecked("block.grass.place"); + case "BLOCK_GRASS_STEP": return EsSound.createUnchecked("block.grass.step"); + case "BLOCK_GRAVEL_BREAK": return EsSound.createUnchecked("block.gravel.break"); + case "BLOCK_GRAVEL_FALL": return EsSound.createUnchecked("block.gravel.fall"); + case "BLOCK_GRAVEL_HIT": return EsSound.createUnchecked("block.gravel.hit"); + case "BLOCK_GRAVEL_PLACE": return EsSound.createUnchecked("block.gravel.place"); + case "BLOCK_GRAVEL_STEP": return EsSound.createUnchecked("block.gravel.step"); + case "BLOCK_GRINDSTONE_USE": return EsSound.createUnchecked("block.grindstone.use"); + case "BLOCK_HONEY_BLOCK_BREAK": return EsSound.createUnchecked("block.honey_block.break"); + case "BLOCK_HONEY_BLOCK_FALL": return EsSound.createUnchecked("block.honey_block.fall"); + case "BLOCK_HONEY_BLOCK_HIT": return EsSound.createUnchecked("block.honey_block.hit"); + case "BLOCK_HONEY_BLOCK_PLACE": return EsSound.createUnchecked("block.honey_block.place"); + case "BLOCK_HONEY_BLOCK_SLIDE": return EsSound.createUnchecked("block.honey_block.slide"); + case "BLOCK_HONEY_BLOCK_STEP": return EsSound.createUnchecked("block.honey_block.step"); + case "BLOCK_IRON_DOOR_CLOSE": return EsSound.createUnchecked("block.iron_door.close"); + case "BLOCK_IRON_DOOR_OPEN": return EsSound.createUnchecked("block.iron_door.open"); + case "BLOCK_IRON_TRAPDOOR_CLOSE": return EsSound.createUnchecked("block.iron_trapdoor.close"); + case "BLOCK_IRON_TRAPDOOR_OPEN": return EsSound.createUnchecked("block.iron_trapdoor.open"); + case "BLOCK_LADDER_BREAK": return EsSound.createUnchecked("block.ladder.break"); + case "BLOCK_LADDER_FALL": return EsSound.createUnchecked("block.ladder.fall"); + case "BLOCK_LADDER_HIT": return EsSound.createUnchecked("block.ladder.hit"); + case "BLOCK_LADDER_PLACE": return EsSound.createUnchecked("block.ladder.place"); + case "BLOCK_LADDER_STEP": return EsSound.createUnchecked("block.ladder.step"); + case "BLOCK_LANTERN_BREAK": return EsSound.createUnchecked("block.lantern.break"); + case "BLOCK_LANTERN_FALL": return EsSound.createUnchecked("block.lantern.fall"); + case "BLOCK_LANTERN_HIT": return EsSound.createUnchecked("block.lantern.hit"); + case "BLOCK_LANTERN_PLACE": return EsSound.createUnchecked("block.lantern.place"); + case "BLOCK_LANTERN_STEP": return EsSound.createUnchecked("block.lantern.step"); + case "BLOCK_LAVA_AMBIENT": return EsSound.createUnchecked("block.lava.ambient"); + case "BLOCK_LAVA_EXTINGUISH": return EsSound.createUnchecked("block.lava.extinguish"); + case "BLOCK_LAVA_POP": return EsSound.createUnchecked("block.lava.pop"); + case "BLOCK_LEVER_CLICK": return EsSound.createUnchecked("block.lever.click"); + case "BLOCK_LILY_PAD_PLACE": return EsSound.createUnchecked("block.lily_pad.place"); + case "BLOCK_LODESTONE_BREAK": return EsSound.createUnchecked("block.lodestone.break"); + case "BLOCK_LODESTONE_FALL": return EsSound.createUnchecked("block.lodestone.fall"); + case "BLOCK_LODESTONE_HIT": return EsSound.createUnchecked("block.lodestone.hit"); + case "BLOCK_LODESTONE_PLACE": return EsSound.createUnchecked("block.lodestone.place"); + case "BLOCK_LODESTONE_STEP": return EsSound.createUnchecked("block.lodestone.step"); + case "BLOCK_METAL_BREAK": return EsSound.createUnchecked("block.metal.break"); + case "BLOCK_METAL_FALL": return EsSound.createUnchecked("block.metal.fall"); + case "BLOCK_METAL_HIT": return EsSound.createUnchecked("block.metal.hit"); + case "BLOCK_METAL_PLACE": return EsSound.createUnchecked("block.metal.place"); + case "BLOCK_METAL_PRESSURE_PLATE_CLICK_OFF": return EsSound.createUnchecked("block.metal_pressure_plate.click_off"); + case "BLOCK_METAL_PRESSURE_PLATE_CLICK_ON": return EsSound.createUnchecked("block.metal_pressure_plate.click_on"); + case "BLOCK_METAL_STEP": return EsSound.createUnchecked("block.metal.step"); + case "BLOCK_NETHERITE_BLOCK_BREAK": return EsSound.createUnchecked("block.netherite_block.break"); + case "BLOCK_NETHERITE_BLOCK_FALL": return EsSound.createUnchecked("block.netherite_block.fall"); + case "BLOCK_NETHERITE_BLOCK_HIT": return EsSound.createUnchecked("block.netherite_block.hit"); + case "BLOCK_NETHERITE_BLOCK_PLACE": return EsSound.createUnchecked("block.netherite_block.place"); + case "BLOCK_NETHERITE_BLOCK_STEP": return EsSound.createUnchecked("block.netherite_block.step"); + case "BLOCK_NETHERRACK_BREAK": return EsSound.createUnchecked("block.netherrack.break"); + case "BLOCK_NETHERRACK_FALL": return EsSound.createUnchecked("block.netherrack.fall"); + case "BLOCK_NETHERRACK_HIT": return EsSound.createUnchecked("block.netherrack.hit"); + case "BLOCK_NETHERRACK_PLACE": return EsSound.createUnchecked("block.netherrack.place"); + case "BLOCK_NETHERRACK_STEP": return EsSound.createUnchecked("block.netherrack.step"); + case "BLOCK_NETHER_BRICKS_BREAK": return EsSound.createUnchecked("block.nether_bricks.break"); + case "BLOCK_NETHER_BRICKS_FALL": return EsSound.createUnchecked("block.nether_bricks.fall"); + case "BLOCK_NETHER_BRICKS_HIT": return EsSound.createUnchecked("block.nether_bricks.hit"); + case "BLOCK_NETHER_BRICKS_PLACE": return EsSound.createUnchecked("block.nether_bricks.place"); + case "BLOCK_NETHER_BRICKS_STEP": return EsSound.createUnchecked("block.nether_bricks.step"); + case "BLOCK_NETHER_GOLD_ORE_BREAK": return EsSound.createUnchecked("block.nether_gold_ore.break"); + case "BLOCK_NETHER_GOLD_ORE_FALL": return EsSound.createUnchecked("block.nether_gold_ore.fall"); + case "BLOCK_NETHER_GOLD_ORE_HIT": return EsSound.createUnchecked("block.nether_gold_ore.hit"); + case "BLOCK_NETHER_GOLD_ORE_PLACE": return EsSound.createUnchecked("block.nether_gold_ore.place"); + case "BLOCK_NETHER_GOLD_ORE_STEP": return EsSound.createUnchecked("block.nether_gold_ore.step"); + case "BLOCK_NETHER_ORE_BREAK": return EsSound.createUnchecked("block.nether_ore.break"); + case "BLOCK_NETHER_ORE_FALL": return EsSound.createUnchecked("block.nether_ore.fall"); + case "BLOCK_NETHER_ORE_HIT": return EsSound.createUnchecked("block.nether_ore.hit"); + case "BLOCK_NETHER_ORE_PLACE": return EsSound.createUnchecked("block.nether_ore.place"); + case "BLOCK_NETHER_ORE_STEP": return EsSound.createUnchecked("block.nether_ore.step"); + case "BLOCK_NETHER_SPROUTS_BREAK": return EsSound.createUnchecked("block.nether_sprouts.break"); + case "BLOCK_NETHER_SPROUTS_FALL": return EsSound.createUnchecked("block.nether_sprouts.fall"); + case "BLOCK_NETHER_SPROUTS_HIT": return EsSound.createUnchecked("block.nether_sprouts.hit"); + case "BLOCK_NETHER_SPROUTS_PLACE": return EsSound.createUnchecked("block.nether_sprouts.place"); + case "BLOCK_NETHER_SPROUTS_STEP": return EsSound.createUnchecked("block.nether_sprouts.step"); + case "BLOCK_NETHER_WART_BREAK": return EsSound.createUnchecked("block.nether_wart.break"); + case "BLOCK_NOTE_BLOCK_BANJO": return EsSound.createUnchecked("block.note_block.banjo"); + case "BLOCK_NOTE_BLOCK_BASEDRUM": return EsSound.createUnchecked("block.note_block.basedrum"); + case "BLOCK_NOTE_BLOCK_BASS": return EsSound.createUnchecked("block.note_block.bass"); + case "BLOCK_NOTE_BLOCK_BELL": return EsSound.createUnchecked("block.note_block.bell"); + case "BLOCK_NOTE_BLOCK_BIT": return EsSound.createUnchecked("block.note_block.bit"); + case "BLOCK_NOTE_BLOCK_CHIME": return EsSound.createUnchecked("block.note_block.chime"); + case "BLOCK_NOTE_BLOCK_COW_BELL": return EsSound.createUnchecked("block.note_block.cow_bell"); + case "BLOCK_NOTE_BLOCK_DIDGERIDOO": return EsSound.createUnchecked("block.note_block.didgeridoo"); + case "BLOCK_NOTE_BLOCK_FLUTE": return EsSound.createUnchecked("block.note_block.flute"); + case "BLOCK_NOTE_BLOCK_GUITAR": return EsSound.createUnchecked("block.note_block.guitar"); + case "BLOCK_NOTE_BLOCK_HARP": return EsSound.createUnchecked("block.note_block.harp"); + case "BLOCK_NOTE_BLOCK_HAT": return EsSound.createUnchecked("block.note_block.hat"); + case "BLOCK_NOTE_BLOCK_IRON_XYLOPHONE": return EsSound.createUnchecked("block.note_block.iron_xylophone"); + case "BLOCK_NOTE_BLOCK_PLING": return EsSound.createUnchecked("block.note_block.pling"); + case "BLOCK_NOTE_BLOCK_SNARE": return EsSound.createUnchecked("block.note_block.snare"); + case "BLOCK_NOTE_BLOCK_XYLOPHONE": return EsSound.createUnchecked("block.note_block.xylophone"); + case "BLOCK_NYLIUM_BREAK": return EsSound.createUnchecked("block.nylium.break"); + case "BLOCK_NYLIUM_FALL": return EsSound.createUnchecked("block.nylium.fall"); + case "BLOCK_NYLIUM_HIT": return EsSound.createUnchecked("block.nylium.hit"); + case "BLOCK_NYLIUM_PLACE": return EsSound.createUnchecked("block.nylium.place"); + case "BLOCK_NYLIUM_STEP": return EsSound.createUnchecked("block.nylium.step"); + case "BLOCK_PISTON_CONTRACT": return EsSound.createUnchecked("block.piston.contract"); + case "BLOCK_PISTON_EXTEND": return EsSound.createUnchecked("block.piston.extend"); + case "BLOCK_PORTAL_AMBIENT": return EsSound.createUnchecked("block.portal.ambient"); + case "BLOCK_PORTAL_TRAVEL": return EsSound.createUnchecked("block.portal.travel"); + case "BLOCK_PORTAL_TRIGGER": return EsSound.createUnchecked("block.portal.trigger"); + case "BLOCK_PUMPKIN_CARVE": return EsSound.createUnchecked("block.pumpkin.carve"); + case "BLOCK_REDSTONE_TORCH_BURNOUT": return EsSound.createUnchecked("block.redstone_torch.burnout"); + case "BLOCK_RESPAWN_ANCHOR_AMBIENT": return EsSound.createUnchecked("block.respawn_anchor.ambient"); + case "BLOCK_RESPAWN_ANCHOR_CHARGE": return EsSound.createUnchecked("block.respawn_anchor.charge"); + case "BLOCK_RESPAWN_ANCHOR_DEPLETE": return EsSound.createUnchecked("block.respawn_anchor.deplete"); + case "BLOCK_RESPAWN_ANCHOR_SET_SPAWN": return EsSound.createUnchecked("block.respawn_anchor.set_spawn"); + case "BLOCK_ROOTS_BREAK": return EsSound.createUnchecked("block.roots.break"); + case "BLOCK_ROOTS_FALL": return EsSound.createUnchecked("block.roots.fall"); + case "BLOCK_ROOTS_HIT": return EsSound.createUnchecked("block.roots.hit"); + case "BLOCK_ROOTS_PLACE": return EsSound.createUnchecked("block.roots.place"); + case "BLOCK_ROOTS_STEP": return EsSound.createUnchecked("block.roots.step"); + case "BLOCK_SAND_BREAK": return EsSound.createUnchecked("block.sand.break"); + case "BLOCK_SAND_FALL": return EsSound.createUnchecked("block.sand.fall"); + case "BLOCK_SAND_HIT": return EsSound.createUnchecked("block.sand.hit"); + case "BLOCK_SAND_PLACE": return EsSound.createUnchecked("block.sand.place"); + case "BLOCK_SAND_STEP": return EsSound.createUnchecked("block.sand.step"); + case "BLOCK_SCAFFOLDING_BREAK": return EsSound.createUnchecked("block.scaffolding.break"); + case "BLOCK_SCAFFOLDING_FALL": return EsSound.createUnchecked("block.scaffolding.fall"); + case "BLOCK_SCAFFOLDING_HIT": return EsSound.createUnchecked("block.scaffolding.hit"); + case "BLOCK_SCAFFOLDING_PLACE": return EsSound.createUnchecked("block.scaffolding.place"); + case "BLOCK_SCAFFOLDING_STEP": return EsSound.createUnchecked("block.scaffolding.step"); + case "BLOCK_SHROOMLIGHT_BREAK": return EsSound.createUnchecked("block.shroomlight.break"); + case "BLOCK_SHROOMLIGHT_FALL": return EsSound.createUnchecked("block.shroomlight.fall"); + case "BLOCK_SHROOMLIGHT_HIT": return EsSound.createUnchecked("block.shroomlight.hit"); + case "BLOCK_SHROOMLIGHT_PLACE": return EsSound.createUnchecked("block.shroomlight.place"); + case "BLOCK_SHROOMLIGHT_STEP": return EsSound.createUnchecked("block.shroomlight.step"); + case "BLOCK_SHULKER_BOX_CLOSE": return EsSound.createUnchecked("block.shulker_box.close"); + case "BLOCK_SHULKER_BOX_OPEN": return EsSound.createUnchecked("block.shulker_box.open"); + case "BLOCK_SLIME_BLOCK_BREAK": return EsSound.createUnchecked("block.slime_block.break"); + case "BLOCK_SLIME_BLOCK_FALL": return EsSound.createUnchecked("block.slime_block.fall"); + case "BLOCK_SLIME_BLOCK_HIT": return EsSound.createUnchecked("block.slime_block.hit"); + case "BLOCK_SLIME_BLOCK_PLACE": return EsSound.createUnchecked("block.slime_block.place"); + case "BLOCK_SLIME_BLOCK_STEP": return EsSound.createUnchecked("block.slime_block.step"); + case "BLOCK_SMITHING_TABLE_USE": return EsSound.createUnchecked("block.smithing_table.use"); + case "BLOCK_SMOKER_SMOKE": return EsSound.createUnchecked("block.smoker.smoke"); + case "BLOCK_SNOW_BREAK": return EsSound.createUnchecked("block.snow.break"); + case "BLOCK_SNOW_FALL": return EsSound.createUnchecked("block.snow.fall"); + case "BLOCK_SNOW_HIT": return EsSound.createUnchecked("block.snow.hit"); + case "BLOCK_SNOW_PLACE": return EsSound.createUnchecked("block.snow.place"); + case "BLOCK_SNOW_STEP": return EsSound.createUnchecked("block.snow.step"); + case "BLOCK_SOUL_SAND_BREAK": return EsSound.createUnchecked("block.soul_sand.break"); + case "BLOCK_SOUL_SAND_FALL": return EsSound.createUnchecked("block.soul_sand.fall"); + case "BLOCK_SOUL_SAND_HIT": return EsSound.createUnchecked("block.soul_sand.hit"); + case "BLOCK_SOUL_SAND_PLACE": return EsSound.createUnchecked("block.soul_sand.place"); + case "BLOCK_SOUL_SAND_STEP": return EsSound.createUnchecked("block.soul_sand.step"); + case "BLOCK_SOUL_SOIL_BREAK": return EsSound.createUnchecked("block.soul_soil.break"); + case "BLOCK_SOUL_SOIL_FALL": return EsSound.createUnchecked("block.soul_soil.fall"); + case "BLOCK_SOUL_SOIL_HIT": return EsSound.createUnchecked("block.soul_soil.hit"); + case "BLOCK_SOUL_SOIL_PLACE": return EsSound.createUnchecked("block.soul_soil.place"); + case "BLOCK_SOUL_SOIL_STEP": return EsSound.createUnchecked("block.soul_soil.step"); + case "BLOCK_STEM_BREAK": return EsSound.createUnchecked("block.stem.break"); + case "BLOCK_STEM_FALL": return EsSound.createUnchecked("block.stem.fall"); + case "BLOCK_STEM_HIT": return EsSound.createUnchecked("block.stem.hit"); + case "BLOCK_STEM_PLACE": return EsSound.createUnchecked("block.stem.place"); + case "BLOCK_STEM_STEP": return EsSound.createUnchecked("block.stem.step"); + case "BLOCK_STONE_BREAK": return EsSound.createUnchecked("block.stone.break"); + case "BLOCK_STONE_BUTTON_CLICK_OFF": return EsSound.createUnchecked("block.stone_button.click_off"); + case "BLOCK_STONE_BUTTON_CLICK_ON": return EsSound.createUnchecked("block.stone_button.click_on"); + case "BLOCK_STONE_FALL": return EsSound.createUnchecked("block.stone.fall"); + case "BLOCK_STONE_HIT": return EsSound.createUnchecked("block.stone.hit"); + case "BLOCK_STONE_PLACE": return EsSound.createUnchecked("block.stone.place"); + case "BLOCK_STONE_PRESSURE_PLATE_CLICK_OFF": return EsSound.createUnchecked("block.stone_pressure_plate.click_off"); + case "BLOCK_STONE_PRESSURE_PLATE_CLICK_ON": return EsSound.createUnchecked("block.stone_pressure_plate.click_on"); + case "BLOCK_STONE_STEP": return EsSound.createUnchecked("block.stone.step"); + case "BLOCK_SWEET_BERRY_BUSH_BREAK": return EsSound.createUnchecked("block.sweet_berry_bush.break"); + case "BLOCK_SWEET_BERRY_BUSH_PLACE": return EsSound.createUnchecked("block.sweet_berry_bush.place"); + case "BLOCK_TRIPWIRE_ATTACH": return EsSound.createUnchecked("block.tripwire.attach"); + case "BLOCK_TRIPWIRE_CLICK_OFF": return EsSound.createUnchecked("block.tripwire.click_off"); + case "BLOCK_TRIPWIRE_CLICK_ON": return EsSound.createUnchecked("block.tripwire.click_on"); + case "BLOCK_TRIPWIRE_DETACH": return EsSound.createUnchecked("block.tripwire.detach"); + case "BLOCK_VINE_STEP": return EsSound.createUnchecked("block.vine.step"); + case "BLOCK_WART_BLOCK_BREAK": return EsSound.createUnchecked("block.wart_block.break"); + case "BLOCK_WART_BLOCK_FALL": return EsSound.createUnchecked("block.wart_block.fall"); + case "BLOCK_WART_BLOCK_HIT": return EsSound.createUnchecked("block.wart_block.hit"); + case "BLOCK_WART_BLOCK_PLACE": return EsSound.createUnchecked("block.wart_block.place"); + case "BLOCK_WART_BLOCK_STEP": return EsSound.createUnchecked("block.wart_block.step"); + case "BLOCK_WATER_AMBIENT": return EsSound.createUnchecked("block.water.ambient"); + case "BLOCK_WEEPING_VINES_BREAK": return EsSound.createUnchecked("block.weeping_vines.break"); + case "BLOCK_WEEPING_VINES_FALL": return EsSound.createUnchecked("block.weeping_vines.fall"); + case "BLOCK_WEEPING_VINES_HIT": return EsSound.createUnchecked("block.weeping_vines.hit"); + case "BLOCK_WEEPING_VINES_PLACE": return EsSound.createUnchecked("block.weeping_vines.place"); + case "BLOCK_WEEPING_VINES_STEP": return EsSound.createUnchecked("block.weeping_vines.step"); + case "BLOCK_WET_GRASS_BREAK": return EsSound.createUnchecked("block.wet_grass.break"); + case "BLOCK_WET_GRASS_FALL": return EsSound.createUnchecked("block.wet_grass.fall"); + case "BLOCK_WET_GRASS_HIT": return EsSound.createUnchecked("block.wet_grass.hit"); + case "BLOCK_WET_GRASS_PLACE": return EsSound.createUnchecked("block.wet_grass.place"); + case "BLOCK_WET_GRASS_STEP": return EsSound.createUnchecked("block.wet_grass.step"); + case "BLOCK_WOODEN_BUTTON_CLICK_OFF": return EsSound.createUnchecked("block.wooden_button.click_off"); + case "BLOCK_WOODEN_BUTTON_CLICK_ON": return EsSound.createUnchecked("block.wooden_button.click_on"); + case "BLOCK_WOODEN_DOOR_CLOSE": return EsSound.createUnchecked("block.wooden_door.close"); + case "BLOCK_WOODEN_DOOR_OPEN": return EsSound.createUnchecked("block.wooden_door.open"); + case "BLOCK_WOODEN_PRESSURE_PLATE_CLICK_OFF": return EsSound.createUnchecked("block.wooden_pressure_plate.click_off"); + case "BLOCK_WOODEN_PRESSURE_PLATE_CLICK_ON": return EsSound.createUnchecked("block.wooden_pressure_plate.click_on"); + case "BLOCK_WOODEN_TRAPDOOR_CLOSE": return EsSound.createUnchecked("block.wooden_trapdoor.close"); + case "BLOCK_WOODEN_TRAPDOOR_OPEN": return EsSound.createUnchecked("block.wooden_trapdoor.open"); + case "BLOCK_WOOD_BREAK": return EsSound.createUnchecked("block.wood.break"); + case "BLOCK_WOOD_FALL": return EsSound.createUnchecked("block.wood.fall"); + case "BLOCK_WOOD_HIT": return EsSound.createUnchecked("block.wood.hit"); + case "BLOCK_WOOD_PLACE": return EsSound.createUnchecked("block.wood.place"); + case "BLOCK_WOOD_STEP": return EsSound.createUnchecked("block.wood.step"); + case "BLOCK_WOOL_BREAK": return EsSound.createUnchecked("block.wool.break"); + case "BLOCK_WOOL_FALL": return EsSound.createUnchecked("block.wool.fall"); + case "BLOCK_WOOL_HIT": return EsSound.createUnchecked("block.wool.hit"); + case "BLOCK_WOOL_PLACE": return EsSound.createUnchecked("block.wool.place"); + case "BLOCK_WOOL_STEP": return EsSound.createUnchecked("block.wool.step"); + case "ENCHANT_THORNS_HIT": return EsSound.createUnchecked("enchant.thorns.hit"); + case "ENTITY_ARMOR_STAND_BREAK": return EsSound.createUnchecked("entity.armor_stand.break"); + case "ENTITY_ARMOR_STAND_FALL": return EsSound.createUnchecked("entity.armor_stand.fall"); + case "ENTITY_ARMOR_STAND_HIT": return EsSound.createUnchecked("entity.armor_stand.hit"); + case "ENTITY_ARMOR_STAND_PLACE": return EsSound.createUnchecked("entity.armor_stand.place"); + case "ENTITY_ARROW_HIT": return EsSound.createUnchecked("entity.arrow.hit"); + case "ENTITY_ARROW_HIT_PLAYER": return EsSound.createUnchecked("entity.arrow.hit_player"); + case "ENTITY_ARROW_SHOOT": return EsSound.createUnchecked("entity.arrow.shoot"); + case "ENTITY_BAT_AMBIENT": return EsSound.createUnchecked("entity.bat.ambient"); + case "ENTITY_BAT_DEATH": return EsSound.createUnchecked("entity.bat.death"); + case "ENTITY_BAT_HURT": return EsSound.createUnchecked("entity.bat.hurt"); + case "ENTITY_BAT_LOOP": return EsSound.createUnchecked("entity.bat.loop"); + case "ENTITY_BAT_TAKEOFF": return EsSound.createUnchecked("entity.bat.takeoff"); + case "ENTITY_BEE_DEATH": return EsSound.createUnchecked("entity.bee.death"); + case "ENTITY_BEE_HURT": return EsSound.createUnchecked("entity.bee.hurt"); + case "ENTITY_BEE_LOOP": return EsSound.createUnchecked("entity.bee.loop"); + case "ENTITY_BEE_LOOP_AGGRESSIVE": return EsSound.createUnchecked("entity.bee.loop_aggressive"); + case "ENTITY_BEE_POLLINATE": return EsSound.createUnchecked("entity.bee.pollinate"); + case "ENTITY_BEE_STING": return EsSound.createUnchecked("entity.bee.sting"); + case "ENTITY_BLAZE_AMBIENT": return EsSound.createUnchecked("entity.blaze.ambient"); + case "ENTITY_BLAZE_BURN": return EsSound.createUnchecked("entity.blaze.burn"); + case "ENTITY_BLAZE_DEATH": return EsSound.createUnchecked("entity.blaze.death"); + case "ENTITY_BLAZE_HURT": return EsSound.createUnchecked("entity.blaze.hurt"); + case "ENTITY_BLAZE_SHOOT": return EsSound.createUnchecked("entity.blaze.shoot"); + case "ENTITY_BOAT_PADDLE_LAND": return EsSound.createUnchecked("entity.boat.paddle_land"); + case "ENTITY_BOAT_PADDLE_WATER": return EsSound.createUnchecked("entity.boat.paddle_water"); + case "ENTITY_CAT_AMBIENT": return EsSound.createUnchecked("entity.cat.ambient"); + case "ENTITY_CAT_BEG_FOR_FOOD": return EsSound.createUnchecked("entity.cat.beg_for_food"); + case "ENTITY_CAT_DEATH": return EsSound.createUnchecked("entity.cat.death"); + case "ENTITY_CAT_EAT": return EsSound.createUnchecked("entity.cat.eat"); + case "ENTITY_CAT_HISS": return EsSound.createUnchecked("entity.cat.hiss"); + case "ENTITY_CAT_HURT": return EsSound.createUnchecked("entity.cat.hurt"); + case "ENTITY_CAT_PURR": return EsSound.createUnchecked("entity.cat.purr"); + case "ENTITY_CAT_PURREOW": return EsSound.createUnchecked("entity.cat.purreow"); + case "ENTITY_CAT_STRAY_AMBIENT": return EsSound.createUnchecked("entity.cat.stray_ambient"); + case "ENTITY_CHICKEN_AMBIENT": return EsSound.createUnchecked("entity.chicken.ambient"); + case "ENTITY_CHICKEN_DEATH": return EsSound.createUnchecked("entity.chicken.death"); + case "ENTITY_CHICKEN_EGG": return EsSound.createUnchecked("entity.chicken.egg"); + case "ENTITY_CHICKEN_HURT": return EsSound.createUnchecked("entity.chicken.hurt"); + case "ENTITY_CHICKEN_STEP": return EsSound.createUnchecked("entity.chicken.step"); + case "ENTITY_COD_AMBIENT": return EsSound.createUnchecked("entity.cod.ambient"); + case "ENTITY_COD_DEATH": return EsSound.createUnchecked("entity.cod.death"); + case "ENTITY_COD_FLOP": return EsSound.createUnchecked("entity.cod.flop"); + case "ENTITY_COD_HURT": return EsSound.createUnchecked("entity.cod.hurt"); + case "ENTITY_COW_AMBIENT": return EsSound.createUnchecked("entity.cow.ambient"); + case "ENTITY_COW_DEATH": return EsSound.createUnchecked("entity.cow.death"); + case "ENTITY_COW_HURT": return EsSound.createUnchecked("entity.cow.hurt"); + case "ENTITY_COW_MILK": return EsSound.createUnchecked("entity.cow.milk"); + case "ENTITY_COW_STEP": return EsSound.createUnchecked("entity.cow.step"); + case "ENTITY_CREEPER_DEATH": return EsSound.createUnchecked("entity.creeper.death"); + case "ENTITY_CREEPER_HURT": return EsSound.createUnchecked("entity.creeper.hurt"); + case "ENTITY_CREEPER_PRIMED": return EsSound.createUnchecked("entity.creeper.primed"); + case "ENTITY_DOLPHIN_AMBIENT": return EsSound.createUnchecked("entity.dolphin.ambient"); + case "ENTITY_DOLPHIN_AMBIENT_WATER": return EsSound.createUnchecked("entity.dolphin.ambient_water"); + case "ENTITY_DOLPHIN_ATTACK": return EsSound.createUnchecked("entity.dolphin.attack"); + case "ENTITY_DOLPHIN_DEATH": return EsSound.createUnchecked("entity.dolphin.death"); + case "ENTITY_DOLPHIN_EAT": return EsSound.createUnchecked("entity.dolphin.eat"); + case "ENTITY_DOLPHIN_HURT": return EsSound.createUnchecked("entity.dolphin.hurt"); + case "ENTITY_DOLPHIN_JUMP": return EsSound.createUnchecked("entity.dolphin.jump"); + case "ENTITY_DOLPHIN_PLAY": return EsSound.createUnchecked("entity.dolphin.play"); + case "ENTITY_DOLPHIN_SPLASH": return EsSound.createUnchecked("entity.dolphin.splash"); + case "ENTITY_DOLPHIN_SWIM": return EsSound.createUnchecked("entity.dolphin.swim"); + case "ENTITY_DONKEY_AMBIENT": return EsSound.createUnchecked("entity.donkey.ambient"); + case "ENTITY_DONKEY_ANGRY": return EsSound.createUnchecked("entity.donkey.angry"); + case "ENTITY_DONKEY_CHEST": return EsSound.createUnchecked("entity.donkey.chest"); + case "ENTITY_DONKEY_DEATH": return EsSound.createUnchecked("entity.donkey.death"); + case "ENTITY_DONKEY_EAT": return EsSound.createUnchecked("entity.donkey.eat"); + case "ENTITY_DONKEY_HURT": return EsSound.createUnchecked("entity.donkey.hurt"); + case "ENTITY_DRAGON_FIREBALL_EXPLODE": return EsSound.createUnchecked("entity.dragon_fireball.explode"); + case "ENTITY_DROWNED_AMBIENT": return EsSound.createUnchecked("entity.drowned.ambient"); + case "ENTITY_DROWNED_AMBIENT_WATER": return EsSound.createUnchecked("entity.drowned.ambient_water"); + case "ENTITY_DROWNED_DEATH": return EsSound.createUnchecked("entity.drowned.death"); + case "ENTITY_DROWNED_DEATH_WATER": return EsSound.createUnchecked("entity.drowned.death_water"); + case "ENTITY_DROWNED_HURT": return EsSound.createUnchecked("entity.drowned.hurt"); + case "ENTITY_DROWNED_HURT_WATER": return EsSound.createUnchecked("entity.drowned.hurt_water"); + case "ENTITY_DROWNED_SHOOT": return EsSound.createUnchecked("entity.drowned.shoot"); + case "ENTITY_DROWNED_STEP": return EsSound.createUnchecked("entity.drowned.step"); + case "ENTITY_DROWNED_SWIM": return EsSound.createUnchecked("entity.drowned.swim"); + case "ENTITY_EGG_THROW": return EsSound.createUnchecked("entity.egg.throw"); + case "ENTITY_ELDER_GUARDIAN_AMBIENT": return EsSound.createUnchecked("entity.elder_guardian.ambient"); + case "ENTITY_ELDER_GUARDIAN_AMBIENT_LAND": return EsSound.createUnchecked("entity.elder_guardian.ambient_land"); + case "ENTITY_ELDER_GUARDIAN_CURSE": return EsSound.createUnchecked("entity.elder_guardian.curse"); + case "ENTITY_ELDER_GUARDIAN_DEATH": return EsSound.createUnchecked("entity.elder_guardian.death"); + case "ENTITY_ELDER_GUARDIAN_DEATH_LAND": return EsSound.createUnchecked("entity.elder_guardian.death_land"); + case "ENTITY_ELDER_GUARDIAN_FLOP": return EsSound.createUnchecked("entity.elder_guardian.flop"); + case "ENTITY_ELDER_GUARDIAN_HURT": return EsSound.createUnchecked("entity.elder_guardian.hurt"); + case "ENTITY_ELDER_GUARDIAN_HURT_LAND": return EsSound.createUnchecked("entity.elder_guardian.hurt_land"); + case "ENTITY_ENDERMAN_AMBIENT": return EsSound.createUnchecked("entity.enderman.ambient"); + case "ENTITY_ENDERMAN_DEATH": return EsSound.createUnchecked("entity.enderman.death"); + case "ENTITY_ENDERMAN_HURT": return EsSound.createUnchecked("entity.enderman.hurt"); + case "ENTITY_ENDERMAN_SCREAM": return EsSound.createUnchecked("entity.enderman.scream"); + case "ENTITY_ENDERMAN_STARE": return EsSound.createUnchecked("entity.enderman.stare"); + case "ENTITY_ENDERMAN_TELEPORT": return EsSound.createUnchecked("entity.enderman.teleport"); + case "ENTITY_ENDERMITE_AMBIENT": return EsSound.createUnchecked("entity.endermite.ambient"); + case "ENTITY_ENDERMITE_DEATH": return EsSound.createUnchecked("entity.endermite.death"); + case "ENTITY_ENDERMITE_HURT": return EsSound.createUnchecked("entity.endermite.hurt"); + case "ENTITY_ENDERMITE_STEP": return EsSound.createUnchecked("entity.endermite.step"); + case "ENTITY_ENDER_DRAGON_AMBIENT": return EsSound.createUnchecked("entity.ender_dragon.ambient"); + case "ENTITY_ENDER_DRAGON_DEATH": return EsSound.createUnchecked("entity.ender_dragon.death"); + case "ENTITY_ENDER_DRAGON_FLAP": return EsSound.createUnchecked("entity.ender_dragon.flap"); + case "ENTITY_ENDER_DRAGON_GROWL": return EsSound.createUnchecked("entity.ender_dragon.growl"); + case "ENTITY_ENDER_DRAGON_HURT": return EsSound.createUnchecked("entity.ender_dragon.hurt"); + case "ENTITY_ENDER_DRAGON_SHOOT": return EsSound.createUnchecked("entity.ender_dragon.shoot"); + case "ENTITY_ENDER_EYE_DEATH": return EsSound.createUnchecked("entity.ender_eye.death"); + case "ENTITY_ENDER_EYE_LAUNCH": return EsSound.createUnchecked("entity.ender_eye.launch"); + case "ENTITY_ENDER_PEARL_THROW": return EsSound.createUnchecked("entity.ender_pearl.throw"); + case "ENTITY_EVOKER_AMBIENT": return EsSound.createUnchecked("entity.evoker.ambient"); + case "ENTITY_EVOKER_CAST_SPELL": return EsSound.createUnchecked("entity.evoker.cast_spell"); + case "ENTITY_EVOKER_CELEBRATE": return EsSound.createUnchecked("entity.evoker.celebrate"); + case "ENTITY_EVOKER_DEATH": return EsSound.createUnchecked("entity.evoker.death"); + case "ENTITY_EVOKER_FANGS_ATTACK": return EsSound.createUnchecked("entity.evoker_fangs.attack"); + case "ENTITY_EVOKER_HURT": return EsSound.createUnchecked("entity.evoker.hurt"); + case "ENTITY_EVOKER_PREPARE_ATTACK": return EsSound.createUnchecked("entity.evoker.prepare_attack"); + case "ENTITY_EVOKER_PREPARE_SUMMON": return EsSound.createUnchecked("entity.evoker.prepare_summon"); + case "ENTITY_EVOKER_PREPARE_WOLOLO": return EsSound.createUnchecked("entity.evoker.prepare_wololo"); + case "ENTITY_EXPERIENCE_BOTTLE_THROW": return EsSound.createUnchecked("entity.experience_bottle.throw"); + case "ENTITY_EXPERIENCE_ORB_PICKUP": return EsSound.createUnchecked("entity.experience_orb.pickup"); + case "ENTITY_FIREWORK_ROCKET_BLAST": return EsSound.createUnchecked("entity.firework_rocket.blast"); + case "ENTITY_FIREWORK_ROCKET_BLAST_FAR": return EsSound.createUnchecked("entity.firework_rocket.blast_far"); + case "ENTITY_FIREWORK_ROCKET_LARGE_BLAST": return EsSound.createUnchecked("entity.firework_rocket.large_blast"); + case "ENTITY_FIREWORK_ROCKET_LARGE_BLAST_FAR": return EsSound.createUnchecked("entity.firework_rocket.large_blast_far"); + case "ENTITY_FIREWORK_ROCKET_LAUNCH": return EsSound.createUnchecked("entity.firework_rocket.launch"); + case "ENTITY_FIREWORK_ROCKET_SHOOT": return EsSound.createUnchecked("entity.firework_rocket.shoot"); + case "ENTITY_FIREWORK_ROCKET_TWINKLE": return EsSound.createUnchecked("entity.firework_rocket.twinkle"); + case "ENTITY_FIREWORK_ROCKET_TWINKLE_FAR": return EsSound.createUnchecked("entity.firework_rocket.twinkle_far"); + case "ENTITY_FISHING_BOBBER_RETRIEVE": return EsSound.createUnchecked("entity.fishing_bobber.retrieve"); + case "ENTITY_FISHING_BOBBER_SPLASH": return EsSound.createUnchecked("entity.fishing_bobber.splash"); + case "ENTITY_FISHING_BOBBER_THROW": return EsSound.createUnchecked("entity.fishing_bobber.throw"); + case "ENTITY_FISH_SWIM": return EsSound.createUnchecked("entity.fish.swim"); + case "ENTITY_FOX_AGGRO": return EsSound.createUnchecked("entity.fox.aggro"); + case "ENTITY_FOX_AMBIENT": return EsSound.createUnchecked("entity.fox.ambient"); + case "ENTITY_FOX_BITE": return EsSound.createUnchecked("entity.fox.bite"); + case "ENTITY_FOX_DEATH": return EsSound.createUnchecked("entity.fox.death"); + case "ENTITY_FOX_EAT": return EsSound.createUnchecked("entity.fox.eat"); + case "ENTITY_FOX_HURT": return EsSound.createUnchecked("entity.fox.hurt"); + case "ENTITY_FOX_SCREECH": return EsSound.createUnchecked("entity.fox.screech"); + case "ENTITY_FOX_SLEEP": return EsSound.createUnchecked("entity.fox.sleep"); + case "ENTITY_FOX_SNIFF": return EsSound.createUnchecked("entity.fox.sniff"); + case "ENTITY_FOX_SPIT": return EsSound.createUnchecked("entity.fox.spit"); + case "ENTITY_FOX_TELEPORT": return EsSound.createUnchecked("entity.fox.teleport"); + case "ENTITY_GENERIC_BIG_FALL": return EsSound.createUnchecked("entity.generic.big_fall"); + case "ENTITY_GENERIC_BURN": return EsSound.createUnchecked("entity.generic.burn"); + case "ENTITY_GENERIC_DEATH": return EsSound.createUnchecked("entity.generic.death"); + case "ENTITY_GENERIC_DRINK": return EsSound.createUnchecked("entity.generic.drink"); + case "ENTITY_GENERIC_EAT": return EsSound.createUnchecked("entity.generic.eat"); + case "ENTITY_GENERIC_EXPLODE": return EsSound.createUnchecked("entity.generic.explode"); + case "ENTITY_GENERIC_EXTINGUISH_FIRE": return EsSound.createUnchecked("entity.generic.extinguish_fire"); + case "ENTITY_GENERIC_HURT": return EsSound.createUnchecked("entity.generic.hurt"); + case "ENTITY_GENERIC_SMALL_FALL": return EsSound.createUnchecked("entity.generic.small_fall"); + case "ENTITY_GENERIC_SPLASH": return EsSound.createUnchecked("entity.generic.splash"); + case "ENTITY_GENERIC_SWIM": return EsSound.createUnchecked("entity.generic.swim"); + case "ENTITY_GHAST_AMBIENT": return EsSound.createUnchecked("entity.ghast.ambient"); + case "ENTITY_GHAST_DEATH": return EsSound.createUnchecked("entity.ghast.death"); + case "ENTITY_GHAST_HURT": return EsSound.createUnchecked("entity.ghast.hurt"); + case "ENTITY_GHAST_SCREAM": return EsSound.createUnchecked("entity.ghast.scream"); + case "ENTITY_GHAST_SHOOT": return EsSound.createUnchecked("entity.ghast.shoot"); + case "ENTITY_GHAST_WARN": return EsSound.createUnchecked("entity.ghast.warn"); + case "ENTITY_GUARDIAN_AMBIENT": return EsSound.createUnchecked("entity.guardian.ambient"); + case "ENTITY_GUARDIAN_AMBIENT_LAND": return EsSound.createUnchecked("entity.guardian.ambient_land"); + case "ENTITY_GUARDIAN_ATTACK": return EsSound.createUnchecked("entity.guardian.attack"); + case "ENTITY_GUARDIAN_DEATH": return EsSound.createUnchecked("entity.guardian.death"); + case "ENTITY_GUARDIAN_DEATH_LAND": return EsSound.createUnchecked("entity.guardian.death_land"); + case "ENTITY_GUARDIAN_FLOP": return EsSound.createUnchecked("entity.guardian.flop"); + case "ENTITY_GUARDIAN_HURT": return EsSound.createUnchecked("entity.guardian.hurt"); + case "ENTITY_GUARDIAN_HURT_LAND": return EsSound.createUnchecked("entity.guardian.hurt_land"); + case "ENTITY_HOGLIN_AMBIENT": return EsSound.createUnchecked("entity.hoglin.ambient"); + case "ENTITY_HOGLIN_ANGRY": return EsSound.createUnchecked("entity.hoglin.angry"); + case "ENTITY_HOGLIN_ATTACK": return EsSound.createUnchecked("entity.hoglin.attack"); + case "ENTITY_HOGLIN_CONVERTED_TO_ZOMBIFIED": return EsSound.createUnchecked("entity.hoglin.converted_to_zombified"); + case "ENTITY_HOGLIN_DEATH": return EsSound.createUnchecked("entity.hoglin.death"); + case "ENTITY_HOGLIN_HURT": return EsSound.createUnchecked("entity.hoglin.hurt"); + case "ENTITY_HOGLIN_RETREAT": return EsSound.createUnchecked("entity.hoglin.retreat"); + case "ENTITY_HOGLIN_STEP": return EsSound.createUnchecked("entity.hoglin.step"); + case "ENTITY_HORSE_AMBIENT": return EsSound.createUnchecked("entity.horse.ambient"); + case "ENTITY_HORSE_ANGRY": return EsSound.createUnchecked("entity.horse.angry"); + case "ENTITY_HORSE_ARMOR": return EsSound.createUnchecked("entity.horse.armor"); + case "ENTITY_HORSE_BREATHE": return EsSound.createUnchecked("entity.horse.breathe"); + case "ENTITY_HORSE_DEATH": return EsSound.createUnchecked("entity.horse.death"); + case "ENTITY_HORSE_EAT": return EsSound.createUnchecked("entity.horse.eat"); + case "ENTITY_HORSE_GALLOP": return EsSound.createUnchecked("entity.horse.gallop"); + case "ENTITY_HORSE_HURT": return EsSound.createUnchecked("entity.horse.hurt"); + case "ENTITY_HORSE_JUMP": return EsSound.createUnchecked("entity.horse.jump"); + case "ENTITY_HORSE_LAND": return EsSound.createUnchecked("entity.horse.land"); + case "ENTITY_HORSE_SADDLE": return EsSound.createUnchecked("entity.horse.saddle"); + case "ENTITY_HORSE_STEP": return EsSound.createUnchecked("entity.horse.step"); + case "ENTITY_HORSE_STEP_WOOD": return EsSound.createUnchecked("entity.horse.step_wood"); + case "ENTITY_HOSTILE_BIG_FALL": return EsSound.createUnchecked("entity.hostile.big_fall"); + case "ENTITY_HOSTILE_DEATH": return EsSound.createUnchecked("entity.hostile.death"); + case "ENTITY_HOSTILE_HURT": return EsSound.createUnchecked("entity.hostile.hurt"); + case "ENTITY_HOSTILE_SMALL_FALL": return EsSound.createUnchecked("entity.hostile.small_fall"); + case "ENTITY_HOSTILE_SPLASH": return EsSound.createUnchecked("entity.hostile.splash"); + case "ENTITY_HOSTILE_SWIM": return EsSound.createUnchecked("entity.hostile.swim"); + case "ENTITY_HUSK_AMBIENT": return EsSound.createUnchecked("entity.husk.ambient"); + case "ENTITY_HUSK_CONVERTED_TO_ZOMBIE": return EsSound.createUnchecked("entity.husk.converted_to_zombie"); + case "ENTITY_HUSK_DEATH": return EsSound.createUnchecked("entity.husk.death"); + case "ENTITY_HUSK_HURT": return EsSound.createUnchecked("entity.husk.hurt"); + case "ENTITY_HUSK_STEP": return EsSound.createUnchecked("entity.husk.step"); + case "ENTITY_ILLUSIONER_AMBIENT": return EsSound.createUnchecked("entity.illusioner.ambient"); + case "ENTITY_ILLUSIONER_CAST_SPELL": return EsSound.createUnchecked("entity.illusioner.cast_spell"); + case "ENTITY_ILLUSIONER_DEATH": return EsSound.createUnchecked("entity.illusioner.death"); + case "ENTITY_ILLUSIONER_HURT": return EsSound.createUnchecked("entity.illusioner.hurt"); + case "ENTITY_ILLUSIONER_MIRROR_MOVE": return EsSound.createUnchecked("entity.illusioner.mirror_move"); + case "ENTITY_ILLUSIONER_PREPARE_BLINDNESS": return EsSound.createUnchecked("entity.illusioner.prepare_blindness"); + case "ENTITY_ILLUSIONER_PREPARE_MIRROR": return EsSound.createUnchecked("entity.illusioner.prepare_mirror"); + case "ENTITY_IRON_GOLEM_ATTACK": return EsSound.createUnchecked("entity.iron_golem.attack"); + case "ENTITY_IRON_GOLEM_DAMAGE": return EsSound.createUnchecked("entity.iron_golem.damage"); + case "ENTITY_IRON_GOLEM_DEATH": return EsSound.createUnchecked("entity.iron_golem.death"); + case "ENTITY_IRON_GOLEM_HURT": return EsSound.createUnchecked("entity.iron_golem.hurt"); + case "ENTITY_IRON_GOLEM_REPAIR": return EsSound.createUnchecked("entity.iron_golem.repair"); + case "ENTITY_IRON_GOLEM_STEP": return EsSound.createUnchecked("entity.iron_golem.step"); + case "ENTITY_ITEM_BREAK": return EsSound.createUnchecked("entity.item.break"); + case "ENTITY_ITEM_FRAME_ADD_ITEM": return EsSound.createUnchecked("entity.item_frame.add_item"); + case "ENTITY_ITEM_FRAME_BREAK": return EsSound.createUnchecked("entity.item_frame.break"); + case "ENTITY_ITEM_FRAME_PLACE": return EsSound.createUnchecked("entity.item_frame.place"); + case "ENTITY_ITEM_FRAME_REMOVE_ITEM": return EsSound.createUnchecked("entity.item_frame.remove_item"); + case "ENTITY_ITEM_FRAME_ROTATE_ITEM": return EsSound.createUnchecked("entity.item_frame.rotate_item"); + case "ENTITY_ITEM_PICKUP": return EsSound.createUnchecked("entity.item.pickup"); + case "ENTITY_LEASH_KNOT_BREAK": return EsSound.createUnchecked("entity.leash_knot.break"); + case "ENTITY_LEASH_KNOT_PLACE": return EsSound.createUnchecked("entity.leash_knot.place"); + case "ENTITY_LIGHTNING_BOLT_IMPACT": return EsSound.createUnchecked("entity.lightning_bolt.impact"); + case "ENTITY_LIGHTNING_BOLT_THUNDER": return EsSound.createUnchecked("entity.lightning_bolt.thunder"); + case "ENTITY_LINGERING_POTION_THROW": return EsSound.createUnchecked("entity.lingering_potion.throw"); + case "ENTITY_LLAMA_AMBIENT": return EsSound.createUnchecked("entity.llama.ambient"); + case "ENTITY_LLAMA_ANGRY": return EsSound.createUnchecked("entity.llama.angry"); + case "ENTITY_LLAMA_CHEST": return EsSound.createUnchecked("entity.llama.chest"); + case "ENTITY_LLAMA_DEATH": return EsSound.createUnchecked("entity.llama.death"); + case "ENTITY_LLAMA_EAT": return EsSound.createUnchecked("entity.llama.eat"); + case "ENTITY_LLAMA_HURT": return EsSound.createUnchecked("entity.llama.hurt"); + case "ENTITY_LLAMA_SPIT": return EsSound.createUnchecked("entity.llama.spit"); + case "ENTITY_LLAMA_STEP": return EsSound.createUnchecked("entity.llama.step"); + case "ENTITY_LLAMA_SWAG": return EsSound.createUnchecked("entity.llama.swag"); + case "ENTITY_MAGMA_CUBE_DEATH": return EsSound.createUnchecked("entity.magma_cube.death"); + case "ENTITY_MAGMA_CUBE_DEATH_SMALL": return EsSound.createUnchecked("entity.magma_cube.death_small"); + case "ENTITY_MAGMA_CUBE_HURT": return EsSound.createUnchecked("entity.magma_cube.hurt"); + case "ENTITY_MAGMA_CUBE_HURT_SMALL": return EsSound.createUnchecked("entity.magma_cube.hurt_small"); + case "ENTITY_MAGMA_CUBE_JUMP": return EsSound.createUnchecked("entity.magma_cube.jump"); + case "ENTITY_MAGMA_CUBE_SQUISH": return EsSound.createUnchecked("entity.magma_cube.squish"); + case "ENTITY_MAGMA_CUBE_SQUISH_SMALL": return EsSound.createUnchecked("entity.magma_cube.squish_small"); + case "ENTITY_MINECART_INSIDE": return EsSound.createUnchecked("entity.minecart.inside"); + case "ENTITY_MINECART_RIDING": return EsSound.createUnchecked("entity.minecart.riding"); + case "ENTITY_MOOSHROOM_CONVERT": return EsSound.createUnchecked("entity.mooshroom.convert"); + case "ENTITY_MOOSHROOM_EAT": return EsSound.createUnchecked("entity.mooshroom.eat"); + case "ENTITY_MOOSHROOM_MILK": return EsSound.createUnchecked("entity.mooshroom.milk"); + case "ENTITY_MOOSHROOM_SHEAR": return EsSound.createUnchecked("entity.mooshroom.shear"); + case "ENTITY_MOOSHROOM_SUSPICIOUS_MILK": return EsSound.createUnchecked("entity.mooshroom.suspicious_milk"); + case "ENTITY_MULE_AMBIENT": return EsSound.createUnchecked("entity.mule.ambient"); + case "ENTITY_MULE_ANGRY": return EsSound.createUnchecked("entity.mule.angry"); + case "ENTITY_MULE_CHEST": return EsSound.createUnchecked("entity.mule.chest"); + case "ENTITY_MULE_DEATH": return EsSound.createUnchecked("entity.mule.death"); + case "ENTITY_MULE_EAT": return EsSound.createUnchecked("entity.mule.eat"); + case "ENTITY_MULE_HURT": return EsSound.createUnchecked("entity.mule.hurt"); + case "ENTITY_OCELOT_AMBIENT": return EsSound.createUnchecked("entity.ocelot.ambient"); + case "ENTITY_OCELOT_DEATH": return EsSound.createUnchecked("entity.ocelot.death"); + case "ENTITY_OCELOT_HURT": return EsSound.createUnchecked("entity.ocelot.hurt"); + case "ENTITY_PAINTING_BREAK": return EsSound.createUnchecked("entity.painting.break"); + case "ENTITY_PAINTING_PLACE": return EsSound.createUnchecked("entity.painting.place"); + case "ENTITY_PANDA_AGGRESSIVE_AMBIENT": return EsSound.createUnchecked("entity.panda.aggressive_ambient"); + case "ENTITY_PANDA_AMBIENT": return EsSound.createUnchecked("entity.panda.ambient"); + case "ENTITY_PANDA_BITE": return EsSound.createUnchecked("entity.panda.bite"); + case "ENTITY_PANDA_CANT_BREED": return EsSound.createUnchecked("entity.panda.cant_breed"); + case "ENTITY_PANDA_DEATH": return EsSound.createUnchecked("entity.panda.death"); + case "ENTITY_PANDA_EAT": return EsSound.createUnchecked("entity.panda.eat"); + case "ENTITY_PANDA_HURT": return EsSound.createUnchecked("entity.panda.hurt"); + case "ENTITY_PANDA_PRE_SNEEZE": return EsSound.createUnchecked("entity.panda.pre_sneeze"); + case "ENTITY_PANDA_SNEEZE": return EsSound.createUnchecked("entity.panda.sneeze"); + case "ENTITY_PANDA_STEP": return EsSound.createUnchecked("entity.panda.step"); + case "ENTITY_PANDA_WORRIED_AMBIENT": return EsSound.createUnchecked("entity.panda.worried_ambient"); + case "ENTITY_PARROT_AMBIENT": return EsSound.createUnchecked("entity.parrot.ambient"); + case "ENTITY_PARROT_DEATH": return EsSound.createUnchecked("entity.parrot.death"); + case "ENTITY_PARROT_EAT": return EsSound.createUnchecked("entity.parrot.eat"); + case "ENTITY_PARROT_FLY": return EsSound.createUnchecked("entity.parrot.fly"); + case "ENTITY_PARROT_HURT": return EsSound.createUnchecked("entity.parrot.hurt"); + case "ENTITY_PARROT_IMITATE_BLAZE": return EsSound.createUnchecked("entity.parrot.imitate.blaze"); + case "ENTITY_PARROT_IMITATE_CREEPER": return EsSound.createUnchecked("entity.parrot.imitate.creeper"); + case "ENTITY_PARROT_IMITATE_DROWNED": return EsSound.createUnchecked("entity.parrot.imitate.drowned"); + case "ENTITY_PARROT_IMITATE_ELDER_GUARDIAN": return EsSound.createUnchecked("entity.parrot.imitate.elder_guardian"); + case "ENTITY_PARROT_IMITATE_ENDERMITE": return EsSound.createUnchecked("entity.parrot.imitate.endermite"); + case "ENTITY_PARROT_IMITATE_ENDER_DRAGON": return EsSound.createUnchecked("entity.parrot.imitate.ender_dragon"); + case "ENTITY_PARROT_IMITATE_EVOKER": return EsSound.createUnchecked("entity.parrot.imitate.evoker"); + case "ENTITY_PARROT_IMITATE_GHAST": return EsSound.createUnchecked("entity.parrot.imitate.ghast"); + case "ENTITY_PARROT_IMITATE_GUARDIAN": return EsSound.createUnchecked("entity.parrot.imitate.guardian"); + case "ENTITY_PARROT_IMITATE_HOGLIN": return EsSound.createUnchecked("entity.parrot.imitate.hoglin"); + case "ENTITY_PARROT_IMITATE_HUSK": return EsSound.createUnchecked("entity.parrot.imitate.husk"); + case "ENTITY_PARROT_IMITATE_ILLUSIONER": return EsSound.createUnchecked("entity.parrot.imitate.illusioner"); + case "ENTITY_PARROT_IMITATE_MAGMA_CUBE": return EsSound.createUnchecked("entity.parrot.imitate.magma_cube"); + case "ENTITY_PARROT_IMITATE_PHANTOM": return EsSound.createUnchecked("entity.parrot.imitate.phantom"); + case "ENTITY_PARROT_IMITATE_PIGLIN": return EsSound.createUnchecked("entity.parrot.imitate.piglin"); + case "ENTITY_PARROT_IMITATE_PIGLIN_BRUTE": return EsSound.createUnchecked("entity.parrot.imitate.piglin_brute"); + case "ENTITY_PARROT_IMITATE_PILLAGER": return EsSound.createUnchecked("entity.parrot.imitate.pillager"); + case "ENTITY_PARROT_IMITATE_RAVAGER": return EsSound.createUnchecked("entity.parrot.imitate.ravager"); + case "ENTITY_PARROT_IMITATE_SHULKER": return EsSound.createUnchecked("entity.parrot.imitate.shulker"); + case "ENTITY_PARROT_IMITATE_SILVERFISH": return EsSound.createUnchecked("entity.parrot.imitate.silverfish"); + case "ENTITY_PARROT_IMITATE_SKELETON": return EsSound.createUnchecked("entity.parrot.imitate.skeleton"); + case "ENTITY_PARROT_IMITATE_SLIME": return EsSound.createUnchecked("entity.parrot.imitate.slime"); + case "ENTITY_PARROT_IMITATE_SPIDER": return EsSound.createUnchecked("entity.parrot.imitate.spider"); + case "ENTITY_PARROT_IMITATE_STRAY": return EsSound.createUnchecked("entity.parrot.imitate.stray"); + case "ENTITY_PARROT_IMITATE_VEX": return EsSound.createUnchecked("entity.parrot.imitate.vex"); + case "ENTITY_PARROT_IMITATE_VINDICATOR": return EsSound.createUnchecked("entity.parrot.imitate.vindicator"); + case "ENTITY_PARROT_IMITATE_WITCH": return EsSound.createUnchecked("entity.parrot.imitate.witch"); + case "ENTITY_PARROT_IMITATE_WITHER": return EsSound.createUnchecked("entity.parrot.imitate.wither"); + case "ENTITY_PARROT_IMITATE_WITHER_SKELETON": return EsSound.createUnchecked("entity.parrot.imitate.wither_skeleton"); + case "ENTITY_PARROT_IMITATE_ZOGLIN": return EsSound.createUnchecked("entity.parrot.imitate.zoglin"); + case "ENTITY_PARROT_IMITATE_ZOMBIE": return EsSound.createUnchecked("entity.parrot.imitate.zombie"); + case "ENTITY_PARROT_IMITATE_ZOMBIE_VILLAGER": return EsSound.createUnchecked("entity.parrot.imitate.zombie_villager"); + case "ENTITY_PARROT_STEP": return EsSound.createUnchecked("entity.parrot.step"); + case "ENTITY_PHANTOM_AMBIENT": return EsSound.createUnchecked("entity.phantom.ambient"); + case "ENTITY_PHANTOM_BITE": return EsSound.createUnchecked("entity.phantom.bite"); + case "ENTITY_PHANTOM_DEATH": return EsSound.createUnchecked("entity.phantom.death"); + case "ENTITY_PHANTOM_FLAP": return EsSound.createUnchecked("entity.phantom.flap"); + case "ENTITY_PHANTOM_HURT": return EsSound.createUnchecked("entity.phantom.hurt"); + case "ENTITY_PHANTOM_SWOOP": return EsSound.createUnchecked("entity.phantom.swoop"); + case "ENTITY_PIGLIN_ADMIRING_ITEM": return EsSound.createUnchecked("entity.piglin.admiring_item"); + case "ENTITY_PIGLIN_AMBIENT": return EsSound.createUnchecked("entity.piglin.ambient"); + case "ENTITY_PIGLIN_ANGRY": return EsSound.createUnchecked("entity.piglin.angry"); + case "ENTITY_PIGLIN_BRUTE_AMBIENT": return EsSound.createUnchecked("entity.piglin_brute.ambient"); + case "ENTITY_PIGLIN_BRUTE_ANGRY": return EsSound.createUnchecked("entity.piglin_brute.angry"); + case "ENTITY_PIGLIN_BRUTE_CONVERTED_TO_ZOMBIFIED": return EsSound.createUnchecked("entity.piglin_brute.converted_to_zombified"); + case "ENTITY_PIGLIN_BRUTE_DEATH": return EsSound.createUnchecked("entity.piglin_brute.death"); + case "ENTITY_PIGLIN_BRUTE_HURT": return EsSound.createUnchecked("entity.piglin_brute.hurt"); + case "ENTITY_PIGLIN_BRUTE_STEP": return EsSound.createUnchecked("entity.piglin_brute.step"); + case "ENTITY_PIGLIN_CELEBRATE": return EsSound.createUnchecked("entity.piglin.celebrate"); + case "ENTITY_PIGLIN_CONVERTED_TO_ZOMBIFIED": return EsSound.createUnchecked("entity.piglin.converted_to_zombified"); + case "ENTITY_PIGLIN_DEATH": return EsSound.createUnchecked("entity.piglin.death"); + case "ENTITY_PIGLIN_HURT": return EsSound.createUnchecked("entity.piglin.hurt"); + case "ENTITY_PIGLIN_JEALOUS": return EsSound.createUnchecked("entity.piglin.jealous"); + case "ENTITY_PIGLIN_RETREAT": return EsSound.createUnchecked("entity.piglin.retreat"); + case "ENTITY_PIGLIN_STEP": return EsSound.createUnchecked("entity.piglin.step"); + case "ENTITY_PIG_AMBIENT": return EsSound.createUnchecked("entity.pig.ambient"); + case "ENTITY_PIG_DEATH": return EsSound.createUnchecked("entity.pig.death"); + case "ENTITY_PIG_HURT": return EsSound.createUnchecked("entity.pig.hurt"); + case "ENTITY_PIG_SADDLE": return EsSound.createUnchecked("entity.pig.saddle"); + case "ENTITY_PIG_STEP": return EsSound.createUnchecked("entity.pig.step"); + case "ENTITY_PILLAGER_AMBIENT": return EsSound.createUnchecked("entity.pillager.ambient"); + case "ENTITY_PILLAGER_CELEBRATE": return EsSound.createUnchecked("entity.pillager.celebrate"); + case "ENTITY_PILLAGER_DEATH": return EsSound.createUnchecked("entity.pillager.death"); + case "ENTITY_PILLAGER_HURT": return EsSound.createUnchecked("entity.pillager.hurt"); + case "ENTITY_PLAYER_ATTACK_CRIT": return EsSound.createUnchecked("entity.player.attack.crit"); + case "ENTITY_PLAYER_ATTACK_KNOCKBACK": return EsSound.createUnchecked("entity.player.attack.knockback"); + case "ENTITY_PLAYER_ATTACK_NODAMAGE": return EsSound.createUnchecked("entity.player.attack.nodamage"); + case "ENTITY_PLAYER_ATTACK_STRONG": return EsSound.createUnchecked("entity.player.attack.strong"); + case "ENTITY_PLAYER_ATTACK_SWEEP": return EsSound.createUnchecked("entity.player.attack.sweep"); + case "ENTITY_PLAYER_ATTACK_WEAK": return EsSound.createUnchecked("entity.player.attack.weak"); + case "ENTITY_PLAYER_BIG_FALL": return EsSound.createUnchecked("entity.player.big_fall"); + case "ENTITY_PLAYER_BREATH": return EsSound.createUnchecked("entity.player.breath"); + case "ENTITY_PLAYER_BURP": return EsSound.createUnchecked("entity.player.burp"); + case "ENTITY_PLAYER_DEATH": return EsSound.createUnchecked("entity.player.death"); + case "ENTITY_PLAYER_HURT": return EsSound.createUnchecked("entity.player.hurt"); + case "ENTITY_PLAYER_HURT_DROWN": return EsSound.createUnchecked("entity.player.hurt_drown"); + case "ENTITY_PLAYER_HURT_ON_FIRE": return EsSound.createUnchecked("entity.player.hurt_on_fire"); + case "ENTITY_PLAYER_HURT_SWEET_BERRY_BUSH": return EsSound.createUnchecked("entity.player.hurt_sweet_berry_bush"); + case "ENTITY_PLAYER_LEVELUP": return EsSound.createUnchecked("entity.player.levelup"); + case "ENTITY_PLAYER_SMALL_FALL": return EsSound.createUnchecked("entity.player.small_fall"); + case "ENTITY_PLAYER_SPLASH": return EsSound.createUnchecked("entity.player.splash"); + case "ENTITY_PLAYER_SPLASH_HIGH_SPEED": return EsSound.createUnchecked("entity.player.splash.high_speed"); + case "ENTITY_PLAYER_SWIM": return EsSound.createUnchecked("entity.player.swim"); + case "ENTITY_POLAR_BEAR_AMBIENT": return EsSound.createUnchecked("entity.polar_bear.ambient"); + case "ENTITY_POLAR_BEAR_AMBIENT_BABY": return EsSound.createUnchecked("entity.polar_bear.ambient_baby"); + case "ENTITY_POLAR_BEAR_DEATH": return EsSound.createUnchecked("entity.polar_bear.death"); + case "ENTITY_POLAR_BEAR_HURT": return EsSound.createUnchecked("entity.polar_bear.hurt"); + case "ENTITY_POLAR_BEAR_STEP": return EsSound.createUnchecked("entity.polar_bear.step"); + case "ENTITY_POLAR_BEAR_WARNING": return EsSound.createUnchecked("entity.polar_bear.warning"); + case "ENTITY_PUFFER_FISH_AMBIENT": return EsSound.createUnchecked("entity.puffer_fish.ambient"); + case "ENTITY_PUFFER_FISH_BLOW_OUT": return EsSound.createUnchecked("entity.puffer_fish.blow_out"); + case "ENTITY_PUFFER_FISH_BLOW_UP": return EsSound.createUnchecked("entity.puffer_fish.blow_up"); + case "ENTITY_PUFFER_FISH_DEATH": return EsSound.createUnchecked("entity.puffer_fish.death"); + case "ENTITY_PUFFER_FISH_FLOP": return EsSound.createUnchecked("entity.puffer_fish.flop"); + case "ENTITY_PUFFER_FISH_HURT": return EsSound.createUnchecked("entity.puffer_fish.hurt"); + case "ENTITY_PUFFER_FISH_STING": return EsSound.createUnchecked("entity.puffer_fish.sting"); + case "ENTITY_RABBIT_AMBIENT": return EsSound.createUnchecked("entity.rabbit.ambient"); + case "ENTITY_RABBIT_ATTACK": return EsSound.createUnchecked("entity.rabbit.attack"); + case "ENTITY_RABBIT_DEATH": return EsSound.createUnchecked("entity.rabbit.death"); + case "ENTITY_RABBIT_HURT": return EsSound.createUnchecked("entity.rabbit.hurt"); + case "ENTITY_RABBIT_JUMP": return EsSound.createUnchecked("entity.rabbit.jump"); + case "ENTITY_RAVAGER_AMBIENT": return EsSound.createUnchecked("entity.ravager.ambient"); + case "ENTITY_RAVAGER_ATTACK": return EsSound.createUnchecked("entity.ravager.attack"); + case "ENTITY_RAVAGER_CELEBRATE": return EsSound.createUnchecked("entity.ravager.celebrate"); + case "ENTITY_RAVAGER_DEATH": return EsSound.createUnchecked("entity.ravager.death"); + case "ENTITY_RAVAGER_HURT": return EsSound.createUnchecked("entity.ravager.hurt"); + case "ENTITY_RAVAGER_ROAR": return EsSound.createUnchecked("entity.ravager.roar"); + case "ENTITY_RAVAGER_STEP": return EsSound.createUnchecked("entity.ravager.step"); + case "ENTITY_RAVAGER_STUNNED": return EsSound.createUnchecked("entity.ravager.stunned"); + case "ENTITY_SALMON_AMBIENT": return EsSound.createUnchecked("entity.salmon.ambient"); + case "ENTITY_SALMON_DEATH": return EsSound.createUnchecked("entity.salmon.death"); + case "ENTITY_SALMON_FLOP": return EsSound.createUnchecked("entity.salmon.flop"); + case "ENTITY_SALMON_HURT": return EsSound.createUnchecked("entity.salmon.hurt"); + case "ENTITY_SHEEP_AMBIENT": return EsSound.createUnchecked("entity.sheep.ambient"); + case "ENTITY_SHEEP_DEATH": return EsSound.createUnchecked("entity.sheep.death"); + case "ENTITY_SHEEP_HURT": return EsSound.createUnchecked("entity.sheep.hurt"); + case "ENTITY_SHEEP_SHEAR": return EsSound.createUnchecked("entity.sheep.shear"); + case "ENTITY_SHEEP_STEP": return EsSound.createUnchecked("entity.sheep.step"); + case "ENTITY_SHULKER_AMBIENT": return EsSound.createUnchecked("entity.shulker.ambient"); + case "ENTITY_SHULKER_BULLET_HIT": return EsSound.createUnchecked("entity.shulker_bullet.hit"); + case "ENTITY_SHULKER_BULLET_HURT": return EsSound.createUnchecked("entity.shulker_bullet.hurt"); + case "ENTITY_SHULKER_CLOSE": return EsSound.createUnchecked("entity.shulker.close"); + case "ENTITY_SHULKER_DEATH": return EsSound.createUnchecked("entity.shulker.death"); + case "ENTITY_SHULKER_HURT": return EsSound.createUnchecked("entity.shulker.hurt"); + case "ENTITY_SHULKER_HURT_CLOSED": return EsSound.createUnchecked("entity.shulker.hurt_closed"); + case "ENTITY_SHULKER_OPEN": return EsSound.createUnchecked("entity.shulker.open"); + case "ENTITY_SHULKER_SHOOT": return EsSound.createUnchecked("entity.shulker.shoot"); + case "ENTITY_SHULKER_TELEPORT": return EsSound.createUnchecked("entity.shulker.teleport"); + case "ENTITY_SILVERFISH_AMBIENT": return EsSound.createUnchecked("entity.silverfish.ambient"); + case "ENTITY_SILVERFISH_DEATH": return EsSound.createUnchecked("entity.silverfish.death"); + case "ENTITY_SILVERFISH_HURT": return EsSound.createUnchecked("entity.silverfish.hurt"); + case "ENTITY_SILVERFISH_STEP": return EsSound.createUnchecked("entity.silverfish.step"); + case "ENTITY_SKELETON_AMBIENT": return EsSound.createUnchecked("entity.skeleton.ambient"); + case "ENTITY_SKELETON_DEATH": return EsSound.createUnchecked("entity.skeleton.death"); + case "ENTITY_SKELETON_HORSE_AMBIENT": return EsSound.createUnchecked("entity.skeleton_horse.ambient"); + case "ENTITY_SKELETON_HORSE_AMBIENT_WATER": return EsSound.createUnchecked("entity.skeleton_horse.ambient_water"); + case "ENTITY_SKELETON_HORSE_DEATH": return EsSound.createUnchecked("entity.skeleton_horse.death"); + case "ENTITY_SKELETON_HORSE_GALLOP_WATER": return EsSound.createUnchecked("entity.skeleton_horse.gallop_water"); + case "ENTITY_SKELETON_HORSE_HURT": return EsSound.createUnchecked("entity.skeleton_horse.hurt"); + case "ENTITY_SKELETON_HORSE_JUMP_WATER": return EsSound.createUnchecked("entity.skeleton_horse.jump_water"); + case "ENTITY_SKELETON_HORSE_STEP_WATER": return EsSound.createUnchecked("entity.skeleton_horse.step_water"); + case "ENTITY_SKELETON_HORSE_SWIM": return EsSound.createUnchecked("entity.skeleton_horse.swim"); + case "ENTITY_SKELETON_HURT": return EsSound.createUnchecked("entity.skeleton.hurt"); + case "ENTITY_SKELETON_SHOOT": return EsSound.createUnchecked("entity.skeleton.shoot"); + case "ENTITY_SKELETON_STEP": return EsSound.createUnchecked("entity.skeleton.step"); + case "ENTITY_SLIME_ATTACK": return EsSound.createUnchecked("entity.slime.attack"); + case "ENTITY_SLIME_DEATH": return EsSound.createUnchecked("entity.slime.death"); + case "ENTITY_SLIME_DEATH_SMALL": return EsSound.createUnchecked("entity.slime.death_small"); + case "ENTITY_SLIME_HURT": return EsSound.createUnchecked("entity.slime.hurt"); + case "ENTITY_SLIME_HURT_SMALL": return EsSound.createUnchecked("entity.slime.hurt_small"); + case "ENTITY_SLIME_JUMP": return EsSound.createUnchecked("entity.slime.jump"); + case "ENTITY_SLIME_JUMP_SMALL": return EsSound.createUnchecked("entity.slime.jump_small"); + case "ENTITY_SLIME_SQUISH": return EsSound.createUnchecked("entity.slime.squish"); + case "ENTITY_SLIME_SQUISH_SMALL": return EsSound.createUnchecked("entity.slime.squish_small"); + case "ENTITY_SNOWBALL_THROW": return EsSound.createUnchecked("entity.snowball.throw"); + case "ENTITY_SNOW_GOLEM_AMBIENT": return EsSound.createUnchecked("entity.snow_golem.ambient"); + case "ENTITY_SNOW_GOLEM_DEATH": return EsSound.createUnchecked("entity.snow_golem.death"); + case "ENTITY_SNOW_GOLEM_HURT": return EsSound.createUnchecked("entity.snow_golem.hurt"); + case "ENTITY_SNOW_GOLEM_SHEAR": return EsSound.createUnchecked("entity.snow_golem.shear"); + case "ENTITY_SNOW_GOLEM_SHOOT": return EsSound.createUnchecked("entity.snow_golem.shoot"); + case "ENTITY_SPIDER_AMBIENT": return EsSound.createUnchecked("entity.spider.ambient"); + case "ENTITY_SPIDER_DEATH": return EsSound.createUnchecked("entity.spider.death"); + case "ENTITY_SPIDER_HURT": return EsSound.createUnchecked("entity.spider.hurt"); + case "ENTITY_SPIDER_STEP": return EsSound.createUnchecked("entity.spider.step"); + case "ENTITY_SPLASH_POTION_BREAK": return EsSound.createUnchecked("entity.splash_potion.break"); + case "ENTITY_SPLASH_POTION_THROW": return EsSound.createUnchecked("entity.splash_potion.throw"); + case "ENTITY_SQUID_AMBIENT": return EsSound.createUnchecked("entity.squid.ambient"); + case "ENTITY_SQUID_DEATH": return EsSound.createUnchecked("entity.squid.death"); + case "ENTITY_SQUID_HURT": return EsSound.createUnchecked("entity.squid.hurt"); + case "ENTITY_SQUID_SQUIRT": return EsSound.createUnchecked("entity.squid.squirt"); + case "ENTITY_STRAY_AMBIENT": return EsSound.createUnchecked("entity.stray.ambient"); + case "ENTITY_STRAY_DEATH": return EsSound.createUnchecked("entity.stray.death"); + case "ENTITY_STRAY_HURT": return EsSound.createUnchecked("entity.stray.hurt"); + case "ENTITY_STRAY_STEP": return EsSound.createUnchecked("entity.stray.step"); + case "ENTITY_STRIDER_AMBIENT": return EsSound.createUnchecked("entity.strider.ambient"); + case "ENTITY_STRIDER_DEATH": return EsSound.createUnchecked("entity.strider.death"); + case "ENTITY_STRIDER_EAT": return EsSound.createUnchecked("entity.strider.eat"); + case "ENTITY_STRIDER_HAPPY": return EsSound.createUnchecked("entity.strider.happy"); + case "ENTITY_STRIDER_HURT": return EsSound.createUnchecked("entity.strider.hurt"); + case "ENTITY_STRIDER_RETREAT": return EsSound.createUnchecked("entity.strider.retreat"); + case "ENTITY_STRIDER_SADDLE": return EsSound.createUnchecked("entity.strider.saddle"); + case "ENTITY_STRIDER_STEP": return EsSound.createUnchecked("entity.strider.step"); + case "ENTITY_STRIDER_STEP_LAVA": return EsSound.createUnchecked("entity.strider.step_lava"); + case "ENTITY_TNT_PRIMED": return EsSound.createUnchecked("entity.tnt.primed"); + case "ENTITY_TROPICAL_FISH_AMBIENT": return EsSound.createUnchecked("entity.tropical_fish.ambient"); + case "ENTITY_TROPICAL_FISH_DEATH": return EsSound.createUnchecked("entity.tropical_fish.death"); + case "ENTITY_TROPICAL_FISH_FLOP": return EsSound.createUnchecked("entity.tropical_fish.flop"); + case "ENTITY_TROPICAL_FISH_HURT": return EsSound.createUnchecked("entity.tropical_fish.hurt"); + case "ENTITY_TURTLE_AMBIENT_LAND": return EsSound.createUnchecked("entity.turtle.ambient_land"); + case "ENTITY_TURTLE_DEATH": return EsSound.createUnchecked("entity.turtle.death"); + case "ENTITY_TURTLE_DEATH_BABY": return EsSound.createUnchecked("entity.turtle.death_baby"); + case "ENTITY_TURTLE_EGG_BREAK": return EsSound.createUnchecked("entity.turtle.egg_break"); + case "ENTITY_TURTLE_EGG_CRACK": return EsSound.createUnchecked("entity.turtle.egg_crack"); + case "ENTITY_TURTLE_EGG_HATCH": return EsSound.createUnchecked("entity.turtle.egg_hatch"); + case "ENTITY_TURTLE_HURT": return EsSound.createUnchecked("entity.turtle.hurt"); + case "ENTITY_TURTLE_HURT_BABY": return EsSound.createUnchecked("entity.turtle.hurt_baby"); + case "ENTITY_TURTLE_LAY_EGG": return EsSound.createUnchecked("entity.turtle.lay_egg"); + case "ENTITY_TURTLE_SHAMBLE": return EsSound.createUnchecked("entity.turtle.shamble"); + case "ENTITY_TURTLE_SHAMBLE_BABY": return EsSound.createUnchecked("entity.turtle.shamble_baby"); + case "ENTITY_TURTLE_SWIM": return EsSound.createUnchecked("entity.turtle.swim"); + case "ENTITY_VEX_AMBIENT": return EsSound.createUnchecked("entity.vex.ambient"); + case "ENTITY_VEX_CHARGE": return EsSound.createUnchecked("entity.vex.charge"); + case "ENTITY_VEX_DEATH": return EsSound.createUnchecked("entity.vex.death"); + case "ENTITY_VEX_HURT": return EsSound.createUnchecked("entity.vex.hurt"); + case "ENTITY_VILLAGER_AMBIENT": return EsSound.createUnchecked("entity.villager.ambient"); + case "ENTITY_VILLAGER_CELEBRATE": return EsSound.createUnchecked("entity.villager.celebrate"); + case "ENTITY_VILLAGER_DEATH": return EsSound.createUnchecked("entity.villager.death"); + case "ENTITY_VILLAGER_HURT": return EsSound.createUnchecked("entity.villager.hurt"); + case "ENTITY_VILLAGER_NO": return EsSound.createUnchecked("entity.villager.no"); + case "ENTITY_VILLAGER_TRADE": return EsSound.createUnchecked("entity.villager.trade"); + case "ENTITY_VILLAGER_WORK_ARMORER": return EsSound.createUnchecked("entity.villager.work_armorer"); + case "ENTITY_VILLAGER_WORK_BUTCHER": return EsSound.createUnchecked("entity.villager.work_butcher"); + case "ENTITY_VILLAGER_WORK_CARTOGRAPHER": return EsSound.createUnchecked("entity.villager.work_cartographer"); + case "ENTITY_VILLAGER_WORK_CLERIC": return EsSound.createUnchecked("entity.villager.work_cleric"); + case "ENTITY_VILLAGER_WORK_FARMER": return EsSound.createUnchecked("entity.villager.work_farmer"); + case "ENTITY_VILLAGER_WORK_FISHERMAN": return EsSound.createUnchecked("entity.villager.work_fisherman"); + case "ENTITY_VILLAGER_WORK_FLETCHER": return EsSound.createUnchecked("entity.villager.work_fletcher"); + case "ENTITY_VILLAGER_WORK_LEATHERWORKER": return EsSound.createUnchecked("entity.villager.work_leatherworker"); + case "ENTITY_VILLAGER_WORK_LIBRARIAN": return EsSound.createUnchecked("entity.villager.work_librarian"); + case "ENTITY_VILLAGER_WORK_MASON": return EsSound.createUnchecked("entity.villager.work_mason"); + case "ENTITY_VILLAGER_WORK_SHEPHERD": return EsSound.createUnchecked("entity.villager.work_shepherd"); + case "ENTITY_VILLAGER_WORK_TOOLSMITH": return EsSound.createUnchecked("entity.villager.work_toolsmith"); + case "ENTITY_VILLAGER_WORK_WEAPONSMITH": return EsSound.createUnchecked("entity.villager.work_weaponsmith"); + case "ENTITY_VILLAGER_YES": return EsSound.createUnchecked("entity.villager.yes"); + case "ENTITY_VINDICATOR_AMBIENT": return EsSound.createUnchecked("entity.vindicator.ambient"); + case "ENTITY_VINDICATOR_CELEBRATE": return EsSound.createUnchecked("entity.vindicator.celebrate"); + case "ENTITY_VINDICATOR_DEATH": return EsSound.createUnchecked("entity.vindicator.death"); + case "ENTITY_VINDICATOR_HURT": return EsSound.createUnchecked("entity.vindicator.hurt"); + case "ENTITY_WANDERING_TRADER_AMBIENT": return EsSound.createUnchecked("entity.wandering_trader.ambient"); + case "ENTITY_WANDERING_TRADER_DEATH": return EsSound.createUnchecked("entity.wandering_trader.death"); + case "ENTITY_WANDERING_TRADER_DISAPPEARED": return EsSound.createUnchecked("entity.wandering_trader.disappeared"); + case "ENTITY_WANDERING_TRADER_DRINK_MILK": return EsSound.createUnchecked("entity.wandering_trader.drink_milk"); + case "ENTITY_WANDERING_TRADER_DRINK_POTION": return EsSound.createUnchecked("entity.wandering_trader.drink_potion"); + case "ENTITY_WANDERING_TRADER_HURT": return EsSound.createUnchecked("entity.wandering_trader.hurt"); + case "ENTITY_WANDERING_TRADER_NO": return EsSound.createUnchecked("entity.wandering_trader.no"); + case "ENTITY_WANDERING_TRADER_REAPPEARED": return EsSound.createUnchecked("entity.wandering_trader.reappeared"); + case "ENTITY_WANDERING_TRADER_TRADE": return EsSound.createUnchecked("entity.wandering_trader.trade"); + case "ENTITY_WANDERING_TRADER_YES": return EsSound.createUnchecked("entity.wandering_trader.yes"); + case "ENTITY_WITCH_AMBIENT": return EsSound.createUnchecked("entity.witch.ambient"); + case "ENTITY_WITCH_CELEBRATE": return EsSound.createUnchecked("entity.witch.celebrate"); + case "ENTITY_WITCH_DEATH": return EsSound.createUnchecked("entity.witch.death"); + case "ENTITY_WITCH_DRINK": return EsSound.createUnchecked("entity.witch.drink"); + case "ENTITY_WITCH_HURT": return EsSound.createUnchecked("entity.witch.hurt"); + case "ENTITY_WITCH_THROW": return EsSound.createUnchecked("entity.witch.throw"); + case "ENTITY_WITHER_AMBIENT": return EsSound.createUnchecked("entity.wither.ambient"); + case "ENTITY_WITHER_BREAK_BLOCK": return EsSound.createUnchecked("entity.wither.break_block"); + case "ENTITY_WITHER_DEATH": return EsSound.createUnchecked("entity.wither.death"); + case "ENTITY_WITHER_HURT": return EsSound.createUnchecked("entity.wither.hurt"); + case "ENTITY_WITHER_SHOOT": return EsSound.createUnchecked("entity.wither.shoot"); + case "ENTITY_WITHER_SKELETON_AMBIENT": return EsSound.createUnchecked("entity.wither_skeleton.ambient"); + case "ENTITY_WITHER_SKELETON_DEATH": return EsSound.createUnchecked("entity.wither_skeleton.death"); + case "ENTITY_WITHER_SKELETON_HURT": return EsSound.createUnchecked("entity.wither_skeleton.hurt"); + case "ENTITY_WITHER_SKELETON_STEP": return EsSound.createUnchecked("entity.wither_skeleton.step"); + case "ENTITY_WITHER_SPAWN": return EsSound.createUnchecked("entity.wither.spawn"); + case "ENTITY_WOLF_AMBIENT": return EsSound.createUnchecked("entity.wolf.ambient"); + case "ENTITY_WOLF_DEATH": return EsSound.createUnchecked("entity.wolf.death"); + case "ENTITY_WOLF_GROWL": return EsSound.createUnchecked("entity.wolf.growl"); + case "ENTITY_WOLF_HOWL": return EsSound.createUnchecked("entity.wolf.howl"); + case "ENTITY_WOLF_HURT": return EsSound.createUnchecked("entity.wolf.hurt"); + case "ENTITY_WOLF_PANT": return EsSound.createUnchecked("entity.wolf.pant"); + case "ENTITY_WOLF_SHAKE": return EsSound.createUnchecked("entity.wolf.shake"); + case "ENTITY_WOLF_STEP": return EsSound.createUnchecked("entity.wolf.step"); + case "ENTITY_WOLF_WHINE": return EsSound.createUnchecked("entity.wolf.whine"); + case "ENTITY_ZOGLIN_AMBIENT": return EsSound.createUnchecked("entity.zoglin.ambient"); + case "ENTITY_ZOGLIN_ANGRY": return EsSound.createUnchecked("entity.zoglin.angry"); + case "ENTITY_ZOGLIN_ATTACK": return EsSound.createUnchecked("entity.zoglin.attack"); + case "ENTITY_ZOGLIN_DEATH": return EsSound.createUnchecked("entity.zoglin.death"); + case "ENTITY_ZOGLIN_HURT": return EsSound.createUnchecked("entity.zoglin.hurt"); + case "ENTITY_ZOGLIN_STEP": return EsSound.createUnchecked("entity.zoglin.step"); + case "ENTITY_ZOMBIE_AMBIENT": return EsSound.createUnchecked("entity.zombie.ambient"); + case "ENTITY_ZOMBIE_ATTACK_IRON_DOOR": return EsSound.createUnchecked("entity.zombie.attack_iron_door"); + case "ENTITY_ZOMBIE_ATTACK_WOODEN_DOOR": return EsSound.createUnchecked("entity.zombie.attack_wooden_door"); + case "ENTITY_ZOMBIE_BREAK_WOODEN_DOOR": return EsSound.createUnchecked("entity.zombie.break_wooden_door"); + case "ENTITY_ZOMBIE_CONVERTED_TO_DROWNED": return EsSound.createUnchecked("entity.zombie.converted_to_drowned"); + case "ENTITY_ZOMBIE_DEATH": return EsSound.createUnchecked("entity.zombie.death"); + case "ENTITY_ZOMBIE_DESTROY_EGG": return EsSound.createUnchecked("entity.zombie.destroy_egg"); + case "ENTITY_ZOMBIE_HORSE_AMBIENT": return EsSound.createUnchecked("entity.zombie_horse.ambient"); + case "ENTITY_ZOMBIE_HORSE_DEATH": return EsSound.createUnchecked("entity.zombie_horse.death"); + case "ENTITY_ZOMBIE_HORSE_HURT": return EsSound.createUnchecked("entity.zombie_horse.hurt"); + case "ENTITY_ZOMBIE_HURT": return EsSound.createUnchecked("entity.zombie.hurt"); + case "ENTITY_ZOMBIE_INFECT": return EsSound.createUnchecked("entity.zombie.infect"); + case "ENTITY_ZOMBIE_STEP": return EsSound.createUnchecked("entity.zombie.step"); + case "ENTITY_ZOMBIE_VILLAGER_AMBIENT": return EsSound.createUnchecked("entity.zombie_villager.ambient"); + case "ENTITY_ZOMBIE_VILLAGER_CONVERTED": return EsSound.createUnchecked("entity.zombie_villager.converted"); + case "ENTITY_ZOMBIE_VILLAGER_CURE": return EsSound.createUnchecked("entity.zombie_villager.cure"); + case "ENTITY_ZOMBIE_VILLAGER_DEATH": return EsSound.createUnchecked("entity.zombie_villager.death"); + case "ENTITY_ZOMBIE_VILLAGER_HURT": return EsSound.createUnchecked("entity.zombie_villager.hurt"); + case "ENTITY_ZOMBIE_VILLAGER_STEP": return EsSound.createUnchecked("entity.zombie_villager.step"); + case "ENTITY_ZOMBIFIED_PIGLIN_AMBIENT": return EsSound.createUnchecked("entity.zombified_piglin.ambient"); + case "ENTITY_ZOMBIFIED_PIGLIN_ANGRY": return EsSound.createUnchecked("entity.zombified_piglin.angry"); + case "ENTITY_ZOMBIFIED_PIGLIN_DEATH": return EsSound.createUnchecked("entity.zombified_piglin.death"); + case "ENTITY_ZOMBIFIED_PIGLIN_HURT": return EsSound.createUnchecked("entity.zombified_piglin.hurt"); + case "EVENT_RAID_HORN": return EsSound.createUnchecked("event.raid.horn"); + case "ITEM_ARMOR_EQUIP_CHAIN": return EsSound.createUnchecked("item.armor.equip_chain"); + case "ITEM_ARMOR_EQUIP_DIAMOND": return EsSound.createUnchecked("item.armor.equip_diamond"); + case "ITEM_ARMOR_EQUIP_ELYTRA": return EsSound.createUnchecked("item.armor.equip_elytra"); + case "ITEM_ARMOR_EQUIP_GENERIC": return EsSound.createUnchecked("item.armor.equip_generic"); + case "ITEM_ARMOR_EQUIP_GOLD": return EsSound.createUnchecked("item.armor.equip_gold"); + case "ITEM_ARMOR_EQUIP_IRON": return EsSound.createUnchecked("item.armor.equip_iron"); + case "ITEM_ARMOR_EQUIP_LEATHER": return EsSound.createUnchecked("item.armor.equip_leather"); + case "ITEM_ARMOR_EQUIP_NETHERITE": return EsSound.createUnchecked("item.armor.equip_netherite"); + case "ITEM_ARMOR_EQUIP_TURTLE": return EsSound.createUnchecked("item.armor.equip_turtle"); + case "ITEM_AXE_STRIP": return EsSound.createUnchecked("item.axe.strip"); + case "ITEM_BOOK_PAGE_TURN": return EsSound.createUnchecked("item.book.page_turn"); + case "ITEM_BOOK_PUT": return EsSound.createUnchecked("item.book.put"); + case "ITEM_BOTTLE_EMPTY": return EsSound.createUnchecked("item.bottle.empty"); + case "ITEM_BOTTLE_FILL": return EsSound.createUnchecked("item.bottle.fill"); + case "ITEM_BOTTLE_FILL_DRAGONBREATH": return EsSound.createUnchecked("item.bottle.fill_dragonbreath"); + case "ITEM_BUCKET_EMPTY": return EsSound.createUnchecked("item.bucket.empty"); + case "ITEM_BUCKET_EMPTY_FISH": return EsSound.createUnchecked("item.bucket.empty_fish"); + case "ITEM_BUCKET_EMPTY_LAVA": return EsSound.createUnchecked("item.bucket.empty_lava"); + case "ITEM_BUCKET_FILL": return EsSound.createUnchecked("item.bucket.fill"); + case "ITEM_BUCKET_FILL_FISH": return EsSound.createUnchecked("item.bucket.fill_fish"); + case "ITEM_BUCKET_FILL_LAVA": return EsSound.createUnchecked("item.bucket.fill_lava"); + case "ITEM_CHORUS_FRUIT_TELEPORT": return EsSound.createUnchecked("item.chorus_fruit.teleport"); + case "ITEM_CROP_PLANT": return EsSound.createUnchecked("item.crop.plant"); + case "ITEM_CROSSBOW_HIT": return EsSound.createUnchecked("item.crossbow.hit"); + case "ITEM_CROSSBOW_LOADING_END": return EsSound.createUnchecked("item.crossbow.loading_end"); + case "ITEM_CROSSBOW_LOADING_MIDDLE": return EsSound.createUnchecked("item.crossbow.loading_middle"); + case "ITEM_CROSSBOW_LOADING_START": return EsSound.createUnchecked("item.crossbow.loading_start"); + case "ITEM_CROSSBOW_QUICK_CHARGE_1": return EsSound.createUnchecked("item.crossbow.quick_charge_1"); + case "ITEM_CROSSBOW_QUICK_CHARGE_2": return EsSound.createUnchecked("item.crossbow.quick_charge_2"); + case "ITEM_CROSSBOW_QUICK_CHARGE_3": return EsSound.createUnchecked("item.crossbow.quick_charge_3"); + case "ITEM_CROSSBOW_SHOOT": return EsSound.createUnchecked("item.crossbow.shoot"); + case "ITEM_ELYTRA_FLYING": return EsSound.createUnchecked("item.elytra.flying"); + case "ITEM_FIRECHARGE_USE": return EsSound.createUnchecked("item.firecharge.use"); + case "ITEM_FLINTANDSTEEL_USE": return EsSound.createUnchecked("item.flintandsteel.use"); + case "ITEM_HOE_TILL": return EsSound.createUnchecked("item.hoe.till"); + case "ITEM_HONEY_BOTTLE_DRINK": return EsSound.createUnchecked("item.honey_bottle.drink"); + case "ITEM_LODESTONE_COMPASS_LOCK": return EsSound.createUnchecked("item.lodestone_compass.lock"); + case "ITEM_NETHER_WART_PLANT": return EsSound.createUnchecked("item.nether_wart.plant"); + case "ITEM_SHIELD_BLOCK": return EsSound.createUnchecked("item.shield.block"); + case "ITEM_SHIELD_BREAK": return EsSound.createUnchecked("item.shield.break"); + case "ITEM_SHOVEL_FLATTEN": return EsSound.createUnchecked("item.shovel.flatten"); + case "ITEM_SWEET_BERRIES_PICK_FROM_BUSH": return EsSound.createUnchecked("item.sweet_berries.pick_from_bush"); + case "ITEM_TOTEM_USE": return EsSound.createUnchecked("item.totem.use"); + case "ITEM_TRIDENT_HIT": return EsSound.createUnchecked("item.trident.hit"); + case "ITEM_TRIDENT_HIT_GROUND": return EsSound.createUnchecked("item.trident.hit_ground"); + case "ITEM_TRIDENT_RETURN": return EsSound.createUnchecked("item.trident.return"); + case "ITEM_TRIDENT_RIPTIDE_1": return EsSound.createUnchecked("item.trident.riptide_1"); + case "ITEM_TRIDENT_RIPTIDE_2": return EsSound.createUnchecked("item.trident.riptide_2"); + case "ITEM_TRIDENT_RIPTIDE_3": return EsSound.createUnchecked("item.trident.riptide_3"); + case "ITEM_TRIDENT_THROW": return EsSound.createUnchecked("item.trident.throw"); + case "ITEM_TRIDENT_THUNDER": return EsSound.createUnchecked("item.trident.thunder"); + case "MUSIC_CREATIVE": return EsSound.createUnchecked("music.creative"); + case "MUSIC_CREDITS": return EsSound.createUnchecked("music.credits"); + case "MUSIC_DISC_11": return EsSound.createUnchecked("music_disc.11"); + case "MUSIC_DISC_13": return EsSound.createUnchecked("music_disc.13"); + case "MUSIC_DISC_BLOCKS": return EsSound.createUnchecked("music_disc.blocks"); + case "MUSIC_DISC_CAT": return EsSound.createUnchecked("music_disc.cat"); + case "MUSIC_DISC_CHIRP": return EsSound.createUnchecked("music_disc.chirp"); + case "MUSIC_DISC_FAR": return EsSound.createUnchecked("music_disc.far"); + case "MUSIC_DISC_MALL": return EsSound.createUnchecked("music_disc.mall"); + case "MUSIC_DISC_MELLOHI": return EsSound.createUnchecked("music_disc.mellohi"); + case "MUSIC_DISC_PIGSTEP": return EsSound.createUnchecked("music_disc.pigstep"); + case "MUSIC_DISC_STAL": return EsSound.createUnchecked("music_disc.stal"); + case "MUSIC_DISC_STRAD": return EsSound.createUnchecked("music_disc.strad"); + case "MUSIC_DISC_WAIT": return EsSound.createUnchecked("music_disc.wait"); + case "MUSIC_DISC_WARD": return EsSound.createUnchecked("music_disc.ward"); + case "MUSIC_DRAGON": return EsSound.createUnchecked("music.dragon"); + case "MUSIC_END": return EsSound.createUnchecked("music.end"); + case "MUSIC_GAME": return EsSound.createUnchecked("music.game"); + case "MUSIC_MENU": return EsSound.createUnchecked("music.menu"); + case "MUSIC_NETHER_BASALT_DELTAS": return EsSound.createUnchecked("music.nether.basalt_deltas"); + case "MUSIC_NETHER_CRIMSON_FOREST": return EsSound.createUnchecked("music.nether.crimson_forest"); + case "MUSIC_NETHER_NETHER_WASTES": return EsSound.createUnchecked("music.nether.nether_wastes"); + case "MUSIC_NETHER_SOUL_SAND_VALLEY": return EsSound.createUnchecked("music.nether.soul_sand_valley"); + case "MUSIC_NETHER_WARPED_FOREST": return EsSound.createUnchecked("music.nether.warped_forest"); + case "MUSIC_UNDER_WATER": return EsSound.createUnchecked("music.under_water"); + case "PARTICLE_SOUL_ESCAPE": return EsSound.createUnchecked("particle.soul_escape"); + case "UI_BUTTON_CLICK": return EsSound.createUnchecked("ui.button.click"); + case "UI_CARTOGRAPHY_TABLE_TAKE_RESULT": return EsSound.createUnchecked("ui.cartography_table.take_result"); + case "UI_LOOM_SELECT_PATTERN": return EsSound.createUnchecked("ui.loom.select_pattern"); + case "UI_LOOM_TAKE_RESULT": return EsSound.createUnchecked("ui.loom.take_result"); + case "UI_STONECUTTER_SELECT_RECIPE": return EsSound.createUnchecked("ui.stonecutter.select_recipe"); + case "UI_STONECUTTER_TAKE_RESULT": return EsSound.createUnchecked("ui.stonecutter.take_result"); + case "UI_TOAST_CHALLENGE_COMPLETE": return EsSound.createUnchecked("ui.toast.challenge_complete"); + case "UI_TOAST_IN": return EsSound.createUnchecked("ui.toast.in"); + case "UI_TOAST_OUT": return EsSound.createUnchecked("ui.toast.out"); + case "WEATHER_RAIN": return EsSound.createUnchecked("weather.rain"); + case "WEATHER_RAIN_ABOVE": return EsSound.createUnchecked("weather.rain.above"); + + // Older versions had different sounds, these I have converted. + case "RECORD_11": return EsSound.createUnchecked("music_disc.11"); + case "RECORD_13": return EsSound.createUnchecked("music_disc.13"); + case "RECORD_BLOCKS": return EsSound.createUnchecked("music_disc.blocks"); + case "RECORD_CAT": return EsSound.createUnchecked("music_disc.cat"); + case "RECORD_CHIRP": return EsSound.createUnchecked("music_disc.chirp"); + case "RECORD_FAR": return EsSound.createUnchecked("music_disc.far"); + case "RECORD_MALL": return EsSound.createUnchecked("music_disc.mall"); + case "RECORD_MELLOHI": return EsSound.createUnchecked("music_disc.mellohi"); + case "RECORD_STAL": return EsSound.createUnchecked("music_disc.stal"); + case "RECORD_STRAD": return EsSound.createUnchecked("music_disc.strad"); + case "RECORD_WAIT": return EsSound.createUnchecked("music_disc.wait"); + case "RECORD_WARD": return EsSound.createUnchecked("music_disc.ward"); + + // this sound used to be called something else, until we need it we can generate an incorrect name. + default: return EsSound.createUnchecked(eVal.replace('_', '.')); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java index fe8fe41..760a5f9 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -3,10 +3,11 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSound; import net.serble.estools.ServerApi.EsSoundCategory; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; -import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; @@ -122,8 +123,8 @@ public boolean isFlying() { } @Override - public void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { - bukkitPlayer.playSound(FoliaHelper.toBukkitLocation(loc), Sound.valueOf(sound), FoliaHelper.toBukkitSoundCategory(category), volume, pitch); + public void playSound(EsSound sound, EsSoundCategory category, EsLocation loc, int volume, int pitch) { + bukkitPlayer.playSound(FoliaHelper.toBukkitLocation(loc), BukkitHelper.toBukkitSound(sound), FoliaHelper.toBukkitSoundCategory(category), volume, pitch); } @SuppressWarnings("UnstableApiUsage") // Yes I know it's a bug diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 6ecd81b..1f43cdc 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -33,6 +33,7 @@ public class FoliaServer implements EsServer { private static TaskChainFactory taskChainFactory; private static final Set materials = new HashSet<>(); private static final Set itemMaterials = new HashSet<>(); + private static final Set sounds = new HashSet<>(); public FoliaServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; @@ -43,21 +44,18 @@ public FoliaServer(Object pluginObj) { @Override public void initialise() { for (Material mat : Material.values()) { - EsMaterial esMat; - if (Main.minecraftVersion.getMinor() > 12) { - esMat = EsMaterial.createUnchecked(mat.getKey().getKey().toLowerCase()); - - if (mat.isItem()) { - itemMaterials.add(esMat); - } - } else { - esMat = EsMaterial.createUnchecked(mat.name().toLowerCase()); - + EsMaterial esMat = EsMaterial.createUnchecked(mat.getKey().getKey().toLowerCase()); + if (mat.isItem()) { itemMaterials.add(esMat); } materials.add(esMat); } + + for (Sound sound : Registry.SOUNDS) { + EsSound esSound = EsSound.createUnchecked(sound.getKey().getKey()); + sounds.add(esSound); + } } public static TaskChain newChain() { @@ -226,13 +224,8 @@ public Set getEnchantments() { } @Override - public String[] getSounds() { - Sound[] sounds = Sound.values(); - String[] strSounds = new String[sounds.length]; - for (int i = 0; i < sounds.length; i++) { - strSounds[i] = sounds[i].name(); - } - return strSounds; + public Set getSounds() { + return sounds; } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java index c3c1713..0fc174b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsPlayer.java @@ -2,6 +2,7 @@ import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.EsLocation; +import net.serble.estools.ServerApi.EsSound; import net.serble.estools.ServerApi.EsSoundCategory; @SuppressWarnings("unused") @@ -23,7 +24,7 @@ public interface EsPlayer extends EsLivingEntity { boolean getAllowFlight(); void setAllowFlight(boolean val); boolean isFlying(); - void playSound(String sound, EsSoundCategory category, EsLocation loc, int volume, int pitch); + void playSound(EsSound sound, EsSoundCategory category, EsLocation loc, int volume, int pitch); void updateInventory(); boolean hasPermission(String node); boolean isPermissionSet(String node); diff --git a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java index 1296c46..900cc5b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Interfaces/EsServer.java @@ -22,7 +22,7 @@ public interface EsServer { Set getPotionEffectTypes(); Set getOldPotionTypes(); Set getEnchantments(); - String[] getSounds(); + Set getSounds(); void initialise(); /** From 517943f2054fc52ec37c93af54b12de8437729aa Mon Sep 17 00:00:00 2001 From: CoPokBl Date: Thu, 20 Jun 2024 09:15:57 +1000 Subject: [PATCH 37/74] spelling and grammar in BukkitSoundEnumConverter --- .../Bukkit/Helpers/BukkitSoundEnumConverter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java index 9b5f270..129cc08 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundEnumConverter.java @@ -5,12 +5,12 @@ /** * This class converts the Sound.name() to an EsSound, EsSounds use the sound key and Sound.name() is a semi arbitrary - * name, since there is no way to differenciate between underscores and dots in Sound.name() names, + * name, since there is no way to differentiate between underscores and dots in Sound.name() names, * I generated a switch statement that converts between them. *

* This was generated from minecraft 1.16.5, newer versions can use Sound.getKey().getKey() to get this info. *

- * Unfortunately, some old keys will be different, but i made Records convert to the new types. + * Unfortunately, some old keys will be different, but I made Records convert to the new types. */ public class BukkitSoundEnumConverter { public static String convertKeyToEnum(EsSound sound) { From 3eed408c11e48c91d8ad36808258a8588034bd53 Mon Sep 17 00:00:00 2001 From: CoPokBl <59753822+CoPokBl@users.noreply.github.com> Date: Fri, 21 Jun 2024 10:20:07 +1000 Subject: [PATCH 38/74] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 15c9f0d..ae99890 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,9 @@ To find out what versions have been tested look at TestedVersions.txt in the pro We will likely be slow to test new Minecraft versions, but you can probably assume that EsTools will be fully functional on new versions. -**We officially support Spigot/Bukkit/Paper 1.0.0 and above.** +**We officially support the following:** +- Bukkit/Spigot/Paper 1.0.0+ +- Folia Latest Build For support DM us at: Discord: @copokbl and @calcilore. Or email support@serble.net. From 42bcef265ebb915d6dac052888ce4abc5086b79d Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 16:44:25 +1000 Subject: [PATCH 39/74] Fix movement away from String ids from breaking serialisation --- .../serble/estools/Config/ConfigManager.java | 20 ++++++++++++------- .../estools/ServerApi/EsEnchantment.java | 5 +++++ .../serble/estools/ServerApi/EsMaterial.java | 5 +++++ .../estools/ServerApi/EsPotionEffectType.java | 5 +++++ .../net/serble/estools/ServerApi/EsSound.java | 5 +++++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/serble/estools/Config/ConfigManager.java b/src/main/java/net/serble/estools/Config/ConfigManager.java index 32c29ff..18552fd 100644 --- a/src/main/java/net/serble/estools/Config/ConfigManager.java +++ b/src/main/java/net/serble/estools/Config/ConfigManager.java @@ -4,6 +4,7 @@ import java.util.Arrays; import net.serble.estools.Main; +import org.jetbrains.annotations.NotNull; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; @@ -44,13 +45,7 @@ public static T load(File configFile, Class base, String... all } } - LoaderOptions options = new LoaderOptions(); - TagInspector taginspector = tag -> - tag.getClassName().equals(base.getName()) || - Arrays.stream(allowedClasses).anyMatch(c -> c.equals(tag.getClassName())) || - Arrays.stream(Main.server.getRelevantInternalTypes()).anyMatch(c -> tag.getClassName().endsWith(c)); - options.setTagInspector(taginspector); - Yaml yaml = new Yaml(new Constructor(base, options)); + Yaml yaml = createYamlLoader(base, allowedClasses); try { return yaml.load(new FileInputStream(configFile)); } catch (FileNotFoundException e) { @@ -59,6 +54,17 @@ public static T load(File configFile, Class base, String... all } } + private static @NotNull Yaml createYamlLoader(Class base, String[] allowedClasses) { + LoaderOptions options = new LoaderOptions(); + TagInspector taginspector = tag -> + tag.getClassName().equals(base.getName()) || + Arrays.stream(allowedClasses).anyMatch(c -> c.equals(tag.getClassName())) || + Arrays.stream(Main.server.getRelevantInternalTypes()).anyMatch(c -> tag.getClassName().endsWith(c) || + tag.getClassName().startsWith("net.serble.estools.ServerApi")); // Allow all server api classes (Stuff like EsMaterial) + options.setTagInspector(taginspector); + return new Yaml(new Constructor(base, options)); + } + public static void save(String file, Object obj) { File configFile = new File(Main.server.getDataFolder(), file); save(configFile, obj); diff --git a/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java index e0c8967..e7b5743 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java +++ b/src/main/java/net/serble/estools/ServerApi/EsEnchantment.java @@ -28,6 +28,11 @@ public String getKey() { return key; } + /** This exists for YAML serialisation only, DO NOT USE, use EsSound.fromKey(String) instead. */ + public void setKey(String key) { + this.key = key; + } + @Override public boolean equals(Object obj) { if (obj instanceof EsEnchantment) { diff --git a/src/main/java/net/serble/estools/ServerApi/EsMaterial.java b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java index afbd8a0..9f6c153 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsMaterial.java +++ b/src/main/java/net/serble/estools/ServerApi/EsMaterial.java @@ -28,6 +28,11 @@ public String getKey() { return key; } + /** This exists for YAML serialisation only, DO NOT USE, use EsMaterial.fromKey(String) instead. */ + public void setKey(String key) { + this.key = key; + } + @Override public boolean equals(Object obj) { if (obj instanceof EsMaterial) { diff --git a/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java index 1e4b59c..33519ed 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java +++ b/src/main/java/net/serble/estools/ServerApi/EsPotionEffectType.java @@ -28,6 +28,11 @@ public String getKey() { return key; } + /** This exists for YAML serialisation only, DO NOT USE, use EsPotionEffectType.fromKey(String) instead. */ + public void setKey(String key) { + this.key = key; + } + @Override public boolean equals(Object obj) { if (obj instanceof EsPotionEffectType) { diff --git a/src/main/java/net/serble/estools/ServerApi/EsSound.java b/src/main/java/net/serble/estools/ServerApi/EsSound.java index 05f33fd..136a0a3 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSound.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSound.java @@ -28,6 +28,11 @@ public String getKey() { return key; } + /** This exists for YAML serialisation only, DO NOT USE, use EsSound.fromKey(String) instead. */ + public void setKey(String key) { + this.key = key; + } + @Override public boolean equals(Object obj) { if (obj instanceof EsSound) { From 3556fb0627bf36cc196f687528977414a94815d2 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 17:39:03 +1000 Subject: [PATCH 40/74] Refactor, fix bugs that never surfaced and fixed getinfo bug from EsEntity constructors being used wrong --- src/main/java/net/serble/estools/Main.java | 2 +- .../Implementations/Bukkit/BukkitEntity.java | 9 +++-- .../Bukkit/BukkitInventoryView.java | 7 ++-- .../Bukkit/BukkitItemStack.java | 2 +- .../Implementations/Bukkit/BukkitServer.java | 10 ++--- .../Implementations/Bukkit/BukkitWorld.java | 7 ++-- .../{ => Helpers}/BukkitConfigMigrator.java | 14 +++---- .../Bukkit/Helpers/BukkitHelper.java | 20 +++++++--- .../Bukkit/{ => Helpers}/OldWarpLocation.java | 5 ++- .../Folia/FoliaEnchantmentHelper.java | 3 +- .../Implementations/Folia/FoliaEntity.java | 33 +++++++++++++++- .../Implementations/Folia/FoliaHelper.java | 39 ++++++++++++------- .../Folia/FoliaInventoryView.java | 6 +-- .../Implementations/Folia/FoliaItemStack.java | 4 +- .../Implementations/Folia/FoliaServer.java | 8 ++-- .../Implementations/Folia/FoliaSign.java | 2 +- .../Implementations/Folia/FoliaWorld.java | 8 ++-- 17 files changed, 117 insertions(+), 62 deletions(-) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{ => Helpers}/BukkitConfigMigrator.java (97%) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{ => Helpers}/OldWarpLocation.java (85%) diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index b3888d6..dfe7f20 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -13,7 +13,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitConfigMigrator; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitConfigMigrator; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index 0dd0a4f..b58fc8a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -15,9 +15,9 @@ import java.util.UUID; public class BukkitEntity implements EsEntity { - private final org.bukkit.entity.Entity bukkitEntity; + private final Entity bukkitEntity; - public BukkitEntity(org.bukkit.entity.Entity bukkitEntity) { + public BukkitEntity(Entity bukkitEntity) { this.bukkitEntity = bukkitEntity; } @@ -46,7 +46,8 @@ public String getName() { } if (bukkitEntity instanceof LivingEntity) { - String name = ((LivingEntity)bukkitEntity).getCustomName(); + // Cast isn't redundant because that method is only in LivingEntity in old versions + @SuppressWarnings("RedundantCast") String name = ((LivingEntity)bukkitEntity).getCustomName(); if (name != null) { return name; } @@ -96,7 +97,7 @@ public List getPassengers() { List bukkitList = bukkitEntity.getPassengers(); List list = new ArrayList<>(); for (Entity entity : bukkitList) { - list.add(new BukkitEntity(entity)); + list.add(BukkitHelper.fromBukkitEntity(entity)); } return list; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java index 792ef44..3901034 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java @@ -1,6 +1,7 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsInventory; import net.serble.estools.ServerApi.Interfaces.EsInventoryView; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -16,12 +17,12 @@ public BukkitInventoryView(InventoryView child) { @Override public EsInventory getTopInventory() { - return new BukkitInventory(bukkitView.getTopInventory()); + return BukkitHelper.fromBukkitInventory(bukkitView.getTopInventory()); } @Override public EsInventory getBottomInventory() { - return new BukkitInventory(bukkitView.getBottomInventory()); + return BukkitHelper.fromBukkitInventory(bukkitView.getBottomInventory()); } @Override @@ -32,7 +33,7 @@ public EsInventory getInventory(int slot) { } return getTopInventory(); } - return new BukkitInventory(bukkitView.getInventory(slot)); + return BukkitHelper.fromBukkitInventory(bukkitView.getInventory(slot)); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index e2929e8..ee5484b 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -128,7 +128,7 @@ public int getDamage() { @SuppressWarnings("MethodDoesntCallSuperMethod") // I don't care, it doesn't work like that @Override public EsItemStack clone() { - return new BukkitItemStack(bukkitItem.clone()); + return BukkitHelper.fromBukkitItem(bukkitItem.clone()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index a89e2c8..546ac7d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -83,13 +83,13 @@ public EsEntity getEntity(UUID uuid) { if (entity == null) { return null; } - return new BukkitEntity(entity); + return BukkitHelper.fromBukkitEntity(entity); } for (World world : Bukkit.getWorlds()) { for (Entity entity : world.getEntities()) { if (entity.getUniqueId() == uuid) { - return new BukkitEntity(entity); + return BukkitHelper.fromBukkitEntity(entity); } } } @@ -131,15 +131,15 @@ public Collection getOnlinePlayers() { try { if (Bukkit.class.getMethod("getOnlinePlayers").getReturnType() == Collection.class) { List players = new ArrayList<>(); - for (org.bukkit.entity.Player p : Bukkit.getOnlinePlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { players.add(new BukkitPlayer(p)); } return players; } else { - org.bukkit.entity.Player[] players = (org.bukkit.entity.Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); + Player[] players = (Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); List users = new ArrayList<>(); - for (org.bukkit.entity.Player p : players) { + for (Player p : players) { users.add(new BukkitPlayer(p)); } return users; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java index 7e606cd..1af5c4c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitWorld.java @@ -5,6 +5,7 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsWorld; +import org.bukkit.World; import org.bukkit.entity.Entity; import java.util.ArrayList; @@ -15,7 +16,7 @@ public class BukkitWorld implements EsWorld { private final org.bukkit.World bukkitWorld; - public BukkitWorld(org.bukkit.World world) { + public BukkitWorld(World world) { bukkitWorld = world; } @@ -26,9 +27,9 @@ public String getName() { @Override public List getEntities() { - List bEntities = bukkitWorld.getEntities(); + List bEntities = bukkitWorld.getEntities(); List entities = new ArrayList<>(); - for (org.bukkit.entity.Entity bEntity : bEntities) { + for (Entity bEntity : bEntities) { entities.add(BukkitHelper.fromBukkitEntity(bEntity)); } return entities; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitConfigMigrator.java similarity index 97% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitConfigMigrator.java index b84e286..1995139 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConfigMigrator.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitConfigMigrator.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Commands.Warps.WarpLocation; import net.serble.estools.Config.ConfigManager; @@ -8,7 +8,7 @@ import net.serble.estools.Config.Schemas.Give.GiveSettings; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsSerialisableItemStack; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemStack; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; @@ -75,7 +75,7 @@ private static void migrateMainConfig() { // config.yml } if (file.length() == 0) { - file.delete(); + boolean ignored = file.delete(); return; } @@ -117,7 +117,7 @@ private static void migrateGiveConfig() { // config.yml } if (file.length() == 0) { - file.delete(); + boolean ignored = file.delete(); return; } @@ -164,7 +164,7 @@ private static void migrateGodsConfig() { // config.yml } if (file.length() == 0) { - file.delete(); + boolean ignored = file.delete(); return; } @@ -200,7 +200,7 @@ private static void migrateWarpsConfig() { // config.yml } if (file.length() == 0) { - file.delete(); + boolean ignored = file.delete(); return; } @@ -267,7 +267,7 @@ private static void migrateCChestsConfig() { // config.yml List savedPlayers = new ArrayList<>(Arrays.asList(files)); savedPlayers.removeIf(file -> { if (file.length() == 0) { - file.delete(); + boolean ignored = file.delete(); return true; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index c66abab..bdf4042 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -5,6 +5,7 @@ import net.serble.estools.ServerApi.*; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.*; +import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayerInventory; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; @@ -23,6 +24,7 @@ import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; @@ -77,7 +79,7 @@ public static Vector toVector(Position pos) { return new Vector(pos.getX(), pos.getY(), pos.getZ()); } - public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.CommandSender sender) { + public static EsCommandSender fromBukkitCommandSender(CommandSender sender) { if (sender == null) { return null; } @@ -87,11 +89,11 @@ public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.Command } if (sender instanceof LivingEntity) { - return new BukkitLivingEntity((org.bukkit.entity.LivingEntity) sender); + return new BukkitLivingEntity((LivingEntity) sender); } if (sender instanceof Entity) { - return new BukkitEntity((org.bukkit.entity.Entity) sender); + return new BukkitEntity((Entity) sender); } if (sender instanceof ConsoleCommandSender) { @@ -129,8 +131,12 @@ public static CommandSender toBukkitCommandSender(EsCommandSender sender) { throw new RuntimeException("Unrecognised command sender"); } - public static EsEntity fromBukkitEntity(org.bukkit.entity.Entity entity) { - if (entity instanceof org.bukkit.entity.LivingEntity) { + public static EsEntity fromBukkitEntity(Entity entity) { + if (entity instanceof Player) { + return new BukkitPlayer((Player) entity); + } + + if (entity instanceof LivingEntity) { return new BukkitLivingEntity((LivingEntity) entity); } @@ -570,6 +576,10 @@ public static EsInventory fromBukkitInventory(Inventory inv) { return null; } + if (inv instanceof PlayerInventory) { + return new FoliaPlayerInventory((PlayerInventory) inv); + } + return new BukkitInventory(inv); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/OldWarpLocation.java similarity index 85% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/OldWarpLocation.java index 8d6f9c4..ddd5ce3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/OldWarpLocation.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/OldWarpLocation.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import org.bukkit.Location; import org.bukkit.configuration.serialization.ConfigurationSerializable; @@ -8,6 +8,9 @@ import java.util.HashMap; import java.util.Map; +/** + * This class exists for the BukkitConfigMigrator so that it can parse the old warps.yml file. + */ @SerializableAs("WarpLocation") public class OldWarpLocation implements ConfigurationSerializable { public Location location; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java index cebc49f..85fa7be 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEnchantmentHelper.java @@ -2,6 +2,5 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; -// This class exists because the registry doesn't exist pre 1.13, so all the enchantments names have to be hardcoded. -// only enchantments before 1.13 need to be here because this class isn't used past 1.13 +// This is here just in case something changes in Folia that requires methods to be overridden public class FoliaEnchantmentHelper extends BukkitEnchantmentHelper { } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java index ff84924..4274bad 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEntity.java @@ -1,18 +1,49 @@ package net.serble.estools.ServerApi.Implementations.Folia; import io.papermc.lib.PaperLib; +import net.serble.estools.Main; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitEntity; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.EsEntity; import org.bukkit.entity.Entity; +import java.util.ArrayList; +import java.util.List; + public class FoliaEntity extends BukkitEntity { - private final org.bukkit.entity.Entity bukkitEntity; + private final Entity bukkitEntity; public FoliaEntity(Entity bukkitEntity) { super(bukkitEntity); this.bukkitEntity = bukkitEntity; } + @Override + public List getPassengers() { + List bukkitList = bukkitEntity.getPassengers(); + List list = new ArrayList<>(); + for (Entity entity : bukkitList) { + list.add(BukkitHelper.fromBukkitEntity(entity)); + } + return list; + } + + @Override + public void addPassenger(EsEntity entity) { + if (Main.minecraftVersion.getMinor() > 11) { + bukkitEntity.addPassenger(((FoliaEntity) entity).getBukkitEntity()); + } else { + //noinspection deprecation + bukkitEntity.setPassenger(((FoliaEntity) entity).getBukkitEntity()); + } + } + + @Override + public EsLocation getLocation() { + return FoliaHelper.fromBukkitLocation(bukkitEntity.getLocation()); + } + @Override public void teleport(EsLocation loc) { PaperLib.teleportAsync(bukkitEntity, FoliaHelper.toBukkitLocation(loc)); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 9d78ff0..4b40684 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -23,6 +23,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.PotionMeta; import java.lang.reflect.InvocationTargetException; @@ -126,23 +127,23 @@ public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.Command } if (sender instanceof Player) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer((Player) sender); + return new FoliaPlayer((Player) sender); } if (sender instanceof LivingEntity) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity((org.bukkit.entity.LivingEntity) sender); + return new FoliaLivingEntity((org.bukkit.entity.LivingEntity) sender); } if (sender instanceof Entity) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity((org.bukkit.entity.Entity) sender); + return new FoliaEntity((org.bukkit.entity.Entity) sender); } if (sender instanceof ConsoleCommandSender) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaConsoleSender((ConsoleCommandSender) sender); + return new FoliaConsoleSender((ConsoleCommandSender) sender); } if (sender instanceof BlockCommandSender) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaCommandBlockSender((BlockCommandSender) sender); + return new FoliaCommandBlockSender((BlockCommandSender) sender); } throw new RuntimeException("Unrecognised command sender"); @@ -150,15 +151,15 @@ public static EsCommandSender fromBukkitCommandSender(org.bukkit.command.Command public static CommandSender toBukkitCommandSender(EsCommandSender sender) { if (sender instanceof EsPlayer) { - return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer) sender).getBukkitPlayer(); + return ((FoliaPlayer) sender).getBukkitPlayer(); } if (sender instanceof EsLivingEntity) { - return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity) sender).getBukkitEntity(); + return ((FoliaLivingEntity) sender).getBukkitEntity(); } if (sender instanceof EsEntity) { - return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity) sender).getBukkitEntity(); + return ((FoliaEntity) sender).getBukkitEntity(); } if (sender instanceof EsConsoleSender) { @@ -166,28 +167,32 @@ public static CommandSender toBukkitCommandSender(EsCommandSender sender) { } if (sender instanceof EsCommandBlockSender) { - return ((net.serble.estools.ServerApi.Implementations.Folia.FoliaCommandBlockSender) sender).getBukkitSender(); + return ((FoliaCommandBlockSender) sender).getBukkitSender(); } throw new RuntimeException("Unrecognised command sender"); } - public static EsEntity fromBukkitEntity(org.bukkit.entity.Entity entity) { - if (entity instanceof org.bukkit.entity.LivingEntity) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaLivingEntity((LivingEntity) entity); + public static EsEntity fromBukkitEntity(Entity entity) { + if (entity instanceof Player) { + return new FoliaPlayer((Player) entity); } - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaEntity(entity); + if (entity instanceof LivingEntity) { + return new FoliaLivingEntity((LivingEntity) entity); + } + + return new FoliaEntity(entity); } public static EsBlock fromBukkitBlock(Block block) { BlockState state = block.getState(); if (state instanceof Sign) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaSign((Sign) state); + return new FoliaSign((Sign) state); } - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaBlock(state); + return new FoliaBlock(state); } public static EsItemStack fromBukkitItem(ItemStack item) { @@ -238,6 +243,10 @@ public static EsInventory fromBukkitInventory(Inventory inv) { return null; } + if (inv instanceof PlayerInventory) { + return new FoliaPlayerInventory((PlayerInventory) inv); + } + return new FoliaInventory(inv); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java index d2d7468..69ecc6a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaInventoryView.java @@ -16,17 +16,17 @@ public FoliaInventoryView(InventoryView child) { @Override public EsInventory getTopInventory() { - return new FoliaInventory(bukkitView.getTopInventory()); + return FoliaHelper.fromBukkitInventory(bukkitView.getTopInventory()); } @Override public EsInventory getBottomInventory() { - return new FoliaInventory(bukkitView.getBottomInventory()); + return FoliaHelper.fromBukkitInventory(bukkitView.getBottomInventory()); } @Override public EsInventory getInventory(int slot) { - return new FoliaInventory(bukkitView.getInventory(slot)); + return FoliaHelper.fromBukkitInventory(bukkitView.getInventory(slot)); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 1b543f3..3e8b019 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -22,7 +22,7 @@ public class FoliaItemStack implements EsItemStack { private final ItemStack bukkitItem; public FoliaItemStack(EsMaterial mat, int amount) { - bukkitItem = new org.bukkit.inventory.ItemStack(BukkitHelper.toBukkitMaterial(mat), amount); + bukkitItem = new ItemStack(BukkitHelper.toBukkitMaterial(mat), amount); } public FoliaItemStack(ItemStack child) { @@ -118,7 +118,7 @@ public int getDamage() { @SuppressWarnings("MethodDoesntCallSuperMethod") // I don't care, it doesn't work like that @Override public EsItemStack clone() { - return new FoliaItemStack(bukkitItem.clone()); + return FoliaHelper.fromBukkitItem(bukkitItem.clone()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index 1f43cdc..bde383d 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -78,7 +78,7 @@ public EsEntity getEntity(UUID uuid) { if (entity == null) { return null; } - return new FoliaEntity(entity); + return FoliaHelper.fromBukkitEntity(entity); } for (World world : Bukkit.getWorlds()) { @@ -126,15 +126,15 @@ public Collection getOnlinePlayers() { try { if (Bukkit.class.getMethod("getOnlinePlayers").getReturnType() == Collection.class) { List players = new ArrayList<>(); - for (org.bukkit.entity.Player p : Bukkit.getOnlinePlayers()) { + for (Player p : Bukkit.getOnlinePlayers()) { players.add(new FoliaPlayer(p)); } return players; } else { - org.bukkit.entity.Player[] players = (org.bukkit.entity.Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); + Player[] players = (Player[]) Bukkit.class.getMethod("getOnlinePlayers").invoke(null, new Object[0]); List users = new ArrayList<>(); - for (org.bukkit.entity.Player p : players) { + for (Player p : players) { users.add(new FoliaPlayer(p)); } return users; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java index 4eddf2d..518cf41 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaSign.java @@ -50,6 +50,6 @@ public String[] getLines() { @Override public EsSignSide getTargetSide(EsPlayer player) { - return new net.serble.estools.ServerApi.Implementations.Folia.FoliaSignSide(bukkitSign.getTargetSide(((net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer) player).getBukkitPlayer())); + return new FoliaSignSide(bukkitSign.getTargetSide(((FoliaPlayer) player).getBukkitPlayer())); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java index 8db17c6..7129165 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaWorld.java @@ -11,7 +11,7 @@ import java.util.List; public class FoliaWorld extends BukkitWorld { - private final org.bukkit.World bukkitWorld; + private final World bukkitWorld; public FoliaWorld(World world) { super(world); @@ -20,9 +20,9 @@ public FoliaWorld(World world) { @Override public List getEntities() { - List bEntities = bukkitWorld.getEntities(); + List bEntities = bukkitWorld.getEntities(); List entities = new ArrayList<>(); - for (org.bukkit.entity.Entity bEntity : bEntities) { + for (Entity bEntity : bEntities) { entities.add(FoliaHelper.fromBukkitEntity(bEntity)); } return entities; @@ -32,7 +32,7 @@ public List getEntities() { public List getNearbyEntities(EsLocation loc, double xoff, double yoff, double zoff) { Collection bEntities = bukkitWorld.getNearbyEntities(FoliaHelper.toBukkitLocation(loc), xoff, yoff, zoff); List entities = new ArrayList<>(); - for (org.bukkit.entity.Entity bEntity : bEntities) { + for (Entity bEntity : bEntities) { entities.add(FoliaHelper.fromBukkitEntity(bEntity)); } return entities; From da969817b956601c64b64bef870e885f442fe465 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 18:34:09 +1000 Subject: [PATCH 41/74] Added missing commands to /estools test --- src/main/java/net/serble/estools/Tester.java | 42 +++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index 0b66b51..aef2877 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -1,6 +1,7 @@ package net.serble.estools; import net.serble.estools.Commands.SafeTp; +import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsPlayer; import java.io.PrintWriter; @@ -16,6 +17,7 @@ public class Tester { @SuppressWarnings("unchecked") // It doesn't allow specifying type private static final Tuple[] routine = new Tuple[] { + new Tuple<>("music", 1), new Tuple<>("gmsp", 1.5), new Tuple<>("gmc", 1.5), new Tuple<>("gma", 1.5), @@ -80,8 +82,12 @@ public class Tester { new Tuple<>("lore add Silly &ldirt", 4), new Tuple<>("lore insert 1 &cNot very", 2), new Tuple<>("lore remove 1", 2), - new Tuple<>("music", 3), + new Tuple<>("rain", 3), + new Tuple<>("sun", 3), + new Tuple<>("thunder", 3), new Tuple<>("night", 1), + new Tuple<>("noon", 1), + new Tuple<>("midnight", 1), new Tuple<>("day", 1), new Tuple<>("potion speed", 2), new Tuple<>("rename &6This dirt is special", 4), @@ -96,8 +102,15 @@ public class Tester { new Tuple<>("getpersistentdata estools:hello string", 2), new Tuple<>("removepersistentdata estools:hello", 2), new Tuple<>("getpersistentdata estools:hello string", 2), + new Tuple<>("mount {entity}", 2), + new Tuple<>("dismount", 1), + new Tuple<>("dismount", 1), + new Tuple<>("mount {player}", 1), + new Tuple<>("mount {player} {entity}", 2), + new Tuple<>("dismount {entity}", 1), + new Tuple<>("sethealth 1", 0.1), new Tuple<>("buddha", -1), - new Tuple<>("suicide", 4), + new Tuple<>("suicide", 1), }; public Tester(EsPlayer p) { @@ -144,6 +157,31 @@ private void execNextCommand() { cmd = cmd.replace("{randomplayer}", randomPlayer.getName()); } + + if (cmd.contains("{entity}")) { + // Find the nearest entity + EsEntity nearestEntity = null; + double nearestDistance = 60000000; + + for (EsEntity en : player.getWorld().getEntities()) { + if (en instanceof EsPlayer) { + continue; + } + + double distance = player.getLocation().distanceTo(en.getLocation()); + if (distance < nearestDistance) { + nearestDistance = distance; + nearestEntity = en; + } + } + + if (nearestEntity == null) { + nearestEntity = player; + } + + cmd = cmd.replace("{entity}", nearestEntity.getUniqueId().toString()); + } + cmd = cmd.replace("{player}", player.getName()); try { From 4f72b663b6f2ebfe7a5d48388bc81026c55bbaf6 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 18:38:11 +1000 Subject: [PATCH 42/74] Fixed HideFlags error when you are not holding anything --- src/main/java/net/serble/estools/Commands/HideFlags.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/HideFlags.java b/src/main/java/net/serble/estools/Commands/HideFlags.java index 72e3167..6285b7a 100644 --- a/src/main/java/net/serble/estools/Commands/HideFlags.java +++ b/src/main/java/net/serble/estools/Commands/HideFlags.java @@ -2,6 +2,7 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.ServerApi.EsItemFlag; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; @@ -19,7 +20,7 @@ public boolean execute(EsCommandSender sender, String[] args) { EsItemStack is = ((EsPlayer) sender).getMainHand(); - if (is == null || Objects.equals(is.getType(), "AIR") || is.getItemMeta() == null) { + if (is == null || Objects.equals(is.getType(), EsMaterial.createUnchecked("AIR")) || is.getItemMeta() == null) { send(sender, "&cMust be a damageable item"); return false; } From 7099bed605291175542c1546e2e99d78e257c10c Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 18:46:05 +1000 Subject: [PATCH 43/74] Fixed Ench error when you are not holding anything --- src/main/java/net/serble/estools/Commands/Ench.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/Ench.java b/src/main/java/net/serble/estools/Commands/Ench.java index 0544d22..b4ddb0c 100644 --- a/src/main/java/net/serble/estools/Commands/Ench.java +++ b/src/main/java/net/serble/estools/Commands/Ench.java @@ -6,6 +6,7 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsEnchantment; +import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -46,7 +47,7 @@ public boolean execute(EsCommandSender sender, String[] args) { is = ((EsPlayer) sender).getMainHand(); - if (is == null || Objects.equals(is.getType(), "AIR")) { + if (is == null || Objects.equals(is.getType(), EsMaterial.createUnchecked("AIR"))) { send(sender, "&cYou must be holding an item to enchant it"); return false; } @@ -59,7 +60,7 @@ public boolean execute(EsCommandSender sender, String[] args) { is = p.getMainHand(); - if (is == null || Objects.equals(is.getType(), "AIR")) { + if (is == null || Objects.equals(is.getType(), EsMaterial.createUnchecked("AIR"))) { send(sender, "&c" + p.getName() + " isn't holding an item"); return false; } From 978a2aa49cf3105e3ca199c72f07e48d90038d1b Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 18:53:34 +1000 Subject: [PATCH 44/74] Fixed GetInfo amplifier starting at 0 --- src/main/java/net/serble/estools/Commands/GetInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/GetInfo.java b/src/main/java/net/serble/estools/Commands/GetInfo.java index 8ac1a78..c852b25 100644 --- a/src/main/java/net/serble/estools/Commands/GetInfo.java +++ b/src/main/java/net/serble/estools/Commands/GetInfo.java @@ -80,7 +80,7 @@ public boolean execute(EsCommandSender sender, String[] args) { potionEffects.append("None"); } else { potionEffects.append(potions.stream() - .map(pot -> String.format("%s at %s for %s seconds", pot.getType(), pot.getAmp(), pot.getDuration()/20)) + .map(pot -> String.format("%s at %s for %s seconds", pot.getType(), pot.getAmp()+1, pot.getDuration()/20)) .collect(Collectors.joining(", "))); } } From 5fb456210bd1487bf398cd1c728df7573a484376 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 19:08:47 +1000 Subject: [PATCH 45/74] Fix dumb Folia in bukkit error --- .../Implementations/Bukkit/Helpers/BukkitHelper.java | 3 +-- .../ServerApi/Implementations/Folia/FoliaHelper.java | 9 --------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index bdf4042..ee16ba8 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -5,7 +5,6 @@ import net.serble.estools.ServerApi.*; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.*; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayerInventory; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.block.Block; @@ -577,7 +576,7 @@ public static EsInventory fromBukkitInventory(Inventory inv) { } if (inv instanceof PlayerInventory) { - return new FoliaPlayerInventory((PlayerInventory) inv); + return new BukkitPlayerInventory((PlayerInventory) inv); } return new BukkitInventory(inv); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 4b40684..1e01499 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -229,15 +229,6 @@ public static EsBlock fromBukkitBlock(BlockState block) { return new FoliaBlock(block); } - public static boolean isFolia() { - try { - Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } - public static EsInventory fromBukkitInventory(Inventory inv) { if (inv == null) { return null; From f39d0852d4fcb2a5a92cb9e5f71b2c40ddf7adff Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 19:28:27 +1000 Subject: [PATCH 46/74] Fixed setItem with equipment slot checking the wrong version --- .../ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java index c4dbd8f..e7efa76 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayerInventory.java @@ -58,7 +58,7 @@ public EsItemStack getBoots() { @Override public void setItem(EsEquipmentSlot slot, EsItemStack item) { - if (Main.minecraftVersion.getMinor() <= 12) { + if (Main.minecraftVersion.getMinor() <= 14) { int slotId; switch (slot) { case Head: From 5b3c593a3c3698c999b660e2de3fee9886942396 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 19:38:45 +1000 Subject: [PATCH 47/74] Fixed give commands breaking in 1.13 specifically --- .../Implementations/Bukkit/Helpers/BukkitHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index ee16ba8..eea4ddc 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -342,7 +342,7 @@ public static EsPotionEffect fromBukkitPotionEffect(PotionEffect effect) { } public static Material toBukkitMaterial(EsMaterial mat) { - if (Main.minecraftVersion.getMinor() > 12) { + if (Main.minecraftVersion.getMinor() > 13) { return Registry.MATERIAL.get(NamespacedKey.minecraft(mat.getKey())); } @@ -350,7 +350,7 @@ public static Material toBukkitMaterial(EsMaterial mat) { } public static EsMaterial fromBukkitMaterial(Material mat) { - if (Main.minecraftVersion.getMinor() > 12) { + if (Main.minecraftVersion.getMinor() > 13) { return EsMaterial.createUnchecked(mat.getKey().getKey()); } From b49ada6914d8952c8f4815ff21df083af459a313 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 19:41:21 +1000 Subject: [PATCH 48/74] Fix dumb Folia in bukkit errors again --- .../Implementations/Bukkit/BukkitEventsListener.java | 2 +- .../Implementations/Bukkit/Helpers/BukkitHelper.java | 4 ++++ .../ServerApi/Implementations/Folia/FoliaEventsListener.java | 2 +- .../estools/ServerApi/Implementations/Folia/FoliaHelper.java | 4 ++++ .../estools/ServerApi/Implementations/Folia/FoliaPlayer.java | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java index aad0e70..e79a9ad 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java @@ -156,7 +156,7 @@ public void onDrag(InventoryDragEvent e) { @EventHandler public void onInteract(PlayerInteractEvent e) { - EsBlock cb = e.getClickedBlock() == null ? null : new BukkitBlock(e.getClickedBlock().getState()); + EsBlock cb = BukkitHelper.fromBukkitBlock(e.getClickedBlock()); EsPlayer p = new BukkitPlayer(e.getPlayer()); EsAction ac = BukkitHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index ee16ba8..ad77bf3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -231,6 +231,10 @@ public static EsTeleportCause fromBukkitTeleportCause(PlayerTeleportEvent.Telepo } public static EsBlock fromBukkitBlock(Block block) { + if (block == null) { + return null; + } + BlockState state = block.getState(); return fromBukkitBlock(state); } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index 41f2771..f762bee 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -148,7 +148,7 @@ public void onDrag(InventoryDragEvent e) { @EventHandler public void onInteract(PlayerInteractEvent e) { - EsBlock cb = e.getClickedBlock() == null ? null : new FoliaBlock(e.getClickedBlock().getState()); + EsBlock cb = FoliaHelper.fromBukkitBlock(e.getClickedBlock()); EsPlayer p = new FoliaPlayer(e.getPlayer()); EsAction ac = FoliaHelper.fromBukkitAction(e.getAction()); EsPlayerInteractEvent ee = new EsPlayerInteractEvent(p, cb, ac); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 1e01499..f2c4784 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -186,6 +186,10 @@ public static EsEntity fromBukkitEntity(Entity entity) { } public static EsBlock fromBukkitBlock(Block block) { + if (block == null) { + return null; + } + BlockState state = block.getState(); if (state instanceof Sign) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java index 760a5f9..ecc1ed3 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaPlayer.java @@ -104,7 +104,7 @@ public EsGameMode getGameMode() { return null; } - return FoliaHelper.fromBukkitBlock(target.getState()); + return FoliaHelper.fromBukkitBlock(target); } @Override From 51d98d2a4c8364266632c3cdfd0f49f4cda78735 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 20:07:07 +1000 Subject: [PATCH 49/74] Fix signs and remove Vault support --- src/main/java/net/serble/estools/Main.java | 8 +-- .../net/serble/estools/Signs/Balance.java | 13 ---- .../java/net/serble/estools/Signs/Feed.java | 4 -- .../java/net/serble/estools/Signs/Give.java | 5 -- .../java/net/serble/estools/Signs/Heal.java | 4 -- .../java/net/serble/estools/Signs/Repair.java | 4 -- .../java/net/serble/estools/Signs/Sell.java | 35 ----------- .../net/serble/estools/Signs/SignMain.java | 2 - .../net/serble/estools/Signs/SignType.java | 25 -------- src/main/java/net/serble/estools/Vault.java | 62 ------------------- 10 files changed, 1 insertion(+), 161 deletions(-) delete mode 100644 src/main/java/net/serble/estools/Signs/Balance.java delete mode 100644 src/main/java/net/serble/estools/Signs/Sell.java delete mode 100644 src/main/java/net/serble/estools/Vault.java diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index dfe7f20..433e0ac 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -67,12 +67,6 @@ public void enable() { if (platform == ServerPlatform.Bukkit) { // We have to support old configs BukkitConfigMigrator.checkPerformMigration(); - - try { - Vault.setupEconomy(); - } catch (Exception e) { - logger.warning("No Vault plugin found, please install vault for economy functionality."); - } } // Load the config @@ -81,7 +75,7 @@ public void enable() { // Metrics if (config.isMetrics() && platform.supportsMetrics()) { Metrics metrics = new Metrics(EsToolsBukkit.plugin, bStatsId); - metrics.addCustomChart(new SimplePie("vault_enabled", () -> String.valueOf(Vault.economy != null))); + metrics.addCustomChart(new SimplePie("vault_enabled", () -> "false")); logger.info("Started bStat metrics"); } else { logger.info("Metrics are disabled"); diff --git a/src/main/java/net/serble/estools/Signs/Balance.java b/src/main/java/net/serble/estools/Signs/Balance.java deleted file mode 100644 index 4787041..0000000 --- a/src/main/java/net/serble/estools/Signs/Balance.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.serble.estools.Signs; - -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import net.serble.estools.Vault; - -public class Balance extends SignType { - - @Override - public void run(EsPlayer p, String[] lines) { - send(p, "&aYour current balance is &6$%s", String.valueOf(Vault.economy.getBalance(((FoliaPlayer)p).getBukkitPlayer()))); - } -} diff --git a/src/main/java/net/serble/estools/Signs/Feed.java b/src/main/java/net/serble/estools/Signs/Feed.java index 11e976f..a50ca5a 100644 --- a/src/main/java/net/serble/estools/Signs/Feed.java +++ b/src/main/java/net/serble/estools/Signs/Feed.java @@ -6,10 +6,6 @@ public class Feed extends SignType { @Override public void run(EsPlayer p, String[] lines) { - if (!takeMoney(lines[1], p)) { - return; - } - p.setFoodLevel(20); p.setSaturation(6); } diff --git a/src/main/java/net/serble/estools/Signs/Give.java b/src/main/java/net/serble/estools/Signs/Give.java index e6a1a3d..e42e71e 100644 --- a/src/main/java/net/serble/estools/Signs/Give.java +++ b/src/main/java/net/serble/estools/Signs/Give.java @@ -19,11 +19,6 @@ public void run(EsPlayer p, String[] lines) { return; } - if (!takeMoney(lines[3], p)) { - send(p, "&cYou cannot afford this!"); - return; - } - p.getInventory().addItem(item.clone()); } } diff --git a/src/main/java/net/serble/estools/Signs/Heal.java b/src/main/java/net/serble/estools/Signs/Heal.java index b5d592b..3e76b5f 100644 --- a/src/main/java/net/serble/estools/Signs/Heal.java +++ b/src/main/java/net/serble/estools/Signs/Heal.java @@ -6,10 +6,6 @@ public class Heal extends SignType { @Override public void run(EsPlayer p, String[] lines) { - if (!takeMoney(lines[1], p)) { - return; - } - p.setHealth(p.getMaxHealth()); p.setOnFireTicks(0); } diff --git a/src/main/java/net/serble/estools/Signs/Repair.java b/src/main/java/net/serble/estools/Signs/Repair.java index 453ab2e..d8348bb 100644 --- a/src/main/java/net/serble/estools/Signs/Repair.java +++ b/src/main/java/net/serble/estools/Signs/Repair.java @@ -8,10 +8,6 @@ public class Repair extends SignType { @Override public void run(EsPlayer p, String[] lines) { - if (!takeMoney(lines[1], p)) { - return; - } - EsItemStack item = p.getMainHand(); Fix.repair(item); } diff --git a/src/main/java/net/serble/estools/Signs/Sell.java b/src/main/java/net/serble/estools/Signs/Sell.java deleted file mode 100644 index 74f1618..0000000 --- a/src/main/java/net/serble/estools/Signs/Sell.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.serble.estools.Signs; - -import net.serble.estools.Commands.Give.Give; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; - -public class Sell extends SignType { - - @Override - public void run(EsPlayer p, String[] lines) { - int amount = 1; - try { - amount = Integer.parseInt(lines[3]); - } catch (Exception e) { /* ignore */ } - - EsItemStack item = Give.getItem(lines[1], amount); - if (item == null) { - send(p, "&cItem not found!"); - return; - } - - for (EsItemStack e : p.getInventory().getContents()) { - if (!e.getType().equals(item.getType()) || e.getAmount() < item.getAmount()) { - continue; - } - - if (payMoney(lines[2], p)) { - e.setAmount(e.getAmount() - item.getAmount()); - } - return; - } - - send(p, "&cCant find item!"); - } -} diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index 8889a56..f4e90d6 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -21,9 +21,7 @@ public static void init() { addSign(new Give(), "[give]", EsToolsCommand.translate("&1[Give]")); addSign(new Heal(), "[heal]", EsToolsCommand.translate("&1[Heal]")); addSign(new Feed(), "[feed]", EsToolsCommand.translate("&1[Feed]")); - addSign(new Balance(), "[balance]", EsToolsCommand.translate("&1[Balance]")); addSign(new Repair(), "[repair]", EsToolsCommand.translate("&1[Repair]")); - addSign(new Sell(), "[sell]", EsToolsCommand.translate("&1[Sell]")); Main.registerEvents(new SignMain()); } diff --git a/src/main/java/net/serble/estools/Signs/SignType.java b/src/main/java/net/serble/estools/Signs/SignType.java index 74d839a..65dc5e0 100644 --- a/src/main/java/net/serble/estools/Signs/SignType.java +++ b/src/main/java/net/serble/estools/Signs/SignType.java @@ -3,35 +3,10 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import net.serble.estools.Vault; public abstract class SignType { public void run(EsPlayer p, String[] lines) { } - public static double getSignMoney(String line, EsPlayer p) { - double price = 0; - try { - String priceString = line.substring(1); - - if (!line.startsWith("$")) { - send(p, "&cMoney must be formatted with \"$COST\""); - return -1; - } - - price = Double.parseDouble(priceString); - } catch (NumberFormatException e) { /* Ignored */ } - - return price; - } - - public static boolean takeMoney(String line, EsPlayer p) { - return Vault.takeMoney(getSignMoney(line, p), p); - } - - public static boolean payMoney(String line, EsPlayer p) { - return Vault.payMoney(getSignMoney(line, p), p); - } - public static void send(EsCommandSender s, String msg, Object... args) { EsToolsCommand.send(s, msg, args); } diff --git a/src/main/java/net/serble/estools/Vault.java b/src/main/java/net/serble/estools/Vault.java deleted file mode 100644 index 182226b..0000000 --- a/src/main/java/net/serble/estools/Vault.java +++ /dev/null @@ -1,62 +0,0 @@ -package net.serble.estools; - -import net.milkbowl.vault.economy.Economy; -import net.milkbowl.vault.economy.EconomyResponse; -import net.serble.estools.ServerApi.Implementations.Folia.FoliaPlayer; -import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import org.bukkit.Bukkit; -import org.bukkit.plugin.RegisteredServiceProvider; - -public class Vault { - public static Economy economy = null; - - public static void setupEconomy() { - if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) { - Bukkit.getLogger().warning("No Vault plugin found, please install vault for economy functionality"); - return; - } - - RegisteredServiceProvider rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); - if (rsp == null) { - Bukkit.getLogger().warning("No vault service found, make sure vault and a economy plugin is installed!"); - return; - } - - economy = rsp.getProvider(); - - //noinspection ConstantValue - if (economy == null) { - Bukkit.getLogger().warning("Vault functionality failed to initialise"); - } - - } - - public static boolean takeMoney(double price, EsPlayer p) { - if (price <= 0) { - return true; - } - - if (Vault.economy == null) { - EsToolsCommand.send(p, "&cVault is required for economy!"); - return false; - } - - if (Vault.economy.getBalance(((FoliaPlayer) p).getBukkitPlayer()) < price) { - EsToolsCommand.send(p, "&cYou do not have enough money to purchase this item."); - return false; - } - - Vault.economy.withdrawPlayer(((FoliaPlayer) p).getBukkitPlayer(), price); - return true; - } - - public static boolean payMoney(double price, EsPlayer p) { - if (Vault.economy == null) { - EsToolsCommand.send(p, "&cVault is required for economy!"); - return false; - } - - EconomyResponse economyResponse = Vault.economy.depositPlayer(((FoliaPlayer) p).getBukkitPlayer(), price); - return economyResponse.transactionSuccess(); - } -} From d80407d722e728c767f532493af875321814c2a3 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 20:11:07 +1000 Subject: [PATCH 50/74] Fixed give commands breaking in 1.13 specifically --- .../Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java index 44dd7eb..c158209 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnchantmentHelper.java @@ -61,7 +61,7 @@ public static Set getEnchantmentList() { if (Main.minecraftVersion.getMinor() >= 13) { name = ench.getKey().getKey(); } else { - name = ench.getName(); + name = ench.getName().toLowerCase(); if (nameReplacers.containsKey(name)) { name = nameReplacers.get(name); From cc1b2a75fdbffec39a46b2c43cb0a4aebd8a4209 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 20:24:48 +1000 Subject: [PATCH 51/74] Fix mount error on invalid rider --- src/main/java/net/serble/estools/Commands/Mount.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/Mount.java b/src/main/java/net/serble/estools/Commands/Mount.java index 45f1a7a..fd7a1fc 100644 --- a/src/main/java/net/serble/estools/Commands/Mount.java +++ b/src/main/java/net/serble/estools/Commands/Mount.java @@ -25,7 +25,11 @@ public boolean execute(EsCommandSender sender, String[] args) { List riders = new ArrayList<>(); for (int i = 1; i < args.length; i++) { - riders.add(getNonLivingEntity(sender, args[i])); + EsEntity rider = getNonLivingEntity(sender, args[i]); + if (rider == null) { + return false; + } + riders.add(rider); } if (riders.isEmpty()) { From 6349fd0ea19f68574bea62049da3b7df77621c31 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 20:34:54 +1000 Subject: [PATCH 52/74] Fixed getInventory having wrong version for compat in 1.9-1.10 --- .../ServerApi/Implementations/Bukkit/BukkitInventoryView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java index 3901034..61e8085 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java @@ -27,7 +27,7 @@ public EsInventory getBottomInventory() { @Override public EsInventory getInventory(int slot) { - if (Main.minecraftVersion.getMinor() <= 7) { // .getInventory(slot) doesn't exist + if (Main.minecraftVersion.getMinor() <= 10) { // .getInventory(slot) doesn't exist if (slot >= getTopInventory().getSize()) { // Bottom inv return getBottomInventory(); } From 2acd27e3671efd3d0c4b7bfdc54a775ea906e992 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 20:37:07 +1000 Subject: [PATCH 53/74] Fixed /back not seeing /tp as a valid teleport reason --- src/main/java/net/serble/estools/Commands/Back.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/Back.java b/src/main/java/net/serble/estools/Commands/Back.java index d3962ef..50a9c64 100644 --- a/src/main/java/net/serble/estools/Commands/Back.java +++ b/src/main/java/net/serble/estools/Commands/Back.java @@ -58,7 +58,7 @@ public void executeEvent(EsEvent event) { if (event instanceof EsPlayerTeleportEvent) { EsPlayerTeleportEvent e = (EsPlayerTeleportEvent) event; if (Main.minecraftVersion.getMinor() > 9 && - !equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin)) { + !equalsOr(e.getCause(), EsTeleportCause.Command, EsTeleportCause.Plugin, EsTeleportCause.Unknown)) { return; } prevLocations.put(e.getPlayer().getUniqueId(), e.getPlayer().getLocation()); From 4f2696a7ed0cb3546e784cdc0c7b1f3af0a36936 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 20:53:38 +1000 Subject: [PATCH 54/74] Fix tester invalid coords --- src/main/java/net/serble/estools/Tester.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index aef2877..f51aa36 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -39,10 +39,10 @@ public class Tester { new Tuple<>("tpall", 2), new Tuple<>("warps add test", 2), new Tuple<>("safetp", 2), - new Tuple<>("tp ~50 ~4000 ~50", 1), + new Tuple<>("tp ~50 512 ~50", 1), new Tuple<>("warp test", 2), new Tuple<>("warps remove test", 2), - new Tuple<>("tp ~50 ~4000 ~50", 1), + new Tuple<>("tp ~50 512 ~50", 1), new Tuple<>("back", 1), new Tuple<>("cchest", -1), new Tuple<>("ci", 1), From ce9a4f37bc63f78533b46c0a66382d59c3b7f9c2 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 20:57:17 +1000 Subject: [PATCH 55/74] Fix get entity failing --- .../estools/ServerApi/Implementations/Bukkit/BukkitServer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 546ac7d..cd07a92 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -88,7 +88,7 @@ public EsEntity getEntity(UUID uuid) { for (World world : Bukkit.getWorlds()) { for (Entity entity : world.getEntities()) { - if (entity.getUniqueId() == uuid) { + if (entity.getUniqueId().equals(uuid)) { return BukkitHelper.fromBukkitEntity(entity); } } From 10f953c0e3c89e2197df5affdbda53af033e042f Mon Sep 17 00:00:00 2001 From: Calcilore Date: Fri, 21 Jun 2024 21:35:48 +1000 Subject: [PATCH 56/74] Fixed potion amplifiers starting at 1 in older versions --- src/main/java/net/serble/estools/Commands/Potion.java | 5 +++++ .../Implementations/Bukkit/BukkitServer.java | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/Commands/Potion.java b/src/main/java/net/serble/estools/Commands/Potion.java index dd69c17..6e14ad0 100644 --- a/src/main/java/net/serble/estools/Commands/Potion.java +++ b/src/main/java/net/serble/estools/Commands/Potion.java @@ -37,6 +37,11 @@ public boolean execute(EsCommandSender sender, String[] args) { if (args.length >= 2) { // amp 0 is level 1, so subtract 1 from player input amp = tryParseInt(args[1], 1) - 1; + + if (Main.minecraftVersion.getMinor() <= 7 && amp < 0 || amp > 1) { + send(sender, "&cAmplifier must be 1 or 2!"); + return false; + } } int duration = 60*20*5; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index cd07a92..e0f001e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -177,7 +177,16 @@ public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amoun return null; } - Potion potion = new Potion(type, effect.getAmp()); + // in 1.7 and below potions start at amplifier 1, and can only be 1 or 2, after that it starts at 0 + int amplifier = effect.getAmp(); + if (Main.minecraftVersion.getMinor() <= 7) { + amplifier++; + if (amplifier < 1 || amplifier > 2) { + return null; + } + } + + Potion potion = new Potion(type, amplifier); potion.setSplash(potType == EsPotType.splash); return new BukkitPotion(potion.toItemStack(amount)); From 6695ab08a7bc07fb0c3a330d4fa8682230fbdf58 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 21:41:35 +1000 Subject: [PATCH 57/74] Fix locations on old versions (and remove unneeded code), test versions --- TestedVersions.txt | 11 +++++++++-- .../Implementations/Bukkit/Helpers/BukkitHelper.java | 1 - 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/TestedVersions.txt b/TestedVersions.txt index 604b4a7..9790dba 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -2,12 +2,19 @@ Folia: 1.20.4 Failing (/estools test) Bukkit: -1.20.4 +1.21 +1.20.6 1.19.4 1.18.2 1.17.1 1.16.1 +1.15.2 1.14.4 +1.13.2 1.12.2 1.11.2 -1.10.2 \ No newline at end of file +1.10.2 +1.9.4 +1.8.8 +1.7.10 +1.6.4 \ No newline at end of file diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 33d2faa..4c6e176 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -37,7 +37,6 @@ public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { Location bLoc = new Location(Bukkit.getWorld(loc.getWorld().getName()), loc.getX(), loc.getY(), loc.getZ()); - if (loc.getDirection() != null) bLoc.setDirection(toVector(loc.getDirection())); bLoc.setPitch((float) loc.getPitch()); bLoc.setYaw((float) loc.getYaw()); return bLoc; From 659bf7ae281ed648d0979bce038a3f5bb547a8be Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 21:45:33 +1000 Subject: [PATCH 58/74] Change SemanticVersion methods --- .../net/serble/estools/SemanticVersion.java | 29 ++++++++++++------- .../Implementations/Bukkit/BukkitServer.java | 2 +- .../Bukkit/Helpers/BukkitEffectHelper.java | 4 +-- .../Bukkit/Helpers/BukkitHelper.java | 5 ++-- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/serble/estools/SemanticVersion.java b/src/main/java/net/serble/estools/SemanticVersion.java index e2bd488..a83fbb2 100644 --- a/src/main/java/net/serble/estools/SemanticVersion.java +++ b/src/main/java/net/serble/estools/SemanticVersion.java @@ -38,34 +38,43 @@ public String getString() { } public boolean isLowerThan(SemanticVersion other) { - if (getMajor() < other.getMajor()) { + return isLowerThan(other.major, other.minor, other.patch); + } + + public boolean isLowerThan(int major, int minor, int patch) { + if (getMajor() < major) { return true; - } else if (getMajor() > other.getMajor()) { + } else if (getMajor() > major) { return false; } - if (getMinor() < other.getMinor()) { + if (getMinor() < minor) { return true; - } else if (getMinor() > other.getMinor()) { + } else if (getMinor() > minor) { return false; } - return getPatch() < other.getPatch(); + return getPatch() < patch; } + @SuppressWarnings("unused") // Future-proof public boolean isAtLeast(SemanticVersion other) { - if (getMajor() < other.getMajor()) { + return isAtLeast(other.major, other.minor, other.patch); + } + + public boolean isAtLeast(int major, int minor, int patch) { + if (getMajor() < major) { return false; - } else if (getMajor() > other.getMajor()) { + } else if (getMajor() > major) { return true; } - if (getMinor() < other.getMinor()) { + if (getMinor() < minor) { return false; - } else if (getMinor() > other.getMinor()) { + } else if (getMinor() > minor) { return true; } - return getPatch() >= other.getPatch(); + return getPatch() >= patch; } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index e0f001e..811d6fd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -54,7 +54,7 @@ public void initialise() { materials.add(esMat); } - if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + if (Main.minecraftVersion.isAtLeast(1, 16, 4)) { for (Sound sound : Registry.SOUNDS) { EsSound esSound = EsSound.createUnchecked(sound.getKey().getKey()); sounds.add(esSound); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java index 4d3fc85..96bb11e 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEffectHelper.java @@ -1,7 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Main; -import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsPotionEffectType; import org.bukkit.Registry; import org.bukkit.potion.PotionEffectType; @@ -36,6 +35,7 @@ public static Set getPotionList() { return OLD_POTIONS.keySet(); } + @SuppressWarnings("deprecation") // It's for old versions public static void load() { // in 1.8 and below, there were no custom potions, just some hardcoded ones. // these are PotionTypes, and we need to generate them based on EsPotionEffectTypes @@ -77,7 +77,7 @@ public static void load() { }}; // Registry.EFFECT was added in 1.20.3... - if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 20, 3))) { + if (Main.minecraftVersion.isAtLeast(1, 20, 3)) { for (PotionEffectType type : Registry.EFFECT) { EsPotionEffectType esType = EsPotionEffectType.createUnchecked(type.getKey().getKey()); ES_TO_BUKKIT.put(esType, type); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 4c6e176..624edfd 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -1,7 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.Entrypoints.EsToolsBukkit; -import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.*; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.*; @@ -54,7 +53,7 @@ public static EsLocation fromBukkitLocation(Location loc) { } public static Sound toBukkitSound(EsSound sound) { - if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + if (Main.minecraftVersion.isAtLeast(1, 16, 4)) { return Registry.SOUNDS.get(NamespacedKey.minecraft(sound.getKey())); } else { return Sound.valueOf(BukkitSoundEnumConverter.convertKeyToEnum(sound)); @@ -62,7 +61,7 @@ public static Sound toBukkitSound(EsSound sound) { } public static EsSound fromBukkitSound(Sound sound) { - if (Main.minecraftVersion.isAtLeast(new SemanticVersion(1, 16, 4))) { + if (Main.minecraftVersion.isAtLeast(1, 16, 4)) { return EsSound.createUnchecked(sound.getKey().getKey()); } else { return BukkitSoundEnumConverter.convertEnumToKey(sound.name()); From df967ebf6d11829e255e4c7e74e8c1bd9732107b Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 21 Jun 2024 22:01:50 +1000 Subject: [PATCH 59/74] Remove Vault softdepend --- src/main/resources/plugin.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4346032..dbf9a94 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ main: net.serble.estools.Entrypoints.EsToolsBukkit name: EsTools description: Simple highly compatible general purpose plugin. version: '${project.version}' -softdepend: [Vault] +softdepend: [] authors: [Calcilator, CoPokBl] api-version: 1.13 folia-supported: true From e14e460d664c1519387f235280deadbd7f29f48c Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 00:22:30 +1000 Subject: [PATCH 60/74] Fix 1.4 support, events not working --- .../Implementations/Bukkit/BukkitServer.java | 8 +- .../BukkitEventsListener.java | 43 +++-- .../BukkitEventsListenerPost1_4.java | 30 ++++ .../Bukkit/Helpers/BukkitHelper.java | 162 ----------------- .../Helpers/BukkitInventoryClickHelper.java | 169 ++++++++++++++++++ .../Implementations/Folia/FoliaHelper.java | 164 +++++++++++++++++ 6 files changed, 395 insertions(+), 181 deletions(-) rename src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/{ => EventHandlers}/BukkitEventsListener.java (86%) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 811d6fd..5ea0b95 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -3,6 +3,8 @@ import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListener; +import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListenerPost1_4; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; @@ -32,7 +34,11 @@ public class BukkitServer implements EsServer { public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; - listener = new BukkitEventsListener(); + if (getVersion().isAtLeast(1, 5, 0)) { + listener = new BukkitEventsListenerPost1_4(); + } else { + listener = new BukkitEventsListener(); + } } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java similarity index 86% rename from src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java rename to src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java index e79a9ad..e0d063f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java @@ -1,4 +1,4 @@ -package net.serble.estools.ServerApi.Implementations.Bukkit; +package net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers; import net.serble.estools.Main; import net.serble.estools.ServerApi.EsAction; @@ -6,8 +6,10 @@ import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEquipmentSlotHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitInventoryClickHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -23,7 +25,6 @@ import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; @@ -31,7 +32,6 @@ import java.lang.reflect.InvocationTargetException; import java.util.Objects; -import java.util.Set; public class BukkitEventsListener implements Listener, CommandExecutor { @@ -110,10 +110,29 @@ public void onEntityDamage(EntityDamageEvent e) { @EventHandler public void onInvClick(InventoryClickEvent e) { - EsInventory clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); + EsInventory clInv; + if (Main.minecraftVersion.isAtLeast(1, 6, 0)) { + clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); + } else { + clInv = BukkitHelper.fromBukkitInventory(e.getInventory()); // Get clicked inv doesn't exist + } + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); - EsClickType ct = BukkitHelper.fromBukkitClickType(e.getClick()); - EsInventoryAction ac = BukkitHelper.fromBukkitInventoryAction(e.getAction()); + + EsClickType ct; + if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { + ct = BukkitInventoryClickHelper.fromBukkitClickType(e.getClick()); + } else { + ct = EsClickType.Unknown; // getClick() doesn't exist in 1.4.x + } + + EsInventoryAction ac; + if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { + ac = BukkitInventoryClickHelper.fromBukkitInventoryAction(e.getAction()); + } else { + ac = EsInventoryAction.Unknown; // getAction() doesn't exist in 1.4.x + } + EsItemStack ci = BukkitHelper.fromBukkitItem(e.getCurrentItem()); EsPlayer cl = new BukkitPlayer((Player) e.getWhoClicked()); EsItemStack cu = BukkitHelper.fromBukkitItem(e.getCursor()); @@ -142,18 +161,6 @@ public void onInvClose(InventoryCloseEvent e) { Main.callEvent(ee); } - @EventHandler - public void onDrag(InventoryDragEvent e) { - EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); - EsPlayer pl = new BukkitPlayer((Player) e.getWhoClicked()); - Set cs = e.getRawSlots(); - EsInventoryView view = new BukkitInventoryView(e.getView()); - EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); - ee.setCancelled(e.isCancelled()); - Main.callEvent(ee); - e.setCancelled(ee.isCancelled()); - } - @EventHandler public void onInteract(PlayerInteractEvent e) { EsBlock cb = BukkitHelper.fromBukkitBlock(e.getClickedBlock()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java new file mode 100644 index 0000000..a613054 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java @@ -0,0 +1,30 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.Events.EsInventoryDragEvent; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitInventoryView; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsInventoryView; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryDragEvent; + +import java.util.Set; + +public class BukkitEventsListenerPost1_4 extends BukkitEventsListener { + + @EventHandler + public void onDrag(InventoryDragEvent e) { + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); + EsPlayer pl = new BukkitPlayer((Player) e.getWhoClicked()); + Set cs = e.getRawSlots(); + EsInventoryView view = new BukkitInventoryView(e.getView()); + EsInventoryDragEvent ee = new EsInventoryDragEvent(pl, inv, cs, view); + ee.setCancelled(e.isCancelled()); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 624edfd..bff19c4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -17,8 +17,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.block.Action; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -368,166 +366,6 @@ public static boolean isFolia() { } } - public static EsClickType fromBukkitClickType(ClickType click) { - switch (click) { - case CREATIVE: - return EsClickType.Creative; - case DROP: - return EsClickType.Drop; - case LEFT: - return EsClickType.Left; - case RIGHT: - return EsClickType.Right; - case SHIFT_LEFT: - return EsClickType.ShiftLeft; - case SHIFT_RIGHT: - return EsClickType.ShiftRight; - case MIDDLE: - return EsClickType.Middle; - case UNKNOWN: - return EsClickType.Unknown; - case NUMBER_KEY: - return EsClickType.NumberKey; - case CONTROL_DROP: - return EsClickType.ControlDrop; - case DOUBLE_CLICK: - return EsClickType.DoubleClick; - case SWAP_OFFHAND: - return EsClickType.SwapOffhand; - case WINDOW_BORDER_LEFT: - return EsClickType.WindowBorderLeft; - case WINDOW_BORDER_RIGHT: - return EsClickType.WindowBorderRight; - default: - throw new RuntimeException("Invalid click type"); - } - } - - public static ClickType toBukkitClickType(EsClickType esClick) { - switch (esClick) { - case Creative: - return ClickType.CREATIVE; - case Drop: - return ClickType.DROP; - case Left: - return ClickType.LEFT; - case Right: - return ClickType.RIGHT; - case ShiftLeft: - return ClickType.SHIFT_LEFT; - case ShiftRight: - return ClickType.SHIFT_RIGHT; - case Middle: - return ClickType.MIDDLE; - case Unknown: - return ClickType.UNKNOWN; - case NumberKey: - return ClickType.NUMBER_KEY; - case ControlDrop: - return ClickType.CONTROL_DROP; - case DoubleClick: - return ClickType.DOUBLE_CLICK; - case SwapOffhand: - return ClickType.SWAP_OFFHAND; - case WindowBorderLeft: - return ClickType.WINDOW_BORDER_LEFT; - case WindowBorderRight: - return ClickType.WINDOW_BORDER_RIGHT; - default: - throw new IllegalArgumentException("Invalid EsClickType"); - } - } - - public static EsInventoryAction fromBukkitInventoryAction(InventoryAction action) { - switch (action) { - case NOTHING: - return EsInventoryAction.Nothing; - case PICKUP_ALL: - return EsInventoryAction.PickupAll; - case PICKUP_SOME: - return EsInventoryAction.PickupSome; - case PICKUP_HALF: - return EsInventoryAction.PickupHalf; - case PICKUP_ONE: - return EsInventoryAction.PickupOne; - case PLACE_ALL: - return EsInventoryAction.PlaceAll; - case PLACE_SOME: - return EsInventoryAction.PlaceSome; - case PLACE_ONE: - return EsInventoryAction.PlaceOne; - case SWAP_WITH_CURSOR: - return EsInventoryAction.SwapWithCursor; - case DROP_ALL_CURSOR: - return EsInventoryAction.DropAllCursor; - case DROP_ONE_CURSOR: - return EsInventoryAction.DropOneCursor; - case DROP_ALL_SLOT: - return EsInventoryAction.DropAllSlot; - case DROP_ONE_SLOT: - return EsInventoryAction.DropOneSlot; - case MOVE_TO_OTHER_INVENTORY: - return EsInventoryAction.MoveToOtherInventory; - case HOTBAR_MOVE_AND_READD: - return EsInventoryAction.HotbarMoveAndReadd; - case HOTBAR_SWAP: - return EsInventoryAction.HotbarSwap; - case CLONE_STACK: - return EsInventoryAction.CloneStack; - case COLLECT_TO_CURSOR: - return EsInventoryAction.CollectToCursor; - case UNKNOWN: - return EsInventoryAction.Unknown; - default: - throw new IllegalArgumentException("Invalid InventoryAction"); - } - } - - public static InventoryAction toBukkitInventoryAction(EsInventoryAction esAction) { - switch (esAction) { - case Nothing: - return InventoryAction.NOTHING; - case PickupAll: - return InventoryAction.PICKUP_ALL; - case PickupSome: - return InventoryAction.PICKUP_SOME; - case PickupHalf: - return InventoryAction.PICKUP_HALF; - case PickupOne: - return InventoryAction.PICKUP_ONE; - case PlaceAll: - return InventoryAction.PLACE_ALL; - case PlaceSome: - return InventoryAction.PLACE_SOME; - case PlaceOne: - return InventoryAction.PLACE_ONE; - case SwapWithCursor: - return InventoryAction.SWAP_WITH_CURSOR; - case DropAllCursor: - return InventoryAction.DROP_ALL_CURSOR; - case DropOneCursor: - return InventoryAction.DROP_ONE_CURSOR; - case DropAllSlot: - return InventoryAction.DROP_ALL_SLOT; - case DropOneSlot: - return InventoryAction.DROP_ONE_SLOT; - case MoveToOtherInventory: - return InventoryAction.MOVE_TO_OTHER_INVENTORY; - case HotbarMoveAndReadd: - return InventoryAction.HOTBAR_MOVE_AND_READD; - case HotbarSwap: - return InventoryAction.HOTBAR_SWAP; - case CloneStack: - return InventoryAction.CLONE_STACK; - case CollectToCursor: - return InventoryAction.COLLECT_TO_CURSOR; - case Unknown: - return InventoryAction.UNKNOWN; - default: - throw new IllegalArgumentException("Invalid EsInventoryAction"); - } - } - public static SoundCategory toBukkitSoundCategory(EsSoundCategory esSoundCategory) { switch (esSoundCategory) { case Master: diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java new file mode 100644 index 0000000..a16c476 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java @@ -0,0 +1,169 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsInventoryAction; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; + +@SuppressWarnings("unused") +public class BukkitInventoryClickHelper { + public static EsInventoryAction fromBukkitInventoryAction(InventoryAction action) { + switch (action) { + case NOTHING: + return EsInventoryAction.Nothing; + case PICKUP_ALL: + return EsInventoryAction.PickupAll; + case PICKUP_SOME: + return EsInventoryAction.PickupSome; + case PICKUP_HALF: + return EsInventoryAction.PickupHalf; + case PICKUP_ONE: + return EsInventoryAction.PickupOne; + case PLACE_ALL: + return EsInventoryAction.PlaceAll; + case PLACE_SOME: + return EsInventoryAction.PlaceSome; + case PLACE_ONE: + return EsInventoryAction.PlaceOne; + case SWAP_WITH_CURSOR: + return EsInventoryAction.SwapWithCursor; + case DROP_ALL_CURSOR: + return EsInventoryAction.DropAllCursor; + case DROP_ONE_CURSOR: + return EsInventoryAction.DropOneCursor; + case DROP_ALL_SLOT: + return EsInventoryAction.DropAllSlot; + case DROP_ONE_SLOT: + return EsInventoryAction.DropOneSlot; + case MOVE_TO_OTHER_INVENTORY: + return EsInventoryAction.MoveToOtherInventory; + case HOTBAR_MOVE_AND_READD: + return EsInventoryAction.HotbarMoveAndReadd; + case HOTBAR_SWAP: + return EsInventoryAction.HotbarSwap; + case CLONE_STACK: + return EsInventoryAction.CloneStack; + case COLLECT_TO_CURSOR: + return EsInventoryAction.CollectToCursor; + case UNKNOWN: + return EsInventoryAction.Unknown; + default: + throw new IllegalArgumentException("Invalid InventoryAction"); + } + } + + public static InventoryAction toBukkitInventoryAction(EsInventoryAction esAction) { + switch (esAction) { + case Nothing: + return InventoryAction.NOTHING; + case PickupAll: + return InventoryAction.PICKUP_ALL; + case PickupSome: + return InventoryAction.PICKUP_SOME; + case PickupHalf: + return InventoryAction.PICKUP_HALF; + case PickupOne: + return InventoryAction.PICKUP_ONE; + case PlaceAll: + return InventoryAction.PLACE_ALL; + case PlaceSome: + return InventoryAction.PLACE_SOME; + case PlaceOne: + return InventoryAction.PLACE_ONE; + case SwapWithCursor: + return InventoryAction.SWAP_WITH_CURSOR; + case DropAllCursor: + return InventoryAction.DROP_ALL_CURSOR; + case DropOneCursor: + return InventoryAction.DROP_ONE_CURSOR; + case DropAllSlot: + return InventoryAction.DROP_ALL_SLOT; + case DropOneSlot: + return InventoryAction.DROP_ONE_SLOT; + case MoveToOtherInventory: + return InventoryAction.MOVE_TO_OTHER_INVENTORY; + case HotbarMoveAndReadd: + return InventoryAction.HOTBAR_MOVE_AND_READD; + case HotbarSwap: + return InventoryAction.HOTBAR_SWAP; + case CloneStack: + return InventoryAction.CLONE_STACK; + case CollectToCursor: + return InventoryAction.COLLECT_TO_CURSOR; + case Unknown: + return InventoryAction.UNKNOWN; + default: + throw new IllegalArgumentException("Invalid EsInventoryAction"); + } + } + + public static EsClickType fromBukkitClickType(ClickType click) { + switch (click) { + case CREATIVE: + return EsClickType.Creative; + case DROP: + return EsClickType.Drop; + case LEFT: + return EsClickType.Left; + case RIGHT: + return EsClickType.Right; + case SHIFT_LEFT: + return EsClickType.ShiftLeft; + case SHIFT_RIGHT: + return EsClickType.ShiftRight; + case MIDDLE: + return EsClickType.Middle; + case UNKNOWN: + return EsClickType.Unknown; + case NUMBER_KEY: + return EsClickType.NumberKey; + case CONTROL_DROP: + return EsClickType.ControlDrop; + case DOUBLE_CLICK: + return EsClickType.DoubleClick; + case SWAP_OFFHAND: + return EsClickType.SwapOffhand; + case WINDOW_BORDER_LEFT: + return EsClickType.WindowBorderLeft; + case WINDOW_BORDER_RIGHT: + return EsClickType.WindowBorderRight; + default: + throw new RuntimeException("Invalid click type"); + } + } + + public static ClickType toBukkitClickType(EsClickType esClick) { + switch (esClick) { + case Creative: + return ClickType.CREATIVE; + case Drop: + return ClickType.DROP; + case Left: + return ClickType.LEFT; + case Right: + return ClickType.RIGHT; + case ShiftLeft: + return ClickType.SHIFT_LEFT; + case ShiftRight: + return ClickType.SHIFT_RIGHT; + case Middle: + return ClickType.MIDDLE; + case Unknown: + return ClickType.UNKNOWN; + case NumberKey: + return ClickType.NUMBER_KEY; + case ControlDrop: + return ClickType.CONTROL_DROP; + case DoubleClick: + return ClickType.DOUBLE_CLICK; + case SwapOffhand: + return ClickType.SWAP_OFFHAND; + case WindowBorderLeft: + return ClickType.WINDOW_BORDER_LEFT; + case WindowBorderRight: + return ClickType.WINDOW_BORDER_RIGHT; + default: + throw new IllegalArgumentException("Invalid EsClickType"); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index f2c4784..1645762 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -6,7 +6,9 @@ import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsEquipmentSlot; +import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; @@ -20,6 +22,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -233,6 +237,166 @@ public static EsBlock fromBukkitBlock(BlockState block) { return new FoliaBlock(block); } + public static EsInventoryAction fromBukkitInventoryAction(InventoryAction action) { + switch (action) { + case NOTHING: + return EsInventoryAction.Nothing; + case PICKUP_ALL: + return EsInventoryAction.PickupAll; + case PICKUP_SOME: + return EsInventoryAction.PickupSome; + case PICKUP_HALF: + return EsInventoryAction.PickupHalf; + case PICKUP_ONE: + return EsInventoryAction.PickupOne; + case PLACE_ALL: + return EsInventoryAction.PlaceAll; + case PLACE_SOME: + return EsInventoryAction.PlaceSome; + case PLACE_ONE: + return EsInventoryAction.PlaceOne; + case SWAP_WITH_CURSOR: + return EsInventoryAction.SwapWithCursor; + case DROP_ALL_CURSOR: + return EsInventoryAction.DropAllCursor; + case DROP_ONE_CURSOR: + return EsInventoryAction.DropOneCursor; + case DROP_ALL_SLOT: + return EsInventoryAction.DropAllSlot; + case DROP_ONE_SLOT: + return EsInventoryAction.DropOneSlot; + case MOVE_TO_OTHER_INVENTORY: + return EsInventoryAction.MoveToOtherInventory; + case HOTBAR_MOVE_AND_READD: + return EsInventoryAction.HotbarMoveAndReadd; + case HOTBAR_SWAP: + return EsInventoryAction.HotbarSwap; + case CLONE_STACK: + return EsInventoryAction.CloneStack; + case COLLECT_TO_CURSOR: + return EsInventoryAction.CollectToCursor; + case UNKNOWN: + return EsInventoryAction.Unknown; + default: + throw new IllegalArgumentException("Invalid InventoryAction"); + } + } + + public static InventoryAction toBukkitInventoryAction(EsInventoryAction esAction) { + switch (esAction) { + case Nothing: + return InventoryAction.NOTHING; + case PickupAll: + return InventoryAction.PICKUP_ALL; + case PickupSome: + return InventoryAction.PICKUP_SOME; + case PickupHalf: + return InventoryAction.PICKUP_HALF; + case PickupOne: + return InventoryAction.PICKUP_ONE; + case PlaceAll: + return InventoryAction.PLACE_ALL; + case PlaceSome: + return InventoryAction.PLACE_SOME; + case PlaceOne: + return InventoryAction.PLACE_ONE; + case SwapWithCursor: + return InventoryAction.SWAP_WITH_CURSOR; + case DropAllCursor: + return InventoryAction.DROP_ALL_CURSOR; + case DropOneCursor: + return InventoryAction.DROP_ONE_CURSOR; + case DropAllSlot: + return InventoryAction.DROP_ALL_SLOT; + case DropOneSlot: + return InventoryAction.DROP_ONE_SLOT; + case MoveToOtherInventory: + return InventoryAction.MOVE_TO_OTHER_INVENTORY; + case HotbarMoveAndReadd: + return InventoryAction.HOTBAR_MOVE_AND_READD; + case HotbarSwap: + return InventoryAction.HOTBAR_SWAP; + case CloneStack: + return InventoryAction.CLONE_STACK; + case CollectToCursor: + return InventoryAction.COLLECT_TO_CURSOR; + case Unknown: + return InventoryAction.UNKNOWN; + default: + throw new IllegalArgumentException("Invalid EsInventoryAction"); + } + } + + public static EsClickType fromBukkitClickType(ClickType click) { + switch (click) { + case CREATIVE: + return EsClickType.Creative; + case DROP: + return EsClickType.Drop; + case LEFT: + return EsClickType.Left; + case RIGHT: + return EsClickType.Right; + case SHIFT_LEFT: + return EsClickType.ShiftLeft; + case SHIFT_RIGHT: + return EsClickType.ShiftRight; + case MIDDLE: + return EsClickType.Middle; + case UNKNOWN: + return EsClickType.Unknown; + case NUMBER_KEY: + return EsClickType.NumberKey; + case CONTROL_DROP: + return EsClickType.ControlDrop; + case DOUBLE_CLICK: + return EsClickType.DoubleClick; + case SWAP_OFFHAND: + return EsClickType.SwapOffhand; + case WINDOW_BORDER_LEFT: + return EsClickType.WindowBorderLeft; + case WINDOW_BORDER_RIGHT: + return EsClickType.WindowBorderRight; + default: + throw new RuntimeException("Invalid click type"); + } + } + + public static ClickType toBukkitClickType(EsClickType esClick) { + switch (esClick) { + case Creative: + return ClickType.CREATIVE; + case Drop: + return ClickType.DROP; + case Left: + return ClickType.LEFT; + case Right: + return ClickType.RIGHT; + case ShiftLeft: + return ClickType.SHIFT_LEFT; + case ShiftRight: + return ClickType.SHIFT_RIGHT; + case Middle: + return ClickType.MIDDLE; + case Unknown: + return ClickType.UNKNOWN; + case NumberKey: + return ClickType.NUMBER_KEY; + case ControlDrop: + return ClickType.CONTROL_DROP; + case DoubleClick: + return ClickType.DOUBLE_CLICK; + case SwapOffhand: + return ClickType.SWAP_OFFHAND; + case WindowBorderLeft: + return ClickType.WINDOW_BORDER_LEFT; + case WindowBorderRight: + return ClickType.WINDOW_BORDER_RIGHT; + default: + throw new IllegalArgumentException("Invalid EsClickType"); + } + } + public static EsInventory fromBukkitInventory(Inventory inv) { if (inv == null) { return null; From bc489a6515261795c70a891bf3500aad1fb7836f Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 01:32:27 +1000 Subject: [PATCH 61/74] Fix 1.3 support, fix setmaxhealth min version --- src/main/java/net/serble/estools/Main.java | 2 +- .../Implementations/Bukkit/BukkitServer.java | 11 ++--------- .../Bukkit/Helpers/BukkitHelper.java | 6 ++++-- .../Helpers/BukkitInventoryClickHelper.java | 3 +++ .../Bukkit/Helpers/BukkitMetaHelper.java | 18 ++++++++++++++++++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 433e0ac..88788ad 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -136,7 +136,7 @@ public void enable() { sc("heal", "heal", new Heal()); sc("suicide", "suicide", new Suicide()); sc("sethealth", "sethealth", new SetHealth()); - sc("setmaxhealth", "setmaxhealth", new SetMaxHealth(), 3); + sc("setmaxhealth", "setmaxhealth", new SetMaxHealth(), 4); sc("getinfo", "getinfo", new GetInfo()); sc("editsign", "editsign", new EditSign()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 5ea0b95..1d5c57a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -5,10 +5,7 @@ import net.serble.estools.ServerApi.*; import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListener; import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListenerPost1_4; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitSoundEnumConverter; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.*; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; @@ -16,7 +13,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionType; @@ -171,10 +167,7 @@ public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amoun potType.toString().toUpperCase() + "_POTION"; ItemStack pot = new ItemStack(Material.valueOf(type), amount); - PotionMeta meta = (PotionMeta) pot.getItemMeta(); - assert meta != null; - meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); - pot.setItemMeta(meta); + BukkitMetaHelper.setPotionType(pot, effect); // No ItemMeta allowed return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index bff19c4..0817572 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -29,7 +29,9 @@ // DO NOT IMPORT THE FOLLOWING BECAUSE THEY BREAK OLDER VERSIONS BECAUSE THEY DON'T EXIST // org.bukkit.inventory.EquipmentSlot - +/** + * Min version: 1.0 + */ @SuppressWarnings("unused") public class BukkitHelper { public static Location toBukkitLocation(EsLocation loc) { @@ -312,7 +314,7 @@ public static EsItemStack fromBukkitItem(ItemStack item) { return new BukkitItemStack(item); } else { - throw new RuntimeException("Potions aren't support in this Minecraft version"); + return new BukkitItemStack(item); // Not a potion } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java index a16c476..2bf438f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryClickHelper.java @@ -5,6 +5,9 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryAction; +/** + * Min version: 1.5 + */ @SuppressWarnings("unused") public class BukkitInventoryClickHelper { public static EsInventoryAction fromBukkitInventoryAction(InventoryAction action) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java new file mode 100644 index 0000000..b23e6a1 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java @@ -0,0 +1,18 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.ServerApi.EsPotionEffect; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; + +/** + * Min version: 1.4 + */ +public class BukkitMetaHelper { + + public static void setPotionType(ItemStack pot, EsPotionEffect effect) { + PotionMeta meta = (PotionMeta) pot.getItemMeta(); + assert meta != null; + meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); + pot.setItemMeta(meta); + } +} From f51ffd5050a7625cbc8d8d95592bb2212b8f0efa Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 02:09:32 +1000 Subject: [PATCH 62/74] Fix 1.2 support, move Sounds away --- TestedVersions.txt | 6 +++- .../Implementations/Bukkit/BukkitServer.java | 22 +++---------- .../Bukkit/Helpers/BukkitSoundHelper.java | 32 +++++++++++++++++++ 3 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundHelper.java diff --git a/TestedVersions.txt b/TestedVersions.txt index 9790dba..499f5be 100644 --- a/TestedVersions.txt +++ b/TestedVersions.txt @@ -17,4 +17,8 @@ Bukkit: 1.9.4 1.8.8 1.7.10 -1.6.4 \ No newline at end of file +1.6.4 +1.5.2 +1.4.7 +1.3.2 +1.2.5 \ No newline at end of file diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 1d5c57a..a305c14 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -16,7 +16,6 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionType; -import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.util.*; @@ -26,7 +25,7 @@ public class BukkitServer implements EsServer { private final BukkitEventsListener listener; private static final Set materials = new HashSet<>(); private static final Set itemMaterials = new HashSet<>(); - private static final Set sounds = new HashSet<>(); + private static Set sounds = new HashSet<>(); public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; @@ -56,17 +55,7 @@ public void initialise() { materials.add(esMat); } - if (Main.minecraftVersion.isAtLeast(1, 16, 4)) { - for (Sound sound : Registry.SOUNDS) { - EsSound esSound = EsSound.createUnchecked(sound.getKey().getKey()); - sounds.add(esSound); - } - } else { - for (Sound sound : Sound.values()) { - EsSound esSound = BukkitSoundEnumConverter.convertEnumToKey(sound.name()); - sounds.add(esSound); - } - } + sounds = BukkitSoundHelper.getSounds(); } @Override @@ -268,13 +257,12 @@ public String getPluginName() { @Override public int runTaskLater(Runnable task, long ticks) { - BukkitTask bukkitTask = Bukkit.getScheduler().runTaskLater(EsToolsBukkit.plugin, task, ticks); - return bukkitTask.getTaskId(); + return Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, task, ticks); } @Override public void runTask(Runnable task) { - Bukkit.getScheduler().runTask(EsToolsBukkit.plugin, task); + Bukkit.getScheduler().scheduleSyncDelayedTask(EsToolsBukkit.plugin, task); } @Override @@ -295,7 +283,7 @@ public void startEvents() { @Override public void registerCommand(String cmd, EsToolsTabCompleter tab) { PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); - if (command.getTabCompleter() == null) { + if (!Main.tabCompleteEnabled || command.getTabCompleter() == null) { command.setExecutor(listener); } if (Main.tabCompleteEnabled) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundHelper.java new file mode 100644 index 0000000..5277da6 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitSoundHelper.java @@ -0,0 +1,32 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsSound; +import org.bukkit.Registry; +import org.bukkit.Sound; + +import java.util.HashSet; + +/** + * Min version: 1.0 + */ +public class BukkitSoundHelper { + + public static HashSet getSounds() { + HashSet sounds = new HashSet<>(); + + if (Main.minecraftVersion.isAtLeast(1, 16, 4)) { + for (Sound sound : Registry.SOUNDS) { + EsSound esSound = EsSound.createUnchecked(sound.getKey().getKey()); + sounds.add(esSound); + } + } else if (Main.minecraftVersion.isAtLeast(1, 3, 0)) { + for (Sound sound : Sound.values()) { + EsSound esSound = BukkitSoundEnumConverter.convertEnumToKey(sound.name()); + sounds.add(esSound); + } + } // Sounds do not exist in the same way in pre 1.3 + + return sounds; + } +} From 2666265594a9425fec334499c9be5d36701f878b Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 11:23:56 +1000 Subject: [PATCH 63/74] Fix 1.1 support, move more events and methods --- .../Implementations/Bukkit/BukkitPlayer.java | 13 +++- .../Implementations/Bukkit/BukkitServer.java | 17 +++-- .../EventHandlers/BukkitEventsListener.java | 48 -------------- .../BukkitEventsListenerPost1_1.java | 63 +++++++++++++++++++ .../BukkitEventsListenerPost1_4.java | 2 +- .../Bukkit/Helpers/BukkitInventoryHelper.java | 15 +++++ 6 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryHelper.java diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index d32ec30..4ed3792 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -177,7 +177,18 @@ public boolean hasPermission(String node) { @Override public void sendMessage(String... args) { - bukkitPlayer.sendMessage(args); + if (Main.minecraftVersion.isAtLeast(1, 2, 0)) { + bukkitPlayer.sendMessage(args); + return; + } + + // The sendMessage(String[]) method doesn't exist + // So combine the args into one String + StringBuilder sb = new StringBuilder(); + for (String s : args) { + sb.append(s); + } + bukkitPlayer.sendMessage(sb.toString()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index a305c14..bd8e089 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -4,6 +4,7 @@ import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListener; +import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListenerPost1_1; import net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers.BukkitEventsListenerPost1_4; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.*; import net.serble.estools.ServerApi.Interfaces.*; @@ -11,7 +12,6 @@ import org.bukkit.command.PluginCommand; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.Potion; @@ -29,8 +29,12 @@ public class BukkitServer implements EsServer { public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; - if (getVersion().isAtLeast(1, 5, 0)) { + + SemanticVersion mcVer = getVersion(); + if (mcVer.isAtLeast(1, 5, 0)) { listener = new BukkitEventsListenerPost1_4(); + } else if (mcVer.isAtLeast(1, 2, 0)) { + listener = new BukkitEventsListenerPost1_1(); } else { listener = new BukkitEventsListener(); } @@ -206,8 +210,13 @@ public EsPotion createPotion(EsPotType potType) { @Override public EsInventory createInventory(EsPlayer owner, int size, String title) { - InventoryHolder holder = owner == null ? null : ((BukkitPlayer) owner).getBukkitPlayer(); - return new BukkitInventory(Bukkit.createInventory(holder, size, title)); + if (!Main.minecraftVersion.isAtLeast(1, 2, 0)) { + // Creating inventories was not a feature in this version + // InventoryHolder also didn't exist, hence why we must use + // a helper class + throw new UnsupportedOperationException("Creating inventories is not supported in this version"); + } + return BukkitInventoryHelper.createInventory(owner, size, title); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java index e0d063f..9586afe 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java @@ -2,14 +2,11 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.EsAction; -import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsEquipmentSlot; -import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEquipmentSlotHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitInventoryClickHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -23,8 +20,6 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; @@ -108,41 +103,6 @@ public void onEntityDamage(EntityDamageEvent e) { e.setCancelled(ee.isCancelled()); } - @EventHandler - public void onInvClick(InventoryClickEvent e) { - EsInventory clInv; - if (Main.minecraftVersion.isAtLeast(1, 6, 0)) { - clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); - } else { - clInv = BukkitHelper.fromBukkitInventory(e.getInventory()); // Get clicked inv doesn't exist - } - - EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); - - EsClickType ct; - if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { - ct = BukkitInventoryClickHelper.fromBukkitClickType(e.getClick()); - } else { - ct = EsClickType.Unknown; // getClick() doesn't exist in 1.4.x - } - - EsInventoryAction ac; - if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { - ac = BukkitInventoryClickHelper.fromBukkitInventoryAction(e.getAction()); - } else { - ac = EsInventoryAction.Unknown; // getAction() doesn't exist in 1.4.x - } - - EsItemStack ci = BukkitHelper.fromBukkitItem(e.getCurrentItem()); - EsPlayer cl = new BukkitPlayer((Player) e.getWhoClicked()); - EsItemStack cu = BukkitHelper.fromBukkitItem(e.getCursor()); - int sl = e.getSlot(); - EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); - ee.setCancelled(e.isCancelled()); - Main.callEvent(ee); - e.setCancelled(ee.isCancelled()); - } - @EventHandler public void onQuit(PlayerQuitEvent e) { Main.callEvent(new EsPlayerQuitEvent(new BukkitPlayer(e.getPlayer()))); @@ -153,14 +113,6 @@ public void onKick(PlayerQuitEvent e) { Main.callEvent(new EsPlayerQuitEvent(new BukkitPlayer(e.getPlayer()))); } - @EventHandler - public void onInvClose(InventoryCloseEvent e) { - EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); - EsPlayer pl = new BukkitPlayer((Player) e.getPlayer()); - EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); - Main.callEvent(ee); - } - @EventHandler public void onInteract(PlayerInteractEvent e) { EsBlock cb = BukkitHelper.fromBukkitBlock(e.getClickedBlock()); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java new file mode 100644 index 0000000..0dae623 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java @@ -0,0 +1,63 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.EventHandlers; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsClickType; +import net.serble.estools.ServerApi.EsInventoryAction; +import net.serble.estools.ServerApi.Events.EsInventoryClickEvent; +import net.serble.estools.ServerApi.Events.EsInventoryCloseEvent; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitInventoryClickHelper; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsItemStack; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; + +public class BukkitEventsListenerPost1_1 extends BukkitEventsListener { + + @EventHandler + public void onInvClick(InventoryClickEvent e) { + EsInventory clInv; + if (Main.minecraftVersion.isAtLeast(1, 6, 0)) { + clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); + } else { + clInv = BukkitHelper.fromBukkitInventory(e.getInventory()); // Get clicked inv doesn't exist + } + + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); + + EsClickType ct; + if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { + ct = BukkitInventoryClickHelper.fromBukkitClickType(e.getClick()); + } else { + ct = EsClickType.Unknown; // getClick() doesn't exist in 1.4.x + } + + EsInventoryAction ac; + if (Main.minecraftVersion.isAtLeast(1, 5, 0)) { + ac = BukkitInventoryClickHelper.fromBukkitInventoryAction(e.getAction()); + } else { + ac = EsInventoryAction.Unknown; // getAction() doesn't exist in 1.4.x + } + + EsItemStack ci = BukkitHelper.fromBukkitItem(e.getCurrentItem()); + EsPlayer cl = new BukkitPlayer((Player) e.getWhoClicked()); + EsItemStack cu = BukkitHelper.fromBukkitItem(e.getCursor()); + int sl = e.getSlot(); + EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); + ee.setCancelled(e.isCancelled()); + Main.callEvent(ee); + e.setCancelled(ee.isCancelled()); + } + + @EventHandler + public void onInvClose(InventoryCloseEvent e) { + EsInventory inv = BukkitHelper.fromBukkitInventory(e.getInventory()); + EsPlayer pl = new BukkitPlayer((Player) e.getPlayer()); + EsInventoryCloseEvent ee = new EsInventoryCloseEvent(pl, inv); + Main.callEvent(ee); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java index a613054..660d8ef 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java @@ -14,7 +14,7 @@ import java.util.Set; -public class BukkitEventsListenerPost1_4 extends BukkitEventsListener { +public class BukkitEventsListenerPost1_4 extends BukkitEventsListenerPost1_1 { @EventHandler public void onDrag(InventoryDragEvent e) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryHelper.java new file mode 100644 index 0000000..460bd9a --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitInventoryHelper.java @@ -0,0 +1,15 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Interfaces.EsInventory; +import net.serble.estools.ServerApi.Interfaces.EsPlayer; +import org.bukkit.Bukkit; +import org.bukkit.inventory.InventoryHolder; + +public class BukkitInventoryHelper { + + public static EsInventory createInventory(EsPlayer owner, int size, String title) { + InventoryHolder holder = owner == null ? null : ((BukkitPlayer) owner).getBukkitPlayer(); + return BukkitHelper.fromBukkitInventory(Bukkit.createInventory(holder, size, title)); + } +} From 786a86fb90cc6f6650b777662939ee0f2d9e7148 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 12:11:48 +1000 Subject: [PATCH 64/74] Add version specific tests --- .../java/net/serble/estools/TestCommand.java | 34 +++ src/main/java/net/serble/estools/Tester.java | 215 +++++++++--------- 2 files changed, 146 insertions(+), 103 deletions(-) create mode 100644 src/main/java/net/serble/estools/TestCommand.java diff --git a/src/main/java/net/serble/estools/TestCommand.java b/src/main/java/net/serble/estools/TestCommand.java new file mode 100644 index 0000000..178c5ec --- /dev/null +++ b/src/main/java/net/serble/estools/TestCommand.java @@ -0,0 +1,34 @@ +package net.serble.estools; + +/** + * A command schema containing information for the Tester. + */ +public class TestCommand { + private final String cmd; + private final double waitAfter; + private final SemanticVersion minVersion; + + public TestCommand(String cmd, double waitAfter, SemanticVersion minVersion) { + this.cmd = cmd; + this.waitAfter = waitAfter; + this.minVersion = minVersion; + } + + public TestCommand(String cmd, double waitAfter) { + this.cmd = cmd; + this.waitAfter = waitAfter; + minVersion = null; + } + + public double getWaitAfter() { + return waitAfter; + } + + public SemanticVersion getMinVersion() { + return minVersion; + } + + public String getCmd() { + return cmd; + } +} diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index f51aa36..89986fe 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -15,102 +15,100 @@ public class Tester { public static final HashMap runningTests = new HashMap<>(); - @SuppressWarnings("unchecked") // It doesn't allow specifying type - private static final Tuple[] routine = new Tuple[] { - new Tuple<>("music", 1), - new Tuple<>("gmsp", 1.5), - new Tuple<>("gmc", 1.5), - new Tuple<>("gma", 1.5), - new Tuple<>("gms", 1.5), - new Tuple<>("i apple", 2), - new Tuple<>("h dirt", 2), - new Tuple<>("fly", 4), - new Tuple<>("flyspeed 10", 2), - new Tuple<>("flyspeed 2", 0.1), - new Tuple<>("fly", 0.1), - new Tuple<>("walkspeed 10", 3), - new Tuple<>("walkspeed 2", 0.1), - new Tuple<>("ps", 0.1), - new Tuple<>("pp", 0.1), - new Tuple<>("pa", 0.1), - new Tuple<>("psh", 0.1), - new Tuple<>("ph", 3), - new Tuple<>("tphere {randomplayer}", 3), - new Tuple<>("tpall", 2), - new Tuple<>("warps add test", 2), - new Tuple<>("safetp", 2), - new Tuple<>("tp ~50 512 ~50", 1), - new Tuple<>("warp test", 2), - new Tuple<>("warps remove test", 2), - new Tuple<>("tp ~50 512 ~50", 1), - new Tuple<>("back", 1), - new Tuple<>("cchest", -1), - new Tuple<>("ci", 1), - new Tuple<>("h sign", 0.1), - new Tuple<>("msg {player} Place and look at the sign (leave it empty)", -1), - new Tuple<>("editsign 1 Hello World!", 1), - new Tuple<>("editsign glow", 1), - new Tuple<>("editsign unglow", 1), - new Tuple<>("editsign 2 Does it work?", 1), - new Tuple<>("msg {player} Look at the back of the sign", -1), - new Tuple<>("editsign 1 Hello World!", 1), - new Tuple<>("editsign glow", 1), - new Tuple<>("editsign unglow", 1), - new Tuple<>("editsign 2 Does it work?", 1), - new Tuple<>("eff speed 1 60 {player}", 3), - new Tuple<>("h iron_sword", 0.1), - new Tuple<>("ench knockback 10", 3), - new Tuple<>("estools", 1), - new Tuple<>("sethealth 1", 2), - new Tuple<>("sethunger 1", 2), - new Tuple<>("feed", 0.1), - new Tuple<>("setsaturation 0", 0.1), - new Tuple<>("msg {player} If you heal health and don't lose hunger, setsaturation didn't work", 6), - new Tuple<>("heal", 1), - new Tuple<>("msg {player} Mine a block with the sword and hold it", -1), - new Tuple<>("fix", 1), - new Tuple<>("getinfo {player}", 3), - new Tuple<>("god", 0.1), - new Tuple<>("tp ~ ~10 ~", 5), - new Tuple<>("smite {player}", 2), - new Tuple<>("god", 0.1), - new Tuple<>("hideflags", 4), - new Tuple<>("h dirt", 0.1), - new Tuple<>("infinite", -1), - new Tuple<>("invsee {randomplayer}", -1), - new Tuple<>("h dirt", 0.1), - new Tuple<>("lore add Silly &ldirt", 4), - new Tuple<>("lore insert 1 &cNot very", 2), - new Tuple<>("lore remove 1", 2), - new Tuple<>("rain", 3), - new Tuple<>("sun", 3), - new Tuple<>("thunder", 3), - new Tuple<>("night", 1), - new Tuple<>("noon", 1), - new Tuple<>("midnight", 1), - new Tuple<>("day", 1), - new Tuple<>("potion speed", 2), - new Tuple<>("rename &6This dirt is special", 4), - new Tuple<>("setmaxhealth 30", 2), - new Tuple<>("setstack 127", 2), - new Tuple<>("h iron_sword", 0.1), - new Tuple<>("setunbreakable", 4), - new Tuple<>("heal", 0.1), - new Tuple<>("sudo {randomplayer} say I have been sudoed", 2), - new Tuple<>("h dirt", 0.1), - new Tuple<>("setpersistentdata estools:hello string wassup", 2), - new Tuple<>("getpersistentdata estools:hello string", 2), - new Tuple<>("removepersistentdata estools:hello", 2), - new Tuple<>("getpersistentdata estools:hello string", 2), - new Tuple<>("mount {entity}", 2), - new Tuple<>("dismount", 1), - new Tuple<>("dismount", 1), - new Tuple<>("mount {player}", 1), - new Tuple<>("mount {player} {entity}", 2), - new Tuple<>("dismount {entity}", 1), - new Tuple<>("sethealth 1", 0.1), - new Tuple<>("buddha", -1), - new Tuple<>("suicide", 1), + private static final TestCommand[] routine = new TestCommand[] { + new TestCommand("music", 1, new SemanticVersion(1, 9, 0)), + new TestCommand("gmsp", 1.5, new SemanticVersion(1, 8, 0)), + new TestCommand("gmc", 1.5), + new TestCommand("gma", 1.5, new SemanticVersion(1, 3, 0)), + new TestCommand("gms", 1.5), + new TestCommand("i apple", 2), + new TestCommand("h dirt", 2), + new TestCommand("fly", 4, new SemanticVersion(1, 2, 0)), + new TestCommand("flyspeed 10", 2, new SemanticVersion(1, 3, 0)), + new TestCommand("flyspeed 2", 0.1, new SemanticVersion(1, 3, 0)), + new TestCommand("fly", 0.1, new SemanticVersion(1, 2, 0)), + new TestCommand("walkspeed 10", 3, new SemanticVersion(1, 4, 0)), + new TestCommand("walkspeed 2", 0.1, new SemanticVersion(1, 4, 0)), + new TestCommand("ps", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("pp", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("pa", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("psh", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("ph", 3, new SemanticVersion(1, 1, 0)), + new TestCommand("tphere {randomplayer}", 3), + new TestCommand("tpall", 2), + new TestCommand("warps add test", 2), + new TestCommand("safetp", 2, new SemanticVersion(1, 1, 0)), + new TestCommand("tp {player} 0 512 0", 1), + new TestCommand("warp test", 2), + new TestCommand("warps remove test", 2), + new TestCommand("tp {player} 0 512 0", 1, new SemanticVersion(1, 1, 0)), + new TestCommand("back", 1, new SemanticVersion(1, 1, 0)), + new TestCommand("cchest", -1, new SemanticVersion(1, 7, 0)), + new TestCommand("ci", 1), + new TestCommand("h sign", 0.1), + new TestCommand("msg Place and look at the sign (leave it empty)", -1), + new TestCommand("editsign 1 Hello World!", 1), + new TestCommand("editsign glow", 1, new SemanticVersion(1, 17,0)), + new TestCommand("editsign unglow", 1, new SemanticVersion(1, 17, 0)), + new TestCommand("editsign 2 Does it work?", 1), + new TestCommand("msg {player} Look at the back of the sign", -1, new SemanticVersion(1, 20, 0)), + new TestCommand("editsign 1 Hello World!", 1, new SemanticVersion(1, 20, 0)), + new TestCommand("editsign glow", 1, new SemanticVersion(1, 20, 0)), + new TestCommand("editsign unglow", 1, new SemanticVersion(1, 20, 0)), + new TestCommand("editsign 2 Does it work?", 1, new SemanticVersion(1, 20 ,0)), + new TestCommand("eff speed 1 60 {player}", 3, new SemanticVersion(1, 1, 0)), + new TestCommand("h iron_sword", 0.1), + new TestCommand("ench knockback 10", 3, new SemanticVersion(1, 1, 0)), + new TestCommand("estools", 0.1), + new TestCommand("sethealth 1", 2), + new TestCommand("sethunger 1", 2), + new TestCommand("feed", 0.1), + new TestCommand("setsaturation 0", 0.1), + new TestCommand("msg If you heal health and don't lose hunger, setsaturation didn't work", 6), + new TestCommand("heal", 1), + new TestCommand("msg Mine a block with the sword and hold it", -1), + new TestCommand("fix", 1), + new TestCommand("getinfo {player}", 3), + new TestCommand("god", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("smite {player}", 2), + new TestCommand("god", 0.1, new SemanticVersion(1, 1, 0)), + new TestCommand("hideflags", 4, new SemanticVersion(1, 8, 0)), + new TestCommand("h dirt", 0.1), + new TestCommand("infinite", -1, new SemanticVersion(1, 1, 0)), + new TestCommand("invsee {randomplayer}", -1, new SemanticVersion(1, 2, 0)), + new TestCommand("h dirt", 0.1), + new TestCommand("lore add Silly &ldirt", 4, new SemanticVersion(1, 4, 6)), + new TestCommand("lore insert 1 &cNot very", 2, new SemanticVersion(1, 4, 6)), + new TestCommand("lore remove 1", 2, new SemanticVersion(1, 4, 6)), + new TestCommand("rain", 3), + new TestCommand("sun", 3), + new TestCommand("thunder", 3), + new TestCommand("night", 1), + new TestCommand("noon", 1), + new TestCommand("midnight", 1), + new TestCommand("day", 1), + new TestCommand("potion speed", 2, new SemanticVersion(1, 4, 0)), + new TestCommand("rename &6This dirt is special", 4, new SemanticVersion(1, 4, 6)), + new TestCommand("setmaxhealth 30", 2, new SemanticVersion(1, 4, 0)), + new TestCommand("setstack 127", 2), + new TestCommand("h iron_sword", 0.1), + new TestCommand("setunbreakable", 4, new SemanticVersion(1, 1, 0)), + new TestCommand("heal", 0.1), + new TestCommand("sudo {randomplayer} say I have been sudoed", 2), + new TestCommand("h dirt", 0.1), + new TestCommand("setpersistentdata estools:hello string wassup", 2, new SemanticVersion(1, 14, 0)), + new TestCommand("getpersistentdata estools:hello string", 2, new SemanticVersion(1, 14, 0)), + new TestCommand("removepersistentdata estools:hello", 2, new SemanticVersion(1, 14, 0)), + new TestCommand("getpersistentdata estools:hello string", 2, new SemanticVersion(1, 14, 0)), + new TestCommand("mount {entity}", 2), + new TestCommand("dismount", 1), + new TestCommand("dismount", 1), + new TestCommand("mount {player}", 1), + new TestCommand("mount {player} {entity}", 2), + new TestCommand("dismount {entity}", 1), + new TestCommand("sethealth 1", 0.1), + new TestCommand("buddha", -1, new SemanticVersion(1, 1, 0)), + new TestCommand("suicide", 1), }; public Tester(EsPlayer p) { @@ -123,6 +121,11 @@ public void startTests() { // Init some stuff SafeTp.enabled = false; + // Find starting command (Where the version is valid) + while (routine[currentCommand].getMinVersion() != null && !Main.minecraftVersion.isAtLeast(routine[currentCommand].getMinVersion())) { + currentCommand++; + } + execNextCommand(); } @@ -137,10 +140,12 @@ private void execNextCommand() { return; } - Tuple cCmd = routine[currentCommand]; - currentCommand++; + TestCommand cCmd = routine[currentCommand]; + do { + currentCommand++; + } while (routine.length != currentCommand && routine[currentCommand].getMinVersion() != null && !Main.minecraftVersion.isAtLeast(routine[currentCommand].getMinVersion())); - String cmd = cCmd.a(); + String cmd = cCmd.getCmd(); if (cmd.contains("{randomplayer}")) { // Find a random player that isn't player EsPlayer randomPlayer = null; @@ -200,13 +205,13 @@ private void execNextCommand() { return; } - Double secVal; - Object val = cCmd.b(); + double secVal; + Object val = cCmd.getWaitAfter(); //noinspection ConstantValue, it's not constant if (val instanceof Integer) { // Stupid java being dumb secVal = ((Integer) val).doubleValue(); } else { - secVal = cCmd.b(); + secVal = cCmd.getWaitAfter(); } if (secVal < 0) { @@ -216,10 +221,14 @@ private void execNextCommand() { @SuppressWarnings("WrapperTypeMayBePrimitive") Double timeInTicks = (secVal * 20.0); // The type cannot be primitive Main.server.runTaskLater(this::execNextCommand, timeInTicks.longValue()); - EsToolsCommand.send(player, "&bWaiting &6" + cCmd.b() + " seconds"); + EsToolsCommand.send(player, "&bWaiting &6" + cCmd.getWaitAfter() + " seconds"); } private void exec(String cmd) { + if (cmd.startsWith("msg ")) { // Overwrite msg to make a fancier way to talking to the player + EsToolsCommand.send(player, "&b[Tester] " + cmd.replace("msg ", "")); + return; + } EsToolsCommand.send(player, "&aExecuting: " + cmd); Main.server.dispatchCommand(player, cmd); } From b4adb1b11b71a355f8dae7bccea07b9f3f753785 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 12:12:12 +1000 Subject: [PATCH 65/74] Fix non player send message bugs on old versions --- .../Bukkit/BukkitConsoleSender.java | 14 +++++++++++++- .../Implementations/Bukkit/BukkitEntity.java | 13 ++++++++++++- .../Implementations/Bukkit/BukkitPlayer.java | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java index 2d30ebd..85c36b7 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java @@ -1,5 +1,6 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; +import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsConsoleSender; import org.bukkit.command.ConsoleCommandSender; @@ -12,7 +13,18 @@ public BukkitConsoleSender(ConsoleCommandSender child) { @Override public void sendMessage(String... msg) { - bukkitSender.sendMessage(msg); + if (Main.minecraftVersion.isAtLeast(1, 2, 0)) { + bukkitSender.sendMessage(msg); + return; + } + + // The sendMessage(String[]) method doesn't exist + // So combine the args into one String + StringBuilder sb = new StringBuilder(); + for (String s : msg) { + sb.append(s); + } + bukkitSender.sendMessage(sb.toString()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index b58fc8a..e113282 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -73,7 +73,18 @@ public boolean isPermissionSet(String node) { @Override public void sendMessage(String... msg) { - bukkitEntity.sendMessage(msg); + if (Main.minecraftVersion.isAtLeast(1, 2, 0)) { + bukkitEntity.sendMessage(msg); + return; + } + + // The sendMessage(String[]) method doesn't exist + // So combine the args into one String + StringBuilder sb = new StringBuilder(); + for (String s : msg) { + sb.append(s); + } + bukkitEntity.sendMessage(sb.toString()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java index 4ed3792..5f93bdc 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPlayer.java @@ -175,6 +175,9 @@ public boolean hasPermission(String node) { return bukkitPlayer.hasPermission(node); } + // I know that this overrides the BukkitEntity.sendMessage method + // It has to because Entity.sendMessage() doesn't exist in old versions + // so Player.sendMessage() needs to be used where possible. @Override public void sendMessage(String... args) { if (Main.minecraftVersion.isAtLeast(1, 2, 0)) { From 9bc8e87debe00a1b77e4f79146b2ae4bcfd9c5b6 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 12:12:31 +1000 Subject: [PATCH 66/74] Make /estools reset and reload work properly --- .../net/serble/estools/Commands/EsTools.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/EsTools.java b/src/main/java/net/serble/estools/Commands/EsTools.java index 2e60e9e..d9c1ef8 100644 --- a/src/main/java/net/serble/estools/Commands/EsTools.java +++ b/src/main/java/net/serble/estools/Commands/EsTools.java @@ -1,7 +1,12 @@ package net.serble.estools.Commands; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; @@ -11,12 +16,12 @@ import net.serble.estools.Updater; import net.serble.estools.EsToolsCommand; -import net.serble.estools.Commands.Give.Give; import net.serble.estools.Main; public class EsTools extends EsToolsCommand { - @Override + @SuppressWarnings({"resource", "ResultOfMethodCallIgnored"}) + @Override public boolean execute(EsCommandSender sender, String[] args) { if (args.length == 0) { if (checkPerms(sender, "version")) { @@ -34,15 +39,7 @@ public boolean execute(EsCommandSender sender, String[] args) { send(sender, "&aReloading..."); - Give.enable(); - - for (EsPlayer p : Main.server.getOnlinePlayers()) { - CChest.savePlayer(p); - } - - for (EsPlayer p : Main.server.getOnlinePlayers()) { - CChest.loadPlayer(p); - } + Main.plugin.enable(); send(sender, "&aReloaded!"); } else if (args[0].equalsIgnoreCase("reset")) { // Resets all configuration files @@ -51,14 +48,18 @@ public boolean execute(EsCommandSender sender, String[] args) { } if (args.length > 1 && args[1].equalsIgnoreCase("confirm")) { - File f = new File(Main.server.getDataFolder(), "give.yml"); - if (!f.delete()) { + File f = Main.server.getDataFolder(); + try { + Files.walk(Paths.get(f.getPath())) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } catch (IOException e) { send(sender, "&cFailed to delete data."); return false; } - - Give.enable(); - send(sender, "&cAll data deleted!"); + + send(sender, "&cAll data has been deleted, &6/estools reload &cto apply changes!"); return true; } From 98c2f5686efa0d39533cfda169abc742989b7315 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 14:32:45 +1000 Subject: [PATCH 67/74] Fix potions in pre 1.4 and move funcs for 1.0 support --- src/main/java/net/serble/estools/Bitmask.java | 26 +++++++ .../net/serble/estools/Commands/Potion.java | 5 -- .../estools/Entrypoints/EsToolsBukkit.java | 10 +++ src/main/java/net/serble/estools/Main.java | 5 +- .../net/serble/estools/SemanticVersion.java | 22 +++++- .../ServerApi/EsSerialisableItemStack.java | 5 +- .../Bukkit/BukkitConsoleSender.java | 34 +++++++++ .../Implementations/Bukkit/BukkitEntity.java | 12 ++- .../Bukkit/BukkitLivingEntity.java | 4 + .../Bukkit/BukkitPotionVeryOld.java | 48 ++++++++++++ .../Implementations/Bukkit/BukkitServer.java | 52 ++++++------- .../EventHandlers/BukkitEventsListener.java | 3 +- .../Bukkit/Helpers/BukkitEnums1_1Plus.java | 69 +++++++++++++++++ .../Bukkit/Helpers/BukkitHelper.java | 74 ++++--------------- .../Bukkit/Helpers/BukkitPotionsHelper.java | 48 ++++++++++++ .../Helpers/BukkitPotionsHelper1_3.java | 40 ++++++++++ .../Folia/FoliaEventsListener.java | 3 +- src/main/java/net/serble/estools/Tester.java | 9 +-- src/main/java/net/serble/estools/Utils.java | 10 +++ 19 files changed, 366 insertions(+), 113 deletions(-) create mode 100644 src/main/java/net/serble/estools/Bitmask.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotionVeryOld.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnums1_1Plus.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java create mode 100644 src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper1_3.java diff --git a/src/main/java/net/serble/estools/Bitmask.java b/src/main/java/net/serble/estools/Bitmask.java new file mode 100644 index 0000000..59b9b49 --- /dev/null +++ b/src/main/java/net/serble/estools/Bitmask.java @@ -0,0 +1,26 @@ +package net.serble.estools; + +public class Bitmask { + private final boolean[] bits; + + public Bitmask(int value) { + bits = new boolean[32]; + for (int i = 0; i < 32; i++) { + bits[i] = (value & (1 << i)) != 0; + } + } + + public boolean getBit(int i) { + return bits[i]; + } + + public int getValueOfFirstBits(int amount) { + int value = 0; + for (int i = 0; i < amount; i++) { + if (bits[i]) { + value |= 1 << i; + } + } + return value; + } +} diff --git a/src/main/java/net/serble/estools/Commands/Potion.java b/src/main/java/net/serble/estools/Commands/Potion.java index 6e14ad0..2b8e5c0 100644 --- a/src/main/java/net/serble/estools/Commands/Potion.java +++ b/src/main/java/net/serble/estools/Commands/Potion.java @@ -86,11 +86,6 @@ public boolean execute(EsCommandSender sender, String[] args) { } } - if (Main.minecraftVersion.getMinor() <= 3) { - send(sender, "&cPotions are not yet supported in this version, they may be in the future."); - return false; - } - EsItemStack pot = Main.server.createPotion(potType, new EsPotionEffect(type, amp, duration), amount); if (pot == null) { send(sender, "&cInvalid potion effect!"); diff --git a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java index 36f2327..582b476 100644 --- a/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java +++ b/src/main/java/net/serble/estools/Entrypoints/EsToolsBukkit.java @@ -1,6 +1,7 @@ package net.serble.estools.Entrypoints; import net.serble.estools.Main; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.ServerPlatform; import org.bukkit.plugin.java.JavaPlugin; @@ -20,6 +21,15 @@ public void onEnable() { Main main = new Main(platform, this); main.enable(); + + // Init any Bukkit helpers that need it. + // It's best to do it here because Folia and Bukkit + // need it. + // This must come after Main.enable() so that mcVersion is set. + // It also only works on 1.1+ + if (Main.minecraftVersion.isAtLeast(1, 1, 0)) { + BukkitEffectHelper.load(); + } } @Override diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index 88788ad..cfb9306 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -14,7 +14,6 @@ import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsGameMode; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitConfigMigrator; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsLogger; @@ -62,8 +61,6 @@ public void enable() { server.initialise(); logger.info("Starting EsTools on platform: " + platform.name() + " (MC: " + minecraftVersion.getString() + ")"); - BukkitEffectHelper.load(); - if (platform == ServerPlatform.Bukkit) { // We have to support old configs BukkitConfigMigrator.checkPerformMigration(); @@ -145,7 +142,7 @@ public void enable() { sc("buddha", "god", new Buddha(), 1); sc("music", "music", new Music(), 9); - sc("potion", "potion", new Potion(), 4); + sc("potion", "potion", new Potion()); sc("setpersistentdata", "setpersistentdata", new SetPersistentData(), 14); sc("getpersistentdata", "getpersistentdata", new GetPersistentData(), 14); diff --git a/src/main/java/net/serble/estools/SemanticVersion.java b/src/main/java/net/serble/estools/SemanticVersion.java index a83fbb2..8370f6c 100644 --- a/src/main/java/net/serble/estools/SemanticVersion.java +++ b/src/main/java/net/serble/estools/SemanticVersion.java @@ -1,5 +1,6 @@ package net.serble.estools; +@SuppressWarnings("unused") // Future-proof, it's a util class why not public class SemanticVersion { private final int major; private final int minor; @@ -57,7 +58,6 @@ public boolean isLowerThan(int major, int minor, int patch) { return getPatch() < patch; } - @SuppressWarnings("unused") // Future-proof public boolean isAtLeast(SemanticVersion other) { return isAtLeast(other.major, other.minor, other.patch); } @@ -77,4 +77,24 @@ public boolean isAtLeast(int major, int minor, int patch) { return getPatch() >= patch; } + + public boolean isMoreThan(int major, int minor, int patch) { + if (getMajor() > major) { + return true; + } else if (getMajor() < major) { + return false; + } + + if (getMinor() > minor) { + return true; + } else if (getMinor() < minor) { + return false; + } + + return getPatch() > patch; + } + + public boolean isMoreThan(SemanticVersion other) { + return isMoreThan(other.major, other.minor, other.patch); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java index 040eee2..d5f80de 100644 --- a/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/EsSerialisableItemStack.java @@ -6,8 +6,9 @@ import java.util.Map; - -// TODO: NBT +/** + * This class is entirely platform independent and replies on the ability to export ItemMeta is a string. + */ @SuppressWarnings("unused") // Needs the methods for YAML serialiser public class EsSerialisableItemStack { private EsMaterial material; diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java index 85c36b7..ffebb26 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitConsoleSender.java @@ -2,8 +2,11 @@ import net.serble.estools.Main; import net.serble.estools.ServerApi.Interfaces.EsConsoleSender; +import net.serble.estools.Utils; import org.bukkit.command.ConsoleCommandSender; +// We have to execute methods using reflection because +// in 1.0 ConsoleCommandSender is a class not an interface public class BukkitConsoleSender implements EsConsoleSender { private final ConsoleCommandSender bukkitSender; @@ -24,16 +27,47 @@ public void sendMessage(String... msg) { for (String s : msg) { sb.append(s); } + + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + try { + ConsoleCommandSender.class.getMethod("sendMessage", String.class).invoke(bukkitSender, sb.toString()); + } catch (Exception e) { + Main.logger.severe(Utils.getStacktrace(e)); + } + return; + } bukkitSender.sendMessage(sb.toString()); } @Override public boolean hasPermission(String node) { + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + try { + return (boolean) ConsoleCommandSender.class + .getMethod("hasPermission", String.class) + .invoke(bukkitSender, node); + } catch (Exception e) { + Main.logger.severe(Utils.getStacktrace(e)); + } + return false; + } + return bukkitSender.hasPermission(node); } @Override public boolean isPermissionSet(String node) { + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + try { + return (boolean) ConsoleCommandSender.class + .getMethod("isPermissionSet", String.class) + .invoke(bukkitSender, node); + } catch (Exception e) { + Main.logger.severe(Utils.getStacktrace(e)); + } + return false; + } + return bukkitSender.isPermissionSet(node); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java index e113282..39fbf56 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitEntity.java @@ -1,6 +1,5 @@ package net.serble.estools.ServerApi.Implementations.Bukkit; -import io.papermc.lib.PaperLib; import net.serble.estools.ServerApi.EsLocation; import net.serble.estools.Main; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; @@ -27,12 +26,19 @@ public Entity getBukkitEntity() { @Override public void teleport(EsLocation loc) { - PaperLib.teleportAsync(bukkitEntity, BukkitHelper.toBukkitLocation(loc)); + // This was moved from PaperLib.teleport() because the PaperLib class imports + // things that don't exist in Bukkit 1.0 and therefore can't load. + bukkitEntity.teleport(BukkitHelper.toBukkitLocation(loc)); } @Override public String getType() { - return bukkitEntity.getType().name(); + if (Main.minecraftVersion.isAtLeast(1, 1, 0)) { + return bukkitEntity.getType().name(); + } + + // getType() doesn't exist + return "ID " + bukkitEntity.getEntityId(); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java index 0765357..10a3997 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitLivingEntity.java @@ -112,6 +112,10 @@ public void removePotionEffect(EsPotionEffectType effect) { @Override public List getActivePotionEffects() { + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + return new ArrayList<>(); + } + Collection bukkitEffects = bukkitEntity.getActivePotionEffects(); List out = new ArrayList<>(bukkitEffects.size()); for (PotionEffect eff : bukkitEffects) { diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotionVeryOld.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotionVeryOld.java new file mode 100644 index 0000000..adce9c3 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitPotionVeryOld.java @@ -0,0 +1,48 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit; + +import net.serble.estools.Bitmask; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitPotionsHelper1_3; +import net.serble.estools.ServerApi.Interfaces.EsPotion; +import org.bukkit.inventory.ItemStack; + +/** + * Potions didn't used to have special code qualities in 1.3 and prior. + * The potion was dictated by its durability value. + * So in this case we need just a simple wrapper of BukkitItemStack that does + * nothing extra. + */ +@SuppressWarnings("deprecation") // This class is for 1.3 and below +public class BukkitPotionVeryOld extends BukkitItemStack implements EsPotion { + private final Bitmask data; + private static final short splashBit = 14; // Decimal val: 16384. 2^14=16384 + private static final short potionNameBits = 6; // There are max of 64 vals, 2^6=64 https://minecraft.wiki/w/Java_Edition_data_values/Pre-flattening#%22Potion_name%22_bits + + public BukkitPotionVeryOld(ItemStack is) { + super(is); + data = new Bitmask(is.getDurability()); + } + + @Override + public EsPotionEffect[] getEffects() { + return new EsPotionEffect[] { + new EsPotionEffect( + BukkitPotionsHelper1_3.getEffectTypeFromRawId((short) data.getValueOfFirstBits(potionNameBits)), + 0, 1) // Amp and duration are ignored because they can't be customised + }; + } + + @Override + public EsPotType getPotionType() { + if (data.getBit(splashBit)) { + return EsPotType.splash; + } + return EsPotType.drink; + } + + @Override + public void addEffect(EsPotionEffect effect) { + // This isn't possible because custom effects don't exist + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index bd8e089..49a069f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -14,8 +14,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionType; import java.io.File; import java.util.*; @@ -164,30 +162,17 @@ public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amoun return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { - PotionType type = BukkitEffectHelper.getPotionFromEffectType(effect.getType()); - if (type == null) { // This can fail if the effect doesn't have a potion for it - return null; - } - - // in 1.7 and below potions start at amplifier 1, and can only be 1 or 2, after that it starts at 0 - int amplifier = effect.getAmp(); - if (Main.minecraftVersion.getMinor() <= 7) { - amplifier++; - if (amplifier < 1 || amplifier > 2) { - return null; - } + return BukkitPotionsHelper.createPotion1_4(potType, effect, amount); + } else { + short potionDat = BukkitPotionsHelper1_3.getPotionIdFromEffectType(effect.getType()); + if (potType == EsPotType.splash || potType == EsPotType.lingering) { + potionDat += 16384; } - - Potion potion = new Potion(type, amplifier); - potion.setSplash(potType == EsPotType.splash); - - return new BukkitPotion(potion.toItemStack(amount)); - } else { // This isn't possible to get to because this class won't load on 1.3 and below - return null; + ItemStack stack = new ItemStack(Material.POTION, 1, potionDat); + return new BukkitPotionVeryOld(stack); } } - @SuppressWarnings("deprecation") @Override public EsPotion createPotion(EsPotType potType) { if (Main.minecraftVersion.getMinor() >= 9) { @@ -199,12 +184,11 @@ public EsPotion createPotion(EsPotType potType) { return new BukkitPotion(pot); } else if (Main.minecraftVersion.getMinor() >= 4) { - Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); - potion.setSplash(potType == EsPotType.splash); - - return new BukkitPotion(potion.toItemStack(1)); - } else { // This isn't possible to get to because this class won't load on 1.3 and below - return null; + return BukkitPotionsHelper.createPotion1_4(potType); + } else { + // This method isn't called pre 1.4 because CChest doesn't work + // With the way this works it wouldn't work in pre 1.4. + throw new UnsupportedOperationException("Proper support in this version has not been implemented."); } } @@ -221,11 +205,17 @@ public EsInventory createInventory(EsPlayer owner, int size, String title) { @Override public Set getPotionEffectTypes() { + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + return BukkitPotionsHelper1_3.getPotionList(); + } return BukkitEffectHelper.getEffectList(); } @Override public Set getOldPotionTypes() { + if (Main.minecraftVersion.isLowerThan(1, 1, 0)) { + return BukkitPotionsHelper1_3.getPotionList(); + } return BukkitEffectHelper.getPotionList(); } @@ -286,7 +276,11 @@ public EsLogger getLogger() { @Override public void startEvents() { - Bukkit.getPluginManager().registerEvents(listener, EsToolsBukkit.plugin); + if (Main.minecraftVersion.isMoreThan(1, 0, 0)) { // Events work differently in 1.0 + Bukkit.getPluginManager().registerEvents(listener, EsToolsBukkit.plugin); + } + + // TODO: 1.0 events } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java index 9586afe..b5ccb5a 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListener.java @@ -5,6 +5,7 @@ import net.serble.estools.ServerApi.EsEquipmentSlot; import net.serble.estools.ServerApi.Events.*; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPlayer; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnums1_1Plus; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEquipmentSlotHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; @@ -73,7 +74,7 @@ public void onBlockPlace(BlockPlaceEvent e) { public void onTeleport(PlayerTeleportEvent e) { EsPlayerTeleportEvent ee = new EsPlayerTeleportEvent( new BukkitPlayer(e.getPlayer()), - BukkitHelper.fromBukkitTeleportCause(e.getCause()), + BukkitEnums1_1Plus.fromBukkitTeleportCause(e.getCause()), BukkitHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); ee.setCancelled(e.isCancelled()); Main.callEvent(ee); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnums1_1Plus.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnums1_1Plus.java new file mode 100644 index 0000000..f98e1eb --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitEnums1_1Plus.java @@ -0,0 +1,69 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.ServerApi.EsTeleportCause; +import org.bukkit.event.player.PlayerTeleportEvent; + +/** + * Min version: 1.1 + */ +@SuppressWarnings("unused") +public class BukkitEnums1_1Plus { + + public static PlayerTeleportEvent.TeleportCause toBukkitTeleportCause(EsTeleportCause esCause) { + switch (esCause) { + case EnderPearl: + return PlayerTeleportEvent.TeleportCause.ENDER_PEARL; + case Command: + return PlayerTeleportEvent.TeleportCause.COMMAND; + case Plugin: + return PlayerTeleportEvent.TeleportCause.PLUGIN; + case NetherPortal: + return PlayerTeleportEvent.TeleportCause.NETHER_PORTAL; + case EndPortal: + return PlayerTeleportEvent.TeleportCause.END_PORTAL; + case Spectate: + return PlayerTeleportEvent.TeleportCause.SPECTATE; + case EndGateway: + return PlayerTeleportEvent.TeleportCause.END_GATEWAY; + case ChorusFruit: + return PlayerTeleportEvent.TeleportCause.CHORUS_FRUIT; + case Dismount: + return PlayerTeleportEvent.TeleportCause.DISMOUNT; + case ExitBed: + return PlayerTeleportEvent.TeleportCause.EXIT_BED; + case Unknown: + return PlayerTeleportEvent.TeleportCause.UNKNOWN; + default: + throw new IllegalArgumentException("Unknown EsTeleportCause: " + esCause); + } + } + + public static EsTeleportCause fromBukkitTeleportCause(PlayerTeleportEvent.TeleportCause bukkitCause) { + switch (bukkitCause) { + case ENDER_PEARL: + return EsTeleportCause.EnderPearl; + case COMMAND: + return EsTeleportCause.Command; + case PLUGIN: + return EsTeleportCause.Plugin; + case NETHER_PORTAL: + return EsTeleportCause.NetherPortal; + case END_PORTAL: + return EsTeleportCause.EndPortal; + case SPECTATE: + return EsTeleportCause.Spectate; + case END_GATEWAY: + return EsTeleportCause.EndGateway; + case CHORUS_FRUIT: + return EsTeleportCause.ChorusFruit; + case DISMOUNT: + return EsTeleportCause.Dismount; + case EXIT_BED: + return EsTeleportCause.ExitBed; + case UNKNOWN: + return EsTeleportCause.Unknown; + default: + throw new IllegalArgumentException("Invalid TeleportCause"); + } + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java index 0817572..e9843c4 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitHelper.java @@ -17,7 +17,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -28,6 +27,15 @@ // DO NOT IMPORT THE FOLLOWING BECAUSE THEY BREAK OLDER VERSIONS BECAUSE THEY DON'T EXIST // org.bukkit.inventory.EquipmentSlot +// org.bukkit.event.player.PlayerTeleportEvent +// org.bukkit.potion.Potion +// org.bukkit.potion.PotionType +// org.bukkit.inventory.InventoryHolder +// org.bukkit.inventory.meta.PotionMeta +// org.bukkit.inventory.meta.ItemMeta +// org.bukkit.Sound +// org.bukkit.event.inventory.ClickType +// org.bukkit.event.inventory.InventoryAction /** * Min version: 1.0 @@ -170,64 +178,6 @@ public static EsGameMode fromBukkitGameMode(GameMode mode) { throw new RuntimeException("Invalid GameMode"); } - public static PlayerTeleportEvent.TeleportCause toBukkitTeleportCause(EsTeleportCause esCause) { - switch (esCause) { - case EnderPearl: - return PlayerTeleportEvent.TeleportCause.ENDER_PEARL; - case Command: - return PlayerTeleportEvent.TeleportCause.COMMAND; - case Plugin: - return PlayerTeleportEvent.TeleportCause.PLUGIN; - case NetherPortal: - return PlayerTeleportEvent.TeleportCause.NETHER_PORTAL; - case EndPortal: - return PlayerTeleportEvent.TeleportCause.END_PORTAL; - case Spectate: - return PlayerTeleportEvent.TeleportCause.SPECTATE; - case EndGateway: - return PlayerTeleportEvent.TeleportCause.END_GATEWAY; - case ChorusFruit: - return PlayerTeleportEvent.TeleportCause.CHORUS_FRUIT; - case Dismount: - return PlayerTeleportEvent.TeleportCause.DISMOUNT; - case ExitBed: - return PlayerTeleportEvent.TeleportCause.EXIT_BED; - case Unknown: - return PlayerTeleportEvent.TeleportCause.UNKNOWN; - default: - throw new IllegalArgumentException("Unknown EsTeleportCause: " + esCause); - } - } - - public static EsTeleportCause fromBukkitTeleportCause(PlayerTeleportEvent.TeleportCause bukkitCause) { - switch (bukkitCause) { - case ENDER_PEARL: - return EsTeleportCause.EnderPearl; - case COMMAND: - return EsTeleportCause.Command; - case PLUGIN: - return EsTeleportCause.Plugin; - case NETHER_PORTAL: - return EsTeleportCause.NetherPortal; - case END_PORTAL: - return EsTeleportCause.EndPortal; - case SPECTATE: - return EsTeleportCause.Spectate; - case END_GATEWAY: - return EsTeleportCause.EndGateway; - case CHORUS_FRUIT: - return EsTeleportCause.ChorusFruit; - case DISMOUNT: - return EsTeleportCause.Dismount; - case EXIT_BED: - return EsTeleportCause.ExitBed; - case UNKNOWN: - return EsTeleportCause.Unknown; - default: - throw new IllegalArgumentException("Invalid TeleportCause"); - } - } - public static EsBlock fromBukkitBlock(Block block) { if (block == null) { return null; @@ -314,7 +264,11 @@ public static EsItemStack fromBukkitItem(ItemStack item) { return new BukkitItemStack(item); } else { - return new BukkitItemStack(item); // Not a potion + if (item.getType().name().endsWith("POTION")) { + return new BukkitPotionVeryOld(item); // The Potion and PotionMeta classes don't exist + } + + return new BukkitItemStack(item); } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java new file mode 100644 index 0000000..9d0957c --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java @@ -0,0 +1,48 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.Main; +import net.serble.estools.ServerApi.EsPotType; +import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPotion; +import net.serble.estools.ServerApi.Interfaces.EsPotion; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionType; + +/** + * Min version: 1.4 + */ +@SuppressWarnings("deprecation") // Old versions go brrrr +public class BukkitPotionsHelper { + + /** Used for 1.4-1.8 */ + public static EsPotion createPotion1_4(EsPotType potType, EsPotionEffect effect, int amount) { + PotionType type = BukkitEffectHelper.getPotionFromEffectType(effect.getType()); + if (type == null) { // This can fail if the effect doesn't have a potion for it + return null; + } + + // in 1.7 and below potions start at amplifier 1, and can only be 1 or 2, after that it starts at 0 + int amplifier = effect.getAmp(); + if (Main.minecraftVersion.getMinor() <= 7) { + amplifier++; + if (amplifier < 1 || amplifier > 2) { + return null; + } + } + + Potion potion = new Potion(type, amplifier); + potion.setSplash(potType == EsPotType.splash); + + return new BukkitPotion(potion.toItemStack(amount)); + } + + /** Used for 1.4-1.8 */ + public static EsPotion createPotion1_4(EsPotType potType) { + Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); + potion.setSplash(potType == EsPotType.splash); + + return new BukkitPotion(potion.toItemStack(1)); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper1_3.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper1_3.java new file mode 100644 index 0000000..0fa5c84 --- /dev/null +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper1_3.java @@ -0,0 +1,40 @@ +package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; + +import net.serble.estools.ServerApi.EsPotionEffectType; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class BukkitPotionsHelper1_3 { + private static final Map POTIONS = new HashMap<>(); + + static { // Potion Type IDs https://minecraft.wiki/w/Java_Edition_data_values/Pre-flattening#%22Potion_effect%22_bits + POTIONS.put(EsPotionEffectType.createUnchecked("regeneration"), (short) 1); + POTIONS.put(EsPotionEffectType.createUnchecked("speed"), (short) 2); + POTIONS.put(EsPotionEffectType.createUnchecked("fire_resistance"), (short) 3); + POTIONS.put(EsPotionEffectType.createUnchecked("poison"), (short) 4); + POTIONS.put(EsPotionEffectType.createUnchecked("instant_health"), (short) 5); + POTIONS.put(EsPotionEffectType.createUnchecked("weakness"), (short) 8); + POTIONS.put(EsPotionEffectType.createUnchecked("strength"), (short) 9); + POTIONS.put(EsPotionEffectType.createUnchecked("slowness"), (short) 10); + POTIONS.put(EsPotionEffectType.createUnchecked("instant_damage"), (short) 12); + } + + public static short getPotionIdFromEffectType(EsPotionEffectType type) { + return POTIONS.get(type); + } + + public static EsPotionEffectType getEffectTypeFromRawId(short id) { + for (Map.Entry entry : POTIONS.entrySet()) { + if (entry.getValue() == id) { + return entry.getKey(); + } + } + return null; + } + + public static Set getPotionList() { + return POTIONS.keySet(); + } +} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java index f762bee..5829fb2 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaEventsListener.java @@ -5,6 +5,7 @@ import net.serble.estools.ServerApi.EsClickType; import net.serble.estools.ServerApi.EsInventoryAction; import net.serble.estools.ServerApi.Events.*; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnums1_1Plus; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -70,7 +71,7 @@ public void onBlockPlace(BlockPlaceEvent e) { public void onTeleport(PlayerTeleportEvent e) { EsPlayerTeleportEvent ee = new EsPlayerTeleportEvent( new FoliaPlayer(e.getPlayer()), - FoliaHelper.fromBukkitTeleportCause(e.getCause()), + BukkitEnums1_1Plus.fromBukkitTeleportCause(e.getCause()), FoliaHelper.fromBukkitLocation(Objects.requireNonNull(e.getTo()))); ee.setCancelled(e.isCancelled()); Main.callEvent(ee); diff --git a/src/main/java/net/serble/estools/Tester.java b/src/main/java/net/serble/estools/Tester.java index 89986fe..5ccd23f 100644 --- a/src/main/java/net/serble/estools/Tester.java +++ b/src/main/java/net/serble/estools/Tester.java @@ -4,8 +4,6 @@ import net.serble.estools.ServerApi.Interfaces.EsEntity; import net.serble.estools.ServerApi.Interfaces.EsPlayer; -import java.io.PrintWriter; -import java.io.StringWriter; import java.util.HashMap; import java.util.UUID; @@ -87,7 +85,7 @@ public class Tester { new TestCommand("noon", 1), new TestCommand("midnight", 1), new TestCommand("day", 1), - new TestCommand("potion speed", 2, new SemanticVersion(1, 4, 0)), + new TestCommand("potion speed", 2), new TestCommand("rename &6This dirt is special", 4, new SemanticVersion(1, 4, 6)), new TestCommand("setmaxhealth 30", 2, new SemanticVersion(1, 4, 0)), new TestCommand("setstack 127", 2), @@ -195,10 +193,7 @@ private void execNextCommand() { EsToolsCommand.send(player, "&cFailed to execute the following command: " + cmd); // Print the full stacktrace not just the message - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - String stackTrace = sw.toString(); + String stackTrace = Utils.getStacktrace(e); EsToolsCommand.send(player, "&c" + stackTrace); Main.logger.severe(stackTrace); diff --git a/src/main/java/net/serble/estools/Utils.java b/src/main/java/net/serble/estools/Utils.java index 4afcacd..49f4a25 100644 --- a/src/main/java/net/serble/estools/Utils.java +++ b/src/main/java/net/serble/estools/Utils.java @@ -1,5 +1,8 @@ package net.serble.estools; +import java.io.PrintWriter; +import java.io.StringWriter; + public class Utils { public static String keyToDisplayName(String key) { StringBuilder name = new StringBuilder(key); @@ -17,4 +20,11 @@ public static String keyToDisplayName(String key) { return name.toString(); } + + public static String getStacktrace(Exception e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + return sw.toString(); + } } From e136d34594f0aab510e814a785312444d575436e Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 15:14:22 +1000 Subject: [PATCH 68/74] Fix /rename not checking if meta is null, also removed MetaHandler which was Bukkit specific to an extent --- .../net/serble/estools/Commands/Lore.java | 11 +++++--- .../net/serble/estools/Commands/Rename.java | 16 ++++++++--- .../java/net/serble/estools/MetaHandler.java | 28 ------------------- .../Bukkit/BukkitItemMeta.java | 3 ++ .../Bukkit/BukkitItemStack.java | 3 +- .../Bukkit/Helpers/BukkitMetaHelper.java | 11 ++++++++ .../Implementations/Folia/FoliaHelper.java | 9 ++++++ .../Implementations/Folia/FoliaItemStack.java | 2 +- 8 files changed, 45 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/net/serble/estools/MetaHandler.java diff --git a/src/main/java/net/serble/estools/Commands/Lore.java b/src/main/java/net/serble/estools/Commands/Lore.java index b6af4ed..bc01cb5 100644 --- a/src/main/java/net/serble/estools/Commands/Lore.java +++ b/src/main/java/net/serble/estools/Commands/Lore.java @@ -1,8 +1,8 @@ package net.serble.estools.Commands; import net.serble.estools.EsToolsCommand; -import net.serble.estools.MetaHandler; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -34,7 +34,9 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - List lore = MetaHandler.getLore(is); + EsItemMeta meta = is.getItemMeta(); + + List lore = meta.getLore(); switch (args[0].toLowerCase()) { case "add": { @@ -81,7 +83,8 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - MetaHandler.setLore(is, lore); + meta.setLore(lore); + is.setItemMeta(meta); return true; } @@ -122,7 +125,7 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA } EsItemStack is = ((EsPlayer)sender).getMainHand(); - List lore = MetaHandler.getLore(is); + List lore = is.getItemMeta().getLore(); for (int i = 1; i <= lore.size(); i++) { tab.add(String.valueOf(i)); } diff --git a/src/main/java/net/serble/estools/Commands/Rename.java b/src/main/java/net/serble/estools/Commands/Rename.java index dd7f58f..22d2366 100644 --- a/src/main/java/net/serble/estools/Commands/Rename.java +++ b/src/main/java/net/serble/estools/Commands/Rename.java @@ -1,9 +1,8 @@ package net.serble.estools.Commands; -import net.serble.estools.MetaHandler; - import net.serble.estools.EsToolsCommand; import net.serble.estools.ServerApi.Interfaces.EsCommandSender; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import net.serble.estools.ServerApi.Interfaces.EsPlayer; @@ -19,14 +18,23 @@ public boolean execute(EsCommandSender sender, String[] args) { EsPlayer p = (EsPlayer) sender; EsItemStack is = p.getMainHand(); + EsItemMeta meta = is.getItemMeta(); + + if (meta == null) { + send(sender, "&cYou must be holding an item"); + return false; + } + if (args.length == 0) { - MetaHandler.renameItem(is, ""); + meta.setDisplayName(""); + is.setItemMeta(meta); send(sender, "&aRemoved item name"); return false; } String name = translate("&r" + String.join(" ", args)); - MetaHandler.renameItem(is, name); + meta.setDisplayName(name); + is.setItemMeta(meta); send(sender, "&aItem renamed to &6%s", name); return true; } diff --git a/src/main/java/net/serble/estools/MetaHandler.java b/src/main/java/net/serble/estools/MetaHandler.java deleted file mode 100644 index 8bff903..0000000 --- a/src/main/java/net/serble/estools/MetaHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.serble.estools; - -import net.serble.estools.ServerApi.Interfaces.EsItemMeta; -import net.serble.estools.ServerApi.Interfaces.EsItemStack; - -import java.util.List; -import java.util.Objects; - -// TODO: This is Bukkit specific (But still works on other platforms), migrate commands away from using it -// This class exists because Minecraft < 1.4 doesn't have ItemMeta. It just encapsulates all ItemMeta methods. -public class MetaHandler { - - public static void renameItem(EsItemStack item, String name) { - EsItemMeta meta = Objects.requireNonNull(item.getItemMeta()); - meta.setDisplayName(name); - item.setItemMeta(meta); - } - - public static List getLore(EsItemStack item) { - return item.getItemMeta().getLore(); - } - - public static void setLore(EsItemStack item, List lore) { - EsItemMeta meta = Objects.requireNonNull(item.getItemMeta()); - meta.setLore(lore); - item.setItemMeta(meta); - } -} diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java index a6b9f3b..24c28f6 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemMeta.java @@ -15,6 +15,9 @@ public class BukkitItemMeta implements EsItemMeta { private final ItemMeta bukkitMeta; public BukkitItemMeta(ItemMeta meta) { + if (meta == null) { + throw new NullPointerException("Meta is null"); + } bukkitMeta = meta; } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java index ee5484b..559eb11 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitItemStack.java @@ -5,6 +5,7 @@ import net.serble.estools.ServerApi.EsMaterial; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEnchantmentHelper; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; +import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitMetaHelper; import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import net.serble.estools.ServerApi.Interfaces.EsItemStack; import org.bukkit.configuration.InvalidConfigurationException; @@ -65,7 +66,7 @@ public void removeEnchantment(EsEnchantment enchantment) { @Override public EsItemMeta getItemMeta() { - return new BukkitItemMeta(bukkitItem.getItemMeta()); + return BukkitMetaHelper.fromBukkitItemMeta(bukkitItem.getItemMeta()); } @Override diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java index b23e6a1..ef017e1 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitMetaHelper.java @@ -1,7 +1,10 @@ package net.serble.estools.ServerApi.Implementations.Bukkit.Helpers; import net.serble.estools.ServerApi.EsPotionEffect; +import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitItemMeta; +import net.serble.estools.ServerApi.Interfaces.EsItemMeta; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; /** @@ -15,4 +18,12 @@ public static void setPotionType(ItemStack pot, EsPotionEffect effect) { meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); pot.setItemMeta(meta); } + + public static EsItemMeta fromBukkitItemMeta(ItemMeta meta) { + if (meta == null) { + return null; + } + + return new BukkitItemMeta(meta); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java index 1645762..e4e3379 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaHelper.java @@ -28,6 +28,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; import java.lang.reflect.InvocationTargetException; @@ -446,4 +447,12 @@ public static EsEquipmentSlot fromBukkitEquipmentSlot(EquipmentSlot slot) { throw new IllegalArgumentException("Invalid EquipmentSlot"); } } + + public static EsItemMeta fromBukkitItemMeta(ItemMeta meta) { + if (meta == null) { + return null; + } + + return new FoliaItemMeta(meta); + } } diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java index 3e8b019..99c6d0c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaItemStack.java @@ -55,7 +55,7 @@ public void setType(EsMaterial val) { @Override public EsItemMeta getItemMeta() { - return new FoliaItemMeta(bukkitItem.getItemMeta()); + return FoliaHelper.fromBukkitItemMeta(bukkitItem.getItemMeta()); } @Override From 895ad71b156ff6d8ae3f5f496683807c1fe4f6f1 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 15:40:47 +1000 Subject: [PATCH 69/74] Fix events not behaving properly on old versions --- .../Implementations/Bukkit/BukkitServer.java | 28 +++++++++++++------ .../BukkitEventsListenerPost1_1.java | 9 ++++-- .../BukkitEventsListenerPost1_4.java | 4 ++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java index 49a069f..7c9e73f 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitServer.java @@ -9,9 +9,11 @@ import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.*; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.Listener; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; @@ -20,7 +22,8 @@ public class BukkitServer implements EsServer { private final JavaPlugin plugin; - private final BukkitEventsListener listener; + private final List listeners; + private final CommandExecutor cmdExecutor; private static final Set materials = new HashSet<>(); private static final Set itemMaterials = new HashSet<>(); private static Set sounds = new HashSet<>(); @@ -28,14 +31,19 @@ public class BukkitServer implements EsServer { public BukkitServer(Object pluginObj) { plugin = (JavaPlugin) pluginObj; + listeners = new ArrayList<>(); + SemanticVersion mcVer = getVersion(); if (mcVer.isAtLeast(1, 5, 0)) { - listener = new BukkitEventsListenerPost1_4(); - } else if (mcVer.isAtLeast(1, 2, 0)) { - listener = new BukkitEventsListenerPost1_1(); - } else { - listener = new BukkitEventsListener(); + listeners.add(new BukkitEventsListenerPost1_4()); + } + if (mcVer.isAtLeast(1, 2, 0)) { + listeners.add(new BukkitEventsListenerPost1_1()); } + + BukkitEventsListener bel = new BukkitEventsListener(); + listeners.add(bel); + cmdExecutor = bel; } @Override @@ -277,17 +285,21 @@ public EsLogger getLogger() { @Override public void startEvents() { if (Main.minecraftVersion.isMoreThan(1, 0, 0)) { // Events work differently in 1.0 - Bukkit.getPluginManager().registerEvents(listener, EsToolsBukkit.plugin); + for (Listener l : listeners) { + Bukkit.getPluginManager().registerEvents(l, EsToolsBukkit.plugin); + } + return; } // TODO: 1.0 events + Main.logger.warning("Events are not currently functional in Minecraft 1.0.0 and below"); } @Override public void registerCommand(String cmd, EsToolsTabCompleter tab) { PluginCommand command = Objects.requireNonNull(Bukkit.getPluginCommand(cmd)); if (!Main.tabCompleteEnabled || command.getTabCompleter() == null) { - command.setExecutor(listener); + command.setExecutor(cmdExecutor); } if (Main.tabCompleteEnabled) { command.setTabCompleter(BukkitTabCompleteGenerator.generate(tab)); diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java index 0dae623..4c3d253 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java @@ -13,10 +13,12 @@ import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; -public class BukkitEventsListenerPost1_1 extends BukkitEventsListener { +// Apparently inheriting BukkitEventsListener stops the events from working in BukkitEventsListener +public class BukkitEventsListenerPost1_1 implements Listener { @EventHandler public void onInvClick(InventoryClickEvent e) { @@ -49,8 +51,11 @@ public void onInvClick(InventoryClickEvent e) { int sl = e.getSlot(); EsInventoryClickEvent ee = new EsInventoryClickEvent(cl, sl, cu, inv, clInv, ci, ac, ct); ee.setCancelled(e.isCancelled()); + boolean wasCancelled = e.isCancelled(); Main.callEvent(ee); - e.setCancelled(ee.isCancelled()); + if (wasCancelled != ee.isCancelled()) { // Fix bug where it cancels + e.setCancelled(ee.isCancelled()); + } } @EventHandler diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java index 660d8ef..0edc79c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_4.java @@ -10,11 +10,13 @@ import net.serble.estools.ServerApi.Interfaces.EsPlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryDragEvent; import java.util.Set; -public class BukkitEventsListenerPost1_4 extends BukkitEventsListenerPost1_1 { +// Apparently inheriting BukkitEventsListener stops the events from working in BukkitEventsListener +public class BukkitEventsListenerPost1_4 implements Listener { @EventHandler public void onDrag(InventoryDragEvent e) { From 4f644624df0e4bf099a6b80c8d7c219bb11d0f07 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 16:14:36 +1000 Subject: [PATCH 70/74] Change version of getClickedInventory being added --- .../Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java index 4c3d253..9ef897c 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/EventHandlers/BukkitEventsListenerPost1_1.java @@ -23,7 +23,7 @@ public class BukkitEventsListenerPost1_1 implements Listener { @EventHandler public void onInvClick(InventoryClickEvent e) { EsInventory clInv; - if (Main.minecraftVersion.isAtLeast(1, 6, 0)) { + if (Main.minecraftVersion.isAtLeast(1, 8, 0)) { clInv = BukkitHelper.fromBukkitInventory(e.getClickedInventory()); } else { clInv = BukkitHelper.fromBukkitInventory(e.getInventory()); // Get clicked inv doesn't exist From ff2a4aebaaabd03ffa1433394c8a21146db134f0 Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 16:42:29 +1000 Subject: [PATCH 71/74] Fix version where potion amp is limited --- .../Implementations/Bukkit/Helpers/BukkitPotionsHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java index 9d0957c..a4b6d90 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/Helpers/BukkitPotionsHelper.java @@ -25,7 +25,7 @@ public static EsPotion createPotion1_4(EsPotType potType, EsPotionEffect effect, // in 1.7 and below potions start at amplifier 1, and can only be 1 or 2, after that it starts at 0 int amplifier = effect.getAmp(); - if (Main.minecraftVersion.getMinor() <= 7) { + if (Main.minecraftVersion.getMinor() <= 8) { amplifier++; if (amplifier < 1 || amplifier > 2) { return null; From f3effc3b287be27d363d5092f75575957363b94f Mon Sep 17 00:00:00 2001 From: Calcilore Date: Sun, 23 Jun 2024 16:46:30 +1000 Subject: [PATCH 72/74] Changed the version where InventoryView.getInventory(int slot) was added. --- .../ServerApi/Implementations/Bukkit/BukkitInventoryView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java index 61e8085..88528c1 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Bukkit/BukkitInventoryView.java @@ -27,7 +27,7 @@ public EsInventory getBottomInventory() { @Override public EsInventory getInventory(int slot) { - if (Main.minecraftVersion.getMinor() <= 10) { // .getInventory(slot) doesn't exist + if (Main.minecraftVersion.getMinor() <= 12) { // .getInventory(slot) doesn't exist if (slot >= getTopInventory().getSize()) { // Bottom inv return getBottomInventory(); } From 4774cc5aa52c927d247c732cae2b89f36b292a4e Mon Sep 17 00:00:00 2001 From: copokbl Date: Sun, 23 Jun 2024 16:58:01 +1000 Subject: [PATCH 73/74] Fix folia creating BukkitPotion --- .../Implementations/Folia/FoliaServer.java | 57 +++++-------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java index bde383d..9ec2851 100644 --- a/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java +++ b/src/main/java/net/serble/estools/ServerApi/Implementations/Folia/FoliaServer.java @@ -7,10 +7,8 @@ import net.serble.estools.*; import net.serble.estools.Entrypoints.EsToolsBukkit; import net.serble.estools.ServerApi.*; -import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitPotion; import net.serble.estools.ServerApi.Implementations.Bukkit.BukkitTabCompleteGenerator; import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitEffectHelper; -import net.serble.estools.ServerApi.Implementations.Bukkit.Helpers.BukkitHelper; import net.serble.estools.ServerApi.Interfaces.*; import org.bukkit.*; import org.bukkit.command.PluginCommand; @@ -20,8 +18,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionType; import java.io.File; import java.util.*; @@ -151,55 +147,30 @@ public EsItemStack createItemStack(EsMaterial material, int amount) { return new FoliaItemStack(material, amount); } - @SuppressWarnings("deprecation") @Override public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amount) { - if (Main.minecraftVersion.getMinor() >= 9) { - String type = potType == EsPotType.drink ? - "POTION" : - potType.toString().toUpperCase() + "_POTION"; - ItemStack pot = new ItemStack(Material.valueOf(type), amount); - - PotionMeta meta = (PotionMeta) pot.getItemMeta(); - assert meta != null; - meta.addCustomEffect(BukkitHelper.toBukkitPotionEffect(effect), true); - pot.setItemMeta(meta); - - return new BukkitPotion(pot); - } else if (Main.minecraftVersion.getMinor() >= 4) { - PotionType type = BukkitEffectHelper.getPotionFromEffectType(effect.getType()); - if (type == null) { // This can fail if the effect doesn't have a potion for it - return null; - } + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; + ItemStack pot = new ItemStack(Material.valueOf(type), amount); - Potion potion = new Potion(type, effect.getAmp()); - potion.setSplash(potType == EsPotType.splash); + PotionMeta meta = (PotionMeta) pot.getItemMeta(); + assert meta != null; + meta.addCustomEffect(FoliaHelper.toBukkitPotionEffect(effect), true); + pot.setItemMeta(meta); - return new BukkitPotion(potion.toItemStack(amount)); - } else { // This isn't possible to get to because this class won't load on 1.3 and below - return null; - } + return new FoliaPotion(pot); } - @SuppressWarnings("deprecation") @Override public EsPotion createPotion(EsPotType potType) { - if (Main.minecraftVersion.getMinor() >= 9) { - String type = potType == EsPotType.drink ? - "POTION" : - potType.toString().toUpperCase() + "_POTION"; + String type = potType == EsPotType.drink ? + "POTION" : + potType.toString().toUpperCase() + "_POTION"; - ItemStack pot = new ItemStack(Material.valueOf(type), 1); + ItemStack pot = new ItemStack(Material.valueOf(type), 1); - return new BukkitPotion(pot); - } else if (Main.minecraftVersion.getMinor() >= 4) { - Potion potion = Potion.fromItemStack(new ItemStack(Material.valueOf("POTION"))); - potion.setSplash(potType == EsPotType.splash); - - return new BukkitPotion(potion.toItemStack(1)); - } else { // This isn't possible to get to because this class won't load on 1.3 and below - return null; - } + return new FoliaPotion(pot); } @Override From fb04e4294ff6379d56e2e21a2c064db57d372ed5 Mon Sep 17 00:00:00 2001 From: Calcilore Date: Sun, 23 Jun 2024 17:28:53 +1000 Subject: [PATCH 74/74] Added old version support for Signs and changed SemanticVersion .getString() to .toString() --- .../net/serble/estools/Commands/EsTools.java | 8 +++--- src/main/java/net/serble/estools/Main.java | 2 +- .../net/serble/estools/SemanticVersion.java | 3 +- .../net/serble/estools/Signs/SignMain.java | 28 +++++++++++-------- src/main/java/net/serble/estools/Updater.java | 4 +-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/serble/estools/Commands/EsTools.java b/src/main/java/net/serble/estools/Commands/EsTools.java index d9c1ef8..e6449b8 100644 --- a/src/main/java/net/serble/estools/Commands/EsTools.java +++ b/src/main/java/net/serble/estools/Commands/EsTools.java @@ -110,7 +110,7 @@ public boolean execute(EsCommandSender sender, String[] args) { return false; } - send(sender, "&aDownloading version: &6" + Main.newVersion.getString()); + send(sender, "&aDownloading version: &6" + Main.newVersion); Updater.downloadNewUpdate(sender); } else if (args[0].equalsIgnoreCase("forceupdate")) { send(sender, "&aForce updating &c(This might downgrade your plugin)"); @@ -142,10 +142,10 @@ public List tabComplete(EsCommandSender sender, String[] args, String lA private void sendVersion(EsCommandSender sender) { if (Main.newVersion == null) { - send(sender, "&aEsTools v" + Main.server.getPluginVersion().getString()); + send(sender, "&aEsTools v" + Main.server.getPluginVersion().toString()); } else { - send(sender, "&aEsTools &cv" + Main.server.getPluginVersion().getString() + ".&c An update is available, use " + - "&6/estools update&c to update to &6v" + Main.newVersion.getString()); + send(sender, "&aEsTools &cv" + Main.server.getPluginVersion().toString() + ".&c An update is available, use " + + "&6/estools update&c to update to &6v" + Main.newVersion.toString()); } } diff --git a/src/main/java/net/serble/estools/Main.java b/src/main/java/net/serble/estools/Main.java index cfb9306..78c44e7 100644 --- a/src/main/java/net/serble/estools/Main.java +++ b/src/main/java/net/serble/estools/Main.java @@ -59,7 +59,7 @@ public void enable() { minecraftVersion = server.getVersion(); server.initialise(); - logger.info("Starting EsTools on platform: " + platform.name() + " (MC: " + minecraftVersion.getString() + ")"); + logger.info("Starting EsTools on platform: " + platform.name() + " (MC: " + minecraftVersion.toString() + ")"); if (platform == ServerPlatform.Bukkit) { // We have to support old configs diff --git a/src/main/java/net/serble/estools/SemanticVersion.java b/src/main/java/net/serble/estools/SemanticVersion.java index 8370f6c..ff86a76 100644 --- a/src/main/java/net/serble/estools/SemanticVersion.java +++ b/src/main/java/net/serble/estools/SemanticVersion.java @@ -34,7 +34,8 @@ public int getPatch() { return patch; } - public String getString() { + @Override + public String toString() { return string; } diff --git a/src/main/java/net/serble/estools/Signs/SignMain.java b/src/main/java/net/serble/estools/Signs/SignMain.java index f4e90d6..a261aa9 100644 --- a/src/main/java/net/serble/estools/Signs/SignMain.java +++ b/src/main/java/net/serble/estools/Signs/SignMain.java @@ -2,6 +2,7 @@ import net.serble.estools.EsToolsCommand; import net.serble.estools.Main; +import net.serble.estools.SemanticVersion; import net.serble.estools.ServerApi.EsAction; import net.serble.estools.ServerApi.EsEventListener; import net.serble.estools.ServerApi.EsEventResult; @@ -9,25 +10,26 @@ import net.serble.estools.ServerApi.Events.EsSignChangeEvent; import net.serble.estools.ServerApi.Interfaces.EsEvent; import net.serble.estools.ServerApi.Interfaces.EsSign; +import net.serble.estools.Tuple; import java.util.HashMap; public class SignMain implements EsEventListener { private static final HashMap signs = new HashMap<>(); - private static final HashMap signConversions = new HashMap<>(); + private static final HashMap> signConversions = new HashMap<>(); public static void init() { - addSign(new Disposal(), "[disposal]", EsToolsCommand.translate("&1[Disposal]")); - addSign(new Give(), "[give]", EsToolsCommand.translate("&1[Give]")); - addSign(new Heal(), "[heal]", EsToolsCommand.translate("&1[Heal]")); - addSign(new Feed(), "[feed]", EsToolsCommand.translate("&1[Feed]")); - addSign(new Repair(), "[repair]", EsToolsCommand.translate("&1[Repair]")); + addSign(new Disposal(), "[disposal]", EsToolsCommand.translate("&1[Disposal]"), new SemanticVersion(1, 2, 0)); + addSign(new Give(), "[give]", EsToolsCommand.translate("&1[Give]"), new SemanticVersion(1, 1, 0)); + addSign(new Heal(), "[heal]", EsToolsCommand.translate("&1[Heal]"), new SemanticVersion(1, 1, 0)); + addSign(new Feed(), "[feed]", EsToolsCommand.translate("&1[Feed]"), new SemanticVersion(1, 1, 0)); + addSign(new Repair(), "[repair]", EsToolsCommand.translate("&1[Repair]"), new SemanticVersion(1, 1, 0)); Main.registerEvents(new SignMain()); } - public static void addSign(SignType signType, String conversion, String sign) { - signConversions.put(conversion, sign); + public static void addSign(SignType signType, String conversion, String sign, SemanticVersion minVersion) { + signConversions.put(conversion, new Tuple<>(sign, minVersion)); signs.put(sign, signType); } @@ -49,10 +51,14 @@ public void changeSign(EsSignChangeEvent e) { return; } - String str = signConversions.get(e.getLine(0).toLowerCase()); + Tuple conversion = signConversions.get(e.getLine(0).toLowerCase()); - if (str != null) { - e.setLine(0, str); + if (conversion != null) { + if (Main.minecraftVersion.isAtLeast(conversion.b())) { + e.setLine(0, conversion.a()); + } else { + EsToolsCommand.send(e.getPlayer(), "&cThis sign is requires minecraft version %s or higher", conversion.b().toString()); + } } } diff --git a/src/main/java/net/serble/estools/Updater.java b/src/main/java/net/serble/estools/Updater.java index b092afc..c5f5af6 100644 --- a/src/main/java/net/serble/estools/Updater.java +++ b/src/main/java/net/serble/estools/Updater.java @@ -58,10 +58,10 @@ private static void checkForUpdateBlocking() { // Announcements if (Main.plugin.getConfig().getUpdater().isWarnOnOutdated()) { - Main.server.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString()), "estools.update"); + Main.server.broadcast(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion + " -> " + onlineVersion), "estools.update"); } if (Main.plugin.getConfig().getUpdater().isLogOnOutdated()) { - Main.logger.info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion.getString() + " -> " + onlineVersion.getString())); + Main.logger.info(EsToolsCommand.translate("&a[EsTools] &cAn update is available, &6" + currentVersion + " -> " + onlineVersion)); } if (!Main.plugin.getConfig().getUpdater().isAutoUpdate()) {