Events not fired (FoodLevelChangeEvent)

Discussion in 'Plugin Development' started by Badeye, Nov 6, 2013.

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

    Badeye

    Hey all,
    my goal was to have a configurationfile where you can set MaxHunger to a value between 0-20. If it's for example 18, the Food level cant go above 18, which would aloow you to eat everytime without waiting til the food level goes down to a value unter 20.

    This was my code so far, but none of the debug statements was called. No errors in the console.

    Code:java
    1. package me.badeye.maxhunger;
    2.  
    3. import java.io.File;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.entity.EntityType;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.FoodLevelChangeEvent;
    12. import org.bukkit.event.player.PlayerRespawnEvent;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class main extends JavaPlugin implements Listener{
    17.  
    18. Logger log = Logger.getLogger("MaxHunger"); //Logger to print stuff in console
    19. FileConfiguration config;//Tell plugin we are going to usa a config file
    20.  
    21. @Override
    22. public void onEnable() {
    23. loadConfiguration();//Load configfile
    24. PluginDescriptionFile pff = this.getDescription();//Get info from plugin.yml
    25. String PluginName = pff.getName();//Get the name of the plugin from plugin.yml
    26. String PluginVersion = pff.getVersion();//Get the version number of the plugin from plugin.yml
    27. log.info(PluginName + PluginVersion + " is enabled.");//Print to console that plugin is enabled
    28. getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. @Override
    32. public void onDisable() {
    33. PluginDescriptionFile pff = this.getDescription();//Get info from plugin.yml
    34. String PluginName = pff.getName();//Get the name of the plugin from plugin.yml
    35. String PluginVersion = pff.getVersion();//Get the version number of the plugin from plugin.yml
    36. log.info(PluginName + PluginVersion + " is disabled.");//Print to console that plugin is disabled
    37. }
    38.  
    39. public void loadConfiguration() {//to load config file
    40. config = getConfig();//get the configuration
    41.  
    42. addDefault("enableMaxFood", "true");
    43. addDefault("FoodLevel", "19.0");
    44. addDefault("enableMaxHealth", "false");
    45. addDefault("HealthLevel", "20.0");
    46.  
    47. config.options().copyDefaults(true);//copy the default from above
    48. saveConfig();//write them to file
    49. }
    50.  
    51.  
    52. private void addDefault(String o, String p) {//add and set default
    53. File configFile = new File("plugins" + File.separator + this.getDescription().getName() + File.separator + "config.yml");//config file
    54. config.addDefault(o, p);//add default to config
    55. if (configFile.exists() == false) {//if there is no config file, set defaults as well!
    56. config.set(o, p);
    57. }
    58. }
    59.  
    60. @EventHandler
    61. public void onPlayerRespawn(PlayerRespawnEvent e){
    62. Player p = e.getPlayer();
    63. System.out.println("PlayerRespawnEvent called");
    64.  
    65. int foodmax = getConfig().getInt("FoodLevel");
    66. int healthm = getConfig().getInt("HealthLevel");
    67. float healthmax = Float.valueOf(healthm);
    68. boolean foodenable = getConfig().getBoolean("enableMaxFood");
    69. boolean healthenable = getConfig().getBoolean("enableMaxHealth");
    70.  
    71. if (foodenable == true){
    72. p.setFoodLevel(foodmax);
    73. p.sendMessage("Respawn Food Level = " + p.getFoodLevel()); //debug to see the current health
    74. }
    75. if (healthenable == true){
    76. p.setHealth(healthmax);
    77. }
    78. }
    79.  
    80. //when a player's food level get's changed, and if it's above 19, it gets set back to 19
    81. @EventHandler
    82. public void onFoodLevelChange(FoodLevelChangeEvent e) {
    83. Player p = (Player)e.getEntity();
    84. System.out.println("FoodLevelChangeEvent called");
    85.  
    86. int foodmax = getConfig().getInt("FoodLevel");
    87. int healthm = getConfig().getInt("HealthLevel");
    88. float healthmax = Float.valueOf(healthm);
    89. boolean foodenable = getConfig().getBoolean("enableMaxFood");
    90. boolean healthenable = getConfig().getBoolean("enableMaxHealth");
    91.  
    92. if (foodenable == true){
    93. if(e.getEntityType() == EntityType.PLAYER) {
    94. p.sendMessage("foodlevel1 = " + e.getFoodLevel()); //debug
    95. if (e.getFoodLevel() > foodmax){
    96. e.setFoodLevel(foodmax);
    97. p.sendMessage("foodlevel2 = " + e.getFoodLevel()); //debug
    98. }
    99. }
    100. }
    101. if (healthenable == true){
    102. if(e.getEntityType() == EntityType.PLAYER) {
    103. p.sendMessage("healthlevel1 = " + p.getHealth()); //debug
    104. if (p.getHealth() > healthmax){
    105. p.setHealth(healthmax);
    106. p.sendMessage("healthlevel2 = " + p.getHealth()); //debug
    107. }
    108. }
    109. }
    110. }
    111. }
    112.  
    113.  


    Yeah, i know, the getHealth() function is underlined but for this bukkit version it can't be handeld without an error. We have to ignore it.
    Again, i just want when you eat a steak that the food value goes just til 19 (or if it goes up to 20 it gets set back to 19). For that reason I've implemented a "food setter" when a player respawns.

    Has anybody an idea? For me it's just not working but from the code so far i cant see any mistakes, did I choose the wrong events?

    Thanks
    badeye
     
  2. Offline

    sd5

    Badeye You are missing the @EventHandler above the event method ;)
    And you have to register the listener in onEnable()
     
    Badeye likes this.
  3. Offline

    Chinwe

    You have to register the listener in onEnable() ;)
     
    Badeye likes this.
  4. Offline

    Badeye

    Okay guys, thanks, but where exactly is the Event handler missing? Thought I've added one...
    And how should i implement the listener (I'm very new to java, you can't believe how proud I am that i came that far :D)
     
  5. Offline

    sd5

    Badeye You've added it here
    Code:java
    1. @EventHandler
    2. public void onPlayerRespawn(PlayerRespawnEvent e){

    but forgot it here
    Code:java
    1. public void onFoodLevelChange(FoodLevelChangeEvent e) {

    To register the listener containing the events add this to your onEnable()
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);
     
    Badeye likes this.
  6. Offline

    Badeye

    Oka ythanks alot, I'll try it out :D
     
  7. Offline

    sd5

  8. Offline

    Badeye

    Thanks, I'll look into that.
    sooo...
    Code:
    18:53:11 [SEVERE] Could not pass event FoodLevelChangeEvent to MaxHunger v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:363)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:479)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:464)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callFoodLevelC
    hangeEvent(CraftEventFactory.java:505)
            at net.minecraft.util.FoodStats.func_75118_a(FoodStats.java:68)
            at net.minecraft.entity.player.EntityPlayer.func_70071_h_(EntityPlayer.j
    ava:432)
            at net.minecraft.entity.player.EntityPlayerMP.func_71127_g(EntityPlayerM
    P.java:398)
            at net.minecraft.network.NetServerHandler.func_72498_a(NetServerHandler.
    java:515)
            at net.minecraft.network.packet.Packet10Flying.func_73279_a(Packet10Flyi
    ng.java:51)
            at net.minecraft.network.TcpConnection.func_74428_b(TcpConnection.java:4
    64)
            at net.minecraft.network.NetServerHandler.func_72570_d(NetServerHandler.
    java:231)
            at net.minecraft.network.NetworkListenThread.func_71747_b(NetworkListenT
    hread.java:54)
            at net.minecraft.server.dedicated.DedicatedServerListenThread.func_71747
    _b(DedicatedServerListenThread.java:34)
            at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.jav
    a:872)
            at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(Dedicated
    Server.java:318)
            at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.jav
    a:741)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:625)
            at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.
    java:16)
    Caused by: java.lang.Error: Unresolved compilation problems:
            The method getHealth() is ambiguous for the type Player
            The method getHealth() is ambiguous for the type Player
            The method getHealth() is ambiguous for the type Player
     
            at me.badeye.maxhunger.main.onFoodLevelChange(main.java:105)
            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:361)
            ... 18 more
    Looks like getHealth is not possible :/

    Code:java
    1. p.sendMessage("foodlevel1 = " + e.getFoodLevel());

    This one got called, but it said "foodlevel1 = 0". My food level was around 18 and i ate a steak (which would set it normaly too food level 20)
     
  9. Offline

    Chinwe

    Read this about ambiguous getHealth() :)
    Proud of second ninja'ing :cool: sd5
     
  10. Offline

    sd5

  11. Offline

    Badeye

    Oh man... I'm getting realy annoying, am I right? :D

    I've read through the posts about the solution/change about the getHealth feature. As far as i understood it, it's now possible to use the get.Health feature when a Double Type is used, or the float type? I've tryed both like this:
    Code:java
    1. Float x = p.getHealth();
    2. Double y = p.getHealth();

    But they are still underlined and marked as ambiguous getHealth()...

    So what am I doing wrong, or am I completly blind and not seeing the obvious? I'm very sorry, this is bothering me on so many levels :(
     
  12. Offline

    sd5

    Badeye likes this.
  13. Offline

    Badeye

    Ahhh! I thought I have to do something with my servers craftbukkit.jar, but because I'm using the modded MCPC+ i was worried that I would not be able to do it... now I got it. Thanks alot!

    sd5 So now to the actual plugin logics.
    I've respawned and I've got the message "
    Respawn Food Level = 0
    nothing happened exept this text.

    I've lost (naturaly after a while) a bit hunger, which caused the FoodLevelChangeEvent to fire. It got launched and my food went imediatly down to 0, so my food bar was empty. Now two debug statements where shown (at the same time):
    foodlevel1 = 19
    foodlevel2 =0
    Thats it. So it went to 19 and shorly after to 0.

    After those 2 statements where shown i've ate a few steaks. The maximun food level was not 19, it went up to 20 again.
    Just wanted to share this, now it's time to solve it :D

    Edit: I've edited the code above to the newest version (just a few fixes).

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

    sd5

    Badeye Did you check your config? Your code in the respawn event looks correct but it seems that it can't get the value from the config and uses 0 then. That could also be the cause in the other event. You print the foodlevel (19) then check if it's greater than the maximum (0, because config failed maybe) so it is also set to 0.
     
  15. Offline

    Badeye

    sd5
    So I've tryed to just replace the functions/values from the config file with actual fixed values in the code. Now whenever i loose hunger the hunger level gets printed out (foodlevel1 = 17... = 16 ... = 15 ...) But when i eat i still get 20 points instead of 19 and no message is printed out (i know, it's strange).

    Here is my idea: When i get the food level with e.getFoodLevel(); it actually just gives me the value before the changing. that would explain why i still get 20 points, because value before change =19, value after change =19+food that the steak gives (3 points), so it's 20 again.

    Respawning still gives me (with fixed values) the message = 0 and no food change. Hmm I'm having some logic erros.

    Edit: woops, totaly forgot to look into my server log oO
    Code:
    22:45:08 [SEVERE] Could not pass event PlayerItemConsumeEvent to DayZ v0.5
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:363)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:479)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:464)
            at net.minecraft.entity.player.EntityPlayer.func_71036_o(EntityPlayer.ja
    va:498)
            at net.minecraft.entity.player.EntityPlayerMP.func_71036_o(EntityPlayerM
    P.java:1204)
            at net.minecraft.entity.player.EntityPlayer.func_70071_h_(EntityPlayer.j
    ava:324)
            at net.minecraft.entity.player.EntityPlayerMP.func_71127_g(EntityPlayerM
    P.java:398)
            at net.minecraft.network.NetServerHandler.func_72498_a(NetServerHandler.
    java:515)
            at net.minecraft.network.packet.Packet10Flying.func_73279_a(Packet10Flyi
    ng.java:51)
            at net.minecraft.network.TcpConnection.func_74428_b(TcpConnection.java:4
    64)
            at net.minecraft.network.NetServerHandler.func_72570_d(NetServerHandler.
    java:231)
            at net.minecraft.network.NetworkListenThread.func_71747_b(NetworkListenT
    hread.java:54)
            at net.minecraft.server.dedicated.DedicatedServerListenThread.func_71747
    _b(DedicatedServerListenThread.java:34)
            at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.jav
    a:872)
            at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(Dedicated
    Server.java:318)
            at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.jav
    a:741)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:625)
            at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.
    java:16)
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 20.0
     
            at org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity.setHealth(Cra
    ftLivingEntity.java:59)
            at me.abridell.DayZ.PlayerListener.onConsume(PlayerListener.java:105)
            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:361)
            ... 18 more
    I've replaced the config value from health back to 20 instead of 20,0 , thats the mistake
     
  16. Offline

    maxben34

    Badeye

    I don't know if you're aware but setting health can be done if it's a double. You can't use an integer value without getting an error... but it works If you use a double (I know this doesn't really solve your problem but hopefully I was some sort of help to you :p)
     
  17. Offline

    Badeye

    Yeah I fixed it with setting it to 20.0 instead of just 20 thanks :)

    When i respawn i get the message food = 19 but it's 20, when i loose hunger naturaly i get messages for every half-food-point (like i described above) but i can still "eat" the level up to 20, without a message :/ During the whole process fixed values where used, no config in this try. This is annoying.
     
Thread Status:
Not open for further replies.

Share This Page