Money /points & permissions

Discussion in 'Plugin Development' started by Weszzz, Sep 22, 2013.

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

    Weszzz

    Hi there guys, I have 2 questions
    1. How can i show the players money/points in there name ? Like this: [20]Weszzz: Hi :)
    2. How can i like sell permissions, if someone clicks on an item to buy a kit in a IconMenu then the system takes the money/points and the player has the permission for the kit.
     
  2. Offline

    Pizza371

    Weszzz every time he gains a point set his nickname?
    Use Vault?
    ~ Pizza
     
  3. Offline

    Weszzz

    Vault okay, but how do i set his nickname everytime he gets a kill ?
     
  4. Offline

    Pizza371

    Weszzz
    Something like p.setDisplayName("[" + points + "] " + p.getName());
    ? I'm not sure if it will work but I think it should, I've never played with nicknames before :p
     
  5. Offline

    Weszzz

    Okay but how do you know if the player who was killed was killed by that player and how can i add points then ? Srryiamkindanoob
     
  6. Offline

    Pizza371

    Weszzz just experiment with stuff and try and learn some stuff
    And you use PlayerDeathEvent to get the players, add them to a HashMap<String(being the playername), Integer(being the kills)>, or if you want permanent storage use YAML or some kind of database.
    Google is still your best friend when learning to make plugins! ;3
     
  7. Offline

    Weszzz

    I seriously dont know what HashMap is xd Can you like explain some more ?
     
  8. Offline

    Pizza371

    Weszzz So something like
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    HashMap<String, Integer> kills = new HashMap<String, Integer>(); //create HashMap
    e.setDeathMessage("");
    Player a = e.getEntity().getKiller();
    if(kills.containsKey(p.getName()) { //if the hashmap contains the players name
    kills.put(a.getName(), kills.get(p.getName()) + 1); //add one to the kills of this player
    } else {
    kills.put(a.getName(), 0); //if not, add the player with 0 kills
    }
    a.setDisplayName("[" + kills.get(a.getName()) + "] " + a.getName()); //set his name to show his kills
    }
    Example.. but I think you get what I'm doing (well, hopefully :p).
     
  9. Offline

    Weszzz

    Pizza371 O i get it, but how can i now let the player buy permissions (kits) with that ? Because that code is kills not vault ;p So how can i let the player buy permissions with his kills then ?
     
  10. Offline

    Pizza371

    Weszzz hook to vault API (really easy check vault page) then type in permission. and it will show you all you can do.
     
  11. Offline

    Weszzz

    Pizza371 But then i can let the player buy permissions with KILLS ? Because this is kills or can i do the same thing with this code but then with coins, so that he gets 1 coin if he kills someone etc etc, ?
     
  12. Offline

    Pizza371

    Weszzz you can do that with kills, but as I told you, it will NOT save over reloads (do you want this????).
    For doing it with this something like:
    if(kills.get(p.getName()) >= amountNeeded) {
    permission.playerAddGroup(player, String permission); //I think this is right but I only used vault once for a minor project :p
    } else {
    you dont have enough points!
    }

    //Try and take in what I'm telling you because it's not beneficial for you to just copy & paste.
     
  13. Offline

    Weszzz

    Pizza371 Okay thnx youre really helping me out but what is not saving over reloads ? This ?:
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    HashMap<String, Integer> kills = new HashMap<String, Integer>(); //create HashMap
    e.setDeathMessage("");
    Player a = e.getEntity().getKiller();
    if(kills.containsKey(p.getName()) { //if the hashmap contains the players name
    kills.put(a.getName(), kills.get(p.getName()) + 1); //add one to the kills of this player
    } else {
    kills.put(a.getName(), 0); //if not, add the player with 0 kills
    }
    a.setDisplayName("[" + kills.get(a.getName()) + "] " + a.getName()); //set his name to show his kills
    }
    Or that other code ?
     
  14. Offline

    Pizza371

    Weszzz All of it. Display names will save so I guess that is a fault (just thought about that... :p) Ok, ill just show you how I do it with YAML (sorry I am not experienced with SQL yet..).
    Code:
    onEnable:
    if(!getDataFolder().exists()) {
    getDataFolder().mkdirs();
    }
    Other methods:
    public void createStorage(Player p) {
    File f = new File(getDataFolder(), "/players.yml");
    if(!f.exists()) {
    f.createnewFile();
    }
    YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
    y.set(p.getName(), 0);
    y.save(f);
    }
    public int getKills(Player p) {
    File f = new File(getDataFolder(), "/players.yml");
    YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
    return y.getInt(p.getName());
    }
    public void incrementKills(Player p) {
    File f = new File(getDataFolder(), "/players.yml");
    YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
    int kills = y.getInt(p.getName());
    kills = kills + 1;
    y.set(p.getName(), kills);
    y.save(f);
    }
    public boolean doesExist(Player p) {
    File f = new File(getDataFolder(), "/players.yml");
    YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
    if(y.contains(p.getName)) { // i believe?
    return true;
    } else {
    return false;
    }
    }
    I wrote this without an IDE, so it won't be perfect.
    you should try and move as much variables that are used by multiple methods outside of the methods and you will get a few errors which require you to sort out saves/creates with try / catch (your IDE should do this for you)
    Good luck!
    -----------------------------------------------------------------
    on death:
    if(doesExist) {
    incrementKills(p);
    } else {
    createStorage(p);
    incrementKills(p);
    }
    on /kills:
    p.sendMessage(getKills(p));
     
  15. Offline

    Weszzz

    Pizza371 Okay, but i want to add money to the players account if he/she makes a kill. So i have this now:
    package me.Weszzz.SurviveIt;

    import java.util.HashMap;

    import net.milkbowl.vault.economy.Economy;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Points extends JavaPlugin
    implements Listener {

    public static Economy econ = null;

    public void onEnable() {
    if (!setupEconomy() ) {
    getLogger().severe(String.format("[%s] SurviveIt mist Vault!", getDescription().getName()));
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    }

    private boolean setupEconomy() {
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
    return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }

    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    HashMap<String, Integer> kills = new HashMap<String, Integer>(); //create HashMap
    e.setDeathMessage("");
    Player player = e.getEntity().getKiller();
    if(kills.containsKey(player.getName())) { //if the hashmap contains the players name
    kills.put(player.getName(), econ.withdrawPlayer(player.getName(), + 1)); //add one to the kills of this player
    } else {
    kills.put(player.getName(), econ.withdrawPlayer(player.getName(), 0)); //if not, add the player with 0 kills
    }
    player.setDisplayName("[" + econ.getBalance(player.getName()) + "] " + player.getName()); //set his name to show his kills
    }
    }
    But i get errors on kills.put, how can i fix this ? thx
     
  16. Offline

    Pizza371

    Weszzz wat r u doing with the econ? I thought you wanted to track kills?
     
  17. Offline

    Weszzz

    Pizza371 I want to add money to the account if a user makes a kill, and you can see the money in his name like this: [20]Weszzz: Like this. I think u didnt understand it ? But how can i do this ? Thanx
     
  18. Offline

    Pizza371

    Weszzz well your trying to add nothing to a hashmap, when you don't need to do anything to the hashmap at all when withdrawing money
    Code:
    package me.Weszzz.SurviveIt;
     
    import java.util.HashMap;
     
    import net.milkbowl.vault.economy.Economy;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Points extends JavaPlugin
    implements Listener {
     
    public static Economy econ = null;
     
    public void onEnable() {
    if (!setupEconomy() ) {
    getLogger().severe(String.format("[%s] SurviveIt mist Vault!", getDescription().getName()));
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    }
     
    private boolean setupEconomy() {
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
    return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }
     
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    Player player = e.getEntity().getKiller();
    econ.depositPlayer(player.getName(), 1);
    player.setDisplayName("[" + econ.getBalance(player.getName()) + "] " + player.getName()); //set his name to show his kills
    }
    }
     
  19. Offline

    Weszzz

    Pizza371 Isnt working :( Why it isnt working ?
     
  20. Offline

    Pizza371

    Weszzz
    Code:
    package me.Weszzz.SurviveIt;
     
    import java.util.HashMap;
     
    import net.milkbowl.vault.economy.Economy;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Points extends JavaPlugin
    implements Listener {
     
    public static Economy econ = null;
     
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this,this);
    if (!setupEconomy() ) {
    getLogger().severe(String.format("[%s] SurviveIt mist Vault!", getDescription().getName()));
    getServer().getPluginManager().disablePlugin(this);
    return;
    }
    }
     
    private boolean setupEconomy() {
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
    return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
    return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }
     
    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
    Player player = e.getEntity().getKiller();
    econ.depositPlayer(player.getName(), 1);
    player.setDisplayName("[" + econ.getBalance(player.getName()) + "] " + player.getName()); //set his name to show his kills
    }
    }
     
  21. Offline

    Weszzz

    Pizza371 Isnt working either :( Whats wrong ?
     
  22. Offline

    Pizza371

    Weszzz is the plugin loading? any errors in the console?
     
  23. Offline

    Weszzz

    Pizza371 The plugin is loading and there are no errors in the console.
     
  24. Offline

    Pizza371

    Weszzz try adding debug messages p.sendMessage("test"); when it is triggered.
    Also try:
    p.setCustomName(); instead of displayname
    and then p.setCustomNameVisible(true);
     
  25. Offline

    Weszzz

  26. Offline

    Pizza371

    Weszzz there must be some kind of error x_O if you've tried all the methods, is it in /pl green?
    do you get the message "test"?
    Have you got the command in the plugin.yml?
     
  27. Offline

    Weszzz

    Pizza371 It is in /pl green, i did not get the message test and there are no commands.
     
  28. Offline

    Pizza371

    Weszzz
    I'm finding it hard to find any problems lol, are you SURE you're registering events?
    Try adding:
    @EventHandler(priority = EventPriority.HIGHEST)
    Try removing essentials (if u have it) temporarily and seeing if it works.
     
  29. Offline

    Weszzz

    Pizza371 I removed essentials etc.. Nothing works, this is my code atm:
    Code:java
    1. package me.Weszzz.SurviveIt;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10. import org.bukkit.plugin.RegisteredServiceProvider;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Points extends JavaPlugin
    14. implements Listener {
    15.  
    16. public static Economy econ = null;
    17. @EventHandler
    18. (priority = EventPriority.HIGHEST)
    19. public void onEnable() {
    20. getServer().getPluginManager().registerEvents(this,this);
    21. if (!setupEconomy() ) {
    22. getLogger().severe(String.format("[%s] SurviveIt mist Vault!", getDescription().getName()));
    23. getServer().getPluginManager().disablePlugin(this);
    24. return;
    25. }
    26. }
    27.  
    28. private boolean setupEconomy() {
    29. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    30. return false;
    31. }
    32. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    33. if (rsp == null) {
    34. return false;
    35. }
    36. econ = rsp.getProvider();
    37. return econ != null;
    38. }
    39.  
    40. @EventHandler
    41. public void onDeath(PlayerDeathEvent e) {
    42. Player player = e.getEntity().getKiller();
    43. econ.depositPlayer(player.getName(), 1);
    44. player.sendMessage("test");
    45. player.setCustomName("[" + econ.getBalance(player.getName()) + "] " + player.getName()); //set his name to show his kills
    46. player.setCustomNameVisible(true);
    47. }
    48. }

    Can u see whats wrong ?
     
  30. Offline

    Pizza371

    Code:
    package me.Weszzz.SurviveIt;
     
    import net.milkbowl.vault.economy.Economy;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Points extends JavaPlugin
    implements Listener {
     
        public static Economy econ = null;
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this,this);
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] SurviveIt mist Vault!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onDeath(PlayerDeathEvent e) {
            Player player = e.getEntity().getKiller();
            econ.depositPlayer(player.getName(), 1);
            player.sendMessage("test");
            player.setCustomName("[" + econ.getBalance(player.getName()) + "] " + player.getName()); //set his name to show his kills
            player.setCustomNameVisible(true);
        }
    }
    I meant before your event. (priority)
    I honestly cant see anything else wrong.. if this doesn't work
    @AttentionBukkitUsers
     
Thread Status:
Not open for further replies.

Share This Page