Event to toggle command off on player logout?

Discussion in 'Plugin Development' started by SuippoKala, Jul 7, 2015.

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

    SuippoKala

    Hello, i was making my plugin and i came across this thing that if i leave my command on, it'll stay on if i log back in and i want it to go off when i log out, i just can't seem to find a way to do it.
    I could use some help!

    My main class:
    Code:
    public class Valo extends JavaPlugin {
     
        List<String> toggled = new ArrayList<String>();
     
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(), this);
            getLogger().info("Valo Käynnissä!");
        }
     
        public void onDisable() {
            getLogger().info("Valo Suljettu!");
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("valo")) {
                if(player.hasPermission("valo.toggle"));
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Sinun täytyy olla pelaaja jotta voit käyttää tätä komentoa!");
    
                    return false;
                }
                if(toggled.contains(player.getName())) {
                    player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "suljettu!");
                    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                    toggled.remove(player.getName());
                    return true;
                }
             
                player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "käytössä!");
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
                toggled.add(player.getName());
                return true;
             
            }
            return false;
        }
    }
    My listener class:
    Code:
    public class ValoListener implements Listener{
       
        List<String> toggled = new ArrayList<String>();
    
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            for (PotionEffect effect : e.getPlayer().getActivePotionEffects())
               e.getPlayer().removePotionEffect(effect.getType());
            if (toggled.contains(e.getPlayer().getName())) {
                toggled.remove(e.getPlayer().getName());
            }
        }
    }
    }
     
  2. Offline

    SuperSniper

    PlayerQuitEvent, remove then from the ArrayList
     
    BagduFagdu likes this.
  3. Offline

    SuippoKala

    @SuperSniper
    I'm new to Java, i asked from someone on some server he said that i need to create list for players?
     
  4. Offline

    SuippoKala

    This is still unresolved i would love to get this out of my way!

    Main class:
    Code:
    public class Valo extends JavaPlugin {
       
        List<String> toggled = new ArrayList<String>();
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(), this);
            getLogger().info("Valo Käynnissä!");
        }
       
        public void onDisable() {
            getLogger().info("Valo Suljettu!");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("valo")) {
                if(player.hasPermission("valo.toggle"));
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Sinun täytyy olla pelaaja jotta voit käyttää tätä komentoa!");
    
                    return false;
                }
                if(toggled.contains(player.getName())) {
                    player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "suljettu!");
                    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                    toggled.remove(player.getName());
                    return true;
                }
               
                player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "käytössä!");
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
                toggled.add(player.getName());
                return true;
               
            }
            return false;
        }
    }
    Listener class:
    Code:
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    
    public class ValoListener implements Listener{
       
        List<String> toggled = new ArrayList<String>();
    
       
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            for (PotionEffect effect : e.getPlayer().getActivePotionEffects())
               e.getPlayer().removePotionEffect(effect.getType());
            toggled.remove(e.getPlayer());
        }
    }
     
  5. Offline

    justin_393

    You never added the player to the List. And just saying. Player is an Object, not a String, you can't store that in a List that stores String :p

    Basically what you need to do is this.

    When player joins, add them to your List. When they leave, remove them from the List.
     
  6. Offline

    SuippoKala

    @justin_393 I'm very rookie to Java, how can i add the to a list? :)
    Thanks.
     
  7. Offline

    justin_393

    Well, when a player joins, add them. (Note, you're going to need to change your List from storing a String to storing a Player, unless you store the player's name with Player#getName();)

    So, first we need to listen for a player to join, which we can do with

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
    }

    Next we want to store the player to the list, we can do this by

    Player p = event.getPlayer;
    toggled.add(p);

    So your final code looks like this!

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
    Player p = event.getPlayer;
    toggled.add(p);
    }

    Also, when you're removing them from the List, you need to check if they're in it first (You're probably getting errors in the console right now)

    if (toggled.contains(p)) {
    toggled.remove(p);
    }
     
  8. Offline

    SuippoKala

    @justin_393 I don't quite understand yet how this works, how will the plugin now check whether the player has the command toggled on if it's for everyone at PlayerJoinEvent ?
     
  9. Offline

    justin_393

    I seem to be misunderstanding you. What exactly are you hoping to accomplish?

    1. A player toggles a command on and it's forever on.

    2. A player toggles a command on and it's on until server reboots.

    3. A player toggles a command on and it's on until they leave and it's turned off auto,
     
  10. Offline

    SuippoKala

    @justin_393

    If a player types my command it will toggle it on, when the player logs out it will auto-toggle it off.
     
  11. Offline

    justin_393

    ah, I understand now. Ok, easy enough fix.

    You have two instances of a List called "toggled". The reason It's not working is because you're storing them in the instance of it that's in the Main class, and trying to remove them from the one made in PlayerListener. (Still surprised you're not getting an error from trying to remove an Object from something that only stores a String)

    Basically, do this. In your main class change the List to

    public List<Player> toggled = new ArrayList<>();

    and in your PlayerListener remove List<String> toggled = new ArryList<>();

    In your PlayerListener class we need to make a constructor so we can pass the instance of your class without making static references.

    so You need this at the top of your class

    private final Main main;

    public PlayerListener(Main main) {
    this.main = main;
    }

    Now you can do main.toggled.remove(p);
    when you want to remove them in your quit event :)
     
  12. Offline

    SuippoKala

    @justin_393 How about this on my main class, it gives me error now:

    Code:
    public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener, this);
     
  13. Offline

    justin_393

    Whoops, forgot to tell you about that :p
    Change it to this:
    Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(this), this);
     
  14. Offline

    SuippoKala

    @justin_393 Thank you for helping me, really. It means alot to me!
    I'll hit you up again if this doesn't work.

    @justin_393 "The constructor ValoListener(Valo) is undefined"

    @justin_393 these are my current classes.

    Main:
    Code:
    package me.Wife;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Valo extends JavaPlugin {
       
        public List<String> toggled = new ArrayList<String>();
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(this), this);
            getLogger().info("Valo Käynnissä!");
        }
       
        public void onDisable() {
            getLogger().info("Valo Suljettu!");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("valo")) {
                if(player.hasPermission("valo.toggle"));
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Sinun täytyy olla pelaaja jotta voit käyttää tätä komentoa!");
    
                    return false;
                }
                if(toggled.contains(player.getName())) {
                    player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "suljettu!");
                    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                    toggled.remove(player.getName());
                    return true;
                }
               
                player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "käytössä!");
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
                toggled.add(player.getName());
                return true;
               
            }
            return false;
        }
    }
    Listener:
    Code:
    package me.Wife;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.craftbukkit.Main;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    
    public class ValoListener implements Listener{
       
        private final Main main;
    
       
        public ValoListener(Main main) {
            this.main = main;
           
        @EventHandler
        public void onPlayerquit(PlayerQuitEvent) {
            main.toggled.remove(p);
        }
        }
    }
     
    Last edited by a moderator: Jul 11, 2015
  15. Offline

    justin_393

    In ValoLidtener you need to move a brace up. Ntice how there's 3 at the bottom? Only needs to be 2. Also, you need to define "p" before you can remove it. Player p = event.getPlayer();
     
  16. Offline

    SuippoKala

    @justin_393 I am still having problems with this:
    Code:
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(this), this);
    That is underlined.
     
  17. Offline

    justin_393

    @SuippoKala
    What's the error when you hover over it,
     
  18. Offline

    SuippoKala

    @justin_393 "The constructor ValoListener(Valo) is undefined"
     
  19. Offline

    justin_393

    Did you put the constructor in your ValoListener class? It looks like you did, but you need to move a curly brace up
     
  20. Offline

    Burn38

    @SuippoKala
    Code:
    package me.Wife;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.craftbukkit.Main;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    public class ValoListener implements Listener{
      
        private final Plugin main;
      
        public ValoListener(Plugin main) {
            this.main = main;
         }
        @EventHandler
        public void onPlayerquit(PlayerQuitEvent e) {
            main.toggled.remove(e.getPlayer().getUniqueId().toString());
        }
    }
    Code:
    package me.Wife;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    public class Valo extends JavaPlugin {
      
        public List<String> toggled = new ArrayList<String>();
      
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new ValoListener(this), this);
            getLogger().info("Valo Käynnissä!");
        }
      
        public void onDisable() {
            getLogger().info("Valo Suljettu!");
        }
      
       
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("valo")) {
    
                if(player.hasPermission("valo.toggle")) {
    
                    if(toggled.contains(player.getUniqueId().toString())) {
                        player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "suljettu!");
                        player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                        toggled.remove(player.getUniqueId().toString());
                        return true;
                    } else {
                        player.sendMessage(ChatColor.GOLD + "Valo on " + ChatColor.RED + "käytössä!");
                        player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE,0));
                        toggled.add(player.getUniqueId().toString());
                        return true;
                    }
    
                } else {
                    player.sendMessage(ChatColor.RED + " You don't have permission to use this command !"); //I don't know your language so I wrote it in English.
                }
            }
    
            return false;
    
        } else {
            sender.sendMessage(ChatColor.RED + "Sinun täytyy olla pelaaja jotta voit käyttää tätä komentoa!");
            return false;
        }
    }
    
    I tried to modify quickly what you done. Tell me if it works. I just made your conditions working correctly (you should check if sender is instanceof player BEFORE doing anything else !). I didn't tested it so I can't promise that it'll work. I changed the constructor of your Listener, I hope it'll work. I also switched from player's names to player's uuids !
     
Thread Status:
Not open for further replies.

Share This Page