Solved Making a Toggle Command

Discussion in 'Plugin Development' started by FallenYouth, Dec 14, 2013.

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

    FallenYouth

    Hello everyone,

    I have been messing around with developing and made a flashlight plugin that I currently use on my server and was curious how would I make my command /flashlight on/off into a toggle command so the user can type /flashlight and it will toggle it on/off
    I know how hashmaps and Arraylist work I just don’t know where to put them and where to add the command into since I haven’t been able to look at a good example of one.

    Here is my current code:
    Code:java
    1. package com.hybrah;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.potion.PotionEffect;
    9. import org.bukkit.potion.PotionEffectType;
    10.  
    11.  
    12. public class FlashlightPlus extends JavaPlugin {
    13.  
    14. public void onEnable() {
    15. this.getConfig().options().copyDefaults(true);
    16. getServer().getPluginManager().disablePlugin(this);
    17. return;
    18. }
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    20.  
    21. if(cmd.getName().equalsIgnoreCase("Flashlight") || cmd.getName().equalsIgnoreCase("flash") || cmd.getName().equalsIgnoreCase("fl")) {
    22.  
    23. if (!(sender instanceof Player)) {
    24. sender.sendMessage(ChatColor.RED + "Must be a player to exucute this command!");
    25. return true;
    26. }
    27.  
    28. if (!sender.hasPermission("flashlight.use")) {
    29. sender.sendMessage(ChatColor.RED + "You do not have permission to perform this command!");
    30. return true;
    31. }
    32.  
    33. if(args.length <= 0){
    34.  
    35. sender.sendMessage(ChatColor.GREEN + "/flashlight on - Turns Flashlight ON!");
    36. sender.sendMessage(ChatColor.GREEN + "/flashlight off - Turns Flashlight OFF!");
    37.  
    38. return true;
    39. }
    40.  
    41. if (args.length == 1) {
    42.  
    43. if (sender instanceof Player) {
    44. Player player = (Player) sender;
    45.  
    46. if(args[0].equalsIgnoreCase("on")) {
    47. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
    48. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOnMsg")));
    49. return true;
    50. }
    51. if(args[0].equalsIgnoreCase("off")) {
    52. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    53. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOffMsg")));
    54. }
    55. }
    56. }
    57. }
    58. return true;
    59. }
    60. }
    61.  


    If anyone has a bit of code that displays how Toggle commands are used or wants to explain what bit of code to put there that would be great:)

    Thanks in advance,

    FallenYouth
     
  2. Offline

    boysnnoco

    Do this:
    Code:java
    1. public final Set<String> flashlight = new HashSet<String>();

    then check when they do the /flashlight command if they are within the flashlight hashmap, if they are remove them and do whatever, if they aren't add them and do whatever :)
     
  3. Offline

    AzubuSan

    FallenYouth You could also just do
    Code:java
    1.  
    2. boolean active = false;
    3.  
    4. @Override
    5. public boolean onCommand(CommandSender sender, Command cmd, String label,
    6. String[] args) {
    7.  
    8. if (cmd.getName().equalsIgnoreCase("Flashlight")
    9. || cmd.getName().equalsIgnoreCase("flash")
    10. || cmd.getName().equalsIgnoreCase("fl")) {
    11.  
    12. // ^^ Your other code ^^
    13.  
    14. if (sender instanceof Player) {
    15. Player player = (Player) sender;
    16.  
    17. if (active == false) {
    18. player.addPotionEffect(new PotionEffect(
    19. PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
    20. sender.sendMessage(ChatColor.translateAlternateColorCodes(
    21. '&', getConfig().getString("FlashlightOnMsg")));
    22. return true;
    23. }
    24. if (active == true) {
    25. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    26. sender.sendMessage(ChatColor.translateAlternateColorCodes(
    27. '&', getConfig().getString("FlashlightOffMsg")));
    28. }
    29. }
    30. }
    31. return true;
    32. }
     
  4. Offline

    FallenYouth

    AzubuSan Does this use a Hashmap and anything like that or is that the code to make it a toggle command?
     
  5. Offline

    AzubuSan

    FallenYouth That's all there is to it. It uses the
    Code:java
    1. boolean active = false;

    to check if the users flashlight is active or not, if it is turn it off on command usage, is it false, turn it on instead.

    EDIT: The code I posted is your code shortened to just for the purpose of showing what I mean, don't forget to add it back (In case your copying it). ;)
     
  6. Offline

    the_merciless

    There is no need to do this when you can just set aliases in the plugin.yml
     
  7. Offline

    FallenYouth

    AzubuSan Ah that makes a lot of sense, Thanks!

    I will add it into my code and give it a test
     
  8. Offline

    AzubuSan

    FallenYouth Also, do what the_merciless said, instead of that jumble of ors just add aliases in your plugin.yml.
    (Like so:
    Code:
    commands:
        flashlight:
          description: Toggles the flashlight
          usage: /<command>
          aliases: [fl,flashl,flight]
    # Or whatever you want your aliases to be
    )
     
  9. Offline

    FallenYouth

    AzubuSan Okay just got it all added and went to test it will turn the flashlight on but will not turn it off when enabled.

    I have a feeling that it is not checking if the player has it enabled.
     
  10. Offline

    the_merciless

    Code:
        List<String> flashlightEnabled = new ArrayList<String>();
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,String[] args) {
     
            if (cmd.getName().equalsIgnoreCase("Flashlight") {
     
                if (sender instanceof Player) {
                    Player player = (Player) sender;
     
                    if (!flashlightEnabled.contains(player.getName())) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOnMsg")));
                        flashlightEnabled.add(player.getName());
                        return true;
                    }else{
                        player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("FlashlightOffMsg")));
                        flashlightEnabled.remove(player.getName());
                        return true;
                    }
                }
            }
            return true;
        }
     
  11. Offline

    AzubuSan

    FallenYouth Do what he said, more efficient. Sorry if mine didn't work! :(
     
  12. Offline

    FallenYouth

    Thank you everyone that helped I got it to work and added some more stuff to it
     
Thread Status:
Not open for further replies.

Share This Page