RemoveItem/AddItem Glitch? Help

Discussion in 'Plugin Development' started by Code0, Apr 11, 2014.

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

    Code0

    Code:java
    1. package me.Darko111.Eagle;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class MainEagle extends JavaPlugin implements Listener {
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this,this);
    18. }
    19. public void onDisable() {
    20.  
    21. }
    22. ArrayList<String> cooldown = new ArrayList<String>();
    23. @EventHandler
    24. public void onPlayerRightClick(final PlayerInteractEvent e) {
    25. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    26. if(e.getPlayer().getItemInHand().getType() == Material.FEATHER) {
    27. if(cooldown.contains(e.getPlayer().getName())) {
    28. e.getPlayer().sendMessage(ChatColor.RED+"You're still too tired to fly.");
    29. }
    30. else {
    31. e.getPlayer().sendMessage(ChatColor.AQUA+"You are now flying!");
    32. e.getPlayer().setAllowFlight(true);
    33. e.getPlayer().setFlying(true);
    34. e.getPlayer().getInventory().remove(Material.FEATHER);
    35. e.getPlayer().getInventory().addItem(new ItemStack(Material.FIREWORK_CHARGE,1));
    36. cooldown.add(e.getPlayer().getName());
    37. Bukkit.getScheduler().scheduleSyncDelayedTask(this,new Runnable()
    38. {
    39. public void run() {
    40. cooldown.remove(e.getPlayer().getName());
    41. }
    42. }
    43. ,200);
    44. }
    45. }
    46. if(e.getPlayer().getItemInHand().getType() == Material.FIREWORK_CHARGE) {
    47.  
    48. e.getPlayer().sendMessage(ChatColor.AQUA+"You are no longer flying!");
    49. e.getPlayer().setAllowFlight(false);
    50. e.getPlayer().setFlying(false);
    51. e.getPlayer().getInventory().remove(Material.FIREWORK_CHARGE);
    52. e.getPlayer().getInventory().addItem(new ItemStack(Material.FEATHER,1));
    53. }
    54. }
    55. }
    56. }
    57.  
    I coded it so whenever I right click a feather, it allows me to fly, removes the feather, and adds a firework charge, and whenever I right click a firework charge, it stops the flying, removes the firework charge, and gives me the feather back.

    Now the problem is:

    Whenever I right click the feather, it doesn't get removed, the firework charge doesn't get added, and it says "You are now flying" and "You are no longer flying" at the same time..

    I would really appreciate some help here :).
     
  2. Offline

    Dominio

    Hey :)
    I am not very good in Java or English, but I'll try to help you. I think problem is that when you click, it give you item and enable flying, thats okay, but then it checks for that item you just got and remove all that.
    Just use "else" before checking if player has firework_charge..
     
  3. Offline

    Zethariel

    That's because both if statements launch one after the other. You need to stop the function as soon as it finishes one or the other, or add an else statment. Currently, your code finds the feather, allows flight, removes feather, puts firework, detects firework, removes firework, stops flight and gives feather, all in one breath
     
  4. Offline

    CreepahMC

    Yah, try adding a return; statement at the end of each if statement to stop each if statement from overlapping each other like @Zethariel said.
     
Thread Status:
Not open for further replies.

Share This Page