Solved PlayerListeners?

Discussion in 'Plugin Development' started by LandonTheGeek, Feb 3, 2013.

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

    LandonTheGeek

    Hey there!
    So I have coded my first basic plugin today, it has one command, with one PlayerListener. What my question is, is if I made another command, would I have to make another Listener on that command?
    Public Boolean Below
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BTGoldMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.BLUE + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Gold.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            return false;
    So like I ask again. Do I have to make another Listener for another boolean?

    Thanks for taking the time reading this!
    Your friend,
    xXTh3B3astXxify
     
  2. Offline

    caseif

    No, you just write
    Code:java
    1. else if (commandLabel.equalsIgnoreCase("whatever"){
    2. // do something
    3. }

    after your first label check.
     
  3. Offline

    LandonTheGeek

    Hmm... I mean, I know how to add a boolean and all to my main class, but do I need to keep remaking listeners though?
     
  4. Offline

    RealDope

    Also if you use one boolean "enabled" you can't change it on a per-player basis. You should use a HashMap<String, Boolean>
     
  5. Offline

    LandonTheGeek

    Wait I just thought of something. Can't I keep making @EventHandlers?
    I'm sorry, like I said I just started making plugins.

    Also if there was an easier way you could explain where to insert everything that'd be great. Once again I'm not the best at Java. xD

    Anyone have the answer to this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    Debels

  7. Offline

    LandonTheGeek

    No, I know how to make a boolean, I just don't know how to add the second command as a listener. If it has to be on the same one or not.
     
  8. Offline

    Debels

    yes it has to be on the same one, just make the check, for example

    if it equals "Something"{

    }else if it equals "SomethingElse"{

    }

    and so on, ofc with the proper syntax :p
     
  9. Offline

    LandonTheGeek

    I still don't get how to add it. Yes im sorry if im annoying you. My skills arent that great. Here is my code.
    Main Class:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BlockTools extends JavaPlugin {
       
        public static BlockTools plugin;
        public final Logger log = Logger.getLogger("Minecraft");
        public boolean enabled = false;
        public final PlayerListener pl = new PlayerListener(this);
        public final ArrayList<Player> BlockToolsUsers = new ArrayList<Player>();
        public String lol = "[BlockTools] ";
       
        @Override
        public void onEnable(){
            log.info(lol + "is now Enabled! ");
           
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(pl, this);
        }
       
        @Override
        public void onDisable(){
            log.info(lol + "is now Disabled! ");
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BTGoldMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.BLUE + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Gold.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            return false;
        }
    }
    
    PlayerListener:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class PlayerListener implements Listener {
       
        public static BlockTools plugin;
        public Logger log = Logger.getLogger("Minecraft");
       
        public PlayerListener(BlockTools instance){
            plugin = instance;
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            if(plugin.enabled){
                Block block = event.getClickedBlock();
               
                if(event.getAction()==Action.RIGHT_CLICK_BLOCK){
                    block.setTypeId(41);
                    event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Turned Into A Gold Block!");
                }
            }
        }
       
     
    }
    
    What is it that I need to change in the listener to make it so i can put two commands into to one?
    Thank you for taking your time to help me. :D
     
  10. Offline

    SergeantMajorME


    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BTGoldMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.BLUE + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Gold.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            else if (commandLabel.equalsIgnoreCase("AnotherCommand"){
                   //Do something
            }
            return false;
        }
     
  11. Offline

    LandonTheGeek

    Wow. Thanks Sergeant and TnT. What do I need to do to the listener? Just make another Event or what? :)
     
  12. Offline

    mastermustard

    depends on what the other commands do.

    also for commands you dont need new booleans for each command you just put in else statements and then add in the commandlabel stuff like usual
     
  13. Offline

    LandonTheGeek

    Pretty much what every command is, it is a blockchanger. Like, rightclick this, changes to a gold block. In the player listener i have the gold block block.setTypeId(41); So I would probably need to make more @Events

    The next command i am adding is the diamond block. So yeah
    -Beast

    Here is my new main class. Can anyone tell me how i would add the "else if" into the listener?
    MainClass:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BlockTools extends JavaPlugin {
       
        public static BlockTools plugin;
        public final Logger log = Logger.getLogger("Minecraft");
        public boolean enabled = false;
        public final PlayerListener pl = new PlayerListener(this);
        public final ArrayList<Player> BlockToolsUsers = new ArrayList<Player>();
        public String lol = "[BlockTools] ";
       
        @Override
        public void onEnable(){
            log.info(lol + "is now Enabled! ");
           
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(pl, this);
        }
       
        @Override
        public void onDisable(){
            log.info(lol + "is now Disabled! ");
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BTGoldMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.BLUE + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Gold.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            else if (commandLabel.equalsIgnoreCase("BTDiamondMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.AQUA + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Diamond.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            return false;
        }
    }
    
    Listener:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class PlayerListener implements Listener {
       
        public static BlockTools plugin;
        public Logger log = Logger.getLogger("Minecraft");
       
        public PlayerListener(BlockTools instance){
            plugin = instance;
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            if(plugin.enabled){
                Block block = event.getClickedBlock();
               
                if(event.getAction()==Action.RIGHT_CLICK_BLOCK){
                    block.setTypeId(41);
                    event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Turned Into A Gold Block!");
                }
            }
        }
       
       
     
    }
    
    Thanks!
    -Beast

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  14. Offline

    mastermustard

    then yes you need a new listener each time
     
  15. Offline

    LandonTheGeek

    Right, but how do i get the second listener to recognize the else if?
     
  16. Offline

    mastermustard

    Sorry thats a bit beyond my knowledge
     
  17. Offline

    LandonTheGeek

    No problem. Does anyone know how to make a second listener class recognize an else if?
     
  18. Offline

    McCastleWars

    Thing is about your code. Your turning any block into gold block by right clicking it. What do you want to happen to turn the block into diamond?
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    You should aim at giving each command their own CommandExecutor. It will be more maintainable when the plugin has several commands, if only to avoid nested if else blocks.
     
  20. Offline

    LandonTheGeek

    So i need to make the check on the listener?

    The same thing. Just by enabling it with commands. Like /BTDiamondBlock. Yes right clicking

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  21. Offline

    caseif

    At this point, I would recommend creating one main command, and then changing the block to the ID defined in the first argument. I have the feeling that it would make things a ton easier.
     
  22. Offline

    LandonTheGeek

    Could you tell me how to code that? I agree with that. :)

    Figured it out! Thanks!

    EDIT: NVM

    So I got the else if statement set up and all. How do I add it to my listener. New people please... Read this whole post. Anyways here are my classes
    Main:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BlockTools extends JavaPlugin {
     
        public static BlockTools plugin;
        public final Logger log = Logger.getLogger("Minecraft");
        public boolean enabled = false;
        public final PlayerListener pl = new PlayerListener(this);
        public final ArrayList<Player> BlockToolsUsers = new ArrayList<Player>();
        public String lol = "[BlockTools] ";
     
        @Override
        public void onEnable(){
            log.info(lol + "is now Enabled! ");
         
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(pl, this);
        }
     
        @Override
        public void onDisable(){
            log.info(lol + "is now Disabled! ");
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("BTGoldMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.BLUE + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Gold.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            else if (commandLabel.equalsIgnoreCase("BTDiamondMaker")){
                if(args.length==0){
                    if(!enabled){
                        enabled = true;
                        ((Player) sender).sendMessage(ChatColor.AQUA + lol + "is now Enabled! Use the Right Mouse Button to turn Block into Diamond.");
                    }
                    else {
                        enabled = false;
                        ((Player) sender).sendMessage(ChatColor.RED + lol + "is now disabled.");
                    }
                }
            }
            return false;
        }
    }
    PlayerListener:
    Code:
    package me.xXTh3B3astXxify.BlockTools;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class PlayerListener implements Listener {
       
        public static BlockTools plugin;
        public Logger log = Logger.getLogger("Minecraft");
       
        public PlayerListener(BlockTools instance){
            plugin = instance;
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            if(plugin.enabled){
                Block block = event.getClickedBlock();
               
                if(event.getAction()==Action.RIGHT_CLICK_BLOCK){
                    block.setTypeId(41);
                    event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Turned Into A Gold Block!");
                }
            }
        }
       
       
     
    }
    
    So do I need to make a new listener or set up something else on the current listener to add the else if?
    Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  23. Offline

    bitWolfy

  24. Offline

    LandonTheGeek

    Wow thanks. Mind if I use the layout of this code? :D
     
Thread Status:
Not open for further replies.

Share This Page