Exp

Discussion in 'Plugin Development' started by Loog, Jul 31, 2012.

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

    Loog

    For a plugin im making I need to implement exp points so you get a certain amount per kill, im my case 50. Then I need to get the amount of exp that they have got with the command /stats and if they have a certain amount, they can rank up to their next rank. (Corporal, Sergeant, ect...)
    I am trying to this with HashMap<String, Integer> the string being the player and the Integer being the exp. Can someone help me out with doing this, especially getting the amount of exp they have.
    Thanks
     
  2. Offline

    r0306

    Loog
    If you only want to count player deaths,
    Code:
    HashMap<String,Integer> exp = new HashMap<String,Integer>();
    @EventHandler
    public void onDeath(PlayerDeathEvent event)
    {
      if (event.getPlayer().getKiller() != null)
      {
        Player killer = event.getPlayer().getKiller();
        exp.put(killer.getName(), exp.containsKey(killer.getName()) ? exp.get(killer.getName()) + 1 : 1);
        if (exp.get(killer.getName()) == 50)
        {
            //rank up
            exp.put(killer.getName(), 0); //reset exp
        }
      }
    }
    To get a value from the hashmap, use
    Code:
    Object o = hashmap.get(object);
    To add a value to the hashmap, use:
    Code:
    hashmap.put(key, value);
    To check if a hashmap contains a key (entry), use:
    Code:
    if (hashmap.containsKey(object))
     
  3. Offline

    Loog

    I'm sorry i'm still very confused, ill post the code im using with the exp.

    Listener:

    Code:
        public static HashMap<String, Integer> exp = new HashMap<String, Integer>();
        int i = 50;
     
     
        @EventHandler
        public void PlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            Player killer = player.getKiller();
            Game.human.remove(player.getDisplayName());
            Game.infected.add(player.getDisplayName());
            exp.put(killer.getName(), exp.get(killer.getName()) + i);
        }
       
    }
    
    The class which needs to get the exp

    Code:
      if(commandLabel.equalsIgnoreCase("stats")) {
                      Object o = GameListener.exp.get(o);
                      sender.sendMessage(ChatColor.RED + sender.getName() + "'s" + ChatColor.YELLOW + " stats:");
                      sender.sendMessage(ChatColor.RED + GET EXP HERE);
                      return true;
     
  4. Offline

    r0306

    Loog
    One thing to be careful of: when you get the killer's value in the exp.put() statement, there's a chance that the killer is not in there yet so you have to check first with the contains() method.
    Code:
        public static HashMap<String, Integer> exp = new HashMap<String, Integer>();
        int i = 50;
     
     
        @EventHandler
        public void PlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            Player killer = player.getKiller();
            Game.human.remove(player.getDisplayName());
            Game.infected.add(player.getDisplayName());
            exp.put(killer.getName(), exp.containsKey(killer.getName()) ? exp.get(killer.getName()) + 1 : 1); //this is basically a condensed version of saying if hashmap does not have the killer, initialize the killer in the hashmap
        }
     
    }
    Code:
      if(commandLabel.equalsIgnoreCase("stats")) {
                      Object o = GameListener.exp.get(o);
                      sender.sendMessage(ChatColor.RED + sender.getName() + "'s" + ChatColor.YELLOW + " stats:");
                      sender.sendMessage(ChatColor.RED + "Sender has " + (classwithhashmap.exp.contains(sender.getName()) ? classwithhashmap.exp.get(sender.getName()) : "no") + " kill(s)."); //again, checking if sender is in hashmap before getting the value to avoid errors
                      return true;
     
  5. Offline

    Loog

    I have an error on the highlighted word
    sender.sendMessage(ChatColor.RED + "Sender has " + (classwithhashmap.exp.contains(sender.getName()) ? classwithhashmap.exp.get(sender.getName()) : "no") + " kill(s).");
    It gives me the options - 'Change to containsKey(..)', 'Change to containsValue(..)' and 'Add cast to GameListener.exp'
    What should I do?
     
  6. Offline

    r0306

    Loog
    Change it to containsKey(). I keep making the mistake. Contains() is for lists and sets.
     
    ZeusAllMighty11 likes this.
  7. Offline

    Loog

    Thank you so much! You just earn't yourself a follower. Also, very nice plugins!
     
  8. Offline

    r0306

    Loog
    Thanks! :) If you have any more questions, just ask.
     
  9. Offline

    Loog

    Okay, so the exp clears on server reload/stop. how can I stop this?

    And also, np :)

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

    r0306

    Loog
    You'd have to save to an external file. I posted a way to do this here: forums.bukkit.org/threads/solved-storing-and-loading-from-a-hashmap.88588

    Just scroll down until you see my post.
     
  11. Offline

    Loog

    r0306 Okay, I shall have a look.

    r0306 I have found you post, do I make the new methods in a new class? Or in the same class. Could you give me an example?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  12. Offline

    r0306

    Loog
    Copy and paste the two methods into your main class. Then change the <String, String> to <String, Integer> for every one of them.

    Now, change the directory of file for both methods to
    Code:
    getDataFolder() + "\\exp.bin"
    Then add the load hashmap to your onEnable() and the save hashmap to your onDisable().
    Code:
    public void onEnable()
    {
      classwithhashmap.exp = loadHashMap();
    }
    public void onDisable()
    {
      saveHashMap(classwithhashmap.exp);
    }
     
  13. Offline

    Loog

    Code:
        public void saveHashMap(HashMap<String, Integer> map)
        {
       
          FileWriter fwriter = new FileWriter("\\plugins\\MCInfected.bin");
          ObjectOutputStream writer = new ObjectOutputStream(fwriter);
       
          writer.writeObject(map);
       
          writer.close(); //don't forget to close the writer
       
        }
       
        public HashMap<String, Integer> loadHashMap()
        {
       
          HashMap<String, Integer> map = new HashMap<String, Integer>();
       
          FileReader freader = new FileReader("\\plugins\\MCInfected.bin");
          ObjectInputStream reader = new ObjectInputStream(freader);
       
          map = (HashMap<String, Integer>) reader.readObject();
       
          reader.close(); //don't forget to close
       
          return map;
       
        }
    }
    I have an error for both methods on new ObjectInputStream(freader); and it gives me the options Remove argument to match 'ObjectInputStream()' and 'change type of freader to InputStream' I also have the yellow line under the loadHashMap method in the map = (HashMap<String, Integer>) reader.readObject(); it gives me the option 'Add @SuppressWarnings unchecked to loadHashMap()' Any idea of how to fix this?
     
  14. Offline

    r0306

    Loog
    Try changing fwriter to
    Code:
    FileOutputStream fwriter = new FileOutputStream("\\plugins\\MCInfected.bin");
    And click add suppress warnings. It's only for the compiler nothing else.
     
  15. Offline

    Loog

    r0306
    That seems to have fixed the saveHashMap method but I still have an error on loadHashMap() in the ObjectInputStream reader = new ObjectInputStream(freader);
     
  16. Offline

    r0306

    Loog
    Same as the writer, change freader to
    Code:
    FileInputStream freader = new FileInputStream("\\plugins\\MCInfected.bin");
     
  17. Offline

    Loog

    r0306
    Okay, that fixed that but now many parts of the method become underlined in red, the options are Add throws declaration and Surround with try/catch
     
  18. Offline

    r0306

    Loog
    Follow the hints given by the IDE and click Add throws declaration.
     
  19. Offline

    Loog

    Okay, that removed all errors, now for me to try it out. :D

    It gives me console errors, and also doesn't save the exp. :(
    The console errors.
    14:52:20 [INFO] [MCInfected] Enabling MCInfected v1.1
    14:52:20 [INFO] MCInfected version 1.1 has been enabled
    14:52:20 [SEVERE] java.io.FileNotFoundException: plugins\MCInfected\plugins\MCIn
    fected\exp.txt (The system cannot find the path specified)
    14:52:20 [SEVERE] at java.io.FileInputStream.open(Native Method)
    14:52:20 [SEVERE] at java.io.FileInputStream.<init>(Unknown Source)
    14:52:20 [SEVERE] at java.io.FileInputStream.<init>(Unknown Source)
    14:52:20 [SEVERE] at me.loogeh.mcinfected.MCInfected.loadHashMap(MCInfecte
    d.java:135)
    14:52:20 [SEVERE] at me.loogeh.mcinfected.MCInfected.onEnable(MCInfected.j
    ava:53)
    14:52:20 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:217)
    14:52:20 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:337)
    14:52:20 [SEVERE] at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:381)
    14:52:20 [SEVERE] at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftSe
    rver.java:257)
    14:52:20 [SEVERE] at org.bukkit.craftbukkit.CraftServer.enablePlugins(Craf
    tServer.java:239)
    14:52:20 [SEVERE] at net.minecraft.server.MinecraftServer.t(MinecraftServe
    r.java:373)
    14:52:20 [SEVERE] at net.minecraft.server.MinecraftServer.a(MinecraftServe
    r.java:360)
    14:52:20 [SEVERE] at net.minecraft.server.MinecraftServer.init(MinecraftSe
    rver.java:189)
    14:52:20 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftSer
    ver.java:424)
    14:52:20 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(Sour
    ceFile:492)
    14:52:20 [INFO] [TempBan] Enabling TempBan v1.2
    14:52:20 [INFO] [MobDisguise] Enabling MobDisguise v1.81
    14:52:20 [INFO] [MobDisguise] by desmin88 version 1.81 enabled.
    14:52:20 [INFO] [Buycraft] Enabling Buycraft v4.5
    14:52:21 [INFO] [Buycraft] Loaded 1 package(s) into the cache.
    14:52:21 [INFO] [Buycraft] Plugin has been successfully enabled.
    14:52:21 [INFO] Server permissions file permissions.yml is empty, ignoring it
    14:52:21 [INFO] Done (2.365s)! For help, type "help" or "?"
    14:52:21 [INFO] /192.168.1.3:55406 lost connection
    14:52:29 [INFO] /192.168.1.3:55418 lost connection
    14:52:35 [INFO] /192.168.1.3:55423 lost connection
    14:52:39 [WARNING] Can't keep up! Did the system time change, or is the server o
    verloaded?
    14:52:41 [INFO] /192.168.1.3:55431 lost connection
    14:52:46 [INFO] _Portal [/118.210.246.154:55436] logged in with entity id 207 at
    ([world] 708.5943435839757, 67.0, 80.30000001192093)
    14:52:46 [SEVERE] Could not pass event PlayerJoinEvent to MCInfected
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:304)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
    at net.minecraft.server.ServerConfigurationManager.c(ServerConfiguration
    Manager.java:134)
    at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:129)
    at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:94)
    at net.minecraft.server.Packet1Login.handle(SourceFile:68)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:246)
    at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:42)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:6
    5)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:559)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:451)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
    at me.loogeh.mcinfected.PlayerListener.onPlayerJoin(PlayerListener.java:
    31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302)
    ... 13 more
    14:52:47 [INFO] [Spout] Successfully authenticated _Portal's Spoutcraft client.
    Running client version: 1548
    14:52:50 [INFO] /192.168.1.3:55440 lost connection
    14:53:14 [INFO] Loogeh [/118.210.246.154:55458] logged in with entity id 6407 at
    ([world] 702.8391139263421, 71.0, 80.80160676760609)
    >

    Okay, I fixed the first set of errors, but I have no idea what is going on in the second part...

    dw, the errors fixed themselves... Thank you sooo much! You're going on the contributors list.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  20. Offline

    r0306

    Np. Lol. Oh. You might wanna add this to fix the errors on the first time:
    Code:
        public HashMap<String, Integer> loadHashMap()
        {
     
          HashMap<String, Integer> map = new HashMap<String, Integer>();
     
          FileReader freader = new FileReader("\\plugins\\MCInfected.bin");
          ObjectInputStream reader = new ObjectInputStream(freader);
          if(reader.available() > 0)
          {
               map = (HashMap<String, Integer>) reader.readObject();
          }
          reader.close(); //don't forget to close
     
          return map;
     
        }
    And also,
    Code:
        public void saveHashMap(HashMap<String, Integer> map)
        {
          File file = new File("\\plugins\\MCInfected.bin");
          if (!file.exists())
          {
              getDataFolder().mkdirs();
              file.createNewFile();
          }
          FileWriter fwriter = new FileWriter("\\plugins\\MCInfected.bin");
          ObjectOutputStream writer = new ObjectOutputStream(fwriter);
     
          writer.writeObject(map);
     
          writer.close(); //don't forget to close the writer
     
        }
     
Thread Status:
Not open for further replies.

Share This Page