[Easy] - I cant clear out an inventory slot, please help!

Discussion in 'Plugin Development' started by FirecatHD, Feb 21, 2014.

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

    FirecatHD

    Hello everyone!

    So i am trying to code a sword that block all explosion damage if the player is blocking, and this works.
    But after this i want to get rid of the sword, plus play and effect and this i what i cant get to work.

    I have tried everything i know, but i am a noob so i dont know much.
    Maybe anyone here know a solution?

    Code:java
    1. package me.FirecatHD.RocketFrenzy;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.HumanEntity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDamageEvent;
    10. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class ExplosionBlockSword implements Listener {
    14.  
    15. @EventHandler
    16. public void onPlayerDamage(EntityDamageEvent e) {
    17.  
    18. if (e.getEntity() instanceof Player) {
    19. Player p = (Player) e.getEntity();
    20.  
    21. if (((HumanEntity) e.getEntity()).getItemInHand().getType() == Material.IRON_SWORD) {
    22. if (((HumanEntity) e.getEntity()).isBlocking()); {
    23. if (e.getCause() == DamageCause.BLOCK_EXPLOSION) {
    24.  
    25. ((HumanEntity) e.getEntity()).getInventory().setItem(8, new ItemStack(Material.AIR));
    26. e.getEntity().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0, 1, 0), Effect.STEP_SOUND, 267);
    27.  
    28. e.setDamage(0.0);
    29.  
    30. }
    31. }
    32. }
    33. }
    34. }
    35. }


    Any help is appriciated, thanks.
    -Firecat

    Tags: Garris0n Chinwe
     
  2. Offline

    xTrollxDudex

    FirecatHD
    Register events. Remove semicolon after if statement.
     
  3. Offline

    FirecatHD

    @xTrollDudex

    That code suddenly just started working :confused:
    But now i have another very similar problem; I am trying to remove the item in the held itemslot and play an effect, but it wont work.

    Code:java
    1. package me.FirecatHD.FirePvP;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. public class AutoSoup implements Listener {
    15.  
    16. @EventHandler
    17. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    18.  
    19. Player p = e.getPlayer();
    20. int i = p.getInventory().getHeldItemSlot();
    21.  
    22. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    23. if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
    24.  
    25. p.getInventory().setItem(i, new ItemStack(Material.AIR)); {
    26. p.getLocation().getWorld().playEffect(p.getLocation().add(0, 1, 0), Effect.STEP_SOUND, 282); {
    27.  
    28. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    29. 6, 9000));
    30. }
    31. }
    32. }
    33. }
    34. }
    35. }


    If anyone can help, please do so :)
    -Firecat
     
  4. Offline

    Arcoz

    Remove {, if you're not using an if statement as in:

    Code:java
    1. p.getInventory().setItem(i, new ItemStack(Material.AIR)); /*{*/
    2. p.getLocation().getWorld().playEffect(p.getLocation().add(0, 1, 0), Effect.STEP_SOUND, 282); /*{*/
     
  5. Offline

    FirecatHD

    Arcoz - It still wont work? Anyone happen to have any ideas?

    I am acually starting to think that this is a Bukkit bug, as the other code just suddenly started working.
    I have also found that the Blindness potion effect does not always get applied, even though its clear that it should. I tested and added 2 seperate potion effects plus blindness with the same code, and the two other got applied but not the blindness. Same goes for nausea.

    I hope a solution will be avilable soon!
    -Firecat :)
     
  6. Offline

    Garris0n

    Dat random tag.

    Anyway, post the current code without messed up formatting.
     
  7. Offline

    FirecatHD

    Garris0n - Sorry for the tag, but i though you could help :) Anyways, i figured that for some reason you cant replace a food item with another item, i have no idea why, but i think that is the case...

    But i am using another item now, and it works fine :)
    -Firecat
     
  8. Offline

    Garris0n

    That doesn't make any sense, what do you mean?

    Edit: Oh, try using null instead of an air item.
     
  9. Offline

    FirecatHD

    Garris0n
    I did :)
     
  10. Offline

    Garris0n

    Well setting the item should work fine, so I don't know what your issue was.
     
  11. Offline

    FirecatHD

    Garris0n

    Its a bug, i have seen other coders having the same problem. I have tried atleast 6 different aproaches on deleting a consumable, but with no luck. Maybe you know an answer?

    -Firecat
     
  12. Offline

    Garris0n

    I haven't tested it, I'll do it later.
     
  13. Offline

    FirecatHD

    I know there is a workaround, but i am not sure how .-.
     
Thread Status:
Not open for further replies.

Share This Page