Leveling system for server

Discussion in 'Spigot Plugin Development' started by lucasouwens, Jun 10, 2015.

  1. So the problem for me is, i can't seem to find a way to get the current player level to the config, does anyone know that? just to be sure that there aren't more bugs i'll also show my current code.

    Code (Text):
    package com.derffamily.main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLevelChangeEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Levelsystem extends JavaPlugin implements Listener {
       

        public static int l;
       
        public void onEnable(Player p) {
           
           
       
           
            getConfig().options().copyDefaults(true);
           
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
           
        }
       
        @EventHandler
        public void playerJoinEvents(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            String name = p.getName();
            String uuid = p.getUniqueId().toString();
            if(!getConfig().contains("Players." + p.getUniqueId().toString())) {
                getConfig().set("Players." + uuid + ".level", l);
                saveConfig();
            }
        }
       

        public final void levelUp(PlayerLevelChangeEvent e, Player p) {
           
            String UUID = p.getUniqueId().toString();
            if(e.getOldLevel() <= e.getNewLevel()) {
               
                getConfig().getInt("Players." + UUID + ".level", l +1);
                p.sendMessage(ChatColor.GREEN + "You have leveled up!");
                saveConfig();
            }
           
           
           
            }
       

       
        }
       
       
               
           
    If you could help that would be greatly appriciated!
     
  2. Edit: Changed: if(e.getOldLevel() <= e.getNewLevel()) { to: if(e.getNewLevel() < l) {
     
  3. Code (Text):

    public static int l;
     
    You did not defined the integer so what you should do is try

    Code (Text):

    l = p.getLevel()
     
    and player p is from the playerjoinevent or the levelup method

    then it should work, also maybe a notice that a public integer is not the best way more try something like add ( int level) to the method and use levelup(event,p,p.getLevel()); in the join event

    then use "level" for the player points
     
  4. Can you give me an example of some sort?
    Where should i put the int l then? since i do not have a player method that is public
     
  5. I now have
    Code (Text):
    package com.derffamily.main;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLevelChangeEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Levelsystem extends JavaPlugin implements Listener {
       
       
       
        public void loadConfiguration(){
           
            this.getConfig().options().copyDefaults(true);
           
            this.saveConfig();
    }

       
       
       
        public void onEnable(Player p, int l) {

            loadConfiguration();
           
            this.levelUp(p.getLevel(), p, p.getLevel());
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
           
        }
       
        @EventHandler
        public void playerJoinEvents(PlayerJoinEvent e, int l) {
       
           
            Player p = e.getPlayer();
            String name = p.getName();
            String uuid = p.getUniqueId().toString();
            if(!getConfig().contains("Players." + p.getUniqueId().toString())) {
                getConfig().set("Players." + uuid + ".level", l);
                saveDefaultConfig();
                saveConfig();
            }
        }
       
        @EventHandler
        public void levelUp(int level, Player p, int l) {
        level = p.getLevel();
           
        String uuid = p.getUniqueId().toString();
       
            if(l < level) {
                this.getConfig().set("Players." + uuid + ".level", l == level);
            }
        }
       
       
        }
       
       
               
           
           
       
     
  6. Code (Text):

    package com.derffamily.main;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLevelChangeEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Levelsystem extends JavaPlugin implements Listener {
       
       
        public void loadConfiguration(){
           
            this.getConfig().options().copyDefaults(true);
           
            this.saveConfig();
    }

       
       
       
        public void onEnable() {

            loadConfiguration();
                 
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
           
        }
       
        @EventHandler
        public void playerJoinEvents(PlayerJoinEvent e) {
       
           
            Player p = e.getPlayer();
            String name = p.getName();
            String uuid = p.getUniqueId().toString();
           
            int level_before_add = p.getLevel();
           
            if(!getConfig().contains("Players." + p.getUniqueId().toString())) {
                getConfig().set("Players." + uuid + ".level", p.getLevel());
                saveDefaultConfig();
                saveConfig();
            }else{
               
                /**
                * Add an integer in here like
                *
                * int new_level = 3;
                *
                * so that means the player gets 3 new level
                *
                * then use "new_level" as the levelUp method integer ("level")
                *
                */
               
                levelUp(new_level, p);
            }
        }
       
        public void levelUp(int level, Player p) {
         int lvl = p.getLevel();
           
        String uuid = p.getUniqueId().toString();
       
        /**
         * Add your "level" now, exactly here.
         */
       
        int new_lvl = p.getLevel();
       
            if(new_lvl>lvl) {
                this.getConfig().set("Players." + uuid + ".level", lvl);
            }
        }
       
       
        }
       

     
    try this, notice that you have to change some code.
     
    • Like Like x 1
  7. You.. you are the REAL MVP!!

    You are a savior of times, i've been stuck with it for 3 days, do you know how happy and grateful i am to you?!

    You da real mvp - Random guy on spigot
     
  8. no problem ^^ helping everyone who needs help :)
     
  9. Do you possibly know how i can add it to my scoreboard (which i just added) and that it keeps updating each time you level?
     
  10. the code is this for the scoreboard

    Code (Text):
        @EventHandler
        public void playerJoinEvents(PlayerJoinEvent e) {
     
            Player p = e.getPlayer();
           
            ScoreboardManager sm = Bukkit.getScoreboardManager();
            Scoreboard sb = sm.getNewScoreboard();
           
           
            Team team = sb.registerNewTeam("Yourmum");
            Objective objective = sb.registerNewObjective("Level", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.GOLD + "Minestory Stats");
            team.addPlayer(p);
            p.setScoreboard(sb);
    (note that this is a part of the code and not full
     
  11. try to add something like Score s = objective.getScore("§aLevel");
    s.setScore(#this-is-an-integer);
    Replace #this-is-an-integer with the players level from config (config.getInt(" path to level");)
    Put that small code with the score above p.setScoreboard(sb);

    Also notice that you dont need the Team and for update do something like at adding a New level use p.getScoreboard().getObjective(DisplaySlot.SIDEBAR).setScore("§aLevel",#insert-your-new-level);

    Maybe you have to change some code because its not written in a PC but it should work :)
     
  12. Also found a small issue in what've postee in the levelup Method wäre you add the level into the config change at the end from lvl to new_level otherwise with the old code nothing should really happen / change
     
  13. Hey, i changed alot of code and now i am using a string list, only now i want to add weapons (custom named etc) that require a certain Level which is saved in the console, Could you give me an example of this. Like: If you arent the right level (which it'll say on the lore) it will deny any use in it. This is my current code:

    Code (Text):
    package com.derffamily.main;

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLevelChangeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    public class Levelsystem extends JavaPlugin implements Listener {
     
        private static Scoreboard board;
        private static Objective o;
        private static Score s;

       

        static HashMap<OfflinePlayer, Score> scores = new HashMap<OfflinePlayer, Score>();
       
       
     
        public void loadConfiguration(){
         
           
           
           
           
            this.getConfig().options().copyDefaults(true);
         
            this.saveConfig();
    }

     
     
     
        public void onEnable() {
           
            board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
            o = board.registerNewObjective("Stats", "dummy");
            o.setDisplayName(ChatColor.GOLD +"Player stats");
            o.setDisplaySlot(DisplaySlot.SIDEBAR);
           
            s = o.getScore(ChatColor.GREEN + "Level:");
           
           
           
            loadConfiguration();    
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
            List<String> s = getConfig().getStringList("levels");
            for(String str : s) {
               
                String[] words = str.split(":");
                scores.put(Bukkit.getServer().getOfflinePlayer(words[0]), o.getScore( Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Level:")));
                scores.get(Bukkit.getServer().getOfflinePlayer(words[0])).setScore(Integer.parseInt(words[1]));
               
            }
            saveDefaultConfig();
           
         
        }
       
        public void onDisable() {
            List<String> s = getConfig().getStringList("levels");
            for(OfflinePlayer p : scores.keySet()) {
               
                s.add(p.getName() + ":" + scores.get(p).getScore());
               
            }
            getConfig().set("levels", s);
            saveConfig();
        }
       

       
        @EventHandler
        public void playerlevel(PlayerLevelChangeEvent e) {
            if(e.getNewLevel() - e.getOldLevel() == 1) {
              scores.get(e.getPlayer()).setScore(scores.get(e.getPlayer()).getScore() + 1);
            } else {
                return;
            }

        }
        @EventHandler
        public void playerJoinEvents(PlayerJoinEvent e) {
     
            Player p = e.getPlayer();
           
            p.setScoreboard(board);
           
            if(scores.get(p) == null) {
                scores.put(p, o.getScore( Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Level:")));
               
            } else {
                return;
            }
           
           
        }
     
       

       // End of levels //
     
     
       
       
       
       
        String prefix = ChatColor.GOLD + "[" + ChatColor.WHITE +  ChatColor.BOLD +  "Level" + ChatColor.GOLD + "]";
       
       
       
       
       
        // Recipes start //

       
       
        }// End class //
     
  14. Try the playerinteractevent and check if the player is Holding a weapon ( getting item in Hand = p.getItemInHand();) if its true check the level from the player is that like which he need then if the level is the right one then don't do anything but when its lower then use e.setCancelled(true);
     
  15. Thank you, but i tried this:

    Code (Text):
    public void Swordevents(PlayerInteractEvent e)  {
            Player p = e.getPlayer();
         
            ItemStack sword1 = new ItemStack(Material.IRON_SWORD, 1);
            ItemMeta sm1 = sword1.getItemMeta();
            sm1.setDisplayName(ChatColor.GOLD + "Rusty Iron Sword");
            sm1.setLore(Arrays.asList(ChatColor.GREEN + "This sword requires level 5!"));
         
            if(e.getItem().isSimilar(sword1) && getConfig().getStringList("levels").get(p.getName() + ":" + scores.get(p).getScore()) >= 5) {
             
            }
        }
    The 'get' doesnt accept a string, so what should i get then?
    The purpose for this sword is that no one that is below the config level of 5 can use it.

    Also, could i add you, or you me on skype so i can ask questions easilier?
    my skype is: lucas.ouwens (the guy with the headphones on)
     
  16. Try getInt and for Skype : ploxh4d