Reputation: 13
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
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
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 :
runTaskTimer
method accept few arguments : the plugin, the runnable scheduler, the time before start (in tick) and the time between each call (in tick)/
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. ExampleDocumentation :
Upvotes: 1