Scoop a specified player join/leave event

Discussion in 'Plugin Development' started by TitaniuMark, Dec 27, 2018.

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

    TitaniuMark

    Hello there. I am trying to make a plugin that retrieve a player informations about the online status of another player.
    And yeah.. I know that I'm "noob" at java... don't judge me please :)
    Code:
        final Main Listener = new Main();
      
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this.Listener, this);
            //Deleted getCommand
      
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player s = (Player) sender;
            if (!(sender instanceof Player)){
                s.sendMessage("Only players can issue that command");
            }//Added ClassCastException
            if (cmd.getName().equalsIgnoreCase("warn")){
                if (args.length == 0) {
                    s.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You need to set a player!");
                }
              
                }
            return true;
                }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent pje)  {
          
           Player p = pje.getPlayer();
          //Now, There i want to implement there the onCommand() method to use: "args[0]"(as player name that will be scooped) and the sender...
               s.sendTitle(ChatColor.DARK_AQUA + "The player you scoop-ed" + ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + p.getName() + ChatColor.BLACK + "]" + ChatColor.GOLD + "Is Now" + ChatColor.BOLD + "Online", "." );
                  }
                }
    what i forgot and/or need to do next
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TitaniuMark Player player = Bukkit.getPlayer(args[0]);
    If player is null then he is offline.
    No need for events here.
     
  3. Offline

    TitaniuMark

    i want that the plugin automatically send a message on player join/leave
    not /scoop <player> to return player is online/offline
     
  4. Offline

    timtower Administrator Administrator Moderator

    @TitaniuMark Your first post doesn't make that clear.
    What do you want the plugin to do?
     
  5. Offline

    ErzLegendZero

    Code:
    if(sender instanceof Player) {
        Player player = (Player) sender;
        Player target = Bukkit.getPlayerExact(args[0]);
       
        if(target == null) {
            player.sendMessage("The player " + args[0] + " is offline!");
        } else {
            player.sendMessage("The player " + args[0] + " is online!");
        }
        return true;
    }
    At first you getting a player by his name (args[0])
    Code:
    Player target = Bukkit.getPlayerExact(args[0])
    then you check if the player is online
    Code:
    if(target == null){} // OFFLINE
    if(target != null){} // ONLINE
    I think something like this should work

    or do you want to set the player join/leave message?


    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        event.setJoinMessage("Player " + event.getPlayer().getDisplayName() + " joined the server"); // JUST PUT IN YOUR MESSAGE
    }
    
    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        event.setQuitMessage("Player " + event.getPlayer().getDisplayName() + " leaved the server"); // JUST PUT IN YOUR MESSAGE
    }
    Down't forget to register your command or listener in the main class! and in the plugin.yml

    I hop that helped you
     
  6. Offline

    TitaniuMark

    for example we have 2 players: jimex1 and jimex2.
    player jimex1 run the command /warn jimex2 then , everytime player jimex2 join or leave the server
    player jimex1 will receive a message saying it "the player you scooped: jimex2 is now online/offline"
     
  7. Offline

    ErzLegendZero

    Okay for that I will use an custom config
    for example:

    Code:
    WARNCONFIG:
    players:
      jimex2:
        - jimex1
      jimex3:
        - jimex1
    ...
    
    
    And now when the jimex1 use /warn jimex2
    you add jimex2 to the config
    And everytime when a player join you check if the player is in the warnconfig:
    then you send the player thet warnd him the message you wand

    Down't know if there is a more simple way to do this but thats the way I would do this!?
     
    unrealdesign likes this.
  8. Offline

    TitaniuMark

    like this?
    Code:
    public class Main extends JavaPlugin implements CommandExecutor, Listener, EventHandler {
    
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            File configFile = new File(getDataFolder().toString() + "config.yml");
            }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("warn")){
                Player s = (Player) sender;
                Player P = (Player) Bukkit.getPlayerExact(args[0]);
                if (!(sender instanceof Player)){
                    sender.sendMessage(ChatColor.RED + "Only players are allowed to do that");
                }
                if (args[0] == null){
                    s.sendMessage("You need to set a player");
                }
                if (args[0] != null){
                    getConfig().set("Players", args[0]);
                }
            }
            return true;
        }
        public void onPlayerJoin(PlayerJoinEvent evt){
            Player x = (Player) Bukkit.getPlayerExact(named);
            if (this.getConfig.getString("Players") == x){
               
               
            }
        }
    }
     
  9. Offline

    ErzLegendZero

    Ok that's how I would try to do this:

    main:
    Code:
    public class main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this); // REGISTER EVENT
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("warn")) {
                if (sender instanceof Player) { // IT THE SENDER IS AN INSTANCE OF PLAYER
                    if(args.length == 1) { // IF THE PLAYER USE THE RIGHT LENGTH OF ARGUMENTS
                        Player target = Bukkit.getPlayerExact(args[0]); // TARGET
                        if(target != null) { // IF THE TARGET PLAYER IS ONLINE
                            List<String> playerlist = getConfig().getStringList("players." + target.getName()); // GET OLD LIST OF PLAYERS
                            playerlist.add(sender.getName()); // ADD THE SENDER TO LIST
                            getConfig().set("players." + target.getName(), playerlist);  // getConfig().set(string Path, object obj) ADD THE LIST TO THE CONFIG
                            saveConfig(); // SAVE TEH CONFIG
                            sender.sendMessage("§aYou now get warned it your player join the server!");
                        } else {
                            sender.sendMessage("§cThe player you chose is offline!");
                        }
                    } else {
                        sender.sendMessage("§cYou need to set a player!");
                    }
                } else {
                    sender.sendMessage("§cYou must be a player!");
                }
            }
            return true;
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent event) { // YOUR EVENT
            if (getConfig().contains("players." + event.getPlayer().getName())) { // If the join player is in the config (if the path "players.JOINENPLAYERNAME" exist)
                List<String> playerlist = getConfig().getStringList("players." + event.getPlayer().getName()); // GETTING THE LIST WITH ALL PLAYER WHO SHOULD GET WARNED
                for(String playerWhoWarnedString : playerlist) { // GOING THROUGH THE LIST
                    Player playerWhoWarned = Bukkit.getPlayerExact(playerWhoWarnedString); // GETTING A THE CURRENT PLAYER WHO SHOULD GET WARNED
                    if(playerWhoWarned != null) { // If the player who should get warned is online
                        playerWhoWarned.sendMessage("§6The player §d" + event.getPlayer().getName() + "§6 you scoped has joined the server!"); // WARN THE PLAYER
                    }
                }
            }
        }
    }
    Plugin.yml
    Code:
    name: WarnPlugin
    version: 0.1
    
    author: ErzLegendZero
    
    main: de.test.main
    
    commands:
      warn:
    I hope you understand everything!
    WarnPL_IngameTest.png WarnPL_IngameTest_Result.png WarnPL_IngameTest_Result_config.png
     
  10. Offline

    TitaniuMark

    I tryed to make onJoin() and onQuit() messages editable from the config and now the e.getPlayer is stuked to the first player that the command was used on.

    Code:
    public class Main extends JavaPlugin implements Listener
    {
        @Override
        public void onEnable(){
            this.getServer().getPluginManager().registerEvents(this, this);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("warn")){
                if (sender instanceof Player){
                    if (args.length == 1){
                        Player target = Bukkit.getPlayerExact(args[0]);
                        if(target != null){
                            List<String> playerlist = getConfig().getStringList("players." + target.getName());
                            playerlist.add(sender.getName());
                            getConfig().set("players." + target.getName(), playerlist);
                            saveConfig();
                            sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "Yow will now get warned when  " + ChatColor.YELLOW + target.getName() + ChatColor.AQUA + ChatColor.BOLD + "  join or leaves the server");
                        } else {
                            sender.sendMessage(ChatColor.RED + "This player is unknown momentanly.\n" + ChatColor.YELLOW + "Try to use this command when target is online!");
                        }
                    }else {
                            sender.sendMessage(ChatColor.BLACK + "You need to set a player");
                        }
                    }
                    }else {
                        sender.sendMessage(ChatColor.GREEN + "Only players can do this!");
                }
           
           
       
            return true;
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            if (getConfig().contains("players." + e.getPlayer().getName())){
                List<String> playerlist = getConfig().getStringList("players." + e.getPlayer().getName());
                for(String playerWhoWarnedString : playerlist){
                    Player playerWhoWarned = Bukkit.getPlayerExact(playerWhoWarnedString);
                    if (getConfig().getString("Messages.online") == null){
                        getConfig().set("Messages.online", ChatColor.BLUE + "" + "The player you scoped: " + ChatColor.GOLD + e.getPlayer().getName() + ChatColor.BLUE + " Is now" + ChatColor.DARK_GREEN + " Online");
                        saveConfig();
                    }
                    if (playerWhoWarned != null){
                        playerWhoWarned.sendMessage(getConfig().getString("Messages.online"));
                    }
                }
               
                }
            }
       
        @EventHandler
        public void onLeave(PlayerQuitEvent evt){
            if (getConfig().contains("players." + evt.getPlayer().getName())){
                List<String> playerlist = getConfig().getStringList("players." + evt.getPlayer().getName());
                for(String playerWhoWarnedString : playerlist){
                    Player playerWhoWarned = Bukkit.getPlayerExact(playerWhoWarnedString);
                    if (getConfig().getString("Messages.offline") == null){
                        getConfig().set("Messages.offline", ChatColor.BLUE + "" + "The player you scoped: " + ChatColor.GOLD + evt.getPlayer().getName() + ChatColor.BLUE + " Is now" + ChatColor.RED + " Offline");
                        saveConfig();
                    }
                    if (playerWhoWarned != null){
                        playerWhoWarned.sendMessage(getConfig().getString("Messages.offline") );
                    }
                }
               
           
            }
        }
    }
    To explain:
    we have three players:
    p1, p2, p3
    p1 and p2 are the first players who use the plugin on this server so: p1 issues /warn p2 then it get's messages about p2 join or leave
    then come p3 and issues /warn p1, it get's messages on p1 events but with name of p2
    How to solve this?
     
  11. Offline

    ErzLegendZero

    can you post the config?
     
Thread Status:
Not open for further replies.

Share This Page