Solved Help with freeze command

Discussion in 'Plugin Development' started by Caixa_No_Lidl, Nov 17, 2020.

  1. Offline

    Caixa_No_Lidl

    Can please someone help me with this /freeze command, for the life of me i can´t figure it out.
    Thank you very much.
    Code:
    package me.caixa.admintools;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class freeze implements CommandExecutor, Listener{
        List<String> alvo = new ArrayList<String>();
       
       
        @EventHandler
        public void onPlayerMovement(PlayerMoveEvent e) {
            Player t = e.getPlayer();
                if (alvo.contains(t.getName())) {
                    e.setTo(e.getFrom());           
            }
                ItemStack coal = new ItemStack(Material.COAL);
                if (t.getInventory().containsAtLeast(coal, 1)) {
                    e.setCancelled(true);
                }
           
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command freeze, String label, String[] args) {
            if (label.equalsIgnoreCase("freeze")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.DARK_RED + "Player only Command");
                    return true;
                }
                Player player = (Player) sender;
                if (player.hasPermission("admintools.freeze")) {
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Usage: " + ChatColor.DARK_RED + "/freeze [player]");
                        return true;
                    }
                    Player p = Bukkit.getPlayer(args[0]);
                    if (p == null) {
                        player.sendMessage(ChatColor.DARK_RED + "Invalid Player name!");
                        return true;
                    }
                        if (alvo.contains(p.getName())) {
                            alvo.remove(p.getName());
                            p.sendMessage(ChatColor.DARK_GREEN + "You are no longer frozen in Place");
                            player.sendMessage(ChatColor.DARK_GREEN + "Player no longer frozen!");
                            return true;
                        }
                        alvo.add(p.getName());
                        p.sendMessage(ChatColor.DARK_RED + "You are now frozen!");
                        player.sendMessage(ChatColor.DARK_GREEN + "Player now frozen!");
                        return true;
                       
                   
                }
                player.sendMessage(ChatColor.DARK_RED + "Sorry you don´t have enough permissions!");
                return true;
            }
            return false;
        }
    }
    
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    Caixa_No_Lidl

    @timtower
    Than you for the reply!
    Basicaly my issue is, when I run the /freeze [player] I get the message that im suposed to, whether it is now frozen or no longer frozen, from that i get the my arraylist is working wonders, the target also recieves the correct messages, but the listerer just doest work, the target is still able to move freely. I made the coal part in the listerer just to work out if it is working, that part works to, when i have coal in my inv i can´t move at all.
    I realy don´t get why is is happening and i looked everywhere!
    Best Regards
    Caixa
     
  4. Online

    timtower Moderator Moderator

    @Caixa_No_Lidl I am just gonna assume that you call new freeze() twice in your onEnable, one for your listener, one for your command.
    Am I correct?
     
  5. Offline

    jonthe445

    @timtower would that cause any sort of issue, calling freeze() twice for both the listener and for the command? Is it better practice to separate the two to their own class?
     
  6. Offline

    Caixa_No_Lidl

    @timtower You are correct!
    This is my main class, sorry the comments are in portuguese,
    Code:
    package me.caixa.admintools;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin implements Listener{
    
        @Override
        public void onEnable() {
           
            //        MENSAGEM INICIAL
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "--------------------------");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "==========================");
            Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "ADMIN Tools is Now Running");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "==========================");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "--------------------------");
       
            //        COMANDOS
            this.getCommand("c").setExecutor(new gamemode());//            DONE
            this.getCommand("s").setExecutor(new gamemode());//            DONE
            this.getCommand("gm").setExecutor(new gamemode());//        DONE
            this.getCommand("feed").setExecutor(new feed()); //            DONE
            this.getCommand("f").setExecutor(new feed());//                DONE
            this.getCommand("heal").setExecutor(new heal());//            DONE
            this.getCommand("sethp").setExecutor(new sethp());//        DONE
            this.getCommand("goditem").setExecutor(new goditem());//    DONE
            this.getCommand("freeze").setExecutor(new freeze());//        NOT DONE
            this.getCommand("kill").setExecutor(new kill());//            DONE
            this.getCommand("admin").setExecutor(new admin());//        NOT DONE
           
            //        LISTENERS ATIVAR
            Bukkit.getServer().getPluginManager().registerEvents(new goditemi(), this);//    DONE
            Bukkit.getServer().getPluginManager().registerEvents(new freeze(), this);//        NOT DONE
            Bukkit.getServer().getPluginManager().registerEvents(new admin(), this);//        NOT DONE
           
            //        INVENTÁRIOS
        }
    
    
    
    
        @Override
        public void onDisable() {}
       
        }
    
     
  7. Online

    timtower Moderator Moderator

    @Caixa_No_Lidl No, you make 1 instance and set it both as executioner and as listener.
    @jonthe445 No, issue is having multiple instances.
     
    jonthe445 likes this.
  8. Offline

    jonthe445

    @timtower
    So when he is calling 'new Freeze()' in both the command and the listener, that isn't correct?
    Should he be creating Freeze freeze = new Freeze() then using freeze when calling

    this.getCommand("freeze").setExecutor(freeze);
    or
    Bukkit.getServer().getPluginManager().registerEvents(freeze, this);
     
  9. Online

    timtower Moderator Moderator

    jonthe445 likes this.
  10. Offline

    Strahan

    I'm confused by your code; if the player is in the alvo list, you teleport them back to whence they came. That's not really a "freeze", but it will work. However if any player has coal, they also are frozen? What is the logic behind that?
     
  11. Offline

    Caixa_No_Lidl

    The coal part was to test if the listerner was working which I think it is. The teleport part was a test inicially i just canceled the movement, but that didn´t work so i tried the teleport thingy, which also didn´t work.
    @timtower i don´t get why the coal part works but the command itself doesn´t?
    The coal part working proves that the listener is working doesnt it?

    Thanks to everyone for the replies, got it working by making a new class for the PlayerMovement Event and importing the arraylist from the frozen class by making it static.
    Best regards to everyone
    Caixa

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 18, 2020

Share This Page