Adds a level per kill

Discussion in 'Plugin Development' started by loplopk, Jun 25, 2012.

  1. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    im trying to make a plugin where it adds a level to a player everytime the player kills another player but i cannot seem to get it to work. post below if you can help. Here is the code
    Code:
    public class zzzz extends JavaPlugin{
       
        public void ondeath(PlayerDeathEvent event){
            Player p = event.getEntity();
            Player k = p.getKiller();
            if(k.getLevel() == 0){
                k.setLevel(1);
            }else if(k.getLevel() > 0){
                k.setLevel(k.getLevel() + 1);
            }
        }
       
    }
  2. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @loplopk
    You should learn the basics of Bukkit plugin developing first. You also need an onenable to register your plugin in.
    Code:
    public class zzzz extends JavaPlugin implements Listener {
     
        public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void onDisable() {
        }
     
        @EventHandler
        public void ondeath(PlayerDeathEvent event){
            Player p = event.getEntity();
            Player k = p.getKiller();
            k.setLevel(k.getLevel() + 1);
        }
     
    }

    This post has been edited 1 time. It was last edited by r0306 Jun 25, 2012.
  3. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I have that in another .class file
  4. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ok then all you need is this line:
    Code:
    k.setLevel(k.getLevel() + 1);
  5. Offline

    Njol

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    His point is still valid as you forgot the @EventHandler annotation and the class shouldn't extend JavaPlugin.
  6. Offline

    V10lator

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I don't believe you cause
    1. Why is you class implementing JavaPlugin then?
    2. The other class should give you an error cause zzzz does not implement Listener so your code shouldn't compile. But how can you tell that it won't work if you can't even compile it?

    Please give us your plugin.yml as well as the structure of all your class files. Like this:
    a.example.package.Some.class extends JavaPlugin implements Listener
    a.example.package.Another.class implements CommandExecuter
    a.example.package.Someother.class

    Also show us how exactly you are registering zzzz in the other class.

    //EDIT: Also don't forget the @EventHandler annotation like @Njol told.

    This post has been edited 1 time. It was last edited by V10lator Jun 25, 2012.
  7. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    ok so heres the other file
    Code:
    package me.nanhyh.pvp;
     
    import java.util.logging.Logger;
     
    import me.nanhyh.pvp.pvp;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class pvp extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static pvp plugin;
           
            public void onDisable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has Been Disabled");
            }
     
            public void onEnable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "has been enabled");
            }
    }
    here is the file rewritten
    Code:
    package me.nanhyh.pvp;
     
    import javax.xml.bind.Marshaller.Listener;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class zzzz extends Listener{
        @EventHandler
        public void ondeath(PlayerDeathEvent event){
            Player k = event.getEntity().getKiller();
            k.setLevel(k.getLevel() + 1);
            }
            }
            
    here is my plugin.yml
    Code:
    name: 1v1
    main: me.nanhyh.pvp.pvp
    version: 1.0
    description: >
                killing plugin.
     
    
  8. Offline

    Eballer48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You actually need to implement Listener not extend JavaPlugin..
  9. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @loplopk
    You didn't register the events for the zzzz class.
    http://wiki.bukkit.org/Introduction_to_the_New_Event_System

    Also, you don't need any of those variables and code you pasted in there... and you also don't need to have a diferent class for the listener, you can place the event in your main class... which name should start with an Upper case letter, recommended name for class: PvP.
  10. Online

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    you forgot to implement org.bukkit.event.Listener at the listener class
    you forgot to register it
    its recommend to get the plugin logger instead the minecraft globlal logger (JavaPlugin.getLogger())
    its recommend that classes start whit an capital letter
    You cant cast an enity to an player whitout an cast
  11. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    register it?
  12. Offline

    Orcem12

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
            getServer().getPluginManager().registerEvents(new Listener(this), this);

    This post has been edited 1 time. It was last edited by Orcem12 Jun 26, 2012.
  13. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @loplopk
    If you didn't ignored my post you would've found out what he meant.
    ferrybig and V10lator like this.
  14. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i saw your post just did not under stand it sorry..
  15. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I meant the link... there you have a really big title named "Registering Events" which explains stuff and gives examples... but anyway :)
  16. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    still working on it but doesnt seem to work heres what i have
    Code:
    package me.nanhyh.kit;
     
    import java.util.logging.Logger;
     
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class zonenable extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static fdsa plugin;
           
            public void onDisable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has Been Disabled");
            }
     
            public void onEnable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "has been enabled");
                getServer().getPluginManager().registerEvents(this, plugin);
            }
    }
    
    Code:
    package me.nanhyh.kit;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class zexp implements Listener{
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent event, Player player){
            Player k = event.getEntity().getKiller();
            Player j = player;
            if(event.getEntity().getKiller() == j){
                k.setExp(player.getExp() + 1);
        }}
    }
    
    k cause im still working on it but it doesnt seem to work

    This post has been edited 1 time. It was last edited by loplopk Jun 26, 2012.
  17. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You didn't register the corect class for the events..... either move that event in the main class OR specify the event class in the registerEvents()... someone provided you with an example above:
    There's also examples about this in the link I gave you.
  18. Online

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    if this is the listener and the main plugin class it works as you want
  19. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    this would be my main
    Code:
    package me.nanhyh.kit;
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class zonenable extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static fdsa plugin;
        public final zexp zexp = new zexp();
           
            public void onDisable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has Been Disabled");
            }
     
            public void onEnable() {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "has been enabled");
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(this.zexp, this);
            }
    }
    
    and this would be my exp plugin
    Code:
    package me.nanhyh.kit;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class zexp implements Listener{
       
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent event, Player player){
            Player k = event.getEntity().getKiller();
            Player j = player;
            if(event.getEntity().getKiller() == j){
                k.setExp(player.getExp() + 1);
        }}
    }
    
  20. Online

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    you already have registered it correctt, but your methode signature is inconrrect for the listener
  21. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    ?......
  22. Online

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    wrong: public void onPlayerDeath(PlayerDeathEvent event, Player player)
    good: public void onPlayerDeath(PlayerDeathEvent event)
  23. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Like this?
    Code:
    package me.nanhyh.pvp;
     
    import javax.xml.bind.Marshaller.Listener;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class zexp extends Listener implements org.bukkit.event.Listener{
        @EventHandler(priority = EventPriority.HIGHEST)
        public void ondeath(PlayerDeathEvent event){
            Player k = event.getEntity().getKiller();
            k.setLevel(k.getLevel() + 1);
            }
            }
           
           
     
    
  24. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    now it is working but im getting this error in consle
    Code:
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:460)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:322)
        at net.minecraft.server.EntityPlayer.die(EntityPlayer.java:173)
        at net.minecraft.server.EntityLiving.damageEntity(EntityLiving.java:677)
        at net.minecraft.server.EntityHuman.damageEntity(EntityHuman.java:595)
        at net.minecraft.server.EntityPlayer.damageEntity(EntityPlayer.java:215)
        at net.minecraft.server.EntityLiving.aA(EntityLiving.java:304)
        at net.minecraft.server.Entity.F_(Entity.java:268)
        at net.minecraft.server.EntityLiving.F_(EntityLiving.java:435)
        at net.minecraft.server.EntityHuman.F_(EntityHuman.java:159)
        at net.minecraft.server.EntityPlayer.a(EntityPlayer.java:228)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:341)
        at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at me.nanhyh.pvp.zz.ondeath(zz.java:14)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
        ... 20 more
    
  25. Online

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    mayby k == null, bacause it was not an player killed him=
  26. Offline

    loplopk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    thank you i fixed it and it works =)

Share This Page