好的我正在制作一个Minecraft java插件,它会播放一首使用预设声音的播放器播放的歌曲:
player.playSound(player.getLocation(), Sound.NOTE_BASS, 2, 10);
我希望它能让它成为歌词,例如锤击时间,所以看起来像这样:
Bukkit.broadcastMessage(ChatColor.DARK_PURPLE +"[Server]" +ChatColor.AQUA + "Na Na Na Na, Na Na");
player.playSound(player.getLocation(), Sound.NOTE_BASS, 1, 10);
player.playSound(player.getLocation(), Sound.NOTE_BASS, 2, 10);
player.playSound(player.getLocation(), Sound.NOTE_BASS, 3, 10);
player.playSound(player.getLocation(), Sound.NOTE_BASS, 4, 10);
player.playSound(player.getLocation(), Sound.NOTE_BASS, 1, 10);
//a sleep statement here
等等......
所以我不想使用Thread.sleep(100)
或wait()
,因为他们需要我使用try
,catch
声明,因为我需要它作为大脚本我carnt在脚本中有一堆try catch。
所以我想我想问我还有其他办法吗?
答案 0 :(得分:2)
您可以妥协并使用一个try-catch
:
try {
player.playSound(...);
Thread.sleep(100);
player.playSound(...);
Thread.sleep(100);
/* etc */
} catch (InterruptedException e) {
e.printStackTrace();
// handle exception
}
答案 1 :(得分:2)
您应该使用Bukkit Scheduler。既然你想尽可能地将它作为脚本,我建议你这样做。它确实使用了一个你可能没有达到的称为接口的东西,但你现在不需要理解它就可以使用它。
制作一个新课程,其中包含您的笔记和implements Runnable
所需的所有信息。
public class Note implements Runnable {
private Sound note;
private Player player;
private float volume;
private float pitch;
public Note(Player player, Sound note, float volume, float pitch) {
this.player = player;
this.note = note;
this.volume = volume;
this.pitch = pitch;
}
@Override
public void run() {
player.playSound(player.getLocation(), note, volume, pitch);
}
}
现在你可以在名为scheduledSyncDelayedTask()
的东西中使用这个类(长名,惊人的结果:P)。有了它,你可以用以下单行语句发送玩家注释:
Player player = Bukkit.getPlayer("InspiredOne");
BukkitScheduler sched = plugin.getServer().getScheduler();
long delayInServerTicks = 20; // There are 20 server ticks per second.
int id; // Used if you need to cancel the task
id = sched.scheduleSyncDelayedTask(plugin, new Note(player, Sound.NOTE_BASS, 1, 10), delayInServerTicks);
此方法实际上并不允许和弦结构,因此如果您想这样做,您可以创建一个基本上包含注释列表的类,如下所示:
public class Note {
public Sound note;
public Player player;
public float volume;
public float pitch;
public Note(Player player, Sound note, float volume, float pitch) {
this.player = player;
this.note = note;
this.volume = volume;
this.pitch = pitch;
}
}
然后你像以前一样制作实现Runnable的东西,但现在使用for循环一次播放所有音符,这样你就会得到一个和弦:
public class runNote implements Runnable {
private Note[] notes;
public runNote(Note[] notes) {
this.notes = notes;
}
@Override
public void run() {
for(Note note:notes) {
note.player.playSound(note.player.getLocation(), note.note, note.pitch, note.volume);
}
}
}
现在你可以这样做:
Player player = Bukkit.getPlayer("InspiredOne");
BukkitScheduler sched = plugin.getServer().getScheduler();
long delayInServerTicks = 20; // There are 20 server ticks per second.
int id; // Used if you need to cancel the task
id = sched.scheduleSyncDelayedTask(plugin, new runNote(new Note[] {new Note(player, Sound.NOTE_BASS, 1, 10)}), delayInServerTicks);
如果您发现经常使用特定的和弦,可以将它们变成这样的变量:
Note[] chord =new Note[] {
new Note(player, Sound.NOTE_BASS, 1, 10),
new Note(player, Sound.NOTE_BASS, 2, 10),
new Note(player, Sound.NOTE_BASS, 3, 10)
// ect...
};
如果你找到 AsychDelayedTask ,那就是一个不推荐使用的方法,这意味着Bukkit正在逐步淘汰它。不要使用它。此外,Bukkit调度程序具有重复的任务方法,允许您轻松地反复重复操作。此方法为scheduleSyncRepeatingTask()
。有了这个你需要取消任务,否则当玩家注销时会冒空指针,或者做一些导致错误的事情。如果您需要取消延迟任务或重复任务,请使用:
sched.cancelTask(id);
我认为这就是一切。希望能帮助到你。 :)