PlayerInteractEvent

Discussion in 'Plugin Development' started by TheGamesHawk2001, Nov 22, 2014.

Thread Status:
Not open for further replies.
  1. Hello, I have a custom recipe thingy and when They craft something using the craft recipe it gives them an iron ingot with a name. how would I make it so if a player right clicks the iron ingot with that name it does something.

    Code :

    Code:java
    1. ItemStack outcome = new ItemStack(Material.IRON_INGOT, 1);
    2. ItemMeta outcomemeta = outcome.getItemMeta();
    3. outcomemeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "ingot");
    4. outcome.setItemMeta(outcomemeta);
    5.  
    6. ShapedRecipe IronIngot = new ShapedRecipe(outcome);
    7. IronIngot.shape("ISI","BBB","SIS");
    8.  
    9. IronIngot.setIngredient('I', Material.INK_SACK);
    10. IronIngot.setIngredient('S', Material.SUGAR);
    11. IronIngot.setIngredient('B', Material.GLASS_BOTTLE);
    12.  
    13. getServer().addRecipe(IronIngot);
    14. }
    15.  
     
  2. Offline

    Avygeil

    TheGamesHawk2001 You mean do something if you right click holding your custom crafted ingot? Listen for PlayerInteractEvent and check for your desired Action. Or maybe you mean right click the ingot while it's still in the crafting inventory?

    EDIT: Didn't see you figured out the event part. Check for the item your player is holding, and check its name. :)
     
  3. Offline

    AoH_Ruthless

    You already have figured out that you require the PlayerInteractEvent. Look at the Javadocs or the Bukkit wiki for help in creating your events. You just need to listen for the Right click block/air action and then execute what you want to happen.
     
  4. Offline

    ChintziSecilMC

    TheGamesHawk2001
    Try this:
    On this example i am making it when you right click it, it will shoot a snowball

    Code:java
    1. ItemStack ironIngotGadget = new ItemStack(Material.IRON_INGOT);
    2. ItemMeta ironIngotGadgetMeta = ironIngotGadget.getItemMeta();
    3.  
    4. ironIngotGadgetMeta.setDisplayName(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Whatever you want to call it");
    5. ironIngotGadget.setItemMeta(ironIngotGadgetMeta);
    6.  
    7. //Action
    8. if (player.getItemInHand().getType().equals(Material.IRON_INGOT)) {
    9. if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "What ever the name was for ironGadgetMeta.setDisplayName")) {
    10. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    11. player.launchProjectile(Snowball.class);
    12. }
    13. }
    14. }
     
  5. ChintziSecilMC Thx. Can you explain what the toString bit means? Haven't really used that much..
     
  6. Offline

    ChintziSecilMC

    TheGamesHawk2001
    Oh don't worry about that, it just prevents this yellow line thing coming up, You only have to use it if your plusing ChatColor.<color> to something thats ChatColor but isn't a color example ChatColor.MAGIC
    By the way did it work?
     
  7. ChintziSecilMC I haven't tested it yet :) I will do it now and reply if it is working :)
     
  8. Offline

    ChintziSecilMC

  9. ChintziSecilMC

    Code:java
    1. ItemStack outcome = new ItemStack(Material.EXP_BOTTLE, 1);
    2. ItemMeta outcomemeta = outcome.getItemMeta();
    3. outcomemeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "BOTTLE");
    4. outcome.setItemMeta(outcomemeta);
    5.  
    6. ShapedRecipe expBottle = new ShapedRecipe(outcome);
    7. expBottle.shape("ISI","BBB","SIS");
    8.  
    9. expBottle.setIngredient('I', Material.INK_SACK);
    10. expBottle.setIngredient('S', Material.SUGAR);
    11. expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    12.  
    13. getServer().addRecipe(expBottle);
    14. }
    15.  
    16. @EventHandler
    17. public void onInteract(PlayerInteractEvent e) {
    18. Player p = e.getPlayer();
    19. if (p.getItemInHand().getType().equals(Material.EXP_BOTTLE)) {
    20. if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "outcomemeta")) {
    21. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    22. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 120, 1));
    23. }
    24. }
    25. }
    26. }
    27. }


    Would this mean if they right click the expbottle with the outcomemeta it will give them strength / increase damage potion?
     
  10. Offline

    ChintziSecilMC

    TheGamesHawk2001
    yes btw if you want to get the item all you have to do is make a command or something doing that
    for example you could have:

    Code:java
    1. ItemStack outcome = new ItemStack(Material.EXP_BOTTLE, 1);
    2. ItemMeta outcomemeta = outcome.getItemMeta();
    3. outcomemeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "BOTTLE");
    4. outcome.setItemMeta(outcomemeta);
    5.  
    6. if (commandLabel.equalsIgnoreCase("givemebottle")) {
    7. if sender instanceof player {
    8. Player player = (Player) sender;
    9. sender.sendMessage(ChatColor.AQUA + "You received an Exp Bottle!");
    10. player.getInventory.addItem(outcome);
    11. player.updateInventory;
    12. }
    13. }
     
  11. ChintziSecilMC how would I add a recipe in a main class ?

    Cus I have more than 1 class. I want to put this in my main class getServer().addRecipe(expBottle); I dont know where to put it cus I tried putting it under onEnable() and it didnt work..
     
  12. Offline

    ChintziSecilMC

    TheGamesHawk2001
    Hmm it should of try this, this code is exactly what i use for mine and it works just replace the names of my item with the name of yours

    Code:java
    1.  
    2. @Override
    3. public void onEnable() {
    4. ItemStack breaker = new ItemStack(Material.STICK, 1);
    5. ItemMeta breakerMeta = breaker.getItemMeta();
    6. ArrayList<String> breakerLore = new ArrayList<String>();
    7. breakerMeta.setDisplayName(ChatColor.BLUE + "Breaker");
    8. breakerLore.add(ChatColor.AQUA + "Left click to break across");
    9. breakerLore.add(ChatColor.AQUA + "Right click to break down");
    10. breakerMeta.setLore(breakerLore);;
    11. breaker.setItemMeta(breakerMeta);
    12.  
    13. ShapedRecipe breakerRecipe = new ShapedRecipe(breaker);
    14. breakerRecipe.shape("STS", "TDT", "BTB");
    15.  
    16. breakerRecipe.setIngredient('T', Material.TNT);
    17. breakerRecipe.setIngredient('D', Material.DIAMOND_PICKAXE);
    18. breakerRecipe.setIngredient('B', Material.BLAZE_POWDER);
    19. breakerRecipe.setIngredient('S', Material.STONE);
    20. getServer().addRecipe((Recipe) breakerRecipe);
    21. //Then what ever else you have in your onEnable()
     
  13. ChintziSecilMC idk but I have an issue, when I right click the bottle it just throws it and doesnt give me potions..
     
  14. Offline

    ChintziSecilMC

    TheGamesHawk2001
    Hmm ok, what you will need to do is cancel the default methods that the Exp Potion has. And are you right clicking on the ground cause if so click in the air. (Action.RIGHT_CLICK_AIR).
    And if this doesnt work just remove the cause i wasnt 100% sure that that would work :/
    Code:java
    1. if (p.getItemInHand().getType().equals(Material.EXP_BOTTLE)) {
     
  15. ChintziSecilMC Hmmz I'll check tomoz cus I'm n my phone atm. And I'll also change the item to a iron ingot and see if that works
     
  16. Offline

    ChintziSecilMC

  17. Offline

    migcabreraes

Thread Status:
Not open for further replies.

Share This Page