Solved Weird glitch; Need help ASAP

Discussion in 'Plugin Development' started by FireBreath14, Mar 5, 2013.

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

    FireBreath14

    On an EntityDamageByEntityEvent, i have these two checks involved:

    Code:
    if(damager instanceof Snowball && victim instanceof Player){
    and

    Code:
    if(damager instanceof Player && victim instanceof Player){
    So i'ts checking if a player was hit by another player (carrying a diamond sword) or a snowball. After each check, it executes some code. But my problem is the checks. Heres the weird glitch:

    Player 1 has 5 hearts left. Player 2 strikes them with a diamond sword. Player 1 dies. The second check doesnt work. Instead of it running the code, it seems like it returns false. But if player 1 has full health and is killed by a sword, it works.

    My server has a blaze rod that fires snowballs. So players kill each other with a snowball and/or sword. But if they mix the two (and kill with sword) the code glitches out. Is there an "instanceof" that I should be using instead of Player to determine if it was a sword hit? Please help ASAP!
     
  2. Offline

    joehot2000

    why not get the item in the hand of the damager?

    Oh, and that code should work. (though i am REALLY tired right now, may have missed a glitch)

    Why not send yourself debug messages?
     
  3. Offline

    RealDope

    Seems either like a bug with your full code (post please) or a problem with this other plugin that makes blaze rods shoot snowballs.
     
  4. Offline

    iWareWolf

  5. Offline

    FireBreath14

    iWareWolf
    RealDope
    K heres the code. lol all help is appreciated :)

    Code:
    public class OnHit implements Listener{
     
    Backbone plugin;
     
        OnHit(Backbone c) {
            plugin = c;
        }
        @EventHandler
        public void onHit(EntityDamageByEntityEvent event){
            Entity damager = event.getDamager();
            Entity victim = event.getEntity();
            if(damager instanceof Snowball && victim instanceof Player){ //if a snowball hit a player
                Player target = (Player)event.getEntity();
                Snowball shot = (Snowball)event.getDamager();
                Player killer = (Player)shot.getShooter();
                World w = target.getWorld();
                //
                //target is the victim; killer is the assasin
                //
                target.damage(4);
           
                if(target.getHealth() <= 0){
                    //:O Theyre dead!
                    target.setHealth(20);
                    String name = target.getName();
                    if(plugin.getConfig().contains("rplist."+name)){
                        double x = plugin.getConfig().getDouble("redspawn.x");
                        double y = plugin.getConfig().getDouble("redspawn.y");
                        double z = plugin.getConfig().getDouble("redspawn.z");
                        Location l = new Location(w,x,y,z);
                        target.teleport(l);
                        String dguy = target.getName();
                        String aguy = killer.getName();
                        killer.sendMessage(ChatColor.GREEN+"You killed "+dguy+"!"+ChatColor.DARK_PURPLE+" +100");
                        target.sendMessage(ChatColor.RED+aguy+" killed you!");               
                        if(plugin.getConfig().contains(aguy)){
                            int points = plugin.getConfig().getInt(aguy);
                            plugin.getConfig().set(aguy, points+100);
                            plugin.saveConfig();
                        }else{
                            plugin.getConfig().set(aguy, 100);
                            plugin.saveConfig();
                        }               
                        PlayerInventory inv = target.getInventory();
                        ItemStack brod = new ItemStack(Material.BLAZE_ROD, 1);
                        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                        inv.addItem(brod);
                        inv.addItem(sword);
                        target.setLevel(40);
                        ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
                        LeatherArmorMeta meta = (LeatherArmorMeta) helm.getItemMeta();
                        meta.setColor(Color.RED);
                        helm.setItemMeta(meta);
                        target.getInventory().setHelmet(helm);
                        //chestplate
                        ItemStack ches = new ItemStack(Material.LEATHER_CHESTPLATE);
                        LeatherArmorMeta metat = (LeatherArmorMeta) ches.getItemMeta();
                        metat.setColor(Color.RED);
                        ches.setItemMeta(metat);
                        target.getInventory().setChestplate(ches);
                        //leggings
                        ItemStack legg = new ItemStack(Material.LEATHER_LEGGINGS);
                        LeatherArmorMeta metatt = (LeatherArmorMeta) legg.getItemMeta();
                        metatt.setColor(Color.RED);
                        legg.setItemMeta(metatt);
                        target.getInventory().setLeggings(legg);
                        //boots
                        ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
                        LeatherArmorMeta metattt = (LeatherArmorMeta) boot.getItemMeta();
                        metattt.setColor(Color.RED);
                        boot.setItemMeta(metattt);
                        target.getInventory().setBoots(boot);
                    }
                    if(plugin.getConfig().contains("bplist."+name)){
                        double x = plugin.getConfig().getDouble("bluespawn.x");
                        double y = plugin.getConfig().getDouble("bluespawn.y");
                        double z = plugin.getConfig().getDouble("bluespawn.z");
                        Location l = new Location(w,x,y,z);
                        target.teleport(l);
                        String dguy = target.getName();
                        String aguy = killer.getName();
                        killer.sendMessage(ChatColor.GREEN+"You killed "+dguy+"!"+ChatColor.DARK_PURPLE+" +100");
                        target.sendMessage(ChatColor.RED+aguy+" killed you!");
                        if(plugin.getConfig().contains(aguy)){
                            int points = plugin.getConfig().getInt(aguy);
                            plugin.getConfig().set(aguy, points+100);
                            plugin.saveConfig();
                        }else{
                            plugin.getConfig().set(aguy, 100);
                            plugin.saveConfig();
                        }
                        PlayerInventory inv = target.getInventory();
                        ItemStack brod = new ItemStack(Material.BLAZE_ROD, 1);
                        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                        inv.addItem(brod);
                        inv.addItem(sword);
                        target.setLevel(40);
                        ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
                        LeatherArmorMeta meta = (LeatherArmorMeta) helm.getItemMeta();
                        meta.setColor(Color.BLUE);
                        helm.setItemMeta(meta);
                        target.getInventory().setHelmet(helm);
                        //chestplate
                        ItemStack ches = new ItemStack(Material.LEATHER_CHESTPLATE);
                        LeatherArmorMeta metat = (LeatherArmorMeta) ches.getItemMeta();
                        metat.setColor(Color.BLUE);
                        ches.setItemMeta(metat);
                        target.getInventory().setChestplate(ches);
                        //leggings
                        ItemStack legg = new ItemStack(Material.LEATHER_LEGGINGS);
                        LeatherArmorMeta metatt = (LeatherArmorMeta) legg.getItemMeta();
                        metatt.setColor(Color.BLUE);
                        legg.setItemMeta(metatt);
                        target.getInventory().setLeggings(legg);
                        //boots
                        ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
                        LeatherArmorMeta metattt = (LeatherArmorMeta) boot.getItemMeta();
                        metattt.setColor(Color.BLUE);
                        boot.setItemMeta(metattt);
                        target.getInventory().setBoots(boot);
                    }
                }
           
            }
            if(damager instanceof Player && victim instanceof Player){ //if a dude hit a dude
                Player target = (Player)event.getEntity();
                Player killer = (Player)event.getDamager();
                World w = target.getWorld();
                //
                //target is the victim; killer is the assasin
                //
                target.damage(4);
           
                if(target.getHealth() <= 0){
                    //:O Theyre dead!
                    target.setHealth(20);
                    String name = target.getName();
                    if(plugin.getConfig().contains("rplist."+name)){
                        double x = plugin.getConfig().getDouble("redspawn.x");
                        double y = plugin.getConfig().getDouble("redspawn.y");
                        double z = plugin.getConfig().getDouble("redspawn.z");
                        Location l = new Location(w,x,y,z);
                        target.teleport(l);
                        String dguy = target.getName();
                        String aguy = killer.getName();
                        killer.sendMessage(ChatColor.GREEN+"You killed "+dguy+"!"+ChatColor.DARK_PURPLE+" +100");
                        target.sendMessage(ChatColor.RED+aguy+" killed you!");
                        PlayerInventory inv = target.getInventory();
                        ItemStack brod = new ItemStack(Material.BLAZE_ROD, 1);
                        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                        inv.addItem(brod);
                        inv.addItem(sword);
                        target.setLevel(40);
                        ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
                        LeatherArmorMeta meta = (LeatherArmorMeta) helm.getItemMeta();
                        meta.setColor(Color.RED);
                        helm.setItemMeta(meta);
                        target.getInventory().setHelmet(helm);
                        //chestplate
                        ItemStack ches = new ItemStack(Material.LEATHER_CHESTPLATE);
                        LeatherArmorMeta metat = (LeatherArmorMeta) ches.getItemMeta();
                        metat.setColor(Color.RED);
                        ches.setItemMeta(metat);
                        target.getInventory().setChestplate(ches);
                        //leggings
                        ItemStack legg = new ItemStack(Material.LEATHER_LEGGINGS);
                        LeatherArmorMeta metatt = (LeatherArmorMeta) legg.getItemMeta();
                        metatt.setColor(Color.RED);
                        legg.setItemMeta(metatt);
                        target.getInventory().setLeggings(legg);
                        //boots
                        ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
                        LeatherArmorMeta metattt = (LeatherArmorMeta) boot.getItemMeta();
                        metattt.setColor(Color.RED);
                        boot.setItemMeta(metattt);
                        target.getInventory().setBoots(boot);
                    }
                    if(plugin.getConfig().contains("bplist."+name)){
                        double x = plugin.getConfig().getDouble("bluespawn.x");
                        double y = plugin.getConfig().getDouble("bluespawn.y");
                        double z = plugin.getConfig().getDouble("bluespawn.z");
                        Location l = new Location(w,x,y,z);
                        target.teleport(l);
                        String dguy = target.getName();
                        String aguy = killer.getName();
                        killer.sendMessage(ChatColor.GREEN+"You killed "+dguy+"!"+ChatColor.DARK_PURPLE+" +100");
                        target.sendMessage(ChatColor.RED+aguy+" killed you!");
                        PlayerInventory inv = target.getInventory();
                        ItemStack brod = new ItemStack(Material.BLAZE_ROD, 1);
                        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                        inv.addItem(brod);
                        inv.addItem(sword);
                        target.setLevel(40);
                        ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
                        LeatherArmorMeta meta = (LeatherArmorMeta) helm.getItemMeta();
                        meta.setColor(Color.BLUE);
                        helm.setItemMeta(meta);
                        target.getInventory().setHelmet(helm);
                        //chestplate
                        ItemStack ches = new ItemStack(Material.LEATHER_CHESTPLATE);
                        LeatherArmorMeta metat = (LeatherArmorMeta) ches.getItemMeta();
                        metat.setColor(Color.BLUE);
                        ches.setItemMeta(metat);
                        target.getInventory().setChestplate(ches);
                        //leggings
                        ItemStack legg = new ItemStack(Material.LEATHER_LEGGINGS);
                        LeatherArmorMeta metatt = (LeatherArmorMeta) legg.getItemMeta();
                        metatt.setColor(Color.BLUE);
                        legg.setItemMeta(metatt);
                        target.getInventory().setLeggings(legg);
                        //boots
                        ItemStack boot = new ItemStack(Material.LEATHER_BOOTS);
                        LeatherArmorMeta metattt = (LeatherArmorMeta) boot.getItemMeta();
                        metattt.setColor(Color.BLUE);
                        boot.setItemMeta(metattt);
                        target.getInventory().setBoots(boot);
                    }
            }
            }
        }
    rplist is the list of player names on the red team. same thing with bplist for blue team

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    Deleted user

    No offence but your code is ugly.
     
  7. Offline

    FireBreath14

    Eballer48 yes ik xD... i edited it a lot. Eclipse does weird indents when it shouldnt

    anyone find anything? maybe ill just remove the sword altogether...?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    iWareWolf

  9. Offline

    RealDope

    Code too gross for me to read.

    I've had problems in the past with EntityDamageByEntityEvent and checking for hp below 0. Use PlayerDeathEvent instead.
     
  10. Offline

    FireBreath14

    iWareWolf

    Players with life already missing get killed by a sword and have to respawn. I dont want that, its a pvp server. but hey nvm i think i can fix it...

    how....nice. no i can fix it i think

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    RealDope

    Okay.. I've used entity.getHealth() - event.getDamage() <= 0 and the code has fired twice for me. Just a warning.
     
Thread Status:
Not open for further replies.

Share This Page