Solved Help me with a plugin please

Discussion in 'Plugin Development' started by AKZOBIE74, May 21, 2014.

Thread Status:
Not open for further replies.
  1. AKZOBIE74 Try this:
    Code:java
    1. package me.AKZOMBIE74;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.meta.ItemMeta;
    12.  
    13. public class Clicklistener implements Listener {
    14.  
    15. @EventHandler
    16. public void interact(PlayerInteractEvent event) {
    17. Player player = event.getPlayer();
    18.  
    19. ItemStack riptide = new ItemStack(Material.DIAMOND_SWORD, 1);
    20. ItemMeta riptideMeta = riptide.getItemMeta();
    21. riptideMeta.setDisplayName(ChatColor.DARK_RED + "Riptide");
    22. riptide.setItemMeta(riptideMeta);
    23.  
    24. ItemStack pen = new ItemStack(Material.STICK, 1);
    25. ItemMeta penMeta = pen.getItemMeta();
    26. penMeta.setDisplayName(ChatColor.WHITE + "Pen");
    27. pen.setItemMeta(penMeta);
    28.  
    29. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    30. if (player.hasPermission("riptide.switch")) {
    31. if (player.getItemInHand().getType().equals(Material.DIAMOND_SWORD) && player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "Riptide")) {
    32. player.setItemInHand(pen);
    33. return;
    34. }
    35. if (player.getItemInHand().getType().equals(Material.STICK) && player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.WHITE + "Pen")) {
    36. player.setItemInHand(riptide);
    37. return;
    38. }
    39. }
    40. }
    41. }
    42. }
     
  2. Offline

    AKZOBIE74

    DJSkepter did you want my main class because another 3 permissions are in my main class but the switch.riptide permission isnt in my main class and a crafting recipe for the pen and riptide are in my main class
     
  3. Offline

    ZodiacTheories

    AKZOBIE74

    Use == as opposed to .equals() because .equals returns a string
     
  4. Offline

    AKZOBIE74

    DJSkepter ok so now i when i switch from a pen to a diamond sword the diamond sword is called riptide but i still cant switch from the riptide to the pen
     
  5. Offline

    AoH_Ruthless

    _feedDz
    You gave the OP a lot of misinformation.

    1. You implied that he has to extend JavaPlugin in all classes. This is wrong and you should never ever do that.
    2. You told him to put all his classes in one package.
    3. He was registering his events just fine before you told him to "fix" it.
    ZodiacTheories
    False. You have your information backwards: Don't compare strings with ==, but you can compare enums with .equals(). In fact, a lot of people will tell you that .equals() is better for enums like Material. This is because == deals with primitives and checks whether object references are equal. For example, you use == when you are checking if two integers or two boolean values are true.

    DJSkepter
    ItemMeta cannot be null so you don't need to check if an itemstack does or does not have itemMeta. Your if checks would be better if:

    Code:
    ItemStack hand = p.getItemInHand();
     
    if (hand.equals(pen)) {
        p.setItemInHand(riptide);
    } else if (hand.equals(riptide)) {
        p.setItemInHand(pen);
    }
     
    
    Edit: But, then again, there is nothing wrong with your code. It can just be more efficient and easier to read :)

    AKZOBIE74
    It doesn't matter what class everything is in. Pass everything through a constructor where necessary and you would be good to go on that count.
     
    Necrodoom and DJSkepter like this.
  6. True but will durability and amount not interfere with the ItemStack when you try to compare them? http://jd.bukkit.org/rb/doxygen/db/d8c/ItemStack_8java_source.html
     
  7. Offline

    AoH_Ruthless

    DJSkepter
    I don't believe either one interferes.

    How do we know for sure? We test it :)
     
  8. AoH_Ruthless According to the documentation, it checks for amount, durability, itemMeta, typeID and itemFactory - hence the reason I used the displayName method to check if they were holding such item.
     
    AoH_Ruthless likes this.
  9. Offline

    AKZOBIE74

    DJSkepter so do i put in Aoh's code?

    AoH_Ruthless ok so i put in your code with a "return;" and it works fine except when i want to turn the sword back into a pen i have to right click a block i can open such as anvils,crafting tables,furnaces etc.I want to right click the air for both of them so they turn into each other you know like when i right click the air with the pen i want it to turn into a riptide same with the riptide how do i fix that? @DJSkepter i also need your help too.

    AoH_Ruthless dude can you check out the post before this?

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

    AoH_Ruthless

    AKZOBIE74
    Only bump your posts once every 24 hours, not twice in an hour (There's a thing called editing your posts).

    You gotta show your code again because I have no idea what you are talking about. You have to put some effort into this. It is clear that everyone in this thread has just handed you the answer. You will never ever learn like that. ("Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime").

    I'm not going to just spoonfeed you the answer, so you have to at least put in the time to show your code and try to figure it out, instead of rushing here every single time you have an error. You should look into some of the links before because these will answer your questions faster than we can (We aren't always on!)

    http://google.com/
    http://jd.bukkit.org/beta/apidocs/ (1.7.2)
    I think you said you were working with 1.6.4 : http://jd.bukkit.org/rb/apidocs/
    For Java issues: http://stackoverflow.com/
    http://forums.bukkit.org/search/
     
  11. Offline

    xTrollxDudex

    I'm saddened to see how spoon feeding turns this thread into a 3 page train wreck of "this doesn't work" "try this..."
     
  12. Offline

    ResultStatic

    o really because i have found these in many large plugins. so your wrong.

    i decompiled the WorldEdit main class.
    private static WorldEdit instance;
    public static WorldEdit getInstance()
    {
    return instance;
    }
    PacketApi:
    public static PacketAPI getInstance() {
    return plugin;
    }

    actually the rule is you should never create a new instance of your main class thats extends JavaPlugin.

    so you shouldnt do Main main = new Main();


    i bought 3 cans of gerber tho

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

    AKZOBIE74

    AoH_Ruthless Ok so i took your advice and tried to put some effort into it and I tried changing somethings around in the code but i had no luck getting the results i wanted.Here is the code I made but it doesn't work.

    Code:
    Code:java
    1. package me.AKZOMBIE74;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.ShapedRecipe;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14.  
    15. public class Clicklistener implements Listener {
    16.  
    17. Riptide mainClass = new Riptide();
    18.  
    19. @EventHandler
    20. public void interact(PlayerInteractEvent event) {
    21. Player player = event.getPlayer();
    22. ItemStack hand = player.getItemInHand();
    23.  
    24. ItemStack Riptide = new ItemStack(Material.DIAMOND_SWORD, 1);
    25. ItemMeta Riptidemeta = Riptide.getItemMeta();
    26. Riptidemeta.setDisplayName(ChatColor.DARK_RED + "Riptide");
    27. Riptide.setItemMeta(Riptidemeta);
    28.  
    29.  
    30. ItemStack pen = new ItemStack(Material.STICK, 1);
    31. ItemMeta penmeta = pen.getItemMeta();
    32. penmeta.setDisplayName(ChatColor.WHITE + "Pen");
    33. pen.setItemMeta(penmeta);
    34.  
    35.  
    36. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    37. if (player.hasPermission("riptide.switch")) {
    38. if (hand.equals(pen)) {
    39. player.setItemInHand(Riptide);
    40. } if (hand.equals(Riptide)) {
    41. player.setItemInHand(pen);
    42. }
    43. return;
    44. }
    45. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    46. } if (player.hasPermission("riptide2.switch")) {
    47. if (hand.equals(Riptide)) {
    48. player.setItemInHand(pen);
    49.  
    50. }
    51. return;
    52. }
    53. }
    54. }
    55. }
     
  14. Offline

    fireblast709

    AoH_Ruthless ( DJSkepter because you were tagged with the post) In fact, have fun with your NullPointerExceptions when fetching ItemMeta from an AIR stack.
    Code:java
    1. // org.bukkit.inventory.ItemStack
    2. public ItemMeta getItemMeta()
    3. {
    4. return this.meta == null ? Bukkit.getItemFactory().getItemMeta(getType0()) : this.meta.clone();
    5. }
    Code:java
    1. //org.bukkit.craftbukkit.inventory.CraftItemFactory
    2. public ItemMeta getItemMeta(Material material)
    3. {
    4. Validate.notNull(material, "Material cannot be null");
    5. return getItemMeta(material, null);
    6. }
    7.  
    8. private ItemMeta getItemMeta(Material material, CraftMetaItem meta)
    9. {
    10. switch (material)
    11. {
    12. case AIR:
    13. return null;
    14. // ... other cases
    That aside, hasItemMeta() will prevent creating ItemMeta instances for no reason.
     
    DJSkepter likes this.
Thread Status:
Not open for further replies.

Share This Page