[Methods] Need help creating a method

Discussion in 'Plugin Development' started by ko47374737, Jun 28, 2014.

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

    ko47374737

    Ok so I have tried and tried to make a method. I made a method
    and its
    n.getEnchantment(Player)
    but I want to know how to add on e.g
    n.getEnchantment(Player).removeEnchantments()
    I know how to remove enchantments but I don't know how to make it like n.getEnchantment(player).removeEnchantments();
    I only know n.removeEnchantments(p);
    So please help if you can
     
  2. Offline

    tommyhoogstra

    Ok, we don't need all that other stuff, what do you want the method to do.
     
  3. Offline

    ko47374737

    All I want to know is how to create sub method kinda things I guess e.g how player.getLocation().getY() ..getLocation() is the main then getY() is sub method I want to know how to make something like that

    Please help if you can!

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

    dsouzamatt

    ko47374737 Oh, I think I get what you're asking. Let's use getting a location as an example. To get a player's location, I would do:

    Code:java
    1. Location theirLocation = player.getLocation();


    Using .getLocation() on a player returns a location. If I wanted to then get their world, I could do:

    Code:java
    1. Location theirLocation = player.getLocation();
    2. World theirWorld = theirLocation.getWorld();
    3.  


    To be more efficient, however, I could simply do:

    Code:java
    1. World theirWorld = player.getLocation().getWorld();


    .getLocation() returns a location, and .getWorld() returns a world. We can skip the middle step of declaring a Location and then getting the World off that location by, in one line, getting the Location and then getting its World.

    Here's another example. Let's say we wanted to check if a player has an item with a durability enchantment and if they do, remove it. Here's what we'd do:

    Code:java
    1. if(player.getItemInHand().getEnchantments().containsKey(Enchantment.DURABILITY))
    2. {
    3. player.getItemInHand().removeEnchantment(Enchantment.DURABILITY);
    4. }


    If the item in their hand has durability, we remove it. You can think of the logic behind it as this:

    IF: (Player -> ItemStack ->HashMap of its Enchantments) has (The enchantment of durability)
    THEN: Player -> ItemStack, remove the Enchantment of durability that is on it.

    I hope this helped you.
     
  5. Offline

    ko47374737

    Nooo
    I want ot crete my own method
    but idk how to
    I want to create a n.getItem().removeEnchantments();
     
  6. Offline

    tommyhoogstra

    I understand what you mean, and am interested in learning it myself.

    Its basically a method inside a method. Not sure of its actual name.
     
  7. Offline

    ko47374737

    Ok I have been looking for a while if you find anything let me know
    P.S Great to know you understand :D

    Anyone got an idea?

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

    ZodiacTheories

    ko47374737

    Maybe edit the class that contains the getItem method and add your own method?
     
  9. Offline

    ko47374737

    I have tried that but I cant find the class that contains it maybe you know ?
     
  10. Offline

    ZodiacTheories

    ko47374737

    What does your variable n refer to?
     
  11. Offline

    ko47374737

    Noob n = new Noob();
    publicvoid enchantItem(Player p, Enchantment enchantment, int level)

    {



    p.getItemInHand().addEnchantment(enchantment, level);

    }
    I would use n.enchantItem(all stuff in here)
    I want to make it n.enchantItem(stuff here).setName("")
     
  12. Offline

    ZodiacTheories

  13. Offline

    ko47374737

    Want me to send code for it?
     
  14. Offline

    dsouzamatt

    ko47374737 Oh, I see now. That would be pretty difficult to do.

    EDIT: Oh, no it won't, let me get some code together

    ko47374737 Actually, just read this

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    ZodiacTheories likes this.
  15. Offline

    tommyhoogstra

  16. Offline

    ko47374737

    That's is not it??

    dsouzamatt
    That's not what I need
    that's extending another class so it can use those methods in that class

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

    Traks

    So you basically want to know how to do something like the following?
    Code:java
    1. Bukkit.getServer().getOnlinePlayers()
     
    tommyhoogstra likes this.
  18. Offline

    tommyhoogstra

    It is the same thing, e.g. PlayerPickUpItemEvent inherits PlayerEvent
    And in the example provided by dsouza, you could do stuff like

    MountBike.setGear
     
  19. Offline

    dsouzamatt


    Yes, which is what many of us believe you are trying to do. We think you are trying to create a subclass of a default class from Bukkit.
     
  20. Offline

    tommyhoogstra

    dsouzamatt is that how the basic System.out.println(); works?

    because you cant just go System.println();
    I think he wants to make the methods like this, where each method ("out") has its own submethods such as "println" "printf" etc
     
  21. Offline

    dsouzamatt

    tommyhoogstra No, not exactly. The "out" part is getting an instance of PrintStream to print the message. Here, take a look at this.

    I don't think you can have "submethods" in Java.
     
  22. Offline

    Traks

    tommyhoogstra No
    System - The System class
    out - A public static (and final) field in the System class, which holds an instance of the PrintStream class
    println - A method in the PrintStream class

    Edit: Indeed dsouzamatt, there is no such thing as sub-methods
     
    dsouzamatt likes this.
  23. Offline

    tommyhoogstra

    Traks then yes, this is what he is trying to do, except with his own stuff.

    e.g. main.loadPlayers().killall()
     
  24. Offline

    ZeusAllMighty11

  25. Offline

    ko47374737

    Traks Yes that's what I want
     
  26. Ok first things first.

    THERE ARE NO SUBMETHODS!
    HOW THE p.getLocation().getY(); WORKS!
    p is an instance of the Player CLASS.
    In it there is a method called getLocation!
    It returns the Location variable from the Player class!
    THE LOCATION VARIABLE IS AN INSTANCE OR THE Location class!
    Which contains a method called getY()!

    DO YOU UNDERSTAND IT?
    If not i suggest you to go watch all of thenewbostons Java tutorials
    Preferably the advanced too!
     
  27. Offline

    ko47374737

    nicba1010 I have watched some of his, does he have a video on this.
     
  28. Offline

    xTigerRebornx

    ko47374737 Why are you even trying to do this? I can see this accomplishing nothing different other them making redundant calls to methods.
     
  29. Offline

    ko47374737

    xTigerRebornx It will help later on... I am making an API too soo yeh..

    Also why respond if your going to say something like that... why do you care.. I just want help

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

    dsouzamatt

Thread Status:
Not open for further replies.

Share This Page