diff --git a/src/main/java/io/github/jhale1805/powerarrow/ForestFireArrow.java b/src/main/java/io/github/jhale1805/powerarrow/ForestFireArrow.java index 8c4fe97..4e583fc 100644 --- a/src/main/java/io/github/jhale1805/powerarrow/ForestFireArrow.java +++ b/src/main/java/io/github/jhale1805/powerarrow/ForestFireArrow.java @@ -3,7 +3,6 @@ package io.github.jhale1805.powerarrow; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Particle; -import org.bukkit.block.Block; import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.inventory.ShapedRecipe; @@ -51,26 +50,7 @@ public class ForestFireArrow extends PowerArrow { protected void onThisProjectileHit(ProjectileHitEvent event) { Location impactLocation = Utilities.getImpactLocation(event); if (impactLocation != null) { - startForestFire(impactLocation); - } - } - - private void startForestFire(Location location) { - for (double x = -1; x <= 1; x++) { - for (double y = 0; y <= 2; y++) { - for (double z = -1; z <= 1; z++) { - Location toCheck = new Location( - location.getWorld(), - location.getX() + x, - location.getY() + y, - location.getZ() + z - ); - Block toIgnite = location.getWorld().getBlockAt(toCheck); - if (toIgnite.getType() == Material.AIR) { - toIgnite.setType(Material.FIRE); - } - } - } + Utilities.replaceAirWith(Material.FIRE, impactLocation, 1); } } diff --git a/src/main/java/io/github/jhale1805/util/Utilities.java b/src/main/java/io/github/jhale1805/util/Utilities.java index 1500015..af3e47d 100644 --- a/src/main/java/io/github/jhale1805/util/Utilities.java +++ b/src/main/java/io/github/jhale1805/util/Utilities.java @@ -1,13 +1,23 @@ package io.github.jhale1805.util; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.Particle; +import org.bukkit.block.Block; import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.util.Vector; public class Utilities { - public static void drawParticles(Location from, Location to, Particle particle){ + /** + * Draws particles of the given type along the line specified by + * `from` to `to`. + * + * @param particle The particle to draw + * @param from The start point of the line of particles + * @param to The end point of the line of particles + */ + public static void drawParticles(Particle particle, Location from, Location to){ if (!from.getWorld().equals(to.getWorld())) return; Vector direction = to.toVector().subtract(from.toVector()).normalize().multiply(0.4); @@ -18,6 +28,15 @@ public class Utilities { } } + /** + * Returns the Location where the projectile hit. + * + * Abstracts out the complexity of determining whether the projectile hit + * a block or an Entity. + * + * @param event The event from which you want the impact location. + * @return The Location where the projectile hit. + */ public static Location getImpactLocation(ProjectileHitEvent event) { Location loc = null; if (event.getHitBlock() != null) { @@ -27,5 +46,45 @@ public class Utilities { } return loc; } + + /** + * Replaces all Air blocks within the specified (cubic) radius of the + * given centerPoint with the specified Material. + * + * @param material The material with which to replace the air. + * @param centerPoint The centerpoint of the scan for air to replace + * @param radius The radius around the centerpoint in which to look for air + * blocks to replace. Recommended max is 3 (this isn't WorldEdit!) + * Expected to be positive, but absolute valued in case you try to + * be tricky :D + */ + public static void replaceAirWith(Material material, Location centerPoint, double radius) { + // Validate parameters + if (material == null) { + throw new IllegalArgumentException("You must specify a Material!"); + } + if (centerPoint == null) { + throw new IllegalArgumentException("You must specify a centerPoint!"); + } + radius = Math.abs(radius); + + // Replace the blocks + for (double dx = -radius; dx <= radius; dx++) { + for (double dy = 0; dy <= radius * 2 ; dy++) { + for (double dz = -radius; dz <= radius; dz++) { + Location toCheck = new Location( + centerPoint.getWorld(), + centerPoint.getX() + dx, + centerPoint.getY() + dy, + centerPoint.getZ() + dz + ); + Block toReplace = centerPoint.getWorld().getBlockAt(toCheck); + if (toReplace.getType() == Material.AIR) { + toReplace.setType(material); + } + } + } + } + } }