From bdb339905af1e6f72224ec98114fa16830092566 Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sat, 25 Jan 2025 21:15:08 +0100 Subject: [PATCH] fix: replace (removed) constants with backwards compatible alternatives --- .../bukkit/listener/EntityEventListener.java | 17 ++++++++++++----- .../bukkit/listener/EntitySpawnListener.java | 8 ++++++-- .../bukkit/listener/PaperListener.java | 8 ++++++-- .../bukkit/listener/PlayerEventListener.java | 4 +++- .../listener/ProjectileEventListener.java | 7 +++++-- .../bukkit/util/BukkitEntityUtil.java | 5 ++++- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java index 358c348dd..4e2d207b0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntityEventListener.java @@ -42,9 +42,11 @@ import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.PlotFlagUtil; import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.util.Enums; import com.sk89q.worldedit.world.block.BlockType; import io.papermc.lib.PaperLib; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.Particle; import org.bukkit.World; import org.bukkit.block.Block; @@ -78,10 +80,14 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Iterator; import java.util.List; +import java.util.Objects; @SuppressWarnings("unused") public class EntityEventListener implements Listener { + private static final NamespacedKey TNT_MINECART = NamespacedKey.minecraft("tnt_minecart"); + private static final Particle EXPLOSION_HUGE = Objects.requireNonNull(Enums.findByValue(Particle.class, "EXPLOSION_EMITTER", "EXPLOSION_HUGE")); + private final BukkitPlatform platform; private final PlotAreaManager plotAreaManager; private final EventDispatcher eventDispatcher; @@ -152,8 +158,8 @@ public class EntityEventListener implements Listener { } } case "REINFORCEMENTS", "NATURAL", "MOUNT", "PATROL", "RAID", "SHEARED", "SILVERFISH_BLOCK", "ENDER_PEARL", - "TRAP", "VILLAGE_DEFENSE", "VILLAGE_INVASION", "BEEHIVE", "CHUNK_GEN", "NETHER_PORTAL", - "FROZEN", "SPELL", "DEFAULT" -> { + "TRAP", "VILLAGE_DEFENSE", "VILLAGE_INVASION", "BEEHIVE", "CHUNK_GEN", "NETHER_PORTAL", + "FROZEN", "SPELL", "DEFAULT" -> { if (!area.isMobSpawning()) { event.setCancelled(true); return; @@ -314,7 +320,7 @@ public class EntityEventListener implements Listener { if (this.lastRadius != 0) { List nearby = event.getEntity().getNearbyEntities(this.lastRadius, this.lastRadius, this.lastRadius); for (Entity near : nearby) { - if (near instanceof TNTPrimed || near.getType().equals(EntityType.MINECART_TNT)) { + if (near instanceof TNTPrimed || near.getType().getKey().equals(TNT_MINECART)) { if (!near.hasMetadata("plot")) { near.setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.platform(), plot)); } @@ -338,7 +344,7 @@ public class EntityEventListener implements Listener { event.setCancelled(true); //Spawn Explosion Particles when enabled in settings if (Settings.General.ALWAYS_SHOW_EXPLOSIONS) { - event.getLocation().getWorld().spawnParticle(Particle.EXPLOSION_HUGE, event.getLocation(), 0); + event.getLocation().getWorld().spawnParticle(EXPLOSION_HUGE, event.getLocation(), 0); } } @@ -365,7 +371,8 @@ public class EntityEventListener implements Listener { // Burning player evaporating powder snow. Use same checks as // trampling farmland BlockType blockType = BukkitAdapter.asBlockType(type); - if (!this.eventDispatcher.checkPlayerBlockEvent(pp, + if (!this.eventDispatcher.checkPlayerBlockEvent( + pp, PlayerBlockEventType.TRIGGER_PHYSICAL, location, blockType, true )) { event.setCancelled(true); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java index 505fee01d..e4a1bc5fe 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/EntitySpawnListener.java @@ -28,6 +28,7 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import io.papermc.lib.PaperLib; import org.bukkit.Chunk; +import org.bukkit.NamespacedKey; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.ArmorStand; @@ -54,6 +55,9 @@ import java.util.List; public class EntitySpawnListener implements Listener { + private static final NamespacedKey ITEM = NamespacedKey.minecraft("item"); + private static final NamespacedKey END_CRYSTAL = NamespacedKey.minecraft("end_crystal"); + private static final String KEY = "P2"; private static boolean ignoreTP = false; private static boolean hasPlotArea = false; @@ -132,7 +136,7 @@ public class EntitySpawnListener implements Listener { Plot plot = location.getOwnedPlotAbs(); EntityType type = entity.getType(); if (plot == null) { - if (type == EntityType.DROPPED_ITEM) { + if (type.getKey().equals(ITEM)) { if (Settings.Enabled_Components.KILL_ROAD_ITEMS) { event.setCancelled(true); } @@ -154,7 +158,7 @@ public class EntitySpawnListener implements Listener { if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { event.setCancelled(true); } - if (type == EntityType.ENDER_CRYSTAL || type == EntityType.ARMOR_STAND) { + if (type.getKey().equals(END_CRYSTAL) || type == EntityType.ARMOR_STAND) { if (BukkitEntityUtil.checkEntity(entity, plot)) { event.setCancelled(true); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java index aa6474be1..41225bdae 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java @@ -52,6 +52,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Chunk; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.TileState; import org.bukkit.entity.Entity; @@ -80,6 +81,9 @@ import java.util.regex.Pattern; @SuppressWarnings("unused") public class PaperListener implements Listener { + private static final NamespacedKey ITEM = NamespacedKey.minecraft("item"); + private static final NamespacedKey FISHING_BOBBER = NamespacedKey.minecraft("fishing_bobber"); + private final PlotAreaManager plotAreaManager; private Chunk lastChunk; @@ -228,7 +232,7 @@ public class PaperListener implements Listener { if (plot == null) { EntityType type = event.getType(); // PreCreatureSpawnEvent **should** not be called for DROPPED_ITEM, just for the sake of consistency - if (type == EntityType.DROPPED_ITEM) { + if (type.getKey().equals(ITEM)) { if (Settings.Enabled_Components.KILL_ROAD_ITEMS) { event.setCancelled(true); } @@ -354,7 +358,7 @@ public class PaperListener implements Listener { event.setCancelled(true); } } else if (!plot.isAdded(pp.getUUID())) { - if (entity.getType().equals(EntityType.FISHING_HOOK)) { + if (entity.getType().getKey().equals(FISHING_BOBBER)) { if (plot.getFlag(FishingFlag.class)) { return; } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java index a10fc01e8..955d6f4c3 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEventListener.java @@ -95,6 +95,7 @@ import org.bukkit.block.data.Waterlogged; import org.bukkit.command.PluginCommand; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Boat; +import org.bukkit.entity.ChestBoat; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.HumanEntity; @@ -376,7 +377,8 @@ public class PlayerEventListener implements Listener { @EventHandler public void onVehicleEntityCollision(VehicleEntityCollisionEvent e) { - if (e.getVehicle().getType() == EntityType.BOAT) { + final Class clazz = e.getVehicle().getType().getEntityClass(); + if (clazz != null && (Boat.class.isAssignableFrom(clazz) || ChestBoat.class.isAssignableFrom(clazz))) { Location location = BukkitUtil.adapt(e.getEntity().getLocation()); if (location.isPlotArea()) { if (e.getEntity() instanceof Player) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java index 8c79b29d3..32a7c0728 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ProjectileEventListener.java @@ -34,6 +34,7 @@ import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.PlotFlagUtil; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -54,6 +55,8 @@ import org.checkerframework.checker.nullness.qual.NonNull; @SuppressWarnings("unused") public class ProjectileEventListener implements Listener { + private static final NamespacedKey FISHING_BOBBER = NamespacedKey.minecraft("fishing_bobber"); + private final PlotAreaManager plotAreaManager; @Inject @@ -134,7 +137,7 @@ public class ProjectileEventListener implements Listener { event.setCancelled(true); } } else if (!plot.isAdded(pp.getUUID())) { - if (entity.getType().equals(EntityType.FISHING_HOOK)) { + if (entity.getType().getKey().equals(FISHING_BOBBER)) { if (plot.getFlag(FishingFlag.class)) { return; } @@ -194,7 +197,7 @@ public class ProjectileEventListener implements Listener { return; } if (plot.isAdded(pp.getUUID()) || pp.hasPermission(Permission.PERMISSION_ADMIN_PROJECTILE_OTHER) || plot.getFlag( - ProjectilesFlag.class) || (entity.getType().equals(EntityType.FISHING_HOOK) && plot.getFlag( + ProjectilesFlag.class) || (entity.getType().getKey().equals(FISHING_BOBBER) && plot.getFlag( FishingFlag.class))) { return; } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java index becc3ddc1..857d2d180 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEntityUtil.java @@ -45,6 +45,7 @@ import com.sk89q.worldedit.bukkit.BukkitAdapter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Arrow; import org.bukkit.entity.Creature; import org.bukkit.entity.Entity; @@ -59,6 +60,8 @@ import java.util.Objects; public class BukkitEntityUtil { + private static final NamespacedKey FIREWORK_ROCKET = NamespacedKey.minecraft("firework_rocket"); + public static final com.sk89q.worldedit.world.entity.EntityType FAKE_ENTITY_TYPE = new com.sk89q.worldedit.world.entity.EntityType("plotsquared:fake"); @@ -342,7 +345,7 @@ public class BukkitEntityUtil { //disable the firework damage. too much of a headache to support at the moment. if (vplot != null) { if (EntityDamageEvent.DamageCause.ENTITY_EXPLOSION == cause - && damager.getType() == EntityType.FIREWORK) { + && damager.getType().getKey().equals(FIREWORK_ROCKET)) { return false; } }