Skip to content

Commit da999dc

Browse files
committed
- 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 <petulikan@gmail.com> Took 2 hours 57 minutes
1 parent 7af0b6b commit da999dc

4 files changed

Lines changed: 91 additions & 13 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.zotware.plugins</groupId>
88
<artifactId>PhysicsToGo</artifactId>
9-
<version>2.1.3</version>
9+
<version>2.1.4</version>
1010

1111
<properties>
1212
<java.version>1.8</java.version>
@@ -127,12 +127,12 @@
127127
<version>7.1.0-SNAPSHOT</version>
128128
<scope>provided</scope>
129129
</dependency>
130-
<dependency>
130+
<!-- <dependency>
131131
<groupId>com.sk89q.worldedit</groupId>
132132
<artifactId>worldedit-bukkit</artifactId>
133133
<version>7.3.0-SNAPSHOT</version>
134134
<scope>provided</scope>
135-
</dependency>
135+
</dependency>-->
136136
<dependency>
137137
<groupId>com.github.TheDevTec</groupId>
138138
<artifactId>TheAPI-Shared</artifactId>

src/main/java/xzot1k/plugins/ptg/core/Listeners.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import xzot1k.plugins.ptg.events.PhysicsActionEvent;
3535

3636
import java.util.ArrayList;
37+
import java.util.HashSet;
3738
import java.util.List;
3839
import java.util.stream.Collectors;
3940

@@ -120,7 +121,7 @@ public void onForm(EntityChangeBlockEvent e) {
120121
e.setCancelled(true);
121122

122123
if (getPluginInstance().getManager().isBlockDataVersion())
123-
e.getEntity().getWorld().spawnParticle(Particle.BLOCK_CRACK, e.getEntity().getLocation(), 10, e.getBlock().getBlockData());
124+
e.getEntity().getWorld().spawnParticle(Ref.isNewerThan(20)?((Particle)Ref.getStatic(Particle.class,"BLOCK")):Particle.BLOCK_CRACK, e.getEntity().getLocation(), 10, e.getBlock().getBlockData());
124125
else
125126
e.getEntity().getWorld().playEffect(e.getEntity().getLocation(), Effect.STEP_SOUND, e.getBlock().getType().getId());
126127
}
@@ -227,6 +228,15 @@ else getPluginInstance().getServer().getScheduler().runTaskLater(getPluginInstan
227228
}
228229
}
229230

231+
@EventHandler
232+
public void onFireStart(BlockIgniteEvent e){
233+
for(BlockState state:new HashSet<>(getPluginInstance().getManager().getSavedBlockStates())){
234+
if(state.getLocation().equals(e.getBlock().getLocation())){
235+
e.setCancelled(true);
236+
}
237+
}
238+
}
239+
230240
@EventHandler(priority = EventPriority.LOWEST)
231241
public void onBlockExplode(BlockExplodeEvent e) {
232242
if (e.isCancelled()) return;
@@ -240,10 +250,21 @@ public void onBlockExplode(BlockExplodeEvent e) {
240250
e.blockList().clear();
241251
return;
242252
}
253+
Block block;
254+
Material type;
255+
if(Ref.isNewerThan(20)){
256+
Object blockState = Ref.invoke(e, "getExplodedBlockState");
257+
block=(Block) Ref.invoke(blockState,"getBlock");
258+
type=(Material) Ref.invoke(blockState,"getType");
259+
}else {
260+
block=e.getBlock();
261+
type=block.getType();
262+
}
243263

244-
e.setYield(handleExplosives(e.blockList(), e.getYield(), null, e.getBlock()));
264+
e.setYield(handleExplosives(e.blockList(), e.getYield(), null, block,type));
245265
}
246266

267+
247268
@EventHandler(priority = EventPriority.LOWEST)
248269
public void onExplode(EntityExplodeEvent e) {
249270
if (e.isCancelled()) return;
@@ -258,14 +279,18 @@ public void onExplode(EntityExplodeEvent e) {
258279
return;
259280
}
260281

261-
e.setYield(handleExplosives(e.blockList(), e.getYield(), e.getEntity(), null));
282+
if(getPluginInstance().getWorldGuardHook().handleExplosion(e)){
283+
return;
284+
}
285+
286+
e.setYield(handleExplosives(e.blockList(), e.getYield(), e.getEntity(), null,null));
262287
}
263288

