Solved Remove a specific String form a StringList

Discussion in 'Spigot Plugin Development' started by MrCreeper1216, Jan 14, 2020.

  1. Hi guys, I have some problems while removing a string from a stringlist.
    I want to remove the entire line which contains the playername and the joined time.
    This is the File with StringList:

    online-staffers:
    - §aCiccioPazzo Joined at 19:59:06

    These are the two listeners that I use, the joinlistener works perfectly, the problem is about quitlistener
    Code (Java):
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            Date ora = new Date();
            SimpleDateFormat formato = new SimpleDateFormat("HH:mm:ss");
            if(p.hasPermission("staffalert.admin")) {
                if(!plugin.onlinestaff.contains(p.getName())) {
                    plugin.onlinestaff.add(p.getName());
                    List<String> staff = plugin.stdata.getStringList("online-staffers");
                    staff.add(ChatColor.GREEN+p.getName() + "§7 Joined at §f" +formato.format(ora));
                    plugin.stdata.set("online-staffers", staff);
                    try {
                        plugin.stdata.save(plugin.staffData);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
               
            }
        }

    Code (Java):
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            if(p.hasPermission("staffalert.admin")) {
                if(plugin.onlinestaff.contains(p.getName())) {
                plugin.onlinestaff.remove(p.getName());
                List<String> staff = plugin.stdata.getStringList("online-staffers");
                staff.remove(ChatColor.GREEN+p.getName());
                plugin.stdata.set("online-staffers", staff);
                try {
                    plugin.stdata.save(plugin.staffData);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                }
            }
        }
     
  2. Can you tell us what is the problem? Will help a lot :D
     
    • Like Like x 1
  3. In join listener use
    Code (Text):
    HashMap<String, Long>
    for adding
    Code (Text):
    hashmap.put(player.getName(), System.currenttimemillis())
    That is simply at use and understanding.

    How i understand you wanna to save all online staff, maybe - for show info "who is online". I think work with configs is not good idea.

    If i create plugin with that feature i create that like:
    1) Create
    Code (Java):
    HashMap<String, Long> onlinestaff = new HashMap<>();
    2) Add to that map if join by method
    Code (Java):
    onlinestaff.put(p.getName(), system.currenttimemillis())
    anf remove from list if quit by
    Code (Java):
    onlinestaff.remove(p.getName())
    3) And construct a message like "&a{PLAYERNAME} &fonline from &a{TIME}"

    Code (Java):
    public static String getFormatedOnlineMessage(Player p){
      String format = "&a{NAME} &fonline from &a{TIME}";
      Long jointime = onlinestaff.get(p.getName());

      return format.replace("{NAME}", p.getName()).replace("{TIME}", new Date(jointime).toString());
    }
    UPD: edit some and sorry for my english
     
    • Like Like x 1
  4. Just add the name, no need to store the whole message.
     
    • Like Like x 1
  5. List#removeIf(string -> string.contains(player.getName()))
    this will remove all strings that contain the players name from the list
     
    • Agree Agree x 2
    • Like Like x 1
  6. Sorry guys, i totally forgot this thread! However i had already solved, this is my solution!
    String join = plugin.data.stdata.getString("staff-joined." + p.getUniqueId().toString()+ ".join");
    plugin.data.stdata.set("staff-joined." + p.getUniqueId().toString()+ ".quit", time());
    List<String> staff = plugin.data.stdata.getStringList("online-staffers");
    staff.remove(ChatColor.GREEN+p.getName()+" §7Joined at §f"+join);

    Thank you all for supporting me!