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 @EventHandlerpublic void onEntityDeath(EntityDeathEvent evt) {try {if (evt instanceof PlayerDeathEvent) {PlayerDeathEvent event = (PlayerDeathEvent) evt;Player killer = event.getEntity().getKiller();String username = killer.getDisplayName();if (!database.checkUserExists(username))database.createUser(username);int current = database.getUsersCoinCount(username);int get = Integer.parseInt(database.getSetting("coins_per_kill"));database.setUsersCoinCount(username, current + get);}} catch (Exception e) { } } And another part Code:java if (cmd.getName().equalsIgnoreCase("coinadd")) {String username = args[0];if (!database.checkUserExists(username))database.createUser(username);int current = database.getUsersCoinCount(username);int get = Integer.parseInt(args[1]);database.setUsersCoinCount(username, current + get);return true;}return false;} 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:
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 @EventHandlerpublic void onPlayerDeath(PlayerDeathEvent evt) {if (evt.getKiller() instanceof Player) {Player killer = (Player) event.getEntity().getKiller();String username = killer.getDisplayName();if (!database.checkUserExists(username)) {database.createUser(username);}int current = database.getUsersCoinCount(username);int get = Integer.parseInt(database.getSetting("coins_per_kill"));database.setUsersCoinCount(username, current + get);}}
Here you go Wizehh Code:java public int getUsersCoinCount(String username){if(!checkUserExists(username))return 0;ResultSet set = controller.getRow("users", "name = '"+username+"'");try{if(set.next())return set.getInt(3);}catch(Exception e){}return 0;} public int getUsersCoinCount(int uid) {ResultSet set = controller.getRow("users", "id = "+uid);try{if(set.next())return set.getInt(3);}catch(Exception e){}return 0;} Code:java public void setUsersCoinCount(String username, int coins){if(checkUserExists(username))controller.updateRow("users", "coins = "+coins, "name='"+username+"'");}
Andq1 Remove the try/catch blocks; they're redundant, especially since you're not printing the stacktrace. PHP: //changeif (set.next())//toset.next() return set.getInt(3);
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 if (evt.getKiller() instanceof Player) { gives the error The method getKiller() is undefined for the type PlayerDeathEvent Code:java 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.
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).
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.
You'd first want to check if the killer is a player, then PHP: Player player = (Player) e.getEntity().getKiller(); However, you're probably best off doing what I suggested in my former post.
AoH_Ruthless Yes, i did. It did return a few erros though Code:java if (evt.getKiller() instanceof Player) { Had to be changed to Code:java if (evt.getEntity().getKiller() instanceof Player) { Code:java 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.
something like ..: Code:java public void addCoins(Player p, int coins) {int coins = cfg.getInt("Player." + p.getName() + ".Coins");int a = coins;cfg.set("Player." + p.getName() + ".Coins", coins+a;this.saveConfig() //Example with config so u see how it works ... same thing with removeCoins simply do coins-a;
Wizehh If your reffering to the part of learning fundementals, i got your point. Theres no need to quote it again
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.
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 @EventHandler public void onEntityDeath(EntityDeathEvent evt) { try { if (evt instanceof PlayerDeathEvent) { PlayerDeathEvent event = (PlayerDeathEvent) evt; Player killer = event.getEntity().getKiller(); String username = killer.getDisplayName(); if (!database.checkUserExists(username)) database.createUser(username); int current = database.getUsersCoinCount(username); int get = Integer.parseInt(database .getSetting("coins_per_kill")); database.setUsersCoinCount(username, current + get); } } catch (Exception e) { e.printStackTrace(); } } 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.