Toggle

Discussion in 'Plugin Development' started by JoloCodeBrahs, Dec 18, 2015.

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

    JoloCodeBrahs

    Their are other topics on this but is their a better way to toggle commands? (1.8.8)
     
  2. Offline

    mcdorli

    if statements.
    if (!getConfig().isXYcommand-allowed) {
    sender.sendMessage("This command is currently not available");
    }
     
  3. Offline

    Henzz

    @JoloCodeBrahs
    Use HashSets or List to store a player's name as a string. Adding or removing the objects from it is pretty much toggling on and off.
     
  4. Offline

    JoloCodeBrahs

    Can i get a better example lets say with fly command so i will have an idea how to remake it with other commands?

    @Henzz
    @mcdorli
     
    Last edited: Dec 18, 2015
  5. Offline

    Henzz

    @JoloCodeBrahs
    Using this from one of my old plugins.

    Let's say the recipients are our players who are permitted to fly.
    joinChannel() = Toggle on
    leaveChannel() = Toggle off
    Code:
    package main.java.net.bigbadcraft.localonly;
    
    import java.util.List;
    
    import org.bukkit.entity.Player;
    
    public class LocalChat {
     
        private List<String> recipients;
     
        public LocalChat(List<String> recipients) {
            this.recipients = recipients;
        }
     
        public void joinChannel(Player player) {
            recipients.add(player.getName());
        }
     
        public void leaveChannel(Player player) {
            recipients.remove(player.getName());
        }
     
        public boolean isInChannel(Player player) {
            return recipients.contains(player.getName()) ? true : false;
        }
     
        public List<String> getRecipients() {
            return recipients;
        }
     
    }
    
    And with the commands you can use if statement checks to verify if a player is in the list or not.
    In this case you'd replace the messages with fly mode code.

    Code:
    package main.java.net.bigbadcraft.localonly.commands;
    
    import main.java.net.bigbadcraft.localonly.LocalOnly;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;
    
    
    public class LocalCommand extends BaseCommand {
    
        private LocalOnly instance;
      
        public LocalCommand(LocalOnly instance) {
            this.instance = instance;
        }
      
        public void run(Player player, Command command, String[] args) {
            if (args.length == 0) {
                if (instance.localChat.isInChannel(player)) {
                    player.sendMessage(ChatColor.YELLOW + "You are already in your local channel.");
                } else {
                    instance.localChat.joinChannel(player);
                    player.sendMessage(ChatColor.YELLOW + "You have joined your local channel.");
                }
            }
        }
      
      
    }
    
     
  6. Offline

    JoloCodeBrahs

Thread Status:
Not open for further replies.

Share This Page