Strange food level behavior

Discussion in 'Plugin Development' started by spoothie, Oct 8, 2012.

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

    spoothie

    Hey there,
    first of all: I couldn't really think of a good title for my problem, so I picked the one you can see above, although it might not be completely accurate as I don't even know where the exact problem lies.

    So basically I am trying to change the output damage of sword swings according to the damagers food level. The higher the foodlevel is, the higher is the damage you deal to your enemies. In theory this should work (if I dind't make some stupid mistakes that I just don't recognize) with the code I've written. The strange thing is that it sometimes works and sometimes doesn't. Here is my code:

    http://pastebin.com/A9Q5qp0g
    http://pastebin.com/cvi81BNz

    I was quite confused about this effect so I made a method to check my current food level and print it on the chat. If you look at the code you will see that when using an iron sword, you should deal 2.5 hearts damage when your food bar is full (20/20). So when I check my food level, it sais 20 and I hit an enemy with the iron sword. Sometimes it does work and I deal 2.5 hearts damage, and sometimes it doesn't work and I only deal 0.5 hearts damage. I can't think of any mistakes I make ingame or reasons why it sometimes doesn't deal the right amount of damage, especially because I always check my food level before hitting someone and in both cases it's 20. Do you guys have an idea why this is happening?
     
  2. Offline

    travja

    Other than the fact that there are no braces all over in the code it looks fine.
     
  3. Offline

    spoothie

    I don't know what you mean, there are braces everywhere where they're needed. I didn't use braces for those conditions that only have one statement, as I think it is better readable, if this is what you mean.
     
  4. Offline

    spoothie

    It's not that I am totally impatiant, but I need to fix this as soon as possible as I am developing this plugin for a server that is waiting for it.
     
  5. Offline

    TwistedMexi

    replace your FoodLevel() >= 20 section with this and tell me what text prints out to console

    Code:
                            else if(player.getFoodLevel() >= 20)
                {
                system.out.println("food level is 20+");
                                if(player.getItemInHand().getType() == Material.WOOD_SWORD)
                    {
                        system.out.println("I think it's a wood sword");
                                        event.setDamage(3);
                    }
                                    else if(player.getItemInHand().getType() == Material.STONE_SWORD
                                                    || player.getItemInHand().getType() == Material.GOLD_SWORD)
                    {
                        system.out.println("I think it's a Stone or Gold sword");
                                        event.setDamage(4);
                    }
                                    else if(player.getItemInHand().getType() == Material.IRON_SWORD)
                    {
                        system.out.println("I think it's an iron sword");
                                            event.setDamage(5);
                    }
                                    else if(player.getItemInHand().getType() == Material.DIAMOND_SWORD)
                    {
                        system.out.println("I think it's a diamond sword");
                                            event.setDamage(6);
                    }
                            }
    If you get nothing, we know it's not hitting the right part of the code at all. Just a first part of troubleshooting.
     
  6. Offline

    spoothie

    I've tried that before, too, and it sometimes worked (aka the messages were printed) and sometimes it didn't. This time I discovered something different, though. It seems that it is only working correctly if I hit an enemies body (thus everything beneath the head). If I hit a player's head, it does always only do 0.5 hearts damage.
    Now this is strange! What the heck does the attacker's food level have to do with the hitboxes of entities? AFAIK there aren't even hitboxes in Minecraft. Maybe I overlooked something, but it clearly looked like this when I was testing it.
     
  7. Offline

    TwistedMexi

  8. Offline

    spoothie

    If I am using your code it doesn't print anything, but if I add a check to the else-branch,
    Code:
    else {
        player.sendMessage("Food level is lower than 10");
        event.setDamage(1);
    }
    I am getting the message above when hitting a player's head, so this part of the code is executed.

    It's the same in both cases, DamageCause.ENTITY_ATTACK.
     
  9. Offline

    TwistedMexi

    You need to print out the player's foodlevel inside that else { } since there's no condition, we can't be sure what their value is without checking. Sorry for the repetitive tasks, I don't have a test environment available to me at the moment.
     
  10. Offline

    spoothie

    Well, there are conditions inside the 'if' and 'else if' branches, so the food level can only be lower than 10 (and IIRC it must be higher than 0). I've tested it anyway and when the code inside the 'else' branch is executed the food level is always 0.
    I found out something new, too. It seems to me that if I am hitting a player from longer distance he always gets 0.5 hearts damage. If I am standing in close distance to him and I am hitting his body, he gets the correct amount of damage.
     
  11. Offline

    TwistedMexi

    What exactly is this supposed to be doing (detecting what?), from meleemechanics:

    1. if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
    2. event.getPlayer().setFoodLevel(0);
    3. }
    4. }

      Seems to me this would be what's possible causing the issue. If you need to make sure this isn't firing when it shouldn't be, try putting a line here and watching when it executes, I'm willing to bet it happens every time you do 0.5 damage, since your else statement says to do 0.5 regardless, you may be hitting the air and causing it to set your food to 0, then when you do hit him it's saying food = 0, so let's set damage to 1.
     
  12. Offline

    spoothie

    Oh yes, I totally forgot this part of the code. Every time a player left-clicks (swings his arm) his food level should be set to 0 again, so that it can recharge afterwards. It seems that when hitting a player's head, the PlayerInteractEvent gets called before the EntityDamageByEntityEvent, although I can't think of a reason why this happens.
    Do you know a way around this? I could only imagine waiting a few ticks after the PlayerInteractEvent is called and then set the food level to 0, so that the EntityDamageByEntityEvent gets called in the meantime.
     
  13. Offline

    TwistedMexi

    do an int FoodLevel = event.getPlayer().getFoodLevel(); to store the value before resetting it to 0,
    and reference FoodLevel in your if conditions instead of the direct .getFoodLevel();
    ( if (FoodLevel > 10) { })


    This will also help optimize a bit, since you won't be calling the foodlevel in every single statement, but instead getting and storing the value once for reuse.
     
Thread Status:
Not open for further replies.

Share This Page