Simple plugin help. A /freeze command

Discussion in 'Plugin Development' started by Lupus, Jun 11, 2012.

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

    Lupus

    Need help. Trying to make it so if you do /freeze [Player] that player will not be able to move, place blocks, and destroy blocks. But as I am new to this, I am unaware of how to do it. Help please? Below is what i have so far.
    Code:
    package us.lupusbikkit.Freeze;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Freeze extends JavaPlugin implements Listener {
    public void onDisable(){
       
            System.out.println(this + " is now disabled!");
    }
         @Override
             public void onEnable(){
       getServer().getPluginManager().registerEvents(this, this);
         }
         public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
       if(cmd.getName().equalsIgnoreCase("freeze"))
       if(args.length == 0){
       sender.sendMessage(ChatColor.RED + "Use: /freeze [player]" );
       return true;
       }
        
           getServer().getPlayer(args[0])
        
       return true;
         }
    }
    
     
  2. Offline

    Sushi

    Make a list of players who are frozen.

    Code:
    static ArrayList<Player> frozen = new ArrayList<Player>();
    Whenever someone is set as frozen, add them to the list of frozen players.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("freeze")) {
            if(args.length > 0) {
                Player target = (getServer().getPlayer(args[0]));
                frozen.add(target);
                sender.sendMessage(ChatColor.GREEN + target.getName() + " has been frozen.";
            } else {
                sender.sendMessage(ChatColor.RED + "Use: /freeze [player]");
                return false;
            }
     
     
    return true;
    }
    Make some Event Handlers to handle what happens when frozen players attempt to move, place blocks, or remove blocks.

    Code:
    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();
        if (frozen.contains(player)) {
            event.setCancelled(true);
        }
    }
    Code:
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        if (frozen.contains(player)) {
            event.setCancelled(true);
        }
    }
    Code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (frozen.contains(player)) {
            event.setTo(event.getFrom());
        }
    }
    Make sure you register the events, of course.
     
  3. Offline

    imjake9

    Sushi — Two things. First of all, you should use a Set rather than an ArrayList in case a user uses the command twice, and this will making adding an unfreeze command simpler. Also, is there any reason to do event.setTo(event.getFrom()) instead of event.setCancelled(true)? Just wondering.
     
  4. Offline

    Sushi

    No reason, I don't think there's much of a difference at all.

    Also, yes, using a Set didn't cross my mind, but it would be better than using an ArrayList.
     
  5. Offline

    imjake9

    Alright, cool, just wondering if one was more effective than the other, or something. Another thing to keep in mind is if this is going to be persistent, a Set<String> might make more sense than a Set<Player>, since the latter could cause problems when players logout, and it would be much more complex to serialize.
     
  6. Offline

    Lupus

    How to i register an event?
     
  7. Offline

    imjake9

    If you're using your plugin class as your Listener, you can just put this line in your onEnable:
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);
    Make sure that all the events you want to register are prefixed with the @EventHandler annotation, or they won't be detected.
     
  8. Offline

    Lupus

    Also: How would I use this to make a command to unfreeze a player
     
  9. Offline

    imjake9

    You would use mostly the same code as for /freeze, but duplicate it and change the "freeze" check to "unfreeze". Then, replaced frozen.add(target) with frozen.remove(target).
     
  10. If you cancel it it glitches and your screen goes mental.
     
  11. Offline

    Sushi

  12. Sushi If you read the whole thread you'll note that the "better" ways ( event.setTo(event.getFro()); or player.teleport() ) are in fact the worst ways. (check the codes instead of believing the trolls). There's another solution in the thread: Surrounding the player with indestructable blocks, but do you really want that? ;)
    So all left is event.setCancelled(true);
     
  13. Offline

    Lupus

    Code:
        public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("unfreeze")) {
        sender.hasPermission("freeze.freeze");
            if(args.length > 0) {
                Player target = (getServer().getPlayer(args[0]));
                frozen.remove(target);
                sender.sendMessage(ChatColor.GREEN + target.getName() + " has been unfrozen.");
                return true;
            } else {
                sender.sendMessage(ChatColor.RED + "Use: /unfreeze [player]");
                return false;
            }
        }
    return true;
    }
    
    I am having trouble with this one. When I try the command, absolutly nothing happens. No error, no unknown command, nothing. EDIT: This happens when I try to unfreeze myself, but it also does this in console.
     
  14. Offline

    imjake9

    You can't do it like that... you can only have one onCommand method. Here's how it should look:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("freeze") && sender.hasPermission("freeze.freeze") {
    3. if (args.length != 0) {
    4. Player target = getServer().getPlayer(args[0]);
    5. frozen.add(target);
    6. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been unfrozen.");
    7. return true;
    8. }
    9. return false; // This should send the usage message automatically
    10. }
    11. if (cmd.getName().equalsIgnoreCase("unfreeze") && sender.hasPermission("freeze.unfreeze") {
    12. if (args.length != 0) {
    13. Player target = getServer().getPlayer(args[0]);
    14. frozen.remove(target);
    15. sender.sendMessage(ChatColor.GREEN + target.getName() + " has been unfrozen.");
    16. return true;
    17. }
    18. return false; // This should send the usage message automatically
    19. }
    20. }
    Obviously, you can customize that to your needs, but this is the basic format that you should use to handle the commands.
     
  15. Offline

    Lupus

    Thanks, but also, I am getting a red line under (cmd.getName()) on both commands. No useable results. Help?
     
  16. Offline

    imjake9

    Whoops, sorry. Either change cmd to command or vice-versa. I accidentally changed the name of the parameter in your code, hence that error.
     
  17. Offline

    Lupus

    thanks!
     
Thread Status:
Not open for further replies.

Share This Page