My code doesn't work?

Discussion in 'Plugin Development' started by Zombeh_Kwer, Aug 15, 2014.

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

    Zombeh_Kwer

    Okay, so i'm very new at plugin development, and as practice i've decided to make a basic plugin that uses exp as health instead of the default hearts. I only wrote code that you receive exp when joining and respawning and die when your exp reaches zero, however it doesn't work when i test it on my server.
    Here is my code:

    package me.zombehkwer.fantasyonlinehealth;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class FantasyOnlineHealth extends JavaPlugin{
    @Override
    public void onDisable() {
    getLogger().info("onDisable has been invoked.");

    }

    @Override
    public void onEnable() {
    getLogger().info("onEnable has been invoked.");
    }
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent event) {
    Player player = event.getPlayer();
    float exp = player.getExp();
    player.giveExp(500);
    if (exp == 0); {
    player.setHealth(0);
    }
    }
    public void onPlayerRespawnEvent(PlayerRespawnEvent event) {
    Player player = event.getPlayer();
    player.giveExp(500);
    }
    }

    Eclipse says that there are no errors, could you please tell me why this code doesn't work, or if i need to put something in my plugin.yml.
     
  2. Offline

    TopTobster5

    Zombeh_Kwer What are the errors? It may also be useful to see your plugin.yml
     
  3. Offline

    Dealyise

    @Zombeh_Kwer
    You did not register your Listeners, and you didn't implement Listener.
     
  4. Offline

    DinosParkour

    Zombeh_Kwer
    Two notes:
    1. Final class? why
    2. Not needed SuppressWarnings("deprecation")?
    Other than these, it is mainly due to what Dealyise noticed :)
     
  5. Offline

    Garris0n

    Because you should under no circumstances extend your main class (or extend JavaPlugin more than once) and I would assume OP is following the plugin tutorial.
     
  6. Offline

    DinosParkour

    Garris0n isn't that his main class .-.
     
  7. Offline

    Garris0n

    Yes, exactly. Which should not be extended. Hence the final keyword...
     
  8. Offline

    MarinD99

    1. You didn't register the Listener, code below.
    2. You didn't implement the Listener(In this case, located in your Main class, though, I highly recommend using multiple classes for stuff like this).
    3. No need for the class to be final...
    4. Example code to help you out in the future:
    Code:java
    1. public class <YourClass> extends JavaPlugin implements Listener {
    2. // import these
    3. @Override
    4. public void onEnable()
    5. {
    6. getPluginManager().registerEvents(this, this);
    7. //register the Listener!
    8. }
    9.  
    10. @EventHandler
    11. public void onPlayerJoin(PlayerJoinEvent event)
    12. {
    13. Player p = event.getPlayer();
    14. // Do desired code here, too lazy to do it myself :P
    15.  
    16. // May contain mistakes, written by hand.
    17. }
     
    DinosParkour likes this.
Thread Status:
Not open for further replies.

Share This Page