Changing Items On Right Click Event

Discussion in 'Plugin Development' started by yewtree8, Jun 8, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    yewtree8

    Ok, So I have started making this plugin and I am trying to make it so that when you right click the item it switches it. but when i right click the blaze rod, the item doesn't change and it gives me both messages telling me that it is parsing the code. I have tried turning it to boolean and returning it differently but it is switching between the two items, one might work but the other won't.


    Code:
    For Disabling Stacker

    Code:java
    1. package Stacker;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16.  
    17. public class StackerDisable implements Listener {
    18.  
    19. public ItemStack setMeta(ItemStack material, String name , List<String> lore, String string) {
    20. if(((material == null) || material.getType() == Material.AIR) || (name == null && lore == null))
    21.  
    22. return null;
    23.  
    24. ItemMeta im = material.getItemMeta();
    25. if (name != null)
    26. im.setDisplayName(name);
    27. if(lore != null) {
    28. im.setLore(lore);
    29.  
    30. material.setItemMeta(im);
    31. return material;
    32.  
    33. }
    34. return material;
    35. }
    36.  
    37. public List<Player> ds = new ArrayList<Player>();
    38. public List<Player> es = new ArrayList<Player>();
    39.  
    40.  
    41. public ItemStack DisableStacker = setMeta(new ItemStack(Material.BLAZE_ROD), (ChatColor.RED + "Disable Stacker"),
    42. Arrays.asList(ChatColor.GREEN + "Right Click To Disable Stacker"), (ChatColor.GREEN + "Stacker Is Currently Enabled"));
    43.  
    44. public ItemStack EnableStacker = setMeta(new ItemStack(Material.STICK), (ChatColor.GREEN + "Enable Stacker"),
    45. Arrays.asList(ChatColor.GREEN + "Right Click To Enable Stacker"), (ChatColor.GREEN + "Stacker Is Currently " + ChatColor.RED + "Disabled"));
    46.  
    47.  
    48. @EventHandler
    49. public void onStackerDisable(PlayerInteractEvent event) {
    50. Player player = event.getPlayer();
    51. if(player.getItemInHand().equals(DisableStacker) && (event.getAction() == Action.RIGHT_CLICK_AIR)) {
    52. player.getInventory().remove(DisableStacker);
    53. player.updateInventory();
    54. player.getInventory().addItem(EnableStacker);
    55. player.updateInventory();
    56. player.sendMessage(ChatColor.RED + "Stacker Disabled");
    57.  
    58.  
    59. }
    60. return;
    61.  
    62.  
    63.  
    64. }
    65.  
    66.  
    67.  
    68. }
    69.  


    For Enabling Stacker:

    Code:java
    1. package Stacker;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16.  
    17. public class StackerEnable implements Listener {
    18.  
    19.  
    20. public ItemStack setMeta(ItemStack material, String name , List<String> lore, String string) {
    21. if(((material == null) || material.getType() == Material.AIR) || (name == null && lore == null))
    22.  
    23. return null;
    24.  
    25. ItemMeta im = material.getItemMeta();
    26. if (name != null)
    27. im.setDisplayName(name);
    28. if(lore != null) {
    29. im.setLore(lore);
    30.  
    31. material.setItemMeta(im);
    32. return material;
    33.  
    34. }
    35. return material;
    36. }
    37.  
    38. public List<Player> ds = new ArrayList<Player>();
    39. public List<Player> es = new ArrayList<Player>();
    40.  
    41.  
    42. public ItemStack DisableStacker = setMeta(new ItemStack(Material.BLAZE_ROD), (ChatColor.RED + "Disable Stacker"),
    43. Arrays.asList(ChatColor.GREEN + "Right Click To Disable Stacker"), (ChatColor.GREEN + "Stacker Is Currently Enabled"));
    44.  
    45. public ItemStack EnableStacker = setMeta(new ItemStack(Material.STICK), (ChatColor.GREEN + "Enable Stacker"),
    46. Arrays.asList(ChatColor.GREEN + "Right Click To Enable Stacker"), (ChatColor.GREEN + "Stacker Is Currently " + ChatColor.RED + "Disabled"));
    47.  
    48.  
    49. @EventHandler
    50. public void onStackerEnable(PlayerInteractEvent event) {
    51. Player player = event.getPlayer();
    52. if(player.getItemInHand().equals(EnableStacker) && (event.getAction() == Action.RIGHT_CLICK_AIR)) {
    53. player.getInventory().remove(EnableStacker);
    54. player.updateInventory();
    55. player.getInventory().addItem(DisableStacker);
    56. player.updateInventory();
    57. player.sendMessage(ChatColor.GREEN + "Stacker Enabled");
    58.  
    59.  
    60. }
    61.  
    62. return;
    63.  
    64. }
    65.  
    66.  
    67. }
    68.  


    Thanks guys and any help would be appreciated.

    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    mine-care

    Wait, what are you trying to archive? You want to change a item or its name and item meta? And what happens with blazerod?
     
  3. I would probably try and do something more like this:

    on the event:
    if (player.getinventory.getItemInHand.getItemMeta.getName.equals("WhatEver") {
    player.getinventory.getitemeinhand.getitemmeta.setname("Other");
    }
    else if (player.getinventory.getiteminhand.getitemmeta.getname.equals("Other") {
    player.getinventory.getiteminhand.getitemmeta.setname("WhatEver") {
    }
     
  4. Offline

    pcgamers123

    Try to add this:
    Code:java
    1. public class StackerDisable extends JavaPlugin implements Listener {
    2. //extends JavaPlugin so you can add onEnable(){!
    3.  

    And then you wanna add:
    Code:java
    1. public void onEnable(){
    2. getServer().getPluginManager().registerEvents(this, this);
    3. }
    4. //This will make it so the plugin can Register Events!
    5.  

    And the same on StackerEnabled!
    Code:java
    1. public class StackerEnabled extends JavaPlugin implements Listener{
    2.  

    And:
    Code:java
    1. public void onEnable(){
    2. getServer().getPluginManager().registerEvents(this, this);
    3. }
    4.  
     
Thread Status:
Not open for further replies.

Share This Page