Solved Activate/deactivate ActionBar by Command

Discussion in 'Spigot Plugin Development' started by Minimigamer, Jan 25, 2020.

  1. Hey guys, Im trying to make a /vanish command with a actionbar that displays: You are invisible
    but with a scheduleSyncRepeatingTask the server the plugin stoped working.
    The Command sould work like this:


    You do the Command and you get added to a ArrayList and the Actionbar sould be displayed
    if you do the command again, you get removed from the ArrayList and the Actionbar is no longer displayed

    The vanish command itself works, but the Aktionbar is a problem

    Thx for your help!
     
  2. Is the actionbar not showing up or are you having problems finding how to send an action bar message.
    Can you show some code,
     
  3. I can send the Actionbar, but the message is only visible for 2 sec and i want it to be visible until you repeat the command
     
  4. Wdym by this: "with a scheduleSyncRepeatingTask the server the plugin stoped working"
    Does the server crash if you call it from a repeating task? Error in console?
     
  5. I fixed it... the scheduler called itself so there are more and more shedulers with a loop...
    and i forgot to cancel the sheduler by repeating the command
     
  6. But if two peple go in vanish and one of them does /vanish the Actionbar is no longer visible for both
    so how do know what task i have to cancel ?
     
  7. You could just stick to a single scheduler. Say you have a task VanishMessageRunnable which runs through a list of players/UUIDs it gets handed in the constructor. You can just schedule that to repeat every 2 seconds. If you modify that list (just also pass the list instance to your command class) people in the list will get the message.
    Don't use static for this, but pass the list as a parameter as I said.

    Code (Java):
    List<UUID> vanishedPlayers = new ArrayList<>();
    new YourCommand(vanishedPlayers);
    new YourMessageUpdaterRunnable(vanishedPlayers);
     
  8. My code looks like this:

    Code (Text):

    if (args.length == 0) {
                    if (p.hasPermission("server.vanish")) {
                        if (Main.vanished.contains(p)) {
                            Main.vanished.remove(p);
                            p.sendMessage(Main.prefix + "§aDu bist jetzt §cnicht §amehr im §3Vanish!");
                           
                            p.sendMessage(String.valueOf(Main.vanisTasks.get(p)));
                           
                            Bukkit.getScheduler().cancelTask(Main.vanisTasks.get(p));
                            Main.vanisTasks.remove(p, Bukkit.getScheduler().getPendingTasks().size());
                            for (Player all : Bukkit.getOnlinePlayers()) {
                                all.showPlayer(p);
                            }

                        } else {
                            Main.vanished.add(p);
                            p.sendMessage(Main.prefix + "§aDu bist jetzt im §3Vanish!");
                            for (Player all : Bukkit.getOnlinePlayers()) {
                                all.hidePlayer(p);
                            }
                            Main.vanisTasks.put(p, Bukkit.getScheduler().getPendingTasks().size());
                            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {
                                   
                                    @Override
                                    public void run() {
                                        de.BunchOfSystems.someMethods.ActionBarManager.sendActionbar(p,
                                                ChatColor.GREEN + "Du bist für andere Spieler unsichtbar!");
                                    }
                                }, 0L, 15L);
                        }
     
  9. static abuse :(
    instead of having a static player list, either inject the list via the constructor or inject a manager class instance that has the list. Using "public static" can lead to hard to maintain, unreadable code and is also very vulnerable to modification.

    Add the list as a constructor param. Then "store" it as a private final variable in both your command and a class called something like VanishMessageRunnable implementing Runnable. By modifying the list it gets edited in both classes (as long as you passed in the same list to both constructors, as variables are just references).
    In your main in onEnable you can start the updater like so:

    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new VanishMessageRunnable(yourVanishedPlayerList), 0L, 15L);

    If you have issues implementing that, you can hit me up on Discord: Gerolmed#7904 (I am German too)
     
  10. Thank you very much it is now working perfectly