Cedric Valentin Piehl
Cedric Valentin Piehl

Reputation: 13

How to use normal Minecraft commands in a Minecraft Plugin?

I want to program a Minecraft plugin and I want to use commands that are standard in Minecraft like the /worldboarder set command.

Specifically, I would like to program a plugin that set the worldboarder on 1000 blocks, and every hour it gets 50 blocks smaller. Is it possible to use the standard command in my plugin in a loop? Like the command /worldboarder set 1000 and how I use them in my plugin.

I would like to do something like this:

int distance = 1000;
while(distance > 100) {
    wait(3600000);
    // "/worldboarder set " + distance -50; // here how to run cmd ?
}

Upvotes: 0

Views: 1257

Answers (2)

Lanfix
Lanfix

Reputation: 131

In newer bukkit versions, you can use the bukkit.WorldBorder object (https://hub.spigotmc.org/javadocs/spigot/org/bukkit/WorldBorder.html)

You can modify the worldborder of a world by fisrt getting the world then getting its WorldBorder object

World world = Bukkit.getWorld("world"); // Most of the time the default world is named "world"
WorldBorder border = world.getWorldBorder();

Then you can do whatever you want with the worldborder

border.setSize(border.getSize() - 50); // Reduce the size of the border by 50 instantly

Hope this will be usefull if someone finds this post

Upvotes: 0

Elikill58
Elikill58

Reputation: 4908

Firstly, you can't use wait function in the server, this will freeze the entiere server which is a big issue. You have to use Scheduler (see later).

Then, to run a simple comment, you have to use this:

Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycmd arg1 arg2");

So, you have to do something like that :

private int border = 1000; // actual border value
private BukkitTask task; // bukkit task to cancel it

@Override
public void onEnable() {
   task = getServer().getScheduler().runTaskTimer(this, () -> { // start lambda expression
      Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "worldboarder set " + border); // run cmd
      if(border == 100) { // no longer need to change world border.
         task.cancel(); // cancel actual task to never run it again
         return;
      }
      border -= 50; // reduce border amount for next time
   }, 20, 60 * 60 * 20);
}

Few additionnal informations :

  • The runTaskTimer method accept few arguments : the plugin, the runnable scheduler, the time before start (in tick) and the time between each call (in tick)
  • 20 ticks = 1 second. Such as you want each hour: 60 * 60 = seconds per hour, so 60 * 60 * 20 = tick per hour
  • The time before start is 20 (so 1s) because spigot start plugin BEFORE fully load world. So it will not find worlds. And it will start to run scheduler after full server start (so after world). 1s is just to be sure, such as it doesn't change anything to gameplay to wait 1s at startup.
  • Don't need "/" in command. The / is the default character to say it's a command. Such as here we clearly say it's a command, we don't have to use it. Example

Documentation :

Upvotes: 1

Related Questions