From 87d7ba19de387dad79f726681e769c9cde392e0b Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 17 Jun 2023 20:06:56 +0100 Subject: [PATCH 1/5] Remove inventory checks in MovementValidator --- .../totalfreedommod/MovementValidator.java | 111 +----------------- 1 file changed, 1 insertion(+), 110 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java index e2b621af..10b82409 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java @@ -1,25 +1,16 @@ package me.totalfreedom.totalfreedommod; -import com.google.common.collect.Multimap; import io.papermc.lib.PaperLib; -import java.util.Collection; -import java.util.Map; import java.util.Objects; import org.bukkit.ChatColor; import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeModifier; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; -import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; public class MovementValidator extends FreedomService { @@ -53,7 +44,7 @@ public class MovementValidator extends FreedomService final Player player = event.getPlayer(); Location from = event.getFrom(); Location to = event.getTo(); - assert to != null; + if (to.getX() >= from.getX() + MAX_DISTANCE_TRAVELED || to.getY() >= from.getY() + MAX_DISTANCE_TRAVELED || to.getZ() >= from.getZ() + MAX_DISTANCE_TRAVELED) { event.setCancelled(true); @@ -65,43 +56,6 @@ public class MovementValidator extends FreedomService event.setCancelled(true); PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); } - - if (exploitItem(event.getPlayer().getInventory().getHelmet())) - { - event.getPlayer().getInventory().setHelmet(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your helmet slot."); - event.setCancelled(true); - } - if (exploitItem(event.getPlayer().getInventory().getBoots())) - { - event.getPlayer().getInventory().setBoots(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your boots slot."); - event.setCancelled(true); - } - if (exploitItem(event.getPlayer().getInventory().getLeggings())) - { - event.getPlayer().getInventory().setLeggings(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your leggings slot."); - event.setCancelled(true); - } - if (exploitItem(event.getPlayer().getInventory().getChestplate())) - { - event.getPlayer().getInventory().setChestplate(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your chestplate slot."); - event.setCancelled(true); - } - if (exploitItem(event.getPlayer().getInventory().getItemInMainHand())) - { - event.getPlayer().getInventory().setItemInMainHand(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your hand."); - event.setCancelled(true); - } - if (exploitItem(event.getPlayer().getInventory().getItemInOffHand())) - { - event.getPlayer().getInventory().setItemInOffHand(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your offhand."); - event.setCancelled(true); - } } @EventHandler(priority = EventPriority.HIGH) @@ -115,67 +69,4 @@ public class MovementValidator extends FreedomService PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn } } - - @EventHandler - public void onPlayerHoldItem(PlayerItemHeldEvent event) - { - if (exploitItem(event.getPlayer().getInventory().getItemInMainHand())) - { - event.getPlayer().getInventory().setItemInMainHand(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your hand."); - } - if (exploitItem(event.getPlayer().getInventory().getItemInOffHand())) - { - event.getPlayer().getInventory().setItemInOffHand(new ItemStack(Material.AIR)); - event.getPlayer().sendMessage(ChatColor.RED + "An item with both negative infinity and positive infinity attributes was cleared from your offhand."); - } - } - - private Boolean exploitItem(ItemStack item) - { - if (item == null) - { - return false; - } - - ItemMeta meta = item.getItemMeta(); - if (meta != null) - { - Multimap attributes = meta.getAttributeModifiers(); - if (attributes != null) - { - Map> attrMap = attributes.asMap(); - - // For every attribute... - for (Attribute attr : attributes.keySet()) - { - // Default values - boolean posInf = false; - boolean negInf = false; - - // For every AttributeModifier... - for (AttributeModifier modifier : attrMap.get(attr)) - { - // Are they ∞ or -∞? - if (modifier.getAmount() == Double.POSITIVE_INFINITY) - { - posInf = true; - } - else if (modifier.getAmount() == Double.NEGATIVE_INFINITY) - { - negInf = true; - } - } - - // Are both values set as true? - if (posInf && negInf) - { - return true; - } - } - } - - } - return false; - } } \ No newline at end of file From 42b68011eafaab38bfb0c75800df4ee1558074d9 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 17 Jun 2023 20:09:22 +0100 Subject: [PATCH 2/5] Improve distance check --- .../me/totalfreedom/totalfreedommod/MovementValidator.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java index 10b82409..30520467 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java @@ -44,8 +44,9 @@ public class MovementValidator extends FreedomService final Player player = event.getPlayer(); Location from = event.getFrom(); Location to = event.getTo(); + double distance = from.distanceSquared(to); - if (to.getX() >= from.getX() + MAX_DISTANCE_TRAVELED || to.getY() >= from.getY() + MAX_DISTANCE_TRAVELED || to.getZ() >= from.getZ() + MAX_DISTANCE_TRAVELED) + if (distance >= MAX_DISTANCE_TRAVELED) { event.setCancelled(true); player.kickPlayer(ChatColor.RED + "You were moving too quickly!"); From cca95dc3f1a45b525a616cfce494563c70513235 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 17 Jun 2023 20:14:02 +0100 Subject: [PATCH 3/5] Add dedicated method for checking if a position is out of bounds --- .../totalfreedommod/MovementValidator.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java index 30520467..1dcbbf61 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java @@ -2,7 +2,6 @@ package me.totalfreedom.totalfreedommod; import io.papermc.lib.PaperLib; -import java.util.Objects; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -32,12 +31,17 @@ public class MovementValidator extends FreedomService public void onPlayerTeleport(PlayerTeleportEvent event) { // Check absolute value to account for negatives - if (Math.abs(Objects.requireNonNull(event.getTo()).getX()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getZ()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getY()) >= MAX_XYZ_COORD) + if (isOutOfBounds(event.getTo())) { event.setCancelled(true); // illegal position, cancel it } } + private boolean isOutOfBounds(final Location position) + { + return Math.abs(position.getX()) >= MAX_XYZ_COORD || Math.abs(position.getY()) >= MAX_XYZ_COORD || Math.abs(position.getZ()) >= MAX_XYZ_COORD; + } + @EventHandler(priority = EventPriority.HIGH) public void onPlayerMove(PlayerMoveEvent event) { @@ -52,7 +56,7 @@ public class MovementValidator extends FreedomService player.kickPlayer(ChatColor.RED + "You were moving too quickly!"); } // Check absolute value to account for negatives - if (Math.abs(event.getTo().getX()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getZ()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getY()) >= MAX_XYZ_COORD) + if (isOutOfBounds(event.getTo())) { event.setCancelled(true); PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); @@ -65,7 +69,7 @@ public class MovementValidator extends FreedomService final Player player = event.getPlayer(); // Validate position - if (Math.abs(player.getLocation().getX()) >= MAX_XYZ_COORD || Math.abs(player.getLocation().getZ()) >= MAX_XYZ_COORD || Math.abs(player.getLocation().getY()) >= MAX_XYZ_COORD) + if (isOutOfBounds(player.getLocation())) { PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn } From d30e335f577a713f6a5d976a397ac4fdd9f0c30e Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 17 Jun 2023 20:17:18 +0100 Subject: [PATCH 4/5] Disallow custom spawns that are out of bounds --- .../totalfreedommod/MovementValidator.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java index 1dcbbf61..7b70158b 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java @@ -10,6 +10,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.spigotmc.event.player.PlayerSpawnLocationEvent; public class MovementValidator extends FreedomService { @@ -74,4 +75,23 @@ public class MovementValidator extends FreedomService PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn } } + + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerSpawn(PlayerSpawnLocationEvent event) + { + final Location playerSpawn = event.getSpawnLocation(); + final Location worldSpawn = event.getPlayer().getWorld().getSpawnLocation(); + + // If the player's spawn is equal to the world's spawn, there is no need to check. + // This will also prevent any possible feedback loops pertaining to setting an out of bounds world spawn to the same world spawn. + if (playerSpawn == worldSpawn) + { + return; + } + + if (isOutOfBounds(worldSpawn)) + { + event.setSpawnLocation(worldSpawn); + } + } } \ No newline at end of file From dcebf7bbe7e99f852910cb114c126344a05e1315 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 17 Jun 2023 20:22:23 +0100 Subject: [PATCH 5/5] Use Adventure for kick message --- .../me/totalfreedom/totalfreedommod/MovementValidator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java index 7b70158b..764e1515 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/MovementValidator.java @@ -2,7 +2,8 @@ package me.totalfreedom.totalfreedommod; import io.papermc.lib.PaperLib; -import org.bukkit.ChatColor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -54,7 +55,7 @@ public class MovementValidator extends FreedomService if (distance >= MAX_DISTANCE_TRAVELED) { event.setCancelled(true); - player.kickPlayer(ChatColor.RED + "You were moving too quickly!"); + player.kick(Component.text("You were moving too quickly!", NamedTextColor.RED)); } // Check absolute value to account for negatives if (isOutOfBounds(event.getTo()))