Solved How to make 1 command execute 2 different things

Discussion in 'Plugin Development' started by YoloSanta, Jun 8, 2017.

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

    YoloSanta

    I was wanting to know when I type a command for example /fly it enables fly but when i type it again it disables it how do people make it so when they do the command again it issues something different(basically a toggle) I want to make it so I don't have to make a new OnCommand every time, I can just do the command again.

    Code:java
    1.  
    2. package me.imanthe.plugins.potioncommands;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. public class PotionCommands
    15. extends JavaPlugin
    16. implements Listener
    17. {
    18. public void onEnable()
    19. {
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. getConfig().options().copyDefaults(true);
    22. getLogger().info("PotionCommands has been enabled! Any errors? Contact me on bukkit [USER=91211608]@YoloSanta[/USER]");
    23. saveConfig();
    24. reloadConfig();
    25. }
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    27. {
    28. if ((cmd.getName().equalsIgnoreCase("nv-on")) && ((sender instanceof Player)))
    29. {
    30. Player player = (Player)sender;
    31. if (player.hasPermission("pc.use.nv"))
    32. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION,99999999, 1));
    33. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("nightvision-enable")));
    34. return true;
    35. }
    36. if ((cmd.getName().equalsIgnoreCase("nv-off")) && ((sender instanceof Player)))
    37. {
    38. Player player = (Player)sender;
    39. if (player.hasPermission("pc.use.nv"))
    40. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    41. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("nightvision-disable")));
    42. return true;
    43. }
    44. if ((cmd.getName().equalsIgnoreCase("fr-on")) && ((sender instanceof Player)))
    45. {
    46. Player player = (Player)sender;
    47. if (player.hasPermission("pc.use.fr"))
    48. player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,99999999, 1));
    49. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("fireresistance-enable")));
    50. return true;
    51. }
    52. if ((cmd.getName().equalsIgnoreCase("fr-off")) && ((sender instanceof Player)))
    53. {
    54. Player player = (Player)sender;
    55. if (player.hasPermission("pc.use.fr"))
    56. player.removePotionEffect(PotionEffectType.FIRE_RESISTANCE);
    57. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("fireresistance-disable")));
    58. return true;
    59. }
    60. if ((cmd.getName().equalsIgnoreCase("wb-on")) && ((sender instanceof Player)))
    61. {
    62. Player player = (Player)sender;
    63. if (player.hasPermission("pc.use.wb"))
    64. player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING,99999999, 1));
    65. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("waterbreathing-enable")));
    66. return true;
    67. }
    68. if ((cmd.getName().equalsIgnoreCase("wb-off")) && ((sender instanceof Player)))
    69. {
    70. Player player = (Player)sender;
    71. if (player.hasPermission("pc.use.wb"))
    72. player.removePotionEffect(PotionEffectType.WATER_BREATHING);
    73. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("waterbreathing-disable")));
    74. return true;
    75. }
    76. if ((cmd.getName().equalsIgnoreCase("rg-on")) && ((sender instanceof Player)))
    77. {
    78. Player player = (Player)sender;
    79. if (player.hasPermission("pc.use.rg"))
    80. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,99999999, 1));
    81. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("regen-enable")));
    82. return true;
    83. }
    84. if ((cmd.getName().equalsIgnoreCase("rg-off")) && ((sender instanceof Player)))
    85. {
    86. Player player = (Player)sender;
    87. if (player.hasPermission("pc.use.rg"))
    88. player.removePotionEffect(PotionEffectType.REGENERATION);
    89. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("regen-disable")));
    90. return true;
    91. }
    92. if ((cmd.getName().equalsIgnoreCase("rt-on")) && ((sender instanceof Player)))
    93. {
    94. Player player = (Player)sender;
    95. if (player.hasPermission("pc.use.rt"))
    96. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,99999999, 1));
    97. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("resistance-enable")));
    98. return true;
    99. }
    100. if ((cmd.getName().equalsIgnoreCase("rt-off")) && ((sender instanceof Player)))
    101. {
    102. Player player = (Player)sender;
    103. if (player.hasPermission("pc.use.rt"))
    104. player.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
    105. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("resistance-disable")));
    106. return true;
    107. }
    108. if ((cmd.getName().equalsIgnoreCase("inv-on")) && ((sender instanceof Player)))
    109. {
    110. Player player = (Player)sender;
    111. if (player.hasPermission("pc.use.inv"))
    112. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY,99999999, 1));
    113. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("invisibility-enable")));
    114. return true;
    115. }
    116. if ((cmd.getName().equalsIgnoreCase("inv-off")) && ((sender instanceof Player)))
    117. {
    118. Player player = (Player)sender;
    119. if (player.hasPermission("pc.use.inv"))
    120. player.removePotionEffect(PotionEffectType.INVISIBILITY);
    121. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("invisibility-disable")));
    122. return true;
    123. }
    124. if ((cmd.getName().equalsIgnoreCase("sd-on")) && ((sender instanceof Player)))
    125. {
    126. Player player = (Player)sender;
    127. if (player.hasPermission("pc.use.sd"))
    128. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,99999999, 1));
    129. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("speed-enable")));
    130. return true;
    131. }
    132. if ((cmd.getName().equalsIgnoreCase("sd-off")) && ((sender instanceof Player)))
    133. {
    134. Player player = (Player)sender;
    135. if (player.hasPermission("pc.use.sd"))
    136. player.removePotionEffect(PotionEffectType.SPEED);
    137. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("speed-disable")));
    138. return true;
    139. }
    140. if ((cmd.getName().equalsIgnoreCase("sat-on")) && ((sender instanceof Player)))
    141. {
    142. Player player = (Player)sender;
    143. if (player.hasPermission("pc.use.sat"))
    144. player.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION,99999999, 1));
    145. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("saturation-enable")));
    146. return true;
    147. }
    148. if ((cmd.getName().equalsIgnoreCase("sat-off")) && ((sender instanceof Player)))
    149. {
    150. Player player = (Player)sender;
    151. if (player.hasPermission("pc.use.sat"))
    152. player.removePotionEffect(PotionEffectType.SATURATION);
    153. player.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("saturation-disable")));
    154. return true;
    155.  
    156. }
    157. return false;
    158. }
    159. }
    160.  
     
  2. Offline

    xX4w3s0m3Xx

    In your example command fly: make a boolean named something like "flyEnabled" and in the command add this "flyEnabled = !flyEnabled" this will toggle it and make an if-statement where it does the different things
     
  3. Offline

    RcExtract

    You would like to save all the players who activate the command for odd times into an ArrayList. When the time the player executes the command is even, remove the player from the ArrayList.

    Also, reloadConfig() inside onEnable() is unnecessary.

    @xX4w3s0m3Xx your method won't work cuz you are binding the status of all players together.
     
    Last edited: Jun 9, 2017
  4. Offline

    xX4w3s0m3Xx

    Yeah I see now. I also program RSPS and there it is programmed per player, so that's why I thought that this would work
     
    RcExtract likes this.
Thread Status:
Not open for further replies.

Share This Page