Skip to content

Commit

Permalink
reiterate over combat modules
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 8, 2024
1 parent 2cd208a commit ddefc8c
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package me.xginko.aef.modules.combat;

import com.cryptomorin.xseries.XMaterial;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.time.Duration;
import java.util.UUID;
Expand Down Expand Up @@ -51,31 +49,29 @@ public void disable() {
private void onAnchorBreak(PlayerInteractEvent event) {
if (breakDelayMillis <= 0) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
final ItemStack interactItem = event.getItem();
if (interactItem == null || interactItem.getType() != Material.GLOWSTONE) return;
final Player player = event.getPlayer();
if (player.getWorld().isRespawnAnchorWorks()) return;
if (event.getClickedBlock().getType() != XMaterial.RESPAWN_ANCHOR.parseMaterial()) return;
if (event.getItem() == null || event.getItem().getType() != XMaterial.GLOWSTONE.parseMaterial()) return;
if (event.getPlayer().getWorld().isRespawnAnchorWorks()) return;

if (breakCooldowns.contains(player.getUniqueId())) {
if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
breakCooldowns.add(player.getUniqueId());
breakCooldowns.add(event.getPlayer().getUniqueId());
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAnchorPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
if (event.getBlock().getType() != Material.RESPAWN_ANCHOR) return;
final Player player = event.getPlayer();
if (player.getWorld().isRespawnAnchorWorks()) return;
if (event.getBlock().getType() != XMaterial.RESPAWN_ANCHOR.parseMaterial()) return;
if (event.getPlayer().getWorld().isRespawnAnchorWorks()) return;

if (placeCooldowns.contains(player.getUniqueId())) {
if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
placeCooldowns.add(player.getUniqueId());
placeCooldowns.add(event.getPlayer().getUniqueId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.papermc.paper.event.player.PlayerBedFailEnterEvent;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -57,8 +56,7 @@ private void onBedBreak(PlayerBedFailEnterEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBedPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
final Block placed = event.getBlockPlaced();
if (!MaterialTags.BEDS.isTagged(placed) || placed.getWorld().isBedWorks()) return;
if (!MaterialTags.BEDS.isTagged(event.getBlockPlaced()) || event.getBlockPlaced().getWorld().isBedWorks()) return;

if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.xginko.aef.modules.combat;

import com.cryptomorin.xseries.XEntityType;
import me.xginko.aef.modules.AEFModule;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -35,10 +35,9 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onProjectileLaunch(EntityShootBowEvent event) {
if (
event.getEntityType() == EntityType.PLAYER
&& event.getProjectile().getVelocity().lengthSquared() > maxBowSquaredVelocity
) {
if (event.getEntityType() != XEntityType.PLAYER.get()) return; // Skeletons also shoot bows

if (event.getProjectile().getVelocity().lengthSquared() > maxBowSquaredVelocity) {
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void onSelfPlace(BlockPlaceEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.getGameMode().equals(GameMode.SURVIVAL)) return;
if (player.getGameMode() != GameMode.SURVIVAL) return;
if (player.isInsideVehicle() || player.isGliding()) return;

final Location playerLocation = player.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onCrystalBreak(PrePlayerAttackEntityEvent event) {
if (breakDelayMillis <= 0 || !event.willAttack()) return;
if (event.getAttacked().getType() != XEntityType.END_CRYSTAL.get()) return;

if (event.getAttacked().getType() == XEntityType.END_CRYSTAL.get()) {
if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
breakCooldowns.add(event.getPlayer().getUniqueId());
}
if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
breakCooldowns.add(event.getPlayer().getUniqueId());
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onCrystalPlace(PlayerInteractEvent event) {
if (placeDelayMillis <= 0) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // Need to right-click a block to place a crystal
final ItemStack interactItem = event.getItem();
if (interactItem == null || interactItem.getType() != XMaterial.END_CRYSTAL.parseMaterial()) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@
import me.xginko.aef.utils.WorldUtil;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.time.Duration;
import java.util.UUID;

public class AnchorAuraDelay extends AEFModule implements Listener {

private final ExpiringSet<UUID> placeCooldowns, breakCooldowns;
private final Material RESPAWN_ANCHOR, GLOWSTONE;
private final Material GLOWSTONE;
private final long placeDelayMillis, breakDelayMillis;
private final boolean updateInv;

public AnchorAuraDelay() {
super("combat.anchor-aura-delay");
this.RESPAWN_ANCHOR = XMaterial.RESPAWN_ANCHOR.parseMaterial();
this.GLOWSTONE = XMaterial.GLOWSTONE.parseMaterial();
this.updateInv = config.getBoolean(configPath + ".update-inventory-on-cancel", false,
"Can help with desync but recommended to leave off unless needed.");
Expand Down Expand Up @@ -56,31 +53,29 @@ public void disable() {
private void onAnchorBreak(PlayerInteractEvent event) {
if (breakDelayMillis <= 0) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
final ItemStack interactItem = event.getItem();
if (interactItem == null || interactItem.getType() != GLOWSTONE) return;
final Player player = event.getPlayer();
if (WorldUtil.isRespawnAnchorWorks(player.getWorld())) return;
if (event.getClickedBlock().getType() != XMaterial.RESPAWN_ANCHOR.parseMaterial()) return;
if (event.getItem() == null || event.getItem().getType() != XMaterial.GLOWSTONE.parseMaterial()) return;
if (WorldUtil.isRespawnAnchorWorks(event.getPlayer().getWorld())) return;

if (breakCooldowns.contains(player.getUniqueId())) {
if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
breakCooldowns.add(player.getUniqueId());
breakCooldowns.add(event.getPlayer().getUniqueId());
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAnchorPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
if (event.getBlock().getType() != RESPAWN_ANCHOR) return;
final Player player = event.getPlayer();
if (WorldUtil.isRespawnAnchorWorks(player.getWorld())) return;
if (event.getBlock().getType() != XMaterial.RESPAWN_ANCHOR.parseMaterial()) return;
if (WorldUtil.isRespawnAnchorWorks(event.getPlayer().getWorld())) return;

if (placeCooldowns.contains(player.getUniqueId())) {
if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
if (updateInv) event.getPlayer().updateInventory();
} else {
placeCooldowns.add(player.getUniqueId());
placeCooldowns.add(event.getPlayer().getUniqueId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XTag;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.WorldUtil;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -59,9 +58,8 @@ public void disable() {
private void onBedBreak(PlayerInteractEvent event) {
if (breakDelayMillis <= 0) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
final Block clicked = event.getClickedBlock();
if (!beds.contains(clicked.getType())) return;
if (clicked.getWorld().getEnvironment() == World.Environment.NORMAL) return;
if (!beds.contains(event.getClickedBlock().getType())) return;
if (WorldUtil.isBedWorks(event.getClickedBlock().getWorld())) return;

if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
Expand All @@ -73,9 +71,8 @@ private void onBedBreak(PlayerInteractEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBedPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
final Block placed = event.getBlockPlaced();
if (!beds.contains(placed.getType())) return;
if (placed.getWorld().getEnvironment() == World.Environment.NORMAL) return;
if (!beds.contains(event.getBlockPlaced().getType())) return;
if (WorldUtil.isBedWorks(event.getBlockPlaced().getWorld())) return;

if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.xginko.aef.modules.combat;

import com.cryptomorin.xseries.XEntityType;
import me.xginko.aef.modules.AEFModule;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -35,10 +35,9 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onProjectileLaunch(EntityShootBowEvent event) {
if (
event.getEntityType() == EntityType.PLAYER
&& event.getProjectile().getVelocity().lengthSquared() > maxBowSquaredVelocity
) {
if (event.getEntityType() != XEntityType.PLAYER.get()) return; // Skeletons also shoot bows

if (event.getProjectile().getVelocity().lengthSquared() > maxBowSquaredVelocity) {
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.time.Duration;
import java.util.UUID;
Expand Down Expand Up @@ -54,26 +53,22 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onCrystalBreak(EntityDamageByEntityEvent event) {
if (breakDelayMillis <= 0) return;
if (event.getEntityType() != XEntityType.END_CRYSTAL.get()) return;
if (event.getDamager().getType() != XEntityType.PLAYER.get()) return;

if (
event.getEntityType() == XEntityType.END_CRYSTAL.get()
&& event.getDamager().getType() == XEntityType.PLAYER.get()
) {
if (breakCooldowns.contains(event.getDamager().getUniqueId())) {
event.setCancelled(true);
if (updateInv) ((Player) event.getDamager()).updateInventory();
} else {
breakCooldowns.add(event.getDamager().getUniqueId());
}
if (breakCooldowns.contains(event.getDamager().getUniqueId())) {
event.setCancelled(true);
if (updateInv) ((Player) event.getDamager()).updateInventory();
} else {
breakCooldowns.add(event.getDamager().getUniqueId());
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onCrystalPlace(PlayerInteractEvent event) {
if (placeDelayMillis <= 0) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
final ItemStack interactItem = event.getItem();
if (interactItem == null || interactItem.getType() != XMaterial.END_CRYSTAL.parseMaterial()) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; // Need to right-click a block to place a crystal
if (event.getItem() == null || event.getItem().getType() != XMaterial.END_CRYSTAL.parseMaterial()) return;

if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

public class WorldUtil {

private static final MethodHandle GET_MIN_WORLD_HEIGHT, RESPAWNANCHOR_WORKS;
private static final boolean GET_MIN_WORLD_HEIGHT_AVAILABLE, RESPAWN_ANCHOR_WORKS_AVAILABLE;
private static final MethodHandle GET_MIN_WORLD_HEIGHT, RESPAWNANCHOR_WORKS, BED_WORKS;
private static final boolean GET_MIN_WORLD_HEIGHT_AVAILABLE, RESPAWN_ANCHOR_WORKS_AVAILABLE, BED_WORKS_AVAILABLE;

static {
GET_MIN_WORLD_HEIGHT_AVAILABLE = Crafty.hasMethod(World.class, "getMinHeight");
GET_MIN_WORLD_HEIGHT = Crafty.findMethod(World.class, "getMinHeight", int.class);
RESPAWN_ANCHOR_WORKS_AVAILABLE = Crafty.hasMethod(World.class, "isRespawnAnchorWorks");
RESPAWNANCHOR_WORKS = Crafty.findMethod(World.class, "isRespawnAnchorWorks", boolean.class);
BED_WORKS_AVAILABLE = Crafty.hasMethod(World.class, "isBedWorks");
BED_WORKS = Crafty.findMethod(World.class, "isBedWorks", boolean.class);
}

public static int getMinWorldHeight(World world) {
Expand All @@ -40,13 +42,26 @@ private static int getMinWorldHeightFromConfig(World world) {

public static boolean isRespawnAnchorWorks(World world) {
if (!RESPAWN_ANCHOR_WORKS_AVAILABLE) {
return world.getEnvironment() == World.Environment.NORMAL;
return world.getEnvironment() != World.Environment.NORMAL;
}

try {
return (boolean) RESPAWNANCHOR_WORKS.invoke(world);
} catch (Throwable t) {
AnarchyExploitFixes.prefixedLogger().error("Error checking if respawn anchors work in world '{}'.", world.getName(), t);
return world.getEnvironment() != World.Environment.NORMAL;
}
}

public static boolean isBedWorks(World world) {
if (!BED_WORKS_AVAILABLE) {
return world.getEnvironment() == World.Environment.NORMAL;
}

try {
return (boolean) BED_WORKS.invoke(world);
} catch (Throwable t) {
AnarchyExploitFixes.prefixedLogger().error("Error checking if beds work in world '{}'.", world.getName(), t);
return world.getEnvironment() == World.Environment.NORMAL;
}
}
Expand Down

0 comments on commit ddefc8c

Please sign in to comment.