Skip to content

Commit

Permalink
- Added a new WorldGuard flag - ptg-explosion
Browse files Browse the repository at this point in the history
- 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 <[email protected]>

Took 2 hours 57 minutes
  • Loading branch information
petulikan1 committed Dec 3, 2024
1 parent 7af0b6b commit da999dc
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.zotware.plugins</groupId>
<artifactId>PhysicsToGo</artifactId>
<version>2.1.3</version>
<version>2.1.4</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -127,12 +127,12 @@
<version>7.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<!-- <dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependency>-->
<dependency>
<groupId>com.github.TheDevTec</groupId>
<artifactId>TheAPI-Shared</artifactId>
Expand Down
40 changes: 34 additions & 6 deletions src/main/java/xzot1k/plugins/ptg/core/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Block> blocklist, float ogYield, Entity entity, Block causeBlock) {
private float handleExplosives(List<Block> 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<Block> blockList = new ArrayList<>(blocklist);
Expand Down Expand Up @@ -305,6 +330,7 @@ private float handleExplosives(List<Block> 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++;
Expand Down Expand Up @@ -333,8 +359,10 @@ private float handleExplosives(List<Block> 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);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/xzot1k/plugins/ptg/core/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<Integer, Boolean> 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) {
Expand Down

0 comments on commit da999dc

Please sign in to comment.