diff --git a/src/main/java/io/github/jhale1805/PowerProjectilePlugin.java b/src/main/java/io/github/jhale1805/PowerProjectilePlugin.java index 3c153d3..abc1801 100644 --- a/src/main/java/io/github/jhale1805/PowerProjectilePlugin.java +++ b/src/main/java/io/github/jhale1805/PowerProjectilePlugin.java @@ -1,6 +1,7 @@ package io.github.jhale1805; import java.io.File; +import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginDescriptionFile; @@ -12,6 +13,7 @@ import io.github.jhale1805.powerrecipe.*; public class PowerProjectilePlugin extends JavaPlugin { public static PowerProjectilePlugin instance; + public static Logger log; public final static String NAME = "PowerProjectile"; public final static int BSTATS_PLUGIN_ID = 12003; @@ -36,13 +38,14 @@ public class PowerProjectilePlugin extends JavaPlugin { @Override public void onEnable() { instance = this; - getLogger().info("Copyright 2021 by Joseph Hale (jhale1805). All Rights Reserved."); - getLogger().info("Report issues to https://github.com/jhale1805/power-projectiles"); - getLogger().info("Loading all Power Projectiles."); + log = getLogger(); + log.info("Copyright 2021 by Joseph Hale (jhale1805). All Rights Reserved."); + log.info("Report issues to https://github.com/jhale1805/power-projectiles"); + log.info("Loading all Power Projectiles."); registerPowerArrows(); registerPowerRecipes(); new Metrics(this, BSTATS_PLUGIN_ID); // Enable bStats metrics - getLogger().info("Ready!"); + log.info("Ready!"); } private void registerPowerArrows() { @@ -61,6 +64,6 @@ public class PowerProjectilePlugin extends JavaPlugin { @Override public void onDisable() { - getLogger().info("Thanks for using Power Projectiles!"); + log.info("Thanks for using Power Projectiles!"); } } diff --git a/src/main/java/io/github/jhale1805/powerarrow/JailArrow.java b/src/main/java/io/github/jhale1805/powerarrow/JailArrow.java new file mode 100644 index 0000000..c6d8480 --- /dev/null +++ b/src/main/java/io/github/jhale1805/powerarrow/JailArrow.java @@ -0,0 +1,64 @@ +package io.github.jhale1805.powerarrow; + +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.ShapedRecipe; + +import io.github.jhale1805.PowerProjectilePlugin; +import io.github.jhale1805.util.BlockTools; +import io.github.jhale1805.util.LocationTools; + +public class JailArrow extends PowerArrow { + + public JailArrow() { + super(); + } + + public JailArrow(int count) { + super(count); + } + + @Override + public String getName() { + return "jail_arrow"; + } + + @Override + public String[] getUsageInstructions() { + return new String[] {"Surrounds hit target", "with iron bars"}; + } + + @Override + public Recipe getRecipe() { + ShapedRecipe recipe = new ShapedRecipe( + this.getRecipeKey(), + new JailArrow() + ); + recipe.shape("III", "IAI", "III"); + recipe.setIngredient('I', Material.IRON_BARS); + recipe.setIngredient('A', Material.ARROW); + return recipe; + } + + @Override + protected void onThisProjectileHit(ProjectileHitEvent event) { + Entity hitEntity = event.getHitEntity(); + if (hitEntity != null) { + Location barsBox1 = LocationTools.blockCoordsOf(hitEntity.getLocation()); + Location barsBox2 = LocationTools.addImmutable(hitEntity.getLocation(), 1, 1, 1); + Location teleportTo = LocationTools.addImmutable(hitEntity.getLocation(), 1, 0, 1); + PowerProjectilePlugin.log.log(Level.FINEST, "[" + getName() + "]" + + " Placing bars from " + LocationTools.stringify(barsBox1) + + " to " + LocationTools.stringify(barsBox1) + ); + BlockTools.replaceAll(null, Material.IRON_BARS, barsBox1, barsBox2); + hitEntity.teleport(teleportTo); + } + } + +} diff --git a/src/main/java/io/github/jhale1805/powerarrow/PowerArrowRegistry.java b/src/main/java/io/github/jhale1805/powerarrow/PowerArrowRegistry.java index 1ae8a95..45b8e70 100644 --- a/src/main/java/io/github/jhale1805/powerarrow/PowerArrowRegistry.java +++ b/src/main/java/io/github/jhale1805/powerarrow/PowerArrowRegistry.java @@ -7,6 +7,7 @@ public class PowerArrowRegistry { private static PowerArrow[] powerArrows = { new ExplosiveArrow(), new ForestFireArrow(), + new JailArrow(), new NetArrow(), new SniperArrow(), new SwapArrow(), diff --git a/src/main/java/io/github/jhale1805/util/BlockTools.java b/src/main/java/io/github/jhale1805/util/BlockTools.java index 6d535b6..73d3b70 100644 --- a/src/main/java/io/github/jhale1805/util/BlockTools.java +++ b/src/main/java/io/github/jhale1805/util/BlockTools.java @@ -16,7 +16,6 @@ public class BlockTools { if (newMaterial == null) throw new IllegalArgumentException("You must specify a newMaterial!"); if (loc1 == null || loc2 == null) throw new IllegalArgumentException("You must specify a complete bounding box!"); if (!loc1.getWorld().equals(loc2.getWorld())) throw new IllegalArgumentException("loc1 and loc2 must be in the same world!"); - // Replace the blocks for (Integer x : new Incrementor(loc1.getBlockX(), loc2.getBlockX())) { for (Integer y : new Incrementor(loc1.getBlockY(), loc2.getBlockY())) { diff --git a/src/main/java/io/github/jhale1805/util/Incrementor.java b/src/main/java/io/github/jhale1805/util/Incrementor.java index cd88ef8..e2ad1e4 100644 --- a/src/main/java/io/github/jhale1805/util/Incrementor.java +++ b/src/main/java/io/github/jhale1805/util/Incrementor.java @@ -24,8 +24,8 @@ public class Incrementor implements Iterable { @Override public boolean hasNext() { - if (start < end) return current - end > 1; - else if (start > end) return end - current > 1; + if (start <= end) return current <= end; + else if (start >= end) return current >= end; else return false; } @@ -33,8 +33,8 @@ public class Incrementor implements Iterable { public Integer next() { int out = current; - if (current < end) current++; - else if (current > end) current--; + if (start <= end) current++; + else if (start >= end) current--; else; // Nothing to increment towards. return out; diff --git a/src/main/java/io/github/jhale1805/util/LocationTools.java b/src/main/java/io/github/jhale1805/util/LocationTools.java index 4a588a0..3801415 100644 --- a/src/main/java/io/github/jhale1805/util/LocationTools.java +++ b/src/main/java/io/github/jhale1805/util/LocationTools.java @@ -20,4 +20,54 @@ public class LocationTools { } } + /** + * Creates a new location based at the block coordinates of the + * given location. + * @param loc The base location. + * @return a new location based at the block coordinates of the + * given location. + */ + public static Location blockCoordsOf(Location loc) { + Location out = new Location( + loc.getWorld(), + loc.getBlockX(), + loc.getBlockY(), + loc.getBlockZ() + ); + return out; + } + + /** + * Creates a new location at the base coordinates plus the given + * coordinate deltas. + * + * @param base The base lcoation. + * @param dx The change in x to apply to the new location. + * @param dy The change in y to apply to the new location. + * @param dz The change in z to apply to the new location. + * @return A new location translated from the given base by the given deltas + */ + public static Location addImmutable(Location base, double dx, double dy, double dz) { + Location out = new Location( + base.getWorld(), + base.getX() + dx, + base.getY() + dy, + base.getZ() + dz + ); + return out; + } + + /** + * Returns a string representation of the given location's coordinates + * + * @param loc The location to stringify + * @return "[worldName](xCoord, yCoord, zCoord)" + */ + public static String stringify(Location loc) { + return "[" + loc.getWorld().getName() + "]" + + "(" + loc.getX() + ", " + + loc.getY() + ", " + + loc.getZ() + ")"; + } + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 05661ff..477395a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ --- # Denotes the start of this YAML document. https://yaml.org/spec/1.2/spec.html#id2760395 name: PowerProjectile main: io.github.jhale1805.PowerProjectilePlugin -version: 1.7.1 +version: 1.8.0 api-version: 1.16 ... # Denotes the end of this YAML document. https://yaml.org/spec/1.2/spec.html#id2760395 \ No newline at end of file diff --git a/src/test/java/io/github/jhale1805/util/IncrementorTest.java b/src/test/java/io/github/jhale1805/util/IncrementorTest.java new file mode 100644 index 0000000..2e70717 --- /dev/null +++ b/src/test/java/io/github/jhale1805/util/IncrementorTest.java @@ -0,0 +1,66 @@ +package io.github.jhale1805.util; + +import org.junit.*; +import static org.junit.Assert.*; + +public class IncrementorTest { + + @BeforeClass + public static void setUpClass() { + + } + + @Before + public void setUp() { + + } + + @After + public void tearDown() { + + } + + @AfterClass + public static void tearDownClass() { + + } + + @Test + public void test_ascending() { + Incrementor ascTest = new Incrementor(1, 5); + int count = 0; + for(Integer i : ascTest) + count++; + assertEquals(5, count); + } + + @Test + public void test_descending() { + Incrementor descTest = new Incrementor(5, 1); + int count = 0; + for(Integer i : descTest) + count++; + assertEquals(5, count); + } + + @Test + public void test_positiveToNegative() { + Incrementor descTest = new Incrementor(1, -1); + int count = 0; + for(Integer i : descTest) + count++; + assertEquals(3, count); + } + + @Test + public void test_negativeToPositive() { + Incrementor descTest = new Incrementor(-1, 1); + int count = 0; + for(Integer i : descTest) + count++; + assertEquals(3, count); + } + + + +} \ No newline at end of file diff --git a/src/test/java/io/github/jhale1805/util/LocationToolsTest.java b/src/test/java/io/github/jhale1805/util/LocationToolsTest.java new file mode 100644 index 0000000..618b8f8 --- /dev/null +++ b/src/test/java/io/github/jhale1805/util/LocationToolsTest.java @@ -0,0 +1,70 @@ +package io.github.jhale1805.util; + +import org.junit.*; +import static org.junit.Assert.*; + +import static org.mockito.Mockito.*; + +import org.bukkit.Location; +import org.bukkit.World; + +public class LocationToolsTest { + + private World mockWorld = mock(World.class); + private String MOCK_WORLD_NAME = "Test World"; + + @BeforeClass + public static void setUpClass() { + + } + + @Before + public void setUp() { + when(mockWorld.getName()).thenReturn(MOCK_WORLD_NAME); + } + + @After + public void tearDown() { + + } + + @AfterClass + public static void tearDownClass() { + + } + + @Test + public void test_stringify() { + Location testLoc = new Location(mockWorld, 0, 0, 0); + assertEquals("[" + MOCK_WORLD_NAME + "](0.0, 0.0, 0.0)", LocationTools.stringify(testLoc)); + } + + @Test + public void test_blockCoordsOf() { + Location baseLoc = new Location(mockWorld, 0.5, 0.5, 0.5); + Location blockified = LocationTools.blockCoordsOf(baseLoc); + // Ensure the original Location is untouched + assertEquals(0.5, baseLoc.getX(), 0.001); + assertEquals(0.5, baseLoc.getY(), 0.001); + assertEquals(0.5, baseLoc.getZ(), 0.001); + // Ensure the new Location is raw block coordinates. + assertEquals(0, blockified.getX(), 0.001); + assertEquals(0, blockified.getY(), 0.001); + assertEquals(0, blockified.getZ(), 0.001); + } + + @Test + public void test_addImmutable() { + Location baseLoc = new Location(mockWorld, 0.5, 0.5, 0.5); + Location blockified = LocationTools.addImmutable(baseLoc, 1, 1, 1); + // Ensure the original Location is untouched + assertEquals(0.5, baseLoc.getX(), 0.001); + assertEquals(0.5, baseLoc.getY(), 0.001); + assertEquals(0.5, baseLoc.getZ(), 0.001); + // Ensure the new Location is raw block coordinates. + assertEquals(1.5, blockified.getX(), 0.001); + assertEquals(1.5, blockified.getY(), 0.001); + assertEquals(1.5, blockified.getZ(), 0.001); + } + +} \ No newline at end of file