From da999dcd89f6116bedb902c62bd7cbaaad1991f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petulikan=20D=C3=A1lnev=C3=ADm?= Date: Wed, 4 Dec 2024 00:51:06 +0100 Subject: [PATCH] - Added a new WorldGuard flag - `ptg-explosion` - Added an event to disable fire spread on blocks that are being regenerated - Added 1.21.1 compatibility and use of new methods to support Respawn Anchor explosion restoration, support is also for particles to work the inteded way on the latest version Signed-off-by: petulikan1 Took 2 hours 57 minutes --- pom.xml | 6 +-- .../xzot1k/plugins/ptg/core/Listeners.java | 40 ++++++++++++--- .../java/xzot1k/plugins/ptg/core/Manager.java | 9 ++-- .../ptg/core/hooks/WorldGuardHook.java | 49 +++++++++++++++++++ 4 files changed, 91 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 8009820..f055aa6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.zotware.plugins PhysicsToGo - 2.1.3 + 2.1.4 1.8 @@ -127,12 +127,12 @@ 7.1.0-SNAPSHOT provided - + com.github.TheDevTec TheAPI-Shared diff --git a/src/main/java/xzot1k/plugins/ptg/core/Listeners.java b/src/main/java/xzot1k/plugins/ptg/core/Listeners.java index e65913d..2e43437 100644 --- a/src/main/java/xzot1k/plugins/ptg/core/Listeners.java +++ b/src/main/java/xzot1k/plugins/ptg/core/Listeners.java @@ -34,6 +34,7 @@ import xzot1k.plugins.ptg.events.PhysicsActionEvent; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -120,7 +121,7 @@ public void onForm(EntityChangeBlockEvent e) { e.setCancelled(true); if (getPluginInstance().getManager().isBlockDataVersion()) - e.getEntity().getWorld().spawnParticle(Particle.BLOCK_CRACK, e.getEntity().getLocation(), 10, e.getBlock().getBlockData()); + e.getEntity().getWorld().spawnParticle(Ref.isNewerThan(20)?((Particle)Ref.getStatic(Particle.class,"BLOCK")):Particle.BLOCK_CRACK, e.getEntity().getLocation(), 10, e.getBlock().getBlockData()); else e.getEntity().getWorld().playEffect(e.getEntity().getLocation(), Effect.STEP_SOUND, e.getBlock().getType().getId()); } @@ -227,6 +228,15 @@ else getPluginInstance().getServer().getScheduler().runTaskLater(getPluginInstan } } + @EventHandler + public void onFireStart(BlockIgniteEvent e){ + for(BlockState state:new HashSet<>(getPluginInstance().getManager().getSavedBlockStates())){ + if(state.getLocation().equals(e.getBlock().getLocation())){ + e.setCancelled(true); + } + } + } + @EventHandler(priority = EventPriority.LOWEST) public void onBlockExplode(BlockExplodeEvent e) { if (e.isCancelled()) return; @@ -240,10 +250,21 @@ public void onBlockExplode(BlockExplodeEvent e) { e.blockList().clear(); return; } + Block block; + Material type; + if(Ref.isNewerThan(20)){ + Object blockState = Ref.invoke(e, "getExplodedBlockState"); + block=(Block) Ref.invoke(blockState,"getBlock"); + type=(Material) Ref.invoke(blockState,"getType"); + }else { + block=e.getBlock(); + type=block.getType(); + } - e.setYield(handleExplosives(e.blockList(), e.getYield(), null, e.getBlock())); + e.setYield(handleExplosives(e.blockList(), e.getYield(), null, block,type)); } + @EventHandler(priority = EventPriority.LOWEST) public void onExplode(EntityExplodeEvent e) { if (e.isCancelled()) return; @@ -258,14 +279,18 @@ public void onExplode(EntityExplodeEvent e) { return; } - e.setYield(handleExplosives(e.blockList(), e.getYield(), e.getEntity(), null)); + if(getPluginInstance().getWorldGuardHook().handleExplosion(e)){ + return; + } + + e.setYield(handleExplosives(e.blockList(), e.getYield(), e.getEntity(), null,null)); } @SuppressWarnings("deprecation") - private float handleExplosives(List blocklist, float ogYield, Entity entity, Block causeBlock) { + private float handleExplosives(List blocklist, float ogYield, Entity entity, Block causeBlock,Material causeBlockType) { boolean yield = false, isBlockedEntity = (causeBlock == null && entity != null && getPluginInstance().getManager().isBlockedExplosiveRegenEntity(entity.getType())), - isBlockedBlock = (entity == null && causeBlock != null && getPluginInstance().getManager().isBlockedExplosiveRegenBlock(causeBlock.getType())); + isBlockedBlock = (entity == null && causeBlock != null && getPluginInstance().getManager().isBlockedExplosiveRegenBlock(causeBlockType)); blocklist.removeIf(block -> getPluginInstance().getManager().isAvoidedMaterial(block.getType(), block.getData())); final List blockList = new ArrayList<>(blocklist); @@ -305,6 +330,7 @@ private float handleExplosives(List blocklist, float ogYield, Entity enti continue; } + if (physics && ((Math.random() * 100) < 15) && fallingBlockCount < (blockList.size() * 0.25)) { getPluginInstance().getManager().createFallingBlock(block, isInventoryHolder ? block.getState() : blockState, true, false); fallingBlockCount++; @@ -333,8 +359,10 @@ private float handleExplosives(List blocklist, float ogYield, Entity enti final boolean inverted = getPluginInstance().getConfig().getBoolean("invert-bmr"); if ((!inverted && getPluginInstance().getManager().isBlockedRegenMaterial(block.getType())) - || (inverted && !getPluginInstance().getManager().isBlockedRegenMaterial(block.getType()))) + || (inverted && !getPluginInstance().getManager().isBlockedRegenMaterial(block.getType()))) { continue; + } + handleSpecialStateRestore(block, isInventoryHolder ? beforeClear : blockState, containerRestore, signRestore); getPluginInstance().getManager().getSavedBlockStates().add(isInventoryHolder ? block.getState() : blockState); diff --git a/src/main/java/xzot1k/plugins/ptg/core/Manager.java b/src/main/java/xzot1k/plugins/ptg/core/Manager.java index b1b9f0e..054bd8a 100644 --- a/src/main/java/xzot1k/plugins/ptg/core/Manager.java +++ b/src/main/java/xzot1k/plugins/ptg/core/Manager.java @@ -298,9 +298,10 @@ public boolean isBlockedWorld(World world) { */ public void playNaturalBlockBreakEffect(Block block) { if (getPluginInstance().getManager().isBlockDataVersion()) { - block.getWorld().spawnParticle(Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), + + block.getWorld().spawnParticle(Ref.isNewerThan(20) ? ((Particle) Ref.getStatic(Particle.class, "DUST")) : Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), ((block.getLocation().getY() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), - ((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), 10, block.getBlockData()); + ((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), 10, new Particle.DustOptions(block.getBlockData().getMapColor(),block.getBlockData().getLightEmission())); Sound sound = Sound.BLOCK_METAL_BREAK; if (block.getType().name().contains("LOG") || block.getType().name().contains("WOOD") @@ -365,9 +366,9 @@ else if (block.getType().name().contains("SCAFFOLD")) */ public void playNaturalBlockPlaceEffect(Block block) { if (getPluginInstance().getManager().isBlockDataVersion()) { - block.getWorld().spawnParticle(Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), + block.getWorld().spawnParticle(Ref.isNewerThan(20) ? ((Particle) Ref.getStatic(Particle.class, "DUST")) : Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), ((block.getLocation().getY() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), - ((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), 5, block.getBlockData()); + ((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), 5, new Particle.DustOptions(block.getBlockData().getMapColor(), block.getBlockData().getLightEmission())); Sound sound = Sound.BLOCK_METAL_PLACE; if (block.getType().name().contains("LOG") || block.getType().name().contains("WOOD") diff --git a/src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java b/src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java index fb5dfe4..4d1c1a6 100644 --- a/src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java +++ b/src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java @@ -4,16 +4,27 @@ package xzot1k.plugins.ptg.core.hooks; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.flags.StateFlag; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionContainer; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.event.entity.EntityExplodeEvent; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Set; public class WorldGuardHook { private static com.sk89q.worldguard.protection.flags.StateFlag PTG_ALLOW; + private static StateFlag EXPLOSION_FLAG; private final com.sk89q.worldguard.bukkit.WorldGuardPlugin worldGuardPlugin; public WorldGuardHook() { @@ -31,13 +42,51 @@ public WorldGuardHook() { if (registry == null) return; try { com.sk89q.worldguard.protection.flags.StateFlag flag = new com.sk89q.worldguard.protection.flags.StateFlag("ptg-allow", false); + StateFlag explosionFlag = new StateFlag("ptg-explosion",false); registry.register(flag); + registry.register(explosionFlag); PTG_ALLOW = flag; + EXPLOSION_FLAG=explosionFlag; } catch (com.sk89q.worldguard.protection.flags.registry.FlagConflictException e) { com.sk89q.worldguard.protection.flags.Flag existing = registry.get("ptg-allow"); + Flag explosion = registry.get("ptg-explosion"); if (existing instanceof com.sk89q.worldguard.protection.flags.StateFlag) PTG_ALLOW = (com.sk89q.worldguard.protection.flags.StateFlag) existing; + if(explosion instanceof StateFlag){ + EXPLOSION_FLAG=(StateFlag) explosion; + } + } + } + + public boolean handleExplosion(EntityExplodeEvent e){ + if(e.isCancelled()) + return false; + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + if (e.getLocation().getWorld() == null) + return false; + com.sk89q.worldedit.util.Location weLoc = BukkitAdapter.adapt(e.getLocation()); + RegionManager manager = container.get(BukkitAdapter.adapt(e.getLocation().getWorld())); + if(manager==null) + return false; + ApplicableRegionSet set = manager.getApplicableRegions(weLoc.toVector().toBlockPoint()); + HashMap priorities = new HashMap<>(); + for(ProtectedRegion region:set.getRegions()){ + StateFlag.State object = region.getFlag(EXPLOSION_FLAG); + if(object==null) + object= StateFlag.State.DENY; + if(priorities.containsKey(region.getPriority())) + continue; + if(region.contains(weLoc.toVector().toBlockPoint())){ + priorities.put(region.getPriority(),object== StateFlag.State.DENY); + } + } + + int highestPriority = priorities.keySet().stream().max(Integer::compareTo).orElse(-125); + if(highestPriority!=-125&& priorities.get(highestPriority)){ + e.blockList().clear(); + return true; } + return false; } public boolean passedWorldGuardHook(Location location) {