264289
@SuppressWarnings("deprecation")
265-
private float handleExplosives(List<Block> blocklist, float ogYield, Entity entity, Block causeBlock) {
290+
private float handleExplosives(List<Block> blocklist, float ogYield, Entity entity, Block causeBlock,Material causeBlockType) {
266291
boolean yield = false,
267292
isBlockedEntity = (causeBlock == null && entity != null && getPluginInstance().getManager().isBlockedExplosiveRegenEntity(entity.getType())),
268-
isBlockedBlock = (entity == null && causeBlock != null && getPluginInstance().getManager().isBlockedExplosiveRegenBlock(causeBlock.getType()));
293+
isBlockedBlock = (entity == null && causeBlock != null && getPluginInstance().getManager().isBlockedExplosiveRegenBlock(causeBlockType));
269294
blocklist.removeIf(block -> getPluginInstance().getManager().isAvoidedMaterial(block.getType(), block.getData()));
270295

271296
final List<Block> blockList = new ArrayList<>(blocklist);
@@ -305,6 +330,7 @@ private float handleExplosives(List<Block> blocklist, float ogYield, Entity enti
305330
continue;
306331
}
307332

333+
308334
if (physics && ((Math.random() * 100) < 15) && fallingBlockCount < (blockList.size() * 0.25)) {
309335
getPluginInstance().getManager().createFallingBlock(block, isInventoryHolder ? block.getState() : blockState, true, false);
310336
fallingBlockCount++;
@@ -333,8 +359,10 @@ private float handleExplosives(List<Block> blocklist, float ogYield, Entity enti
333359

334360
final boolean inverted = getPluginInstance().getConfig().getBoolean("invert-bmr");
335361
if ((!inverted && getPluginInstance().getManager().isBlockedRegenMaterial(block.getType()))
336-
|| (inverted && !getPluginInstance().getManager().isBlockedRegenMaterial(block.getType())))
362+
|| (inverted && !getPluginInstance().getManager().isBlockedRegenMaterial(block.getType()))) {
337363
continue;
364+
}
365+
338366

339367
handleSpecialStateRestore(block, isInventoryHolder ? beforeClear : blockState, containerRestore, signRestore);
340368
getPluginInstance().getManager().getSavedBlockStates().add(isInventoryHolder ? block.getState() : blockState);

src/main/java/xzot1k/plugins/ptg/core/Manager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,10 @@ public boolean isBlockedWorld(World world) {
298298
*/
299299
public void playNaturalBlockBreakEffect(Block block) {
300300
if (getPluginInstance().getManager().isBlockDataVersion()) {
301-
block.getWorld().spawnParticle(Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)),
301+
302+
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)),
302303
((block.getLocation().getY() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)),
303-
((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), 10, block.getBlockData());
304+
((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-1, 1)), 10, new Particle.DustOptions(block.getBlockData().getMapColor(),block.getBlockData().getLightEmission()));
304305

305306
Sound sound = Sound.BLOCK_METAL_BREAK;
306307
if (block.getType().name().contains("LOG") || block.getType().name().contains("WOOD")
@@ -365,9 +366,9 @@ else if (block.getType().name().contains("SCAFFOLD"))
365366
*/
366367
public void playNaturalBlockPlaceEffect(Block block) {
367368
if (getPluginInstance().getManager().isBlockDataVersion()) {
368-
block.getWorld().spawnParticle(Particle.BLOCK_DUST, ((block.getLocation().getX() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)),
369+
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)),
369370
((block.getLocation().getY() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)),
370-
((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), 5, block.getBlockData());
371+
((block.getLocation().getZ() + 0.5) + getPluginInstance().getManager().getRandomInRange(-0.5, 0.5)), 5, new Particle.DustOptions(block.getBlockData().getMapColor(), block.getBlockData().getLightEmission()));
371372

372373
Sound sound = Sound.BLOCK_METAL_PLACE;
373374
if (block.getType().name().contains("LOG") || block.getType().name().contains("WOOD")

src/main/java/xzot1k/plugins/ptg/core/hooks/WorldGuardHook.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@
44

55
package xzot1k.plugins.ptg.core.hooks;
66

7+
import com.sk89q.worldedit.bukkit.BukkitAdapter;
8+
import com.sk89q.worldguard.WorldGuard;
9+
import com.sk89q.worldguard.protection.ApplicableRegionSet;
10+
import com.sk89q.worldguard.protection.flags.Flag;
11+
import com.sk89q.worldguard.protection.flags.StateFlag;
12+
import com.sk89q.worldguard.protection.managers.RegionManager;
13+
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
14+
import com.sk89q.worldguard.protection.regions.RegionContainer;
715
import org.bukkit.Location;
816
import org.bukkit.World;
17+
import org.bukkit.event.entity.EntityExplodeEvent;
918

1019
import java.lang.reflect.InvocationTargetException;
1120
import java.lang.reflect.Method;
21+
import java.util.HashMap;
1222
import java.util.Set;
1323

1424
public class WorldGuardHook {
1525

1626
private static com.sk89q.worldguard.protection.flags.StateFlag PTG_ALLOW;
27+
private static StateFlag EXPLOSION_FLAG;
1728
private final com.sk89q.worldguard.bukkit.WorldGuardPlugin worldGuardPlugin;
1829

1930
public WorldGuardHook() {
@@ -31,13 +42,51 @@ public WorldGuardHook() {
3142
if (registry == null) return;
3243
try {
3344
com.sk89q.worldguard.protection.flags.StateFlag flag = new com.sk89q.worldguard.protection.flags.StateFlag("ptg-allow", false);
45+
StateFlag explosionFlag = new StateFlag("ptg-explosion",false);
3446
registry.register(flag);
47+
registry.register(explosionFlag);
3548
PTG_ALLOW = flag;
49+
EXPLOSION_FLAG=explosionFlag;
3650
} catch (com.sk89q.worldguard.protection.flags.registry.FlagConflictException e) {
3751
com.sk89q.worldguard.protection.flags.Flag<?> existing = registry.get("ptg-allow");
52+
Flag<?> explosion = registry.get("ptg-explosion");
3853
if (existing instanceof com.sk89q.worldguard.protection.flags.StateFlag)
3954
PTG_ALLOW = (com.sk89q.worldguard.protection.flags.StateFlag) existing;
55+
if(explosion instanceof StateFlag){
56+
EXPLOSION_FLAG=(StateFlag) explosion;
57+
}
58+
}
59+
}
60+
61+
public boolean handleExplosion(EntityExplodeEvent e){
62+
if(e.isCancelled())
63+
return false;
64+
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
65+
if (e.getLocation().getWorld() == null)
66+
return false;
67+
com.sk89q.worldedit.util.Location weLoc = BukkitAdapter.adapt(e.getLocation());
68+
RegionManager manager = container.get(BukkitAdapter.adapt(e.getLocation().getWorld()));
69+
if(manager==null)
70+
return false;
71+
ApplicableRegionSet set = manager.getApplicableRegions(weLoc.toVector().toBlockPoint());
72+
HashMap<Integer, Boolean> priorities = new HashMap<>();
73+
for(ProtectedRegion region:set.getRegions()){
74+
StateFlag.State object = region.getFlag(EXPLOSION_FLAG);
75+
if(object==null)
76+
object= StateFlag.State.DENY;
77+
if(priorities.containsKey(region.getPriority()))
78+
continue;
79+
if(region.contains(weLoc.toVector().toBlockPoint())){
80+
priorities.put(region.getPriority(),object== StateFlag.State.DENY);
81+
}
82+
}
83+
84+
int highestPriority = priorities.keySet().stream().max(Integer::compareTo).orElse(-125);
85+
if(highestPriority!=-125&& priorities.get(highestPriority)){
86+
e.blockList().clear();
87+
return true;
4088
}
89+
return false;
4190
}
4291

4392
public boolean passedWorldGuardHook(Location location) {

0 commit comments

Comments
 (0)