Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an inefficient method to do a check like this, please use the utility functions in WorldGuardUtils or a similar algorithm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't find any good function there that might help me in my case, maybe there is something useful in MathUtils


e.setCancelled(!explodingOnCraft);
}
}