From 292b7b1b774c208c27bcfa8827f70d2d90dbc7b4 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 4 Mar 2024 20:56:31 +0100 Subject: [PATCH] Add block stopper to PreventStabby configuration and listeners A block stopper feature has been added to the PreventStabby plugin. This prevents the placement of dangerous blocks near players with PvP turned off. The functionality has been integrated into the ConfigCache configuration class and EnvironmentalListener. This fine-tunes environmental controls in PvP gameplay scenarios, improving the player's safety and the overall game experience. --- .../preventstabby/config/ConfigCache.java | 22 ++++++++++-- .../listeners/EnvironmentalListener.java | 35 ++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java index 6820bf7..16b537b 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java +++ b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java @@ -14,8 +14,14 @@ import java.util.Set; public class ConfigCache { - public final boolean pvp_enabled_by_default, bucket_stopper_enabled, fire_stopper_enabled, punish_for_combat_logout, - punish_for_combat_logout_announce, block_teleports_in_combat, allow_fishing_rod_pull; + public final boolean pvp_enabled_by_default, + bucket_stopper_enabled, + fire_stopper_enabled, + block_stopper_enabled, + punish_for_combat_logout, + punish_for_combat_logout_announce, + block_teleports_in_combat, + allow_fishing_rod_pull; public final String pvp_enabled, pvp_disabled, cannot_attack_victim, cannot_attack_attacker, cannot_attack_pets_victim, cannot_attack_pets_attacker, no_permission, no_such_command, pvp_enabled_other, pvp_disabled_other, punish_for_combat_logout_message, entering_combat, leaving_combat, @@ -28,7 +34,7 @@ public class ConfigCache { cannot_attack_teleport_or_spawn_protection_victim; public final double combat_time, login_protection_time, teleport_protection_time, bucket_stopper_radius, - fire_stopper_radius; + fire_stopper_radius, block_stopper_radius; private final Set combatBlockedCommands = new HashSet<>(); private final FileConfiguration config; @@ -109,6 +115,16 @@ public class ConfigCache { 2.5, List.of("Distance from the player where igniting blocks will be disallowed") ); + this.block_stopper_enabled = getBoolean( + "settings.environmental.block_stopper.enabled", + true, + List.of("Should plugin block placing dangerous blocks near players with pvp off?") + ); + this.block_stopper_radius = getDouble( + "settings.environmental.block_stopper.radius", + 2.5, + List.of("Distance from the player where placing dangerous blocks will be disallowed") + ); // Messages this.pvp_enabled = getString("messages.pvp_enabled", "You enabled PvP!"); diff --git a/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java b/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java index 3d25af5..8e47fba 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java +++ b/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java @@ -12,6 +12,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockIgniteEvent; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.util.BoundingBox; @@ -41,14 +42,15 @@ public class EnvironmentalListener implements Listener { BoundingBox boundingBox = BoundingBox.of(location.toVector(), radius, radius, radius); for (Entity victim : location.getWorld().getNearbyEntities(boundingBox)) { - if (victim == placer) continue; - DamageCheckResult result = plugin.getPlayerManager().canDamage(placer, victim); + if (victim.getUniqueId() == placer.getUniqueId()) continue; + Target victimTarget = Target.getTarget(victim); + if (victimTarget == null) continue; + DamageCheckResult result = plugin.getPlayerManager().canDamage(placer.getUniqueId(), victim.getUniqueId(), victimTarget.classifier); if (!result.ableToDamage()) { event.setCancelled(true); break; } } - } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -65,7 +67,32 @@ public class EnvironmentalListener implements Listener { BoundingBox boundingBox = BoundingBox.of(location.toVector(), radius, radius, radius); for (Entity victim : location.getWorld().getNearbyEntities(boundingBox)) { if (victim.getUniqueId() == igniterTarget.playerUuid) continue; - DamageCheckResult result = plugin.getPlayerManager().canDamage(igniterTarget.playerUuid, victim.getUniqueId(), igniterTarget.classifier); + Target victimTarget = Target.getTarget(victim); + if (victimTarget == null) continue; + DamageCheckResult result = plugin.getPlayerManager().canDamage(igniterTarget.playerUuid, victim.getUniqueId(), victimTarget.classifier); + if (!result.ableToDamage()) { + event.setCancelled(true); + break; + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onBlockIgnite(BlockPlaceEvent event) { + ConfigCache config = plugin.getConfigCache(); + if (!config.block_stopper_enabled) return; + Player player = event.getPlayer(); + Location location = event.getBlock().getLocation().toCenterLocation(); + Target target = Target.getTarget(player); + if (target == null) return; + double radius = config.block_stopper_radius; + + BoundingBox boundingBox = BoundingBox.of(location.toVector(), radius, radius, radius); + for (Entity victim : location.getWorld().getNearbyEntities(boundingBox)) { + if (victim.getUniqueId() == target.playerUuid) continue; + Target victimTarget = Target.getTarget(victim); + if (victimTarget == null) continue; + DamageCheckResult result = plugin.getPlayerManager().canDamage(target.playerUuid, victim.getUniqueId(), victimTarget.classifier); if (!result.ableToDamage()) { event.setCancelled(true); break;