hey guys I have been working on a throwing knife plugin for a friend and I have given a stick and a blaze rod custom names and lore. I set up an event handler to fire a snowball when ONLY the stick or blaze rod with custom names are interacted with. Here is my code Code:java package me.OmDalvi.ThrowingKnives; import java.util.ArrayList;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Effect;import org.bukkit.Material;import org.bukkit.enchantments.Enchantment;import org.bukkit.entity.*;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.ShapedRecipe;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin implements Listener { ItemStack knive = new ItemStack(Material.STICK, 16);ItemStack fireKnive = new ItemStack(Material.BLAZE_ROD, 16); @Overridepublic void onEnable(){ //Meta DataItemMeta fireKniveMeta = fireKnive.getItemMeta();//loreList<String> Firelore = new ArrayList<String>();Firelore.add(ChatColor.GOLD +"This is a disguised throwing knife.");Firelore.add(ChatColor.GREEN +"Boom, Boom, Fia Powa!!!");Firelore.add(ChatColor.BLUE +"+ ∞ damage");fireKniveMeta.setLore(Firelore);//lore end//renamefireKniveMeta.setDisplayName(ChatColor.RED +"Throwing" +ChatColor.BLUE +"Knive");//rename ends//enchantsfireKniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);fireKniveMeta.addEnchant(Enchantment.FIRE_ASPECT, 100, true);fireKniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);fireKniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 100, true);//enchants endknive.setItemMeta(fireKniveMeta);//Meta Data end//RecipeShapedRecipe fireKniveRecipe = new ShapedRecipe(knive);fireKniveRecipe.shape("###","#I#","#S#");fireKniveRecipe.setIngredient('I', Material.IRON_INGOT);fireKniveRecipe.setIngredient('S', Material.BLAZE_ROD);//Recipe End //Meta DataItemMeta kniveMeta = knive.getItemMeta();//loreList<String> lore = new ArrayList<String>();lore.add(ChatColor.GOLD +"This is a disguised throwing knife.");lore.add(ChatColor.GREEN +":) :) :) :) :) :) :) :) :) :) :)");lore.add(ChatColor.BLUE +"+ ∞ damage");kniveMeta.setLore(lore);//lore end//renamekniveMeta.setDisplayName(ChatColor.AQUA +"Throwing" +ChatColor.BLUE +"Knive");//rename ends//enchantskniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);kniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);kniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 100, true);//enchants endknive.setItemMeta(kniveMeta);//Meta Data end//RecipeShapedRecipe kniveRecipe = new ShapedRecipe(knive);kniveRecipe.shape("###","#I#","#S#");kniveRecipe.setIngredient('I', Material.IRON_INGOT);kniveRecipe.setIngredient('S', Material.STICK);//Recipe EndBukkit.getServer().addRecipe(kniveRecipe); //EventsBukkit.getServer().getPluginManager().registerEvents(this, this);//Events end } @Overridepublic void onDisable(){Bukkit.getServer().clearRecipes();} @EventHandlerpublic void onPlayerInteract(final PlayerInteractEvent e) {if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;if(e.getItem().getType() == Material.BLAZE_ROD){Snowball s = e.getPlayer().launchProjectile(Snowball.class);s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);}else if(e.getItem().getType() == Material.STICK){Snowball s = e.getPlayer().launchProjectile(Snowball.class);s.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 10);}} } Plz help as my friend wants it done in a week!! Bye!
You shouldn't use a global ItemStack. Try creating a separate ItemStack variable for every recipe inside the onEnable() function instead of defining it once as a global.
He He He... Sorry I was in a rush but what I am trying to have is the event handler (I think that is what its called) sense the colored text then shoot a snowball I also have another problem. I try to upload this to my server but it will not be sensed!!! Here is my Plugin.yml Code: name: ThrowingKnives version: 1.4.1 main: com.live.Peter_Higgs.Main commands: beardify: description: Puts a spiky beard on the specified players head. usage: /<command> [player] Main Class Code:java package com.live.ThrowingKnives; import java.util.ArrayList;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.enchantments.Enchantment;import org.bukkit.entity.Player;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.PlayerInventory;import org.bukkit.inventory.ShapedRecipe;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin{ ItemStack knive = new ItemStack(Material.STICK, 16);ItemStack fireKnive = new ItemStack(Material.BLAZE_ROD, 16);ItemStack head = new ItemStack(Material.CACTUS); @Overridepublic void onEnable(){ //Meta DataItemMeta fireKniveMeta = fireKnive.getItemMeta();//loreList<String> fireLore = new ArrayList<String>();fireLore.add(ChatColor.GOLD +"This is a disguised throwing knife.");fireLore.add(ChatColor.RED +"Boom, Boom, Fia Powa!!!");fireLore.add(ChatColor.BLUE +"+ ∞ damage");fireKniveMeta.setLore(fireLore);//lore end//renamefireKniveMeta.setDisplayName(ChatColor.RED +"Throwing " +ChatColor.BLUE +"Knive");//rename ends//enchantsfireKniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);fireKniveMeta.addEnchant(Enchantment.FIRE_ASPECT, 100, true);fireKniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);fireKniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);//enchants endfireKnive.setItemMeta(fireKniveMeta);//Meta Data end//RecipeShapedRecipe fireKniveRecipe = new ShapedRecipe(fireKnive);fireKniveRecipe.shape("BBB","RIR","PSP");fireKniveRecipe.setIngredient('P', Material.POWERED_RAIL);fireKniveRecipe.setIngredient('R', Material.REDSTONE);fireKniveRecipe.setIngredient('B', Material.BLAZE_POWDER);fireKniveRecipe.setIngredient('I', Material.IRON_INGOT);fireKniveRecipe.setIngredient('S', Material.BLAZE_ROD);//Recipe End //Meta DataItemMeta kniveMeta = knive.getItemMeta();//loreList<String> lore = new ArrayList<String>();lore.add(ChatColor.GOLD +"This is a disguised throwing knife.");lore.add(ChatColor.GREEN +":) :) :) :) :) :) :) :) :) :) :)");lore.add(ChatColor.BLUE +"+ ∞ damage");kniveMeta.setLore(lore);//lore end//renamekniveMeta.setDisplayName(ChatColor.AQUA +"Throwing" +ChatColor.BLUE +"Knive");//rename ends//enchantskniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);kniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);kniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);//enchants endknive.setItemMeta(kniveMeta);//Meta Data end//RecipeShapedRecipe kniveRecipe = new ShapedRecipe(knive);kniveRecipe.shape("###","RIR","PSP");kniveRecipe.setIngredient('P', Material.POWERED_RAIL);kniveRecipe.setIngredient('R', Material.REDSTONE);kniveRecipe.setIngredient('I', Material.IRON_INGOT);kniveRecipe.setIngredient('S', Material.STICK);//Recipe EndBukkit.getServer().addRecipe(kniveRecipe);Bukkit.getServer().addRecipe(fireKniveRecipe); } @Overridepublic void onDisable(){Bukkit.getServer().clearRecipes();}public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ if(sender instanceof Player){//beardif(cmd.getName().equalsIgnoreCase("beardify")){Player p = (Player)sender;if(args.length == 1){ Player target = (Bukkit.getServer().getPlayer(args[0]));if (target == null) {p.sendMessage(args[0] + " is not online!");return false;}else{PlayerInventory TI =target.getInventory();TI.setHelmet(head);} }else{p.sendMessage("Wrong amount of arguments");}//beard ends }}return false;} } I had taken out the event handler as the plugin didnt work What exactly will this do? I try them all inside onEnable, but then took out as now It wont load!!! Maybe if you could copy my code than try it out yourself, then I could know if it works, but till then I am not sure I will keep updating my code so if there are any changes I should do (Or shouldn't have done) I can know. Code: package com.live.ThrowingKnives; import java.util.ArrayList; import java.util.List; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin{ ItemStack knive = new ItemStack(Material.STICK, 16); ItemStack fireKnive = new ItemStack(Material.BLAZE_ROD, 16); ItemStack head = new ItemStack(Material.CACTUS); @Override public void onEnable(){ //Meta Data ItemMeta fireKniveMeta = fireKnive.getItemMeta(); //lore List<String> fireLore = new ArrayList<String>(); fireLore.add(ChatColor.GOLD +"This is a disguised throwing knife."); fireLore.add(ChatColor.RED +"Boom, Boom, Fia Powa!!!"); fireLore.add(ChatColor.BLUE +"+ ∞ damage"); fireKniveMeta.setLore(fireLore); //lore end //rename fireKniveMeta.setDisplayName(ChatColor.RED +"Throwing " +ChatColor.BLUE +"Knive"); //rename ends //enchants fireKniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true); fireKniveMeta.addEnchant(Enchantment.FIRE_ASPECT, 100, true); fireKniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true); fireKniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true); //enchants end fireKnive.setItemMeta(fireKniveMeta); //Meta Data end //Recipe ShapedRecipe fireKniveRecipe = new ShapedRecipe(fireKnive); fireKniveRecipe.shape("BBB","RIR","PSP"); fireKniveRecipe.setIngredient('P', Material.POWERED_RAIL); fireKniveRecipe.setIngredient('R', Material.REDSTONE); fireKniveRecipe.setIngredient('B', Material.BLAZE_POWDER); fireKniveRecipe.setIngredient('I', Material.IRON_INGOT); fireKniveRecipe.setIngredient('S', Material.BLAZE_ROD); //Recipe End //Meta Data ItemMeta kniveMeta = knive.getItemMeta(); //lore List<String> lore = new ArrayList<String>(); lore.add(ChatColor.GOLD +"This is a disguised throwing knife."); lore.add(ChatColor.GREEN +":) :) :) :) :) :) :) :) :) :) :)"); lore.add(ChatColor.BLUE +"+ ∞ damage"); kniveMeta.setLore(lore); //lore end //rename kniveMeta.setDisplayName(ChatColor.AQUA +"Throwing" +ChatColor.BLUE +"Knive"); //rename ends //enchants kniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true); kniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true); kniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true); //enchants end knive.setItemMeta(kniveMeta); //Meta Data end //Recipe ShapedRecipe kniveRecipe = new ShapedRecipe(knive); kniveRecipe.shape("###","RIR","PSP"); kniveRecipe.setIngredient('P', Material.POWERED_RAIL); kniveRecipe.setIngredient('R', Material.REDSTONE); kniveRecipe.setIngredient('I', Material.IRON_INGOT); kniveRecipe.setIngredient('S', Material.STICK); //Recipe End Bukkit.getServer().addRecipe(kniveRecipe); Bukkit.getServer().addRecipe(fireKniveRecipe); } @Override public void onDisable(){ Bukkit.getServer().clearRecipes(); } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Nonono. Go like this: Code: ItemStack knive; ItemStack fireKnive; @Override public void onEnable(){ knive = new ItemStack(etc); fireKnive = new ItemStack(etc); } Otherwise they wont load (in your main class) EDIT: Sorry for crappy formatting on that code
So after i do what u said i would do this right - Code:java public void onPlayerInteractEvent(PlayerInteractEvent e){ if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){ //code here } OK so thanks to my looking over my code I found what was wrong with my plugin.yml and now it works. Now I need an answer to the main question. HOW TO MAKE A STICK* FIRE A SNOWBALL!! Currently my code is Code:java if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){ if(e.getMaterial().equals(fireKnive)){ Player p = e.getPlayer(); } for the event handler. Plz help on how to A) shoot a snowball from a stick* B) take away 1 stick* per shot C) have the snowball give potion effects on whoever/whatever it hits *Stick or Blaze Rod EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Code: Fireball f = p.launchProjectile(Fireball.class); ItemStack is = p.getItemInHand(); int amount = is.getAmount(); if(p.getItemInHand().getAmount() > 1)p.getItemInHand().setAmount(amount - 1);//remove 1 stick if he has more that 1 else{is.setType(Material.AIR);}//If he only has 1 stick, it will remove the itemstack he cant have 0 sticks //NOW: we gotta store the projectile in an arraylist that you put OUTSIDE this method ArrayList<Projectile> projectiles = new ArrayList<Projectile>();//THIS SHOULD BE DONE SOMEWHERE ELSE projectiles.add(f); } @EventHandler public void onEntityDamageByEntity(EntityDamageByEntityEvent event){ if(!(event.getEntity() instanceof Player))return; if(!(event.getDamager() instanceof Projectile))return; Player hitplayer = (Player) event.getEntity(); Projectile p = (Projectile) event.getDamager(); if(!projectiles.contains(p))return; hitplayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20/*1 second = 20*/, 1)); projectiles.remove(p); }
Can I make the fire knife cach someone on fire? Whenever I add ArrayList<Projectile> projectiles = new ArrayList<Projectile>(); projectiles.add(f); it has an error saying that f is not a variable even though I have it set up in the onPlayerInteractEvent. Plz help!! EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Peter_Higgs f is a variable defined only for that event.... You need to globalize it to use the same variable across events.
The ArrayList<Projectile> projectiles; has to be outside of the event, so above lets say the onEnable() Then in the onEnable() put projectiles = new ArrayList<Projectile>
O.K guys i have done what u have recommended and now the stick still wont fire snowballs!!! here is my code- Code:java package com.live.ThrowingKnives; import java.util.ArrayList;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.enchantments.Enchantment;import org.bukkit.entity.Player;import org.bukkit.entity.Projectile;import org.bukkit.entity.Snowball;import org.bukkit.event.block.Action;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.ShapedRecipe;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin{ ItemStack knive;ItemStack fireKnive;ArrayList<Projectile> projectiles;Snowball s; @Overridepublic void onEnable(){ projectiles = new ArrayList<Projectile>(); fireKnive = new ItemStack(Material.BLAZE_ROD, 16);knive = new ItemStack(Material.STICK, 1); //Meta DataItemMeta fireKniveMeta = fireKnive.getItemMeta();//loreList<String> fireLore = new ArrayList<String>();fireLore.add(ChatColor.GOLD +"This is a disguised throwing knife.");fireLore.add(ChatColor.RED +"Boom, Boom, Fia Powa!!!");fireLore.add(ChatColor.BLUE +"+ ∞ damage");fireKniveMeta.setLore(fireLore);//lore end//renamefireKniveMeta.setDisplayName(ChatColor.RED +"Throwing " +ChatColor.BLUE +" Knive");//rename ends//enchantsfireKniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);fireKniveMeta.addEnchant(Enchantment.FIRE_ASPECT, 100, true);fireKniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);fireKniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);//enchants endfireKnive.setItemMeta(fireKniveMeta);//Meta Data end//RecipeShapedRecipe fireKniveRecipe = new ShapedRecipe(fireKnive);fireKniveRecipe.shape("BBB","RIR","PSP");fireKniveRecipe.setIngredient('P', Material.POWERED_RAIL);fireKniveRecipe.setIngredient('R', Material.REDSTONE);fireKniveRecipe.setIngredient('B', Material.BLAZE_POWDER);fireKniveRecipe.setIngredient('I', Material.IRON_INGOT);fireKniveRecipe.setIngredient('S', Material.BLAZE_ROD);//Recipe End //Meta DataItemMeta kniveMeta = knive.getItemMeta();//loreList<String> lore = new ArrayList<String>();lore.add(ChatColor.GOLD +"This is a disguised throwing knife.");lore.add(ChatColor.GREEN +":) :) :) :) :) :) :) :) :) :) :)");lore.add(ChatColor.BLUE +"+ ∞ damage");kniveMeta.setLore(lore);//lore end//renamekniveMeta.setDisplayName(ChatColor.AQUA +"Throwing" +ChatColor.BLUE +" Knive");//rename ends//enchantskniveMeta.addEnchant(Enchantment.KNOCKBACK, 100, true);kniveMeta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 100, true);kniveMeta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);//enchants endknive.setItemMeta(kniveMeta);//Meta Data end//RecipeShapedRecipe kniveRecipe = new ShapedRecipe(knive);kniveRecipe.shape("###","RIR","PSP");kniveRecipe.setIngredient('P', Material.POWERED_RAIL);kniveRecipe.setIngredient('R', Material.REDSTONE);kniveRecipe.setIngredient('I', Material.IRON_INGOT);kniveRecipe.setIngredient('S', Material.STICK);//Recipe EndBukkit.getServer().addRecipe(kniveRecipe);Bukkit.getServer().addRecipe(fireKniveRecipe); } @Overridepublic void onDisable(){ Bukkit.getServer().clearRecipes();} public void onPlayerInteractEvent(PlayerInteractEvent e){ if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){if(e.getMaterial().equals(fireKnive)){Player p = e.getPlayer();fs= p.launchProjectile(Snowball.class);ItemStack is = p.getItemInHand();int amount = is.getAmount();if(p.getItemInHand().getAmount() > 1)p.getItemInHand().setAmount(amount - 1);else{is.setType(Material.AIR);}} projectiles.add(s); } } }