code should work?

Discussion in 'Plugin Development' started by chris8787878787, Aug 17, 2015.

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

    chris8787878787

    Can anyone please tell me why whenever I kill somebody with this code active, if i have a diamond sword it skips straight to an iron sword and ignores the whole code about giving the player a diamond axe. Help?
    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent e)
        {
            Player p = e.getEntity().getPlayer();
            Player killer = e.getEntity().getKiller();
           
            ItemStack ds = new ItemStack(Material.DIAMOND_SWORD);
            p.setHealth(20.0);
            p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 10));
            p.getInventory().clear();
            p.getInventory().addItem(ds);
            if(!(e.getEntity()instanceof Player))
            {
                return;
            }
                if(!(e.getEntity().getKiller() instanceof Player))
                {
                    return;
                }
           
            if(e.getEntity().getKiller().getInventory().contains(Material.DIAMOND_SWORD))
            {
                killer.getInventory().clear();
                ItemStack da = new ItemStack(Material.DIAMOND_AXE);
                killer.getInventory().addItem(da);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }
            if(e.getEntity().getKiller().getInventory().contains(Material.DIAMOND_AXE))
            {
                killer.getInventory().clear();
                ItemStack is = new ItemStack(Material.IRON_SWORD);
                killer.getInventory().addItem(is);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }
            if(e.getEntity().getKiller().getInventory().contains(Material.DIAMOND_AXE)) return;
            if(e.getEntity().getKiller().getInventory().contains(Material.IRON_SWORD))
            {
                killer.getInventory().clear();
                ItemStack ia = new ItemStack(Material.IRON_AXE);
                killer.getInventory().addItem(ia);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }   
            }
     
  2. Offline

    AcePilot10

    First off you're not using half of the variables you put at the top. With your code maybe try getItemInHand() instead of getInventory().contains(Material)?
     
  3. Offline

    john2342

    @AcePilot10 I tried that already, and it didn't work. Those variables are there for the future of that code.
     
  4. Offline

    SkyleTyler1337

    Try to cast the player after you check if it's not a player
     
  5. Offline

    caderape

    @john2342
    - It's playerDeath event, so e.getplayer always return a player.
    - The event is called once the player is dead, so you can't set health or potion
    - Player.getKiller() return null or a player

    The rest of the code is hum, weird.
    You should check the item in the hand and not if the inventory contains an item.
     
  6. Offline

    SkyleTyler1337

    If this code worked and you killed another player with one of the items within your code it will still give you a diamond tool
     
  7. Offline

    chris8787878787

    @john2342 Erm thanks for testing my code I guess and yeah those variables are kinda for the future and @caderape everything else works but the material thing. They get the potion effect and that helath thing cancels the death event basically. The only problem is that the code skips the axe and gives me an ironsword right after I get a kill with the diamond sword when it should give me a diamond axe
     
  8. Offline

    nj2miami

    @chris8787878787 --- Your code is horribly inefficient first off. Why are you IF-condition testing like that??

    You realize you are checking ALL of those conditions EVERY time? Plus you are checking if they have those items ANYWHERE in their inventory.

    So you your question, if you kill someone with a Diamond Sword AND an Iron Sword ANYWHERE in your inventory, you will receive an Iron Axe. Too much repetitious code and needs to be redone much better and you should be checking against the item IN HAND not their entire inventory because why would you care whats in their bag in this case?

    Not sure how you apply potion effects in this event or set health because as the player has just died.
     
  9. Offline

    chris8787878787

    @nj2miami I promise you the code looks MUCH better when I started it. This is just me adding a BUNCH of different ethods in my desperate attempt to make it work. .getItemInHand is what I started with, now I just .getInventory for now. I already edited the code back to the original state of it since I always get the same error. And try the potion and setHealth thing under PlayerDeathEvent, it works.
     
  10. Offline

    nj2miami

    I don't want to bash you here but the potion effect working or not is the least of your worries. Your IF conditionals show a complete lack of Java understanding quite honestly and it is the main reason your question exists. Your code does not look good now or it certainly was not any better in a "before" state.

    You need some foundation in Java before advancing further honestly.

    It has been awhile since I used the PlayerDeathEvent (as there are few useful things that happen there besides a death message) so since it applies the potion effect for you it obviously triggers AFTER the player has died which is why the effect is being placed.
     
  11. Offline

    chris8787878787

    @nj2miami As I said, those if conditions are not supposed to be the final product, they're just a mumble jumble in my desperate attempt at getting the code to work. Here's the original, cleaner code.
    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent e)
        {      
            Player p = e.getEntity().getPlayer();
            Player killer = e.getEntity().getKiller();
            ItemStack ds = new ItemStack(Material.DIAMOND_SWORD);
            p.setHealth(20.0);
            p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 10));
            p.getInventory().clear();
            p.getInventory().addItem(ds);
     
            if(e.getEntity().getKiller().getInventory().getItemInHand().equals(Material.DIAMOND_SWORD))
            {
                killer.getInventory().clear();
                ItemStack da = new ItemStack(Material.DIAMOND_AXE);
                killer.getInventory().setItemInHand(da);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }
            if(e.getEntity().getKiller().getInventory().getItemInHand().equals(Material.DIAMOND_AXE))
            {
                killer.getInventory().clear();
                ItemStack is = new ItemStack(Material.IRON_SWORD);
                killer.getInventory().setItemInHand(is);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }
            if(e.getEntity().getKiller().getInventory().getItemInHand().equals(Material.IRON_SWORD))
            {
                killer.getInventory().clear();
                ItemStack ia = new ItemStack(Material.IRON_AXE);
                killer.getInventory().setItemInHand(ia);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
                return;
            }
    }
     
  12. Offline

    nj2miami

    if(e.getEntity().getKiller().getInventory().getItemInHand().equals(Material.DIAMOND_SWORD))

    @chris8787878787 You cannot compare an ItemStack to a Material.

    .getItemInHand().getType() but you will have to check for nulls as well.
     
  13. Offline

    chris8787878787

    @nj2miami So I should just create an itemstack for the diamond sword and every other weapon used in getItemInHand().equals(ItemStack) is what you're saying, correct?
     
  14. Offline

    nj2miami

    No I am not saying that. I said what I said above. Also, this seems wrong "Player p = e.getEntity().getPlayer();"

    Doesn't e.getEntity() already return the Player object in this event?

    For God's sake, remove the return statements and at worst make them if-else-if statements but it still hurts my eyes looking at that code :)

    Here, this is what clean code looks like.

    DISCLOSURE: I did not test this in an IDE.

    Code:
    @EventHandler
        public void onDeath(PlayerDeathEvent e)
        {
            Player p = e.getEntity();
            Player killer = e.getEntity().getKiller();
    
            Material check = killer.getItemInHand().getType();
            ItemStack give = null;
    
            p.setHealth(20.0);
            p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 10));
       
            if(check.equals(Material.DIAMOND_SWORD))
                give = new ItemStack(Material.DIAMOND_AXE);
     
            else if(check.equals(Material.DIAMOND_AXE))
                give = new ItemStack(Material.IRON_SWORD);
        
            else if(check.equals(Material.IRON_SWORD))
                give = new ItemStack(Material.IRON_AXE);
    
            if(give != null) {
                killer.getInventory().clear();
                killer.getInventory().setItemInHand(give);
                killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 20, 20);
            }
    }
     
    Last edited: Aug 17, 2015
  15. Offline

    chris8787878787

    @nj2miami huh, interesting, never looked at it that way. Man I can't believe I missed that that makes it so much simpler.
     
  16. Offline

    nj2miami

    I am not 100% sure what you are even trying to accomplish but this code does what it seems like you intended. Are you trying to penalize or gimp a person for killing another person? It seems like if someone kills someone their weapon reduces in strength.

    Yet you blind the person killed.
     
  17. Offline

    chris8787878787

    @nj2miami It's just a small chunk of it, it's for a minigame. When a player kills another player with a certain weapon the killer's weapon becomes a tier lower than it was before. (Example: Diamond sword to diamond axe, diamond axe to iron sword, iron sword to iron axe, etc.) But every time you get a kill you get better armour to compensate for the rank down in weapons. First person to kill with a wooden axe will win, if you die you get reset to diamond sword automatically. Also the blinded player will be teleported to a spawn point and the blind kinda acts like a autorespawn, if that makes sense.
     
  18. Offline

    nj2miami

    @chris8787878787

    I figured as much but based on the example code you showed here, not sure you are up to the task to complete your goal. Good luck!
     
  19. Offline

    chris8787878787

    @nj2miami It's already basically done honestly. This was the last bit I needed. And thanks.
     
Thread Status:
Not open for further replies.

Share This Page