Solved Activating an event from a command

Discussion in 'Plugin Development' started by u_mad_bros, Jul 18, 2016.

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

    u_mad_bros

    Hullo der fellow programmers (Except you are better at this than I am)! Today I was working on a simple plugin that if the player is walking on water, it sets it to glass. Now that's great, but I want this event to be toggled via a command. Woo! There's my issue! I've only known how to make events, not toggle them on and off with a command. I don't think I'd really need to show my code for anyone to be able to help me, as this has nothing to do with an error. I just don't know how to execute an event from a command. :p Anyhow, thanks! (P.S The command is in a different class than my Main, in case that changes anything, which I am sure it doesn't.) Thank you for your help in advance. :3
     
  2. Offline

    CraterHater

    Well just have a boolean variable above your code and set it to false or true when the command is called, then in the event check if the boolean is true and if so run the code in the event.
     
  3. Offline

    thapengwin

    If you want this to be toggled on different players, just add a hash map containing their names and if they can do this and once they walk on water check if it should turn to glass. If it's a toggle for everyone, a config option is a possibility.
     
  4. Offline

    u_mad_bros

    EDIT: New issue with the new Command that I created. What I want this command to do is quite simple and explained in the code. I want when you type in the command that while the block below you is Grass, I want it to set it to Glass. This is just testing for now, but it won't work this way. What happens is that when you toggle this command on and you're standing on grass, it'll set that grass block as grass, but only that one. If I move, it won't do it to the rest of the grass I stand on, just the one I was on when toggling this command. I am pretty sure there is something wrong with my while loop. Here is my code:
    Code:
    package commands.project;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class WaterWalker implements CommandExecutor{
        List<String> wwToggled = new ArrayList<String>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if(cmd.getName().equalsIgnoreCase("waterwalker")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage("This command requires you be a player!");
                    return false;
                }
                Player player = (Player) sender;
                if (wwToggled.contains(player.getName())) {
                    player.sendMessage("Water Walker Disabled");
                    wwToggled.remove(player.getName());
                    return true;
                } else {
                 
                wwToggled.add(player.getName());
                    player.sendMessage(ChatColor.BOLD.toString() + ChatColor.BLUE.toString() + "Water Walker has been enabled!");
                while (player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.GRASS) {
                player.getLocation().getBlock().getRelative(BlockFace.DOWN).setType(Material.GLASS);
                }
            }
            return true;
        }
            return true;
        }
    }
    
    
    
     
    Last edited: Jul 19, 2016
  5. @u_mad_bros Remove that for loop, change the list from string to UUID and use Player#getUniqueId then make a PlayerMoveEvent, check if it is a block movement, check the blow bellow, set it to something else.
     
Thread Status:
Not open for further replies.

Share This Page