From 53a8a95e36dcd1d965fcde089c16c515c26c4c3a Mon Sep 17 00:00:00 2001 From: xGinko Date: Tue, 13 Feb 2024 12:56:51 +0100 Subject: [PATCH] improvements to godmode patch --- .../PreventGoingBelowBedrockFloor.java | 22 +++---- .../modules/patches/GodMode.java | 60 +++++++++++++++++-- .../PreventGoingBelowBedrockFloor.java | 23 +++---- .../modules/patches/GodMode.java | 55 ++++++++++++++--- 4 files changed, 125 insertions(+), 35 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java index 1f967f5e2..8b4c70221 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java @@ -23,19 +23,21 @@ public class PreventGoingBelowBedrockFloor implements AnarchyExploitFixesModule, private final Set exemptedWorlds; private final Material fillMaterial; - private final boolean teleport_enabled; - private final boolean filling_enabled; - private final boolean eject_enabled; - private final boolean stop_elytra_enabled; + private final boolean teleport_enabled, filling_enabled, eject_enabled, stop_elytra_enabled; public PreventGoingBelowBedrockFloor() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("bedrock.prevent-going-below-bedrock-floor.enable", "Prevents the player from being hit by a bedrock"); - this.teleport_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.teleport", true, "Teleports the player above the bedrock"); - this.eject_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.eject-player", true, "Eject player from the vehicle"); - this.stop_elytra_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.stop-elytra", true, "Disables a player's elytra flight"); - this.filling_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.fill-bedrock-hole", true, "Indicates whether the hole should be filled from bedorck"); + config.addComment("bedrock.prevent-going-below-bedrock-floor.enable", + "Prevents players from going below bedrock the bedrock floor."); + this.teleport_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.teleport", true, + "Teleports the player above the bedrock"); + this.eject_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.eject-player", true, + "Eject player from the vehicle"); + this.stop_elytra_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.stop-elytra", true, + "Disables a player's elytra flight"); + this.filling_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.fill-bedrock-hole", true, + "Whether the bedrock hole should be filled or not"); this.exemptedWorlds = new HashSet<>(config.getList("bedrock.prevent-going-below-bedrock-floor.exempted-worlds", List.of("world_the_end", "skyblock_world"))); String configuredFillMaterial = config.getString("bedrock.prevent-going-below-bedrock-floor.filler-material", "BEDROCK"); Material filler_material = Material.BEDROCK; @@ -85,7 +87,7 @@ private void onPlayerMove(PlayerMoveEvent event) { // place bedrock at the min world height if (filling_enabled) world.getBlockAt(playerLoc.getBlockX(), world.getMinHeight(), playerLoc.getBlockZ()).setType(fillMaterial); // teleport player on top of that bedrock - if (teleport_enabled) event.setTo(event.getFrom().add(0, 2, 0)); + if (teleport_enabled) event.setTo(event.getFrom().clone().add(0, 2, 0)); if (eject_enabled && player.isInsideVehicle()) player.leaveVehicle(); if (stop_elytra_enabled && player.isGliding()) player.setGliding(false); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java index a1164ec4f..349a7941e 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java @@ -3,19 +3,25 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import me.moomoo.anarchyexploitfixes.utils.LogUtil; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.vehicle.VehicleEnterEvent; import java.util.logging.Level; public class GodMode implements AnarchyExploitFixesModule, Listener { + private final AnarchyExploitFixes plugin; + public GodMode() { shouldEnable(); + this.plugin = AnarchyExploitFixes.getInstance(); AnarchyExploitFixes.getConfiguration().addComment("patches.experimental-godmode-patch", "Removes entities or players if they are \"invalid\". Not sure if this works."); } @@ -32,7 +38,6 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -46,16 +51,59 @@ public void disable() { HandlerList.unregisterAll(this); } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onVehicleEnter(VehicleEnterEvent event) { + final Vehicle vehicle = event.getVehicle(); + if (!vehicle.isTicking() || vehicle.isDead() || !vehicle.isValid()) { + event.setCancelled(true); + } + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) private void onPlayerMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); - if (player.isInsideVehicle() && !player.getVehicle().isValid()) { - player.getVehicle().eject(); - LogUtil.moduleLog(Level.WARNING, name(),"Attempted to prevent godmode on player " + player.getName() + ": Dismounted from entity."); + final Player player = event.getPlayer(); + + if (player.isInsideVehicle()) { + final Entity vehicle = player.getVehicle(); + + if (vehicle == null) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding a null entity. " + + "Dismounting player from entity."); + player.leaveVehicle(); + return; + } + + if (!vehicle.isValid()) { + vehicle.getScheduler().run(plugin, unGod -> { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding an invalid entity. " + + "Ejecting all passengers."); + vehicle.eject(); + }, null); + return; + } + + if (vehicle.isDead()) { + vehicle.getScheduler().run(plugin, unGod -> { + LogUtil.moduleLog(Level.WARNING, name(), "Player '" + player.getName() + "' is riding a dead entity. " + + "Ejecting all passengers."); + vehicle.eject(); + }, null); + return; + } + + if (!vehicle.isTicking()) { + vehicle.getScheduler().run(plugin, unGod -> { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding an entity from a non-ticking chunk. " + + "Ejecting all passengers."); + vehicle.eject(); + }, null); + return; + } } + if (!player.isValid() && !player.isDead()) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is neither valid nor dead. Kicking player."); player.kick(AnarchyExploitFixes.getLang(player.locale()).misc_MaskedKickMessage); - LogUtil.moduleLog(Level.WARNING, name(),"Attempted to prevent godmode on player " + player.getName() + ": Kicked player."); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java index 856152473..fb7aed8af 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PreventGoingBelowBedrockFloor.java @@ -1,6 +1,5 @@ package me.moomoo.anarchyexploitfixes.modules.bedrock; -import com.cryptomorin.xseries.XMaterial; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; @@ -24,19 +23,21 @@ public class PreventGoingBelowBedrockFloor implements AnarchyExploitFixesModule, private final Set exemptedWorlds; private final Material fillMaterial; - private final boolean teleport_enabled; - private final boolean filling_enabled; - private final boolean eject_enabled; - private final boolean stop_elytra_enabled; + private final boolean teleport_enabled, filling_enabled, eject_enabled, stop_elytra_enabled; public PreventGoingBelowBedrockFloor() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("bedrock.prevent-going-below-bedrock-floor.enable", "Fills the bedrock hole and teleports player above."); - this.teleport_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.teleport", true, "Teleports the player above the bedrock"); - this.eject_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.eject-player", true, "Eject player from the vehicle"); - this.stop_elytra_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.stop-elytra", true, "Disables a player's elytra flight"); - this.filling_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.fill-bedrock-hole", true, "Indicates whether the hole should be filled from bedorck"); + config.addComment("bedrock.prevent-going-below-bedrock-floor.enable", + "Prevents players from going below bedrock the bedrock floor."); + this.teleport_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.teleport", true, + "Teleports the player above the bedrock"); + this.eject_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.eject-player", true, + "Eject player from the vehicle"); + this.stop_elytra_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.stop-elytra", true, + "Disables a player's elytra flight"); + this.filling_enabled = config.getBoolean("bedrock.prevent-going-below-bedrock-floor.fill-bedrock-hole", true, + "Whether the bedrock hole should be filled or not"); this.exemptedWorlds = new HashSet<>(config.getList("bedrock.prevent-going-below-bedrock-floor.exempted-worlds", Arrays.asList("world_the_end", "skyblock_world"))); String configuredFillMaterial = config.getString("bedrock.prevent-going-below-bedrock-floor.filler-material", "BEDROCK"); Material filler_material = Material.BEDROCK; @@ -81,7 +82,7 @@ private void onPlayerMove(PlayerMoveEvent event) { // place bedrock at the min world height if (filling_enabled) world.getBlockAt(playerLoc.getBlockX(), ChunkUtil.guessMinWorldHeight(world), playerLoc.getBlockZ()).setType(fillMaterial); // teleport player on top of that bedrock - if (teleport_enabled) event.setTo(event.getFrom().add(0, 2, 0)); + if (teleport_enabled) event.setTo(event.getFrom().clone().add(0, 2, 0)); if (eject_enabled && player.isInsideVehicle()) player.leaveVehicle(); if (stop_elytra_enabled && player.isGliding()) player.setGliding(false); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java index 4e704a8d4..87101a755 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/GodMode.java @@ -3,12 +3,14 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import org.bukkit.ChatColor; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.Vehicle; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.vehicle.VehicleEnterEvent; import java.util.logging.Level; @@ -41,16 +43,53 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("patches.experimental-godmode-patch", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onVehicleEnter(VehicleEnterEvent event) { + final Vehicle vehicle = event.getVehicle(); + if (!vehicle.getChunk().isLoaded() || vehicle.isDead() || !vehicle.isValid()) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) private void onPlayerMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); - if (player.isInsideVehicle() && !player.getVehicle().isValid()) { - player.getVehicle().eject(); - LogUtil.moduleLog(Level.WARNING, name(),"Attempted to prevent godmode on player " + player.getName() + ": Dismounted from entity."); + final Player player = event.getPlayer(); + + if (player.isInsideVehicle()) { + final Entity vehicle = player.getVehicle(); + + if (vehicle == null) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding a null entity. " + + "Dismounting player from entity."); + player.leaveVehicle(); + return; + } + + if (!vehicle.isValid()) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding an invalid entity. " + + "Ejecting all passengers."); + vehicle.eject(); + return; + } + + if (vehicle.isDead()) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding a dead entity. " + + "Ejecting all passengers."); + vehicle.eject(); + return; + } + + if (!vehicle.getChunk().isLoaded()) { + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is riding an entity from a non-loaded chunk. " + + "Ejecting all passengers."); + vehicle.eject(); + return; + } } + if (!player.isValid() && !player.isDead()) { - player.kickPlayer(ChatColor.translateAlternateColorCodes('&', AnarchyExploitFixes.getLang(player.getLocale()).misc_MaskedKickMessage)); - LogUtil.moduleLog(Level.WARNING, name(),"Attempted to prevent godmode on player " + player.getName() + ": Kicked player."); + LogUtil.moduleLog(Level.WARNING, name(),"Player '" + player.getName() + "' is neither valid nor dead. Kicking player."); + player.kickPlayer(AnarchyExploitFixes.getLang(player.getLocale()).misc_MaskedKickMessage); } } }