Freeze command not working.

Discussion in 'Plugin Development' started by Specificus, Aug 5, 2013.

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

    Specificus

    Okay so i have setup a freeze command but it doesnt seem to work, we are using commandexecuter.
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10. import org.bukkit.plugin.Plugin;
    11.  
    12. public class FreezeCommand implements CommandExecutor, Listener {
    13.  
    14. private Main plugin;
    15.  
    16. public FreezeCommand(Main plugin) {
    17. this.plugin = plugin;
    18. }
    19.  
    20.  
    21. ArrayList<String> frozen = new ArrayList<String>();
    22.  
    23. @EventHandler
    24. public void onPlayerMove(PlayerMoveEvent e) {
    25. Player p = e.getPlayer();
    26. if (frozen.contains(p.getName())) {
    27. e.setTo(e.getFrom());
    28. p.sendMessage(ChatColor.RED + "Du er fryst!");
    29. }
    30. }
    31.  
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    34. if (cmd.getName().equalsIgnoreCase("frys")) {
    35. if (args.length == 0) {
    36. sender.sendMessage(ChatColor.RED + "Vennligst angi spiller!");
    37. return true;
    38. }
    39. Player target = Bukkit.getServer().getPlayer(args[0]);
    40. if (target == null) {
    41. sender.sendMessage(ChatColor.RED + "Kunne ikke finne spiller " + args[0] + "!");
    42. return true;
    43. }
    44. if (frozen.contains(target.getName())) {
    45. frozen.remove(target.getName());
    46. sender.sendMessage(ChatColor.GREEN + "Spiller " + target.getName() + " har blitt ufryst!");
    47. return true;
    48. }
    49. frozen.add(target.getName());
    50. sender.sendMessage(ChatColor.GREEN + "Spiller " + target.getName() + " har blitt fryst!");
    51. }
    52. return true;
    53. }
    54. }


    Cause when i write the command it says the player is freezed, but the player can move. Help pl0x.
    .
     
  2. Offline

    Bancey

    Instead of "e.setTo(e.getFrom());"
    try:
    Code:java
    1.  
    2. Location loc = p.getLocation;
    3. p.teleport(loc);
    4.  
     
  3. or maybe cancel the event?...
     
  4. Offline

    AmShaegar

    I bet, you have the same problem as the guy with his maps in that other thread:
    (replace PlayerListener with FreezeCommand)
    Your Listener cannot access the frozen List that was created in the CommandExecutor.
     
  5. Offline

    Venican

    So the getCommads(); should be like this:
    PlayerListener pl = new PlayerListener();
    getCommand("frys").setExecutor(pl);
    pm.registerEvents(pl);

    But then it wouldn't access the command class.
     
  6. Offline

    AmShaegar

    Then show me how it currently looks like.
     
  7. Offline

    Venican

    Code:java
    1. package me.NDevelopers.Nodvendigheter;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.Plugin;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin implements Listener {
    9.  
    10. public void onEnable(){
    11. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    12. Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);
    13. getLogger().info("Nødvendigheter v1.0 har blitt slått på.");
    14. getCommands();
    15. }
    16.  
    17.  
    18. public void onDisable() {
    19. getLogger().info("Nødvendigheter v1.0 har blitt slått av.");
    20. }
    21.  
    22.  
    23. public void getCommands() {
    24. getCommand("frys").setExecutor(new FreezeCommand(this));
    25. getCommand("helbred").setExecutor(new HealFeedCommand(this));
    26. getCommand("mat").setExecutor(new HealFeedCommand(this));
    27. getCommand("kick").setExecutor(new KickBanCommand(this));
    28. getCommand("ban").setExecutor(new KickBanCommand(this));
    29. getCommand("unban").setExecutor(new KickBanCommand(this));
    30. getCommand("kit").setExecutor(new KitCommand(this));
    31. getCommand("vanish").setExecutor(new VanishCommand(this));
    32. getCommand("nyttnavn").setExecutor(new NicknameCommand(this));
    33. getCommand("advar").setExecutor(new WarnCommand(this));
    34. }
    35. }
    36.  
    37.  


    @AmShaegar
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  8. Offline

    AmShaegar

    You do not even register your Listener.
     
  9. Offline

    Venican

    ehm:
    1. public class FreezeCommand implements CommandExecutor, Listener {
     
  10. he created a Listener. Of course its the main class. but he has to register it, too. Like extern Listeners or Executors. Dont pass a new instance of the Listener class, just pass 'this'.
     
  11. Offline

    Venican

    Sorry but i didn't understand that.
     
  12. register the Listener and the Executor, like this:
    Code:
    public void onEnable() {
     
            this.getServer().getPluginManager().registerEvents(this, this);
            this.getCommand("frys").setExecutor(this);
     
        }
    maybe i am wrong, but i think its needed....
     
  13. Offline

    AmShaegar

    Why this? Aren't we talking about the FreezeCommand class?
    Code:java
    1. public class FreezeCommand implements CommandExecutor, Listener {

    This is not the Main class because it is not implementing JavaPlugin. Thus it has no onEnable().
     
  14. Offline

    xTrollxDudex

    That's not how you register a listener....
     
  15. Oh, sry, just switched a little bit between the two classes....^^
    But like Troll said, just implementing the interfaces doesnt register the events
     
  16. Offline

    AmShaegar

    Just in case this was not clear enough now because our conversation could be a bit confusing:

    Code:
    FreezeCommand fc = new FreezeCommand();
    getCommand("frys").setExecutor(fc);
    getServer().getPluginManager().registerEvents(fc, this);
    
     
Thread Status:
Not open for further replies.

Share This Page