Creating Method

Discussion in 'Plugin Development' started by Samthelord1, Dec 27, 2014.

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

    Samthelord1

    I am trying to create a couple of methods within my class that I can use in other classes and in a different plugin, I know how to create the method but I do not know how to show what it returns, all help is appreciated even if its not right ;) As of now, I've set out guidelines of what I would like to do, to give you an idea of what it is that I will need to be returning, mostly integers from configs...


    //if(!p.getTokens().equals(config.getInt("gappleprice")) {
    //p.getInventory.addItem(gapple);
    //p.removeTokens(config.getInt("gappleprice")
    // }
    // else {
    //p.sendMessage(ChatColor.RED + "You do not have enough Tokens!")
     
  2. Offline

    macboinc

    There is no getTokens() method for Player.
    You also said that if a player has not enough tokens, you will give it to them...
    I am not sure what you are trying to prove here
     
  3. Offline

    Samthelord1

    I forgot to change that for that (other one is //if(!p.getTokens() == < config.getInt("lswordprice")) {)
    and also this is my problem, I am trying to create that method not use it..
     
  4. Offline

    macboinc

    There are many types of methods

    http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
    Learn basic java
     
    Last edited by a moderator: Dec 29, 2014
  5. Offline

    TGRHavoc

    @Samthelord1
    I think that what you're wanting to do would require you to change the core bukkit code and add the "getTokens()" method to the Player class...

    You could create a class that contains a HashMap of Player classes and "Token" class. Then call the HashMap#get(Player) to get the Token class then do something like "Token#getTokens()" to get the amount of tokens... It'll be much easier than modifying Bukkits' core code..
     
  6. Offline

    Mysticate

  7. Offline

    Hex_27

    No. He is trying to do this: p.getTokens So basically he is trying to add a method in the bukkit's player class. Which is a bad idea. So don't do it.
     
  8. Offline

    Skionz

    @Samthelord1 Either pass the player instance through a method's arguments, or the constructor like this.
    Code:
    MyPlayer mp = new MyPlayer(player);
    mp.setTokens(Integer.MAX_VALUE);
     
    Last edited: Dec 28, 2014
  9. Offline

    mythbusterma

    @Samthelord1

    Watch this:

    Code:
    if(!this.getTokens(player) < this.getConfig().getInt("gappleprice")) {
       player.getInventory().addItem(gapple);
       this.removeTokens(player, getConfig().getInt("gappleprice");
    }
    else {
        player.sendMessage("You don't have enough tokens......or something....");
    }
    *gasp* I didn't have to extend the Player class, and I barely had to do any work at all....it's so.....simple.

    Just create a method that takes a Player as an argument, it will save you headaches.
     
  10. Offline

    Samthelord1

    I am stuck on this, I know how to create the method;
    public void getTokens(final Player player) {
    but then would i use a hashmap?
     
  11. Offline

    Skionz

    @Samthelord1 You should save it to the disk onDisable by using FileConfiguration#save(File), and load it on Enable using YamlConfiguration.loadConfiguration(File). I believe the loadConfiguration method iterates through each line of the file, parses all of the keys and values, and saves it in a Map. I haven't looked at the source, just a guess.
     
  12. Offline

    mythbusterma

    @Samthelord1

    That would make the most sense to me, or you could just read it from the config.
     
  13. Offline

    Samthelord1

    @mythbusterma I think I will read it from the config, should I create a new string for each player onJoin?

    @mythbusterma @Skionz did this to create the int in the config, how do i return it with my method?
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    if(config.contains("tokens." + e.getPlayer().getName())) {
    return;
    }
    if(!config.contains("tokens." + e.getPlayer().getName())) {
    config.set("tokens." + e.getPlayer().getName(), 0);
    }
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  14. Offline

    Skionz

  15. Offline

    Samthelord1

    @Skionz I've already called fileconfiguration config = getConfig();
    I just need to know how to return the int for getTokens,
    I brainstormed this;
    public static void getTokens(Player player) {
    static int I = config.getInt("tokens." + player);
    return I;
    }
    But void doesn't return int, so I'm stuck...
     
  16. Offline

    mythbusterma

    @Samthelord1

    1. Okay, that's static, and you're using instance variables...why static? Honestly, just stop.
    2. You can't even make a variable static inside a method scope, so just stop.
    3. Stop putting static everywhere, it's awful.
    4. It doesn't return int because it returns void, or nothing.
    Learn Java before you try to write a Bukkit plugin.
     
  17. Offline

    Samthelord1

    @mythbusterma sorry I don't have much experience with methods, a small insight could help?
     
  18. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page