Coins not increasing.

Discussion in 'Plugin Development' started by Andq1, Mar 15, 2014.

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

    Andq1

    Hello there so i have a plugin that does whenever you kill someone, it adds 5 coins to your account which is stored in a MySQL database. Now the whole plugin is working, but i made a mistake somewhere and i have no idea where and why. Because when i kill someone, i'm getting 5 coins, but when i kill someone again it stays a 5. So i discovered that it's because it dosen't increase, it just sets the coins to 5.

    Now this dosen't make sense, because the plugin finds the users current amount and then + 5 coins.
    I'm just gonna give you guys some parts of the code, and i'd really appreciate if you can help :)
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent evt) {
    3. try {
    4. if (evt instanceof PlayerDeathEvent) {
    5. PlayerDeathEvent event = (PlayerDeathEvent) evt;
    6. Player killer = event.getEntity().getKiller();
    7. String username = killer.getDisplayName();
    8. if (!database.checkUserExists(username))
    9. database.createUser(username);
    10. int current = database.getUsersCoinCount(username);
    11. int get = Integer.parseInt(database
    12. .getSetting("coins_per_kill"));
    13. database.setUsersCoinCount(username, current + get);
    14. }
    15. } catch (Exception e) {
    16.  
    17. }
    18.  
    19. }


    And another part

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("coinadd")) {
    2. String username = args[0];
    3. if (!database.checkUserExists(username))
    4. database.createUser(username);
    5. int current = database.getUsersCoinCount(username);
    6. int get = Integer.parseInt(args[1]);
    7. database.setUsersCoinCount(username, current + get);
    8. return true;
    9. }
    10. return false;
    11. }


    If you need more information please just leave a comment, as i'd really appreciate your help,
    Thanks in advance, and sorry for my poorly english c:
     
  2. Offline

    Wizehh

    Can you please post the code for the getUsersCoinCount() and setUsersCountCount() methods?
     
  3. Offline

    AoH_Ruthless

    Andq1
    Well, are you registering your events?
    When you catch an exception, you want to print the stacktrace so you know what the issue is. Also, you need to check if the killer's instance is a player.

    To fix this, just remove the EntityDeathEvent and try/catch block entirely. You can use a PlayerDeathEvent on it's own. The try/catch block should be used sparingly, only when you need it.
    So this is what your event should look like (You need to indent it yourself):

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent evt) {
    3. if (evt.getKiller() instanceof Player) {
    4. Player killer = (Player) event.getEntity().getKiller();
    5. String username = killer.getDisplayName();
    6. if (!database.checkUserExists(username)) {
    7. database.createUser(username);
    8. }
    9. int current = database.getUsersCoinCount(username);
    10. int get = Integer.parseInt(database
    11. .getSetting("coins_per_kill"));
    12. database.setUsersCoinCount(username, current + get);
    13. }
    14. }
     
  4. Offline

    Andq1

    Here you go :) Wizehh
    Code:java
    1. public int getUsersCoinCount(String username){
    2. if(!checkUserExists(username))
    3. return 0;
    4. ResultSet set = controller.getRow("users", "name = '"+username+"'");
    5. try{
    6. if(set.next())
    7. return set.getInt(3);
    8. }catch(Exception e){}
    9. return 0;
    10. }
    11.  
    12. public int getUsersCoinCount(int uid) {
    13. ResultSet set = controller.getRow("users", "id = "+uid);
    14. try{
    15. if(set.next())
    16. return set.getInt(3);
    17. }catch(Exception e){}
    18. return 0;
    19. }
    20.  

    Code:java
    1. public void setUsersCoinCount(String username, int coins){
    2. if(checkUserExists(username))
    3. controller.updateRow("users", "coins = "+coins, "name='"+username+"'");
    4. }


    :)
     
  5. Offline

    Wizehh

    Andq1
    Remove the try/catch blocks; they're redundant, especially since you're not printing the stacktrace.
    PHP:
    //change
    if (set.next())
    //to
    set.next()
        return 
    set.getInt(3);
     
  6. Offline

    Andq1

    I might have done something wrong perhabs, i'm kinda new to plugins so this was a big jump for me. I'm getting to errors with what you suggested.
    Code:java
    1. if (evt.getKiller() instanceof Player) {

    gives the error The method getKiller() is undefined for the type PlayerDeathEvent
    Code:java
    1. Player killer = (Player) event.getEntity().getKiller();

    gives the error event cannot be resolved

    Thanks in advance :)

    Wizehh
    Oh okay. It's done. Didn't fix the problem though :/

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

    Wizehh

    Andq1
    Oh boy. If you're not familiar with the basics of the language, you probably shouldn't be working with Bukkit, least of all databases.
    You should probably start here: http://docs.oracle.com/javase/tutorial/java/index.html , then work your way up to Bukkit. This may sound harsh, but unfortunately, we really can't help you when you don't understand the fundamentals.
    In answer to your question, though, change 'event' to 'evt' ('evt' was your name for the PlayerDeathEvent paramater).
     
  8. Offline

    Andq1

    Wizehh
    AoH_Ruthless
    I've already done that, i figured it out. But i'm still getting the The method getKiller() is undefined for the type PlayerDeathEvent.
     
  9. Offline

    Wizehh

    You'd first want to check if the killer is a player, then
    PHP:
    Player player = (Playere.getEntity().getKiller();
    However, you're probably best off doing what I suggested in my former post.
     
  10. Offline

    AoH_Ruthless

    Andq1
    Did you actually look at the code I restructured for you?
     
  11. Offline

    Andq1

    AoH_Ruthless
    Yes, i did. It did return a few erros though
    Code:java
    1. if (evt.getKiller() instanceof Player) {

    Had to be changed to
    Code:java
    1. if (evt.getEntity().getKiller() instanceof Player) {


    Code:java
    1. Player killer = (Player) event.getEntity().getKiller();

    I just had to replace event with evt.
    I appreciate your help alot, but this did not fix the problem

    Wizehh

    As you can see in AoH_Ruthless reconstruction he already added this :)

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

    Minesuchtiiii

    something like ..:
    Code:java
    1. public void addCoins(Player p, int coins) {
    2. int coins = cfg.getInt("Player." + p.getName() + ".Coins");
    3. int a = coins;
    4. cfg.set("Player." + p.getName() + ".Coins", coins+a;
    5. this.saveConfig() //Example with config so u see how it works ...

    same thing with removeCoins simply do coins-a;
     
  13. Offline

    Andq1

    Wizehh
    If your reffering to the part of learning fundementals, i got your point. Theres no need to quote it again :)
     
  14. Offline

    Wizehh

    This is the problem:
    In your try/catch block, you have it so if there's an error it will return 0 for the player's coin count; so, oubviously, there's an error occuring. I suggest you add this statement in your catch block:
    PHP:
    <exception-variable-name>.printStackTrace();
    That should give you some idea of the problem.
     
  15. Offline

    Andq1

    Wizehh

    I think we're kinda mixing things up a little. If you see what AoH_Ruthless did to the code, he removed the try/catch block. And that's the code i went with. So what i tried was using the old code again and
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent evt) {
    3. try {
    4. if (evt instanceof PlayerDeathEvent) {
    5. PlayerDeathEvent event = (PlayerDeathEvent) evt;
    6. Player killer = event.getEntity().getKiller();
    7. String username = killer.getDisplayName();
    8. if (!database.checkUserExists(username))
    9. database.createUser(username);
    10. int current = database.getUsersCoinCount(username);
    11. int get = Integer.parseInt(database
    12. .getSetting("coins_per_kill"));
    13. database.setUsersCoinCount(username, current + get);
    14. }
    15. } catch (Exception e) {
    16. e.printStackTrace();
    17. }
    18.  
    19. }


    I just discovered if i do /kill i get this message
    Code:
    [19:03:53 INFO]: Andq1 issued server command: /kill
    [19:03:53 WARN]: java.lang.NullPointerException
    [19:03:53 WARN]:        at co.uk.n3network.msgs.MSGSPlugin.onEntityDeath(MSGSPlu
    gin.java:49)
    [19:03:53 WARN]:        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown S
    ource)
    [19:03:53 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
    wn Source)
    [19:03:53 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [19:03:53 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(Jav
    aPluginLoader.java:318)
    [19:03:53 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(Regist
    eredListener.java:62)
    [19:03:53 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(Simpl
    ePluginManager.java:486)
    [19:03:53 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(Simpl
    ePluginManager.java:471)
    [19:03:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactor
    y.callPlayerDeathEvent(CraftEventFactory.java:349)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.EntityPlayer.die(EntityP
    layer.java:368)
    [19:03:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.entity.CraftLivingEnti
    ty.setHealth(CraftLivingEntity.java:85)
    [19:03:53 WARN]:        at org.bukkit.command.defaults.KillCommand.execute(KillC
    ommand.java:33)
    [19:03:53 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:175)
    [19:03:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCo
    mmand(CraftServer.java:683)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.PlayerConnection.handleC
    ommand(PlayerConnection.java:952)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.PlayerConnection.a(Playe
    rConnection.java:814)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(Packe
    tPlayInChat.java:28)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(
    PacketPlayInChat.java:47)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.NetworkManager.a(Network
    Manager.java:146)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.ServerConnection.c(Sourc
    eFile:134)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.u(Minecr
    aftServer.java:655)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.DedicatedServer.u(Dedica
    tedServer.java:250)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.t(Minecr
    aftServer.java:545)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.MinecraftServer.run(Mine
    craftServer.java:457)
    [19:03:53 WARN]:        at net.minecraft.server.v1_7_R1.ThreadServerApplication.
    run(SourceFile:617)
    [19:03:53 INFO]: Andq1 died
    >
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page