diff --git a/src/main/java/io/github/jhale1805/torcharrow/TorchArrow.java b/src/main/java/io/github/jhale1805/torcharrow/TorchArrow.java new file mode 100644 index 0000000..bb07cb0 --- /dev/null +++ b/src/main/java/io/github/jhale1805/torcharrow/TorchArrow.java @@ -0,0 +1,54 @@ +package io.github.jhale1805.torcharrow; + +import java.util.ArrayList; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class TorchArrow extends ItemStack { + + private final static String DISPLAY_NAME = ChatColor.RED + "Torch Arrow"; + + public TorchArrow() { + super(Material.ARROW); + setMetadata(); + } + + public TorchArrow(int count) { + this(); + this.setAmount(count); + } + + private void setMetadata() { + ItemMeta metadata = this.getItemMeta(); + metadata.setDisplayName(DISPLAY_NAME); + + var lore = new ArrayList(); + lore.add("Shoot with Flame to"); + lore.add("place a torch"); + metadata.setLore(lore); + + this.setItemMeta(metadata); + } + + @Override + public boolean isSimilar(ItemStack stack) { + return stack.getItemMeta().equals(this.getItemMeta()); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TorchArrow)) { + return false; + } + + if (((TorchArrow) obj).getAmount() == this.getAmount()) { + return true; + } else { + return false; + } + } + +} diff --git a/src/main/java/io/github/jhale1805/torcharrow/TorchArrowListener.java b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowListener.java index add5a81..d606a6d 100644 --- a/src/main/java/io/github/jhale1805/torcharrow/TorchArrowListener.java +++ b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowListener.java @@ -1,40 +1,85 @@ package io.github.jhale1805.torcharrow; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; import org.bukkit.entity.Arrow; -import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityShootBowEvent; import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerRecipeDiscoverEvent; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; +import org.bukkit.plugin.java.JavaPlugin; public class TorchArrowListener implements Listener { + + private JavaPlugin plugin; + private MetadataValue torchArrowMetadata; + + public TorchArrowListener(JavaPlugin plugin) { + this.plugin = plugin; + this.torchArrowMetadata = new FixedMetadataValue(plugin, "torch-arrow"); + } @EventHandler public void onProjectileHit(ProjectileHitEvent event) { - Projectile projectile = event.getEntity(); - if (projectile instanceof Arrow && ((Arrow) projectile).getFireTicks() > 0) { - Block hitBlock = event.getHitBlock(); - BlockFace hitBlockFace = event.getHitBlockFace(); - if (hitBlock != null) { - Block adj = hitBlock.getWorld().getBlockAt( - hitBlock.getX() + hitBlockFace.getModX(), - hitBlock.getY() + hitBlockFace.getModY(), - hitBlock.getZ() + hitBlockFace.getModZ() - ); - if (!hitBlockFace.equals(BlockFace.DOWN)) { - BlockData torch = Material.TORCH.createBlockData(); - if (!hitBlockFace.equals(BlockFace.UP)) { - torch = Material.WALL_TORCH.createBlockData(); - ((Directional) torch).setFacing(hitBlockFace); - } - adj.setBlockData(torch); - projectile.remove(); - } - } + // Check preconditions for torch conversion + if (!(event.getEntity() instanceof Arrow + && event.getEntity().getFireTicks() > 0 + && event.getEntity().getMetadata("effect").contains(torchArrowMetadata) + && event.getHitBlock() != null + && event.getHitBlockFace() != null + && !event.getHitBlockFace().equals(BlockFace.DOWN))) + return; + + // Get location of block in which to place the torch. + Block hitBlock = event.getHitBlock(); + BlockFace hitBlockFace = event.getHitBlockFace(); + Block adj = hitBlock.getWorld().getBlockAt( + hitBlock.getX() + hitBlockFace.getModX(), + hitBlock.getY() + hitBlockFace.getModY(), + hitBlock.getZ() + hitBlockFace.getModZ() + ); + + // Compute torch orientation + BlockData torch = Material.TORCH.createBlockData(); + if (!hitBlockFace.equals(BlockFace.UP)) { + torch = Material.WALL_TORCH.createBlockData(); + ((Directional) torch).setFacing(hitBlockFace); + } + + // Convert arrow to torch + adj.setBlockData(torch); + event.getEntity().remove(); + + } + + @EventHandler + public void onEntityShootBow(EntityShootBowEvent event) { + if (event.getConsumable().getItemMeta().equals(new TorchArrow().getItemMeta())) { + event.getProjectile().setMetadata("effect", torchArrowMetadata); + } + } + + @EventHandler + public void onPlayerRecipieDiscover(PlayerRecipeDiscoverEvent event) { + // Show the Torch Arrow in the recipie book upon discovering torches. + if(event.getRecipe().getNamespace().equals("minecraft:torch")) { + event.getPlayer().discoverRecipe(new TorchArrowRecipie(plugin).getKey()); + } + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + // Show the Torch Arrow in the recipie book to those that have discovered torches. + if (event.getPlayer().hasDiscoveredRecipe(NamespacedKey.fromString("minecraft:torch"))) { + event.getPlayer().discoverRecipe(new TorchArrowRecipie(plugin).getKey()); } } diff --git a/src/main/java/io/github/jhale1805/torcharrow/TorchArrowPlugin.java b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowPlugin.java index 8ff1b92..6afd7ec 100644 --- a/src/main/java/io/github/jhale1805/torcharrow/TorchArrowPlugin.java +++ b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowPlugin.java @@ -1,17 +1,22 @@ package io.github.jhale1805.torcharrow; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; public class TorchArrowPlugin extends JavaPlugin { @Override public void onEnable() { - System.out.println("You've discovered torch arrows!"); - getServer().getPluginManager().registerEvents(new TorchArrowListener(), this); + getLogger().info("Copyright 2021 by Joseph Hale (jhale1805). All Rights Reserved."); + getLogger().info("Report issues to https://github.com/jhale1805/torch-arrow"); + getLogger().info("Registering Torch Arrow event listeners."); + getServer().getPluginManager().registerEvents(new TorchArrowListener(this), this); + getLogger().info("Adding crafting recipie for Torch Arrows."); + Bukkit.addRecipe(new TorchArrowRecipie(this)); } @Override public void onDisable() { - System.out.println("Thank's for using torch arrows!"); + getLogger().info("Thanks for using Torch Arrow!"); } } diff --git a/src/main/java/io/github/jhale1805/torcharrow/TorchArrowRecipie.java b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowRecipie.java new file mode 100644 index 0000000..7175e16 --- /dev/null +++ b/src/main/java/io/github/jhale1805/torcharrow/TorchArrowRecipie.java @@ -0,0 +1,42 @@ +package io.github.jhale1805.torcharrow; + +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.plugin.Plugin; + +public class TorchArrowRecipie extends ShapedRecipe { + + public static final String NAMESPACED_KEY = "torch_arrow"; + + /** + * The recipie for crafting torch arrows. + * + * This recipie yields 4 torch arrows, enough to generate the same number + * of torches that the single piece of coal could be used to craft. + * + * ------------- + * | | A | | + * ------------- + * | A | C | A | + * ------------- + * | | A | | + * ------------- + * + * @param plugin The Torch Arrow plugin. Required for associating this + * recipie with the plugin. + */ + public TorchArrowRecipie(Plugin plugin) { + super(new NamespacedKey(plugin, NAMESPACED_KEY), new TorchArrow()); + shape(" A ", "ACA", " A "); + setIngredient('C', Material.COAL); + setIngredient('A', Material.ARROW); + } + + @Override + public ItemStack getResult() { + return new TorchArrow(4); + } + +}