Command For A Few Seconds

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Aug 7, 2018.

  1. How do I do a command for a few seconds? (For context, I want to change gamerule for a few seconds then back).

    Code (Java):
    package com.jaffaaaa.prisonevents.events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;

    public class randomTickSpeed implements CommandExecutor {

        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!cmd.getName().equalsIgnoreCase("jaffaticktest")) {
                return false;
            }
           
            if (!sender.hasPermission("prisonevents.ticktest")) {
                sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                return false;
            }
           
            ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
            String command = "gamerule randomTickSpeed 1000 PlotsWorld";
            Bukkit.dispatchCommand(console, command);
           
            Bukkit.broadcastMessage(ChatColor.DARK_RED + "Event: " + ChatColor.AQUA + "Tick speed is now " + ChatColor.GOLD + "33x!");
            return true;
        }
    }
     
  2. Your best bet would be to just use a bukkitrunnable
     
  3. For starters:
    Code (Java):

          ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
             String command = "gamerule randomTickSpeed 1000 PlotsWorld";
            Bukkit.dispatchCommand(console, command);
     
    This is bad practice and shouldn't be done this way. Use the spigot API provided to make changes for the gamerules.

    You should take a look into Schedulers.

    EDIT: for gamerules use: World#setGameRuleValue(java.lang.String rule, java.lang.String value)
     
    #3 darklazerog, Aug 7, 2018
    Last edited: Aug 7, 2018
    • Agree Agree x 1
  4. I don't think I understand BukkitRunnables. Could someone help me a little bit more?
     
  5. This disables a game rule and enables it after 60 seconds.
    Code (Java):
    World w = Bukkit.getWorld("world");
    w.setGameRuleValue("doDaylightCycle", "false");
    Bukkit.getScheduler().runTaskLater(this, () -> w.setGameRuleValue("doDaylightCycle", "true"), 60 * 20L);
     
  6. "The method runTaskLater(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (randomTickSpeed, Runnable, long)"
     
  7. Sorry, my glass sphere broke, I can not look up the code, so would you be so kind and show what you typed? ;)
     
    • Agree Agree x 1
    • Funny Funny x 1
  8. :p
    Code (Java):
    package com.jaffaaaa.prisonevents.events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.scheduler.BukkitRunnable;

    public class randomTickSpeed implements CommandExecutor {

        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!cmd.getName().equalsIgnoreCase("jaffaticktest")) {
                return false;
            }
           
            if (!sender.hasPermission("prisonevents.ticktest")) {
                sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                return false;
            }
           
               World w = Bukkit.getWorld("PlotsWorld");
               w.setGameRuleValue("randomTickSpeed", "100");
               Bukkit.getScheduler().runTaskLater(this, () -> w.setGameRuleValue("randomTickSpeed", "3"), 10 * 20L);
           
            Bukkit.broadcastMessage(ChatColor.DARK_RED + "Event: " + ChatColor.AQUA + "Tick speed is now " + ChatColor.GOLD + "33x!");
            Bukkit.broadcastMessage(ChatColor.AQUA + "Farms will now grow 33x faster for five seconds!");
            return true;
        }
    }
     
  9. You have to declare the instance of your main class.
    Replace this with the instance of the plugin.
     
  10. 1. Rip Java Conventions
    2. Learn Bukkit Schedulers before using them
    3. The first Argument is for the Plugin Instance but you're putting your CommandExecutor Instance
     
    • Agree Agree x 1
  11. Spoon feeding will only cause situations similar to this one. Give him some code, he'll paste it down and come back here asking for more help. Instead of spoon feeding, allow him to take his time to actually learn how to use schedulers.
     
    • Agree Agree x 2