From dc43ba4f0ca0498829a744db2d03502b379ff2c1 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 14 Feb 2024 20:51:58 +0100 Subject: [PATCH] Updated Via API usage --- build.gradle | 2 +- .../viarewind/legacysupport/BukkitPlugin.java | 57 +++++++++---------- .../injector/BoundingBoxFixer.java | 20 +++---- .../listener/AreaEffectCloudListener.java | 2 +- .../listener/BounceListener.java | 2 +- .../listener/BrewingListener.java | 5 +- .../listener/ElytraListener.java | 2 +- .../listener/EnchantingListener.java | 4 +- .../legacysupport/listener/SoundListener.java | 4 +- 9 files changed, 48 insertions(+), 50 deletions(-) diff --git a/build.gradle b/build.gradle index 463320a..94a913e 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ version = project.maven_version group = project.maven_group dependencies { - compileOnly "com.viaversion:viaversion-api:4.9.3-SNAPSHOT" + compileOnly "com.viaversion:viaversion-api:4.10.0-24w07a-SNAPSHOT" compileOnly "org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT" } diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/BukkitPlugin.java b/src/main/java/com/viaversion/viarewind/legacysupport/BukkitPlugin.java index 4378b8a..616f7e8 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/BukkitPlugin.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/BukkitPlugin.java @@ -49,40 +49,37 @@ public class BukkitPlugin extends JavaPlugin { @Override public void run() { - int serverProtocol = Via.getAPI().getServerVersion().lowestSupportedVersion(); - if (serverProtocol == -1) return; + final ProtocolVersion serverProtocol = Via.getAPI().getServerVersion().lowestSupportedProtocolVersion(); + if (!serverProtocol.isKnown()) return; cancel(); - if (serverProtocol >= ProtocolVersion.v1_8.getVersion() && config.getBoolean("enchanting-gui-fix")) { - Bukkit.getPluginManager().registerEvents(new EnchantingListener(), BukkitPlugin.this); + if (serverProtocol.newerThanOrEqualTo(ProtocolVersion.v1_8)) { + if (config.getBoolean("enchanting-gui-fix")) + Bukkit.getPluginManager().registerEvents(new EnchantingListener(), BukkitPlugin.this); + + if (config.getBoolean("slime-fix")) + Bukkit.getPluginManager().registerEvents(new BounceListener(), BukkitPlugin.this); } - if (serverProtocol >= ProtocolVersion.v1_8.getVersion() && config.getBoolean("slime-fix")) { - Bukkit.getPluginManager().registerEvents(new BounceListener(), BukkitPlugin.this); + if (serverProtocol.newerThanOrEqualTo(ProtocolVersion.v1_9)) { + if (config.getBoolean("sound-fix")) + Bukkit.getPluginManager().registerEvents(new SoundListener(BukkitPlugin.this), BukkitPlugin.this); + + if (config.getBoolean("ladder-fix")) // 15w31a + BoundingBoxFixer.fixLadder(getLogger(), serverProtocol); + + if (config.getBoolean("area-effect-cloud-particles")) // 15w32c + Bukkit.getPluginManager().registerEvents(new AreaEffectCloudListener(BukkitPlugin.this), BukkitPlugin.this); + + if (config.getBoolean("elytra-fix")) // 15w40b + Bukkit.getPluginManager().registerEvents(new ElytraListener(), BukkitPlugin.this); + + if (config.getBoolean("brewing-stand-gui-fix")) // 15w41b + Bukkit.getPluginManager().registerEvents(new BrewingListener(), BukkitPlugin.this); + + if (config.getBoolean("lily-pad-fix")) // 15w44b + BoundingBoxFixer.fixLilyPad(getLogger(), serverProtocol); } - if (serverProtocol >= ProtocolVersion.v1_9.getVersion() && config.getBoolean("sound-fix")) { - Bukkit.getPluginManager().registerEvents(new SoundListener(BukkitPlugin.this), BukkitPlugin.this); - } - // Added in 15w31a (1.9) - if (serverProtocol >= ProtocolVersion.v1_9.getVersion() && config.getBoolean("ladder-fix")) { - BoundingBoxFixer.fixLadder(getLogger(), serverProtocol); - } - // Added in 15w32c (1.9) - if (serverProtocol >= ProtocolVersion.v1_9.getVersion() && config.getBoolean("area-effect-cloud-particles")) { - Bukkit.getPluginManager().registerEvents(new AreaEffectCloudListener(BukkitPlugin.this), BukkitPlugin.this); - } - // Added in 15w40b (1.9) - if (serverProtocol > ProtocolVersion.v1_9.getVersion() && config.getBoolean("elytra-fix")) { - Bukkit.getPluginManager().registerEvents(new ElytraListener(), BukkitPlugin.this); - } - // Added in 15w41b (1.9) - if (serverProtocol >= ProtocolVersion.v1_9.getVersion() && config.getBoolean("brewing-stand-gui-fix")) { - Bukkit.getPluginManager().registerEvents(new BrewingListener(), BukkitPlugin.this); - } - // Added in 15w44b (1.9) - if (serverProtocol >= ProtocolVersion.v1_9.getVersion() && config.getBoolean("lily-pad-fix")) { - BoundingBoxFixer.fixLilyPad(getLogger(), serverProtocol); - } - if (serverProtocol >= ProtocolVersion.v1_14_4.getVersion() && config.getBoolean("carpet-fix")) { + if (serverProtocol.newerThanOrEqualTo(ProtocolVersion.v1_14_4) && config.getBoolean("carpet-fix")) { BoundingBoxFixer.fixCarpet(getLogger(), serverProtocol); } diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/injector/BoundingBoxFixer.java b/src/main/java/com/viaversion/viarewind/legacysupport/injector/BoundingBoxFixer.java index d26c285..8ca425f 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/injector/BoundingBoxFixer.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/injector/BoundingBoxFixer.java @@ -30,9 +30,9 @@ import java.util.logging.Logger; public class BoundingBoxFixer { - public static void fixLilyPad(final Logger logger, final int serverVersion) { + public static void fixLilyPad(final Logger logger, final ProtocolVersion serverVersion) { try { - final Field boundingBoxField = ReflectionAPI.getFieldAccessible(NMSReflection.getNMSBlock("BlockWaterLily"), serverVersion <= ProtocolVersion.v1_20_2.getVersion() ? "a" : "b"); + final Field boundingBoxField = ReflectionAPI.getFieldAccessible(NMSReflection.getNMSBlock("BlockWaterLily"), serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_20_2) ? "a" : "b"); setBoundingBox(boundingBoxField.get(null), 0.0625, 0.0, 0.0625, 0.9375, 0.015625, 0.9375); } catch (Exception ex) { @@ -40,23 +40,23 @@ public class BoundingBoxFixer { } } - public static void fixCarpet(final Logger logger, final int serverVersion) { + public static void fixCarpet(final Logger logger, final ProtocolVersion serverVersion) { try { - final Class blockCarpetClass = serverVersion <= ProtocolVersion.v1_16_4.getVersion() ? NMSReflection.getNMSBlock("BlockCarpet") : NMSReflection.getNMSBlock("CarpetBlock"); + final Class blockCarpetClass = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_16_4) ? NMSReflection.getNMSBlock("BlockCarpet") : NMSReflection.getNMSBlock("CarpetBlock"); - final Field boundingBoxField = ReflectionAPI.getFieldAccessible(blockCarpetClass, serverVersion <= ProtocolVersion.v1_20_2.getVersion() ? "a" : "b"); + final Field boundingBoxField = ReflectionAPI.getFieldAccessible(blockCarpetClass, serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_20_2) ? "a" : "b"); setBoundingBox(boundingBoxField.get(0), 0.0D, -0.0000001D, 0.0D, 1.0D, 0.0000001D, 1.0D); } catch (Exception ex) { logger.log(Level.SEVERE, "Could not fix carpet bounding box.", ex); } } - public static void fixLadder(final Logger logger, final int serverVersion) { + public static void fixLadder(final Logger logger, final ProtocolVersion serverVersion) { try { - final boolean pre1_12_2 = serverVersion <= ProtocolVersion.v1_12_2.getVersion(); - final boolean pre1_13_2 = serverVersion <= ProtocolVersion.v1_13_2.getVersion(); - final boolean pre1_16_4 = serverVersion <= ProtocolVersion.v1_16_4.getVersion(); - final boolean pre1_20_2 = serverVersion <= ProtocolVersion.v1_20_2.getVersion(); + final boolean pre1_12_2 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_12_2); + final boolean pre1_13_2 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_13_2); + final boolean pre1_16_4 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_16_4); + final boolean pre1_20_2 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_20_2); final Class blockLadderClass = NMSReflection.getNMSBlock("BlockLadder"); diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/AreaEffectCloudListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/AreaEffectCloudListener.java index 4a7ed72..1c6e492 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/AreaEffectCloudListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/AreaEffectCloudListener.java @@ -41,7 +41,7 @@ public class AreaEffectCloudListener implements Listener { public AreaEffectCloudListener(final BukkitPlugin plugin) { Bukkit.getScheduler().runTaskTimer(plugin, () -> { - final Set affectedPlayers = Bukkit.getOnlinePlayers().stream().filter(p -> Via.getAPI().getPlayerVersion(p) <= ProtocolVersion.v1_8.getVersion()).collect(Collectors.toSet()); + final Set affectedPlayers = Bukkit.getOnlinePlayers().stream().filter(p -> Via.getAPI().getPlayerProtocolVersion(p).newerThanOrEqualTo(ProtocolVersion.v1_8)).collect(Collectors.toSet()); effectClouds.removeIf(e -> !e.isValid()); effectClouds.forEach(cloud -> { final Location location = cloud.getLocation(); diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/BounceListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/BounceListener.java index 3ac9a02..4181118 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/BounceListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/BounceListener.java @@ -36,7 +36,7 @@ public class BounceListener implements Listener { if (e.getTo().getY() >= e.getFrom().getY()) return; // Only check upwards motion final Player player = e.getPlayer(); - if (Via.getAPI().getPlayerVersion(player) >= ProtocolVersion.v1_8.getVersion()) return; // Only apply for 1.7 and below players + if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_8)) return; // Only apply for 1.7 and below players if (Math.floor(e.getTo().getY()) + 0.01 < e.getTo().getY()) return; if (player.isSneaking()) return; diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/BrewingListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/BrewingListener.java index 78b53c3..e6012ef 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/BrewingListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/BrewingListener.java @@ -19,6 +19,7 @@ package com.viaversion.viarewind.legacysupport.listener; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import org.bukkit.Material; import org.bukkit.block.BrewingStand; import org.bukkit.entity.Player; @@ -37,8 +38,8 @@ public class BrewingListener implements Listener { public void onPlayerInteract(PlayerInteractEvent e) { if (!e.hasBlock() || e.getClickedBlock().getType() != Material.BREWING_STAND) return; Player player = e.getPlayer(); - int version = Via.getAPI().getPlayerVersion(player); - if (version > 79) return; + ProtocolVersion version = Via.getAPI().getPlayerProtocolVersion(player); + if (version.newerThan(ProtocolVersion.v1_9)) return; ItemStack blazePowder = new ItemStack(Material.BLAZE_POWDER); ItemStack playerItem = e.getItem(); if (playerItem == null) playerItem = new ItemStack(Material.AIR); diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/ElytraListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/ElytraListener.java index 9b9366b..38b3285 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/ElytraListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/ElytraListener.java @@ -34,7 +34,7 @@ public class ElytraListener implements Listener { public void onPlayerMove(PlayerMoveEvent e) { final Player player = e.getPlayer(); - if (Via.getAPI().getPlayerVersion(player) >= ProtocolVersion.v1_9.getVersion()) return; // Only apply for 1.8 and below players + if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_9)) return; // Only apply for 1.8 and below players if (!player.isGliding()) return; // Only apply if the player is gliding final Vector direction = player.getLocation().getDirection(); diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/EnchantingListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/EnchantingListener.java index 29788eb..97dde4a 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/EnchantingListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/EnchantingListener.java @@ -54,7 +54,7 @@ public class EnchantingListener implements Listener { if (!(e.getInventory() instanceof EnchantingInventory)) return; final Player player = (Player) e.getPlayer(); - if (Via.getAPI().getPlayerVersion(player) >= ProtocolVersion.v1_8.getVersion()) return; + if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_8)) return; final PlayerInventory inv = player.getInventory(); final ItemStack lapis = newMaterialNames ? new ItemStack(lapisMaterial) : new ItemStack(lapisMaterial, 1, (short) 4); @@ -89,7 +89,7 @@ public class EnchantingListener implements Listener { if (!(e.getInventory() instanceof EnchantingInventory)) return; final Player player = (Player) e.getPlayer(); - if (Via.getAPI().getPlayerVersion(player) >= ProtocolVersion.v1_8.getVersion()) return; + if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_8)) return; final PlayerInventory inv = player.getInventory(); final EnchantingInventory replacement = (EnchantingInventory) e.getInventory(); diff --git a/src/main/java/com/viaversion/viarewind/legacysupport/listener/SoundListener.java b/src/main/java/com/viaversion/viarewind/legacysupport/listener/SoundListener.java index ad4298c..c6639ee 100644 --- a/src/main/java/com/viaversion/viarewind/legacysupport/listener/SoundListener.java +++ b/src/main/java/com/viaversion/viarewind/legacysupport/listener/SoundListener.java @@ -83,9 +83,9 @@ public class SoundListener implements Listener { @EventHandler public void onBlockPlace(BlockPlaceEvent e) { final Player player = e.getPlayer(); - if (Via.getAPI().getPlayerVersion(player) >= ProtocolVersion.v1_9.getVersion()) return; + if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_9)) return; - if (Via.getAPI().getServerVersion().lowestSupportedVersion() >= ProtocolVersion.v1_17.getVersion()) { + if (Via.getAPI().getServerVersion().lowestSupportedProtocolVersion().newerThanOrEqualTo(ProtocolVersion.v1_17)) { player.playSound(e.getBlockPlaced().getLocation(), e.getBlock().getBlockData().getSoundGroup().getPlaceSound(), 1.0f, 0.8f); } else { playBlockPlaceSoundNMS(player, e.getBlock());