Solved Problem in My Code

Discussion in 'Plugin Development' started by dlcruz129, Feb 17, 2013.

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

    dlcruz129

    Hello.

    I am writing a plugin to detect headshots. Here is the code:

    Code:
    package com.ravinggames.plugins.Headshot;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Color;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Headshot extends JavaPlugin implements Listener {
     
        Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            logger.info("Headshot v1.0 Enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            logger.info("Headshot v1.0 Disabled");
        }
     
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if(event.getCause() != DamageCause.PROJECTILE) {
                return;
            }
            Projectile proj = (Projectile) event.getDamager();
            if(!(proj.getShooter() instanceof Player)) {
                return;
            }
         
            Entity shot = event.getEntity();
         
            double y = proj.getLocation().getY();
            double shotY = shot.getLocation().getY();
            boolean headshot = y - shotY > 1.35d;
            Player shotPlayer = null;
         
            if (shot instanceof Player) {
                shotPlayer = (Player) shot;
            }
            if(headshot){
                if (shotPlayer.hasPermission("Headshot.protect")) {
                    //Nothing 
                } else {
                    event.setDamage(event.getDamage() * 2);
                    String message = "Headshot on" + ((Player) shot).getDisplayName();
                    ((Player)proj.getShooter()).sendMessage(Color.LIME + message);
                    PotionEffect blind = new PotionEffect(PotionEffectType.BLINDNESS, (2 * 20), 2);
                    PotionEffect nausea = new PotionEffect(PotionEffectType.CONFUSION, (5*20), 2);
                    shotPlayer.addPotionEffect(blind, true);
                    shotPlayer.addPotionEffect(nausea, true);
                }
            }
        }
    }
    
    When I run it on my server, I get: [SEVERE] Could not pass event EntityDamageByEntityEvent to Headshot v1.0 whenever I shoot a monster in the head. The plugin is only for player headshots, but of course I don't want the console being spammed when I shoot a zombie.

    Any help is appreciated.
     
  2. Offline

    iBawb

    Remove the event priority and try again. Sometimes it can cause errors.
     
  3. Offline

    dlcruz129

    iBawb Same error. My current code:
    Code:
    package com.ravinggames.plugins.Headshot;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Color;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Headshot extends JavaPlugin implements Listener {
     
        Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable() {
            logger.info("Headshot v1.0 Enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            logger.info("Headshot v1.0 Disabled");
        }
     
        @EventHandler(ignoreCancelled = false)
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if(event.getCause() != DamageCause.PROJECTILE) {
                return;
            }
            Projectile proj = (Projectile) event.getDamager();
            if(!(proj.getShooter() instanceof Player)) {
                return;
            }
         
            Entity shot = event.getEntity();
         
            double y = proj.getLocation().getY();
            double shotY = shot.getLocation().getY();
            boolean headshot = y - shotY > 1.35d;
            Player shotPlayer = null;
         
            if (shot instanceof Player) {
                shotPlayer = (Player) shot;
            }
            if(headshot){
                if (shotPlayer.hasPermission("Headshot.protect")) {
                    //Nothing 
                } else {
                    event.setDamage(event.getDamage() * 2);
                    String message = "Headshot on" + ((Player) shot).getDisplayName();
                    ((Player)proj.getShooter()).sendMessage(Color.LIME + message);
                    PotionEffect blind = new PotionEffect(PotionEffectType.BLINDNESS, (2 * 20), 2);
                    PotionEffect nausea = new PotionEffect(PotionEffectType.CONFUSION, (5*20), 2);
                    shotPlayer.addPotionEffect(blind, true);
                    shotPlayer.addPotionEffect(nausea, true);
                }
            }
        }
    }
    
     
  4. Offline

    iBawb

    What line is the error in?
     
  5. Offline

    dlcruz129

    iBawb Not sure, it only tells me:
    [SEVERE] Could not pass event EntityDamageByEntityEvent to Headshot v1.0.

    According to eclipse the code has no errors, CraftBukkit is simply failing to pass the event to the plugin.
     
  6. Offline

    iBawb

    Look in your logs, further down from that it should say Caused by:
    Look for the first line referring to one of your classes, and it should have a :# next to it, where # is the line number.
     
  7. Offline

    dlcruz129

    iBawb Ah, there it is, here's the full error:

    Code:
    [SEVERE] Could not pass event EntityDamageByEntityEvent to Headshot v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callEvent(CraftEventFactory.java:81)
        at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:369)
        at org.bukkit.craftbukkit.v1_4_R1.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:391)
        at net.minecraft.server.v1_4_R1.EntityLiving.damageEntity(EntityLiving.java:686)
        at net.minecraft.server.v1_4_R1.EntityMonster.damageEntity(EntityMonster.java:39)
        at net.minecraft.server.v1_4_R1.EntityArrow.j_(EntityArrow.java:226)
        at net.minecraft.server.v1_4_R1.World.entityJoinedWorld(World.java:1334)
        at net.minecraft.server.v1_4_R1.WorldServer.entityJoinedWorld(WorldServer.java:548)
        at net.minecraft.server.v1_4_R1.World.playerJoinedWorld(World.java:1315)
        at net.minecraft.server.v1_4_R1.World.tickEntities(World.java:1193)
        at net.minecraft.server.v1_4_R1.WorldServer.tickEntities(WorldServer.java:445)
        at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:580)
        at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NullPointerException
        at com.ravinggames.plugins.Headshot.Headshot.onEntityDamageByEntity(Headshot.java:50)
        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.java:425)
        ... 19 more
    I believe Headshot.java:50 means line 50, am I correct?

    EDIT: Line 50 is:
    if (shotPlayer.hasPermission("Headshot.protect")) {

    I think that's my problem, its trying to check if the zombie has a permission. A few lines before, I check if the attacked Entity is a Player. If it is not, how do I get it to ignore the rest of the code?
     
  8. Offline

    iBawb

    Code:
    if (shot instanceof Player && headshot) {
        Player shotPlayer = (Player) shot;
        if(shotPlayer.hasPermission("headshot.protect")){
        }else{
    //CODE
        }
    }else{
    }
     
  9. Offline

    dlcruz129

    Thanks a ton!
     
  10. Offline

    iBawb

    No problem! :)
     
  11. Offline

    Agentleader1

    I know this is an old post, and already solved, but seriously?

    Most of the code is copied from BCBroz, at least rearrange it around please! Or something similar...
     
  12. Offline

    Jaaakee224

    @Agentleader1
    This thread is almost two years old, and the OP hasn't been online since March 2013. I don't see any reason why you should have bumped this.

    @timtower Lock this please.
     
    Skionz and Pacothegint like this.
Thread Status:
Not open for further replies.

Share This Page