From f9b4d9abe4dd1ad971fc40a8489cc67c98f1c32d Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Wed, 31 Aug 2016 08:33:45 -0400 Subject: [PATCH] Created a Conflict Compass Crafting listener --- .../ConflictCompassCraftingListener.java | 56 ++++++++++++++++++- .../listeners/DiamondArmorBlocker.java | 3 +- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/alisolarflare/listeners/ConflictCompassCraftingListener.java b/src/alisolarflare/listeners/ConflictCompassCraftingListener.java index e7f1f58..ee2c355 100644 --- a/src/alisolarflare/listeners/ConflictCompassCraftingListener.java +++ b/src/alisolarflare/listeners/ConflictCompassCraftingListener.java @@ -1,14 +1,68 @@ package alisolarflare.listeners; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.CraftItemEvent; +import org.bukkit.inventory.ItemStack; public class ConflictCompassCraftingListener implements Listener{ - + @EventHandler public boolean onConflictCompassCraft(CraftItemEvent event){ + //SANITATION - HARDCORE + if(event.getWhoClicked().getWorld().getName() != "hardcore"){ + return false; + } + //INIT - targetItem + ItemStack targetItem = event.getRecipe().getResult(); + + //SANITATION - NOT COMPASS + if(targetItem.getType() != Material.COMPASS){ + return false; + } + + event.setCancelled(true); + //GIVE - chainmail chestplate + event.getWhoClicked().getInventory().addItem(generateConflictCompass(event.getWhoClicked())); + //PLAY - cave sound + event.getWhoClicked().getWorld().playSound(event.getWhoClicked().getLocation(), Sound.AMBIENT_CAVE,0,0); return false; } + + private ItemStack generateConflictCompass(HumanEntity crafter) { + ItemStack conflictCompass = new ItemStack(Material.COMPASS); + conflictCompass.addUnsafeEnchantment(Enchantment.DURABILITY, 1); + List loreString = new ArrayList(); + loreString.add("The needle is tipped with the scent of"); + + loreString.add(nearestPlayerName(crafter)); + conflictCompass.getItemMeta().setLore(loreString); + return null; + } + + private String nearestPlayerName(HumanEntity crafter) { + Player nearestPlayer = null; + for(Player player: crafter.getWorld().getPlayers()){ + if (player.getUniqueId() == crafter.getUniqueId()){ + //SKIP CODE + }if (nearestPlayer == null){ + nearestPlayer = player; + }else if (nearestPlayer.getLocation().distance(crafter.getLocation()) > player.getLocation().distance(crafter.getLocation())){ + nearestPlayer = player; + } + } + if(nearestPlayer == null){ + return "METAL"; + } + return nearestPlayer.toString(); + } } diff --git a/src/alisolarflare/listeners/DiamondArmorBlocker.java b/src/alisolarflare/listeners/DiamondArmorBlocker.java index 842f307..277dd36 100644 --- a/src/alisolarflare/listeners/DiamondArmorBlocker.java +++ b/src/alisolarflare/listeners/DiamondArmorBlocker.java @@ -43,7 +43,8 @@ public class DiamondArmorBlocker implements Listener{ //INIT - Chainmail's lore List loreString = new ArrayList(); - loreString.add("This world is forever dangerous. There is no protection here"); + loreString.add("This world is forever dangerous."); + loreString.add("There is no protection here."); failArmor.getItemMeta().setLore(loreString); return failArmor; }