我理解刻度,20秒到一秒等等但我没有得到语法。有人能向我解释用滴答声制作东西的步骤吗?我有一个火球移动到这里作为我需要蜱虫的例子;在每次它完成效果之后,我需要它等待,2个滴答。我看过其他例子,但我真的不懂语法
@EventHandler
public void onPlayerInteractBlockFireBall(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.MAGMA_CREAM){
List<Block> targets = player.getLineOfSight((Set)null, 30);
for (Block targetblock : targets){
Location target = targetblock.getLocation();
player.getWorld().playEffect(target, Effect.MOBSPAWNER_FLAMES,5);
}
}
}
我需要知道如何在循环中添加延迟,时间对于我想要制作的插件非常重要,我只需要知道语法。 有人帮忙吗?
答案 0 :(得分:3)
要制作计时器,您可以使用
Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable(){
public void run(){
//code
}
},ticksToWait);//run code in run() after ticksToWait ticks
因此,如果您想在运行函数shootFireball()
之前等待2个滴答,例如,您可以使用
Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable(){
public void run(){
shootFireball();
}
},2L);//run code in run() after 2 ticks
plugin
将是您Main
班级的实例(extends JavaPlugin
)。因此,例如,onEnable
类中的onDisable
和Main
函数可能如下所示:
public static Main that; //in your case "plugin" would be "Main.that"
@Override
public void onEnable(){
that = this; //Main.that is now equal to this class
}
@Override
public void onDisable(){
that = null; //Set to null to prevent memory leaks
}
所以,你的代码看起来像这样:
@EventHandler
public void onPlayerInteractBlockFireBall(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.MAGMA_CREAM){
List<Block> targets = player.getLineOfSight((Set)null, 30);
for (int i = 0; i < targets.size(); i++){
//call the spawnFlames with "i * 2" ticks, because
//every time "i" is incremented, there is a new target block
//which means we should wait 2 more ticks than the previous
//iteration before running the task
spawnFlames(player, targets.get(i), i * 2);
}
}
}
public void spawnFlames(final Player player, final Block target, final long ticks){
Bukkit.getServer().getScheduler().runTaskLater(Main.that, new Runnable(){
public void run(){
Location target = targetblock.getLocation();
player.getWorld().playEffect(target, Effect.MOBSPAWNER_FLAMES,5);
}
},ticks);
//run code in run() after "ticks" ticks
//"ticks" will be equal to "i * 2" from
//the onPlayerInteractBlockFireBall() method
}
答案 1 :(得分:1)
您将开始使用Scheduler Programming。 仔细查看教程的this part。
为此需要重复任务:
BukkitRunnable
。Runnable.run()
方法:每次迭代将调用一次。BukkitRunnable.runTaskTimer(Plugin, long, long)
启动任务。BukkitRunnable.cancel()
停止播放。你可以实现这样的功能:
List<Block> blocks; // The target blocks
BukkitRunnable task = new MagmaEffect(blocks);
Plugin main; // The unique instance of the main class
task.runTaskTimer(main, 0L, 2L);
public class MagmaEffect extends BukkitRunnable {
private List<Block> blocks;
public MagmaEffect(List<Block> blocks) {
this.blocks = blocks;
}
private int index;
@Override
public void run() {
Block block = blocks.get(index);
block.getWorld().playEffect(block.getLocation(), Effect.MOBSPAWNER_FLAMES, 5);
index++;
if (index == blocks.size()) {
cancel();
}
}
}