Solved Use Item to Enable/Disable Speed?

Discussion in 'Plugin Development' started by cosmicARTS, Aug 4, 2014.

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

    cosmicARTS

    So I am making my hub plugin and I have it where you right click your item to either enable/disable speed/jump effects. Right now right clicking it will enable either effect, but I want it where if you right click it again, it disables the effect, as seen on TheArchon.

    Here is my code (for the listener class):

    Code:java
    1. package me.cosmicarts.Lobby;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19.  
    20. public class PlayerJoin implements Listener {
    21.  
    22. @EventHandler
    23. public void onJoin(PlayerJoinEvent event) {
    24. Player player = event.getPlayer();
    25.  
    26. Location spawn = new Location(Bukkit.getWorld("world"), 0.5, 70, 0.5);
    27. player.teleport(spawn);
    28.  
    29. ItemStack buy = new ItemStack(Material.DIAMOND);
    30. ItemMeta buymeta = buy.getItemMeta();
    31. ArrayList<String> buylore = new ArrayList<String>();
    32. buymeta.setDisplayName(ChatColor.GREEN + "Store " + ChatColor.GRAY
    33. + "(Click to view)");
    34. buylore.add(ChatColor.YELLOW + "Get the store link!");
    35. buymeta.setLore(buylore);
    36. buy.setItemMeta(buymeta);
    37.  
    38. ItemStack speed = new ItemStack(Material.FEATHER);
    39. ItemMeta speedmeta = speed.getItemMeta();
    40. speedmeta.setDisplayName(ChatColor.GREEN + "Toggle Speed");
    41. speed.setItemMeta(speedmeta);
    42.  
    43. ItemStack jump = new ItemStack(Material.IRON_BOOTS);
    44. ItemMeta jumpmeta = speed.getItemMeta();
    45. jumpmeta.setDisplayName(ChatColor.GREEN + "Toggle Jump");
    46. jump.setItemMeta(jumpmeta);
    47.  
    48. if (!player.hasPlayedBefore()) {
    49. player.getInventory().setItem(8, buy);
    50. player.getInventory().setItem(7, speed);
    51. player.getInventory().setItem(6, jump);
    52. }
    53.  
    54. }
    55.  
    56. @EventHandler
    57. public void buy(PlayerInteractEvent event) {
    58.  
    59. Player player = event.getPlayer();
    60.  
    61. if (player.getInventory().getItemInHand().getType() == Material.DIAMOND) {
    62. if (event.getAction() == Action.RIGHT_CLICK_AIR
    63. || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    64. player.sendMessage(ChatColor.DARK_GRAY + "> " + ChatColor.BOLD
    65. + "" + ChatColor.GREEN + "Hub " + ChatColor.DARK_GRAY
    66. + "> " + ChatColor.GOLD + "Webstore @ "
    67. + ChatColor.AQUA + "store.potatocraft.us "
    68. + ChatColor.YELLOW + "(Click)");
    69. }
    70. }
    71.  
    72. if (player.getInventory().getItemInHand().getType() == Material.FEATHER) {
    73. if (event.getAction() == Action.RIGHT_CLICK_AIR
    74. || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    75. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 4));
    76. }
    77. }
    78.  
    79. if (player.getInventory().getItemInHand().getType() == Material.IRON_BOOTS) {
    80. if (event.getAction() == Action.RIGHT_CLICK_AIR
    81. || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    82. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 4));
    83. }
    84. }
    85.  
    86. }
    87.  
    88. }
    89.  


    Please help me!
     
  2. cosmicARTS Add a check when you right click with the feather!

    if(player.hasPotionEffect(PotionEffectType.SPEED)){
    player.removePotionEffect(PotionEffectType.SPEED);
    }else{
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 4));
    }

    Same deal with the Iron Boots.
     
  3. Offline

    cosmicARTS

    Joshuaknight1998 Thanks <3 It works perfectly! Had to mess around with some global variables, but now it works perfectly!
     
  4. cosmicARTS Awesome! If there's no further help needed, how about marking the thread as SOLVED? Good luck with your plugin! =]
     
Thread Status:
Not open for further replies.

Share This Page