diff --git a/src/main/java/net/countercraft/movecraft/worldguard/CustomFlags.java b/src/main/java/net/countercraft/movecraft/worldguard/CustomFlags.java index 179004e..3c9dbc8 100644 --- a/src/main/java/net/countercraft/movecraft/worldguard/CustomFlags.java +++ b/src/main/java/net/countercraft/movecraft/worldguard/CustomFlags.java @@ -13,6 +13,7 @@ public class CustomFlags { public static final StateFlag ALLOW_CRAFT_SINK = new StateFlag("allow-craft-sink", false); public static final StateFlag ALLOW_CRAFT_TRANSLATE = new StateFlag("allow-craft-translate", false); public static final StateFlag ALLOW_CRAFT_REPAIR = new StateFlag("allow-craft-repair", false); + public static final StateFlag ONLY_DAMAGE_PILOTED_CRAFTS = new StateFlag("only-damage-piloted-crafts", false); public static void register() { try { @@ -23,6 +24,7 @@ public static void register() { registry.register(ALLOW_CRAFT_SINK); registry.register(ALLOW_CRAFT_TRANSLATE); registry.register(ALLOW_CRAFT_REPAIR); + registry.register(ONLY_DAMAGE_PILOTED_CRAFTS); } catch (Exception e) { MovecraftWorldGuard.getInstance().getLogger().log( diff --git a/src/main/java/net/countercraft/movecraft/worldguard/MovecraftWorldGuard.java b/src/main/java/net/countercraft/movecraft/worldguard/MovecraftWorldGuard.java index e81c901..b7831a8 100644 --- a/src/main/java/net/countercraft/movecraft/worldguard/MovecraftWorldGuard.java +++ b/src/main/java/net/countercraft/movecraft/worldguard/MovecraftWorldGuard.java @@ -72,6 +72,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new CraftSinkListener(), this); getServer().getPluginManager().registerEvents(new CraftTranslateListener(), this); getServer().getPluginManager().registerEvents(new ExplosionListener(), this); + getServer().getPluginManager().registerEvents(new BurnListener(), this); } public WorldGuardUtils getWGUtils() { diff --git a/src/main/java/net/countercraft/movecraft/worldguard/listener/BurnListener.java b/src/main/java/net/countercraft/movecraft/worldguard/listener/BurnListener.java new file mode 100644 index 0000000..a5a11a9 --- /dev/null +++ b/src/main/java/net/countercraft/movecraft/worldguard/listener/BurnListener.java @@ -0,0 +1,30 @@ +package net.countercraft.movecraft.worldguard.listener; + +import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.craft.CraftManager; +import net.countercraft.movecraft.util.MathUtils; +import net.countercraft.movecraft.worldguard.CustomFlags; +import net.countercraft.movecraft.worldguard.MovecraftWorldGuard; +import net.countercraft.movecraft.worldguard.utils.WorldGuardUtils; +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBurnEvent; + +public class BurnListener implements Listener { + @EventHandler + public void onBurn(BlockBurnEvent e) { + Location location = e.getBlock().getLocation(); + + var wgUtils = MovecraftWorldGuard.getInstance().getWGUtils(); + WorldGuardUtils.State onlyDamagePilotedCrafts = wgUtils.getState(null, location, CustomFlags.ONLY_DAMAGE_PILOTED_CRAFTS); + if (onlyDamagePilotedCrafts != WorldGuardUtils.State.ALLOW) { + return; + } + + MovecraftLocation movecraftLocation = MathUtils.bukkit2MovecraftLoc(location); + boolean burningCraft = CraftManager.getInstance().getCraftsInWorld(location.getWorld()).stream().anyMatch(it -> it.getHitBox().contains(movecraftLocation)); + + e.setCancelled(!burningCraft); + } +} diff --git a/src/main/java/net/countercraft/movecraft/worldguard/listener/ExplosionListener.java b/src/main/java/net/countercraft/movecraft/worldguard/listener/ExplosionListener.java index e883b8f..9ddf3bb 100644 --- a/src/main/java/net/countercraft/movecraft/worldguard/listener/ExplosionListener.java +++ b/src/main/java/net/countercraft/movecraft/worldguard/listener/ExplosionListener.java @@ -1,16 +1,24 @@ package net.countercraft.movecraft.worldguard.listener; import com.sk89q.worldguard.protection.flags.Flags; +import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.craft.CraftManager; import net.countercraft.movecraft.events.ExplosionEvent; +import net.countercraft.movecraft.util.MathUtils; +import net.countercraft.movecraft.worldguard.CustomFlags; import net.countercraft.movecraft.worldguard.MovecraftWorldGuard; +import net.countercraft.movecraft.worldguard.utils.WorldGuardUtils; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.Location; public class ExplosionListener implements Listener { @EventHandler public void onExplosion(ExplosionEvent e) { - switch (MovecraftWorldGuard.getInstance().getWGUtils().getState( - null, e.getExplosionLocation(), Flags.OTHER_EXPLOSION)) { + var wgUtils = MovecraftWorldGuard.getInstance().getWGUtils(); + Location explosionLocation = e.getExplosionLocation(); + + switch (wgUtils.getState(null, e.getExplosionLocation(), Flags.OTHER_EXPLOSION)) { case ALLOW: case NONE: break; // Other-explosion is allowed @@ -21,5 +29,15 @@ public void onExplosion(ExplosionEvent e) { default: break; } + + WorldGuardUtils.State onlyDamagePilotedCrafts = wgUtils.getState(null, explosionLocation, CustomFlags.ONLY_DAMAGE_PILOTED_CRAFTS); + if (onlyDamagePilotedCrafts != WorldGuardUtils.State.ALLOW) { + return; + } + + MovecraftLocation movecraftLocation = MathUtils.bukkit2MovecraftLoc(explosionLocation); + boolean explodingOnCraft = CraftManager.getInstance().getCraftsInWorld(explosionLocation.getWorld()).stream().anyMatch(it -> it.getHitBox().contains(movecraftLocation)); + + e.setCancelled(!explodingOnCraft); } }