On & Off Commands

Discussion in 'Plugin Development' started by Trill, Dec 1, 2014.

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

    Trill

    I am trying to get a plugin working where a player would type a command Example: (/hello) and it would say the first thing you set it to say -- Example: (PLAYER said hello). And when you type the command again, it would say the second thing you set it to say -- Example: (PLAYER said bye). I already have ideas for how to begin this plugin but I was wondering if there was such thing as an On-Off command that would alternate what it does every other time.

    Example:
    [Broadcast] Thing 1
    [Broadcast] Thing 2
    [Broadcast] Thing 1
    [Broadcast] Thing 2
    .....

    Any help is appreciated!
     
  2. Offline

    Skionz

  3. Offline

    Trill

    I researched a bit and I found out you can test for an even/odd number and make it do someting. Why not add 1 everytime someone types the command and then check if it is even or odd to choose what Bukkit.broadcastmessage it needs to broadcast?
     
  4. When the player first runs the command, add them to a HashMap<UUID, Integer>. UUID is the players unique id. The Integer, or whole number, will either be 0 or 1 in this case.

    So upon running the command a first time, check if the player is in a hashmap. If so, check if the player ID is paired with a 1 or a 0. If it is paired with 1, say "thing2" and set it back to 0. If it is zero, say "thing1" and set it back to 0. If the player is not in the hashmap, add them.

    hashmapname.put(player.getUniqueId(), 0)

    Here is an example, I am not good at explaining how to do something:

    Code:java
    1.  
    2. public HashMap<UUID, Integer> onoff = new HashMap<UUID, Integer>();
    3.  
    4. // In the command
    5. if (onoff.contains(uuid)) {
    6. if (onoff.get(uuid).contains(0)) {
    7. // This means run the 1st message(on), then up the number one
    8. onoff.put(uuid, 1);
    9. } else if (onoff.get(uuid).contains(1)) {
    10. // This means run the 2nd message(off)
    11. onoff.put(uuid, 0);
    12. }
    13. } else {
    14. // Run thing 1(on), but add the player into the hashmap
    15. onoff.put(uuid, 1);
    16. }
    17.  
    18.  


    That should work. Keep in mind HashMaps are reset when the server reloads/restarts.
     
  5. Offline

    Trill

    HeyAwesomePeople
    I was thinking of something like this:
    Code:java
    1. package me.CubedOutGaming.Record;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Record extends JavaPlugin
    11. {
    12. @Override
    13. public void onEnable()
    14. {
    15.  
    16. }
    17.  
    18. @Override
    19. public void onDisable()
    20. {
    21.  
    22. }
    23.  
    24. public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)
    25. {
    26. if(commandLabel.equalsIgnoreCase("record"))
    27. {
    28. Player player = (Player) theSender;
    29. player.getName();
    30. int commandEvenOrOdd;
    31. commandEvenOrOdd ++;
    32. if(numbers[commandEvenOrOdd]%2 == 0)
    33. Bukkit.broadcastMessage("<" + ChatColor.RED + "Record" + ChatColor.WHITE + ">" + ChatColor.RED + player.getName() + ChatColor.RED + "is no longer" + ChatColor.WHITE + "recording!");
    34. else
    35. Bukkit.broadcastMessage("<" + ChatColor.RED + "Record" + ChatColor.WHITE + ">" + ChatColor.RED + player.getName() + ChatColor.RED + "is now" + ChatColor.WHITE + "recording!");
    36. }
    37.  
    38. return true;
    39. }
    40. }
    41.  

    This looks to see if the amount of times the player typed the command is even/odd and then decides what message to broadcast.
    Odd would be like the number 1 which is the first time they type in the command so it would broadcast that they started recording.
    Even would be... etc...
    Not exactly an on/off command but it should work.
    I just thought of a bug. You need to add 1 to integer commandEvenOrOdd per player and not to the plugin. I'll add that in. Whoops.
     
  6. Offline

    97WaterPolo

    Trill
    Wouldn't you create a new commandEvenOrOdd integer each time the command is run, meaning it would never change?

    Wouldn't it be a lot easier to store the names in a list and check if the are in the list?
    Code:
    List<String> recording = new ArrayList<String>();
    if the cmd is record
      if recording contains the player{
          recording.remove(player.getName());
          broadcast "Player has stopped recording"
      }else{
          recording.add(player.getName());
          broadcast "Player has started recording"
      }
     
    
     
  7. Offline

    Trill

    97WaterPolo
    > Moved commandEvenOrOdd to onEnable()
    > Moved commandEvenOrOdd back to where it was originally.
    >I will try your list idea later... School tests are creeping over me.
    >But I do think it could work. Thanks :)
     
    97WaterPolo likes this.
Thread Status:
Not open for further replies.

Share This Page