下面列出了怎么用org.bukkit.Sound的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public final void onExplode(FireworkPlayerDetails details) {
final Location loc = details.getFirework().getLocation();
if (!_changing && loc.getY() > 255) {
_changing = true;
final World world = loc.getWorld();
world.setStorm(false);
world.playSound(loc, Sound.ENTITY_LIGHTNING_BOLT_THUNDER, 100, 0);
final BukkitTask[] task = new BukkitTask[1];
task[0] = Bukkit.getScheduler().runTaskTimer(getPlugin(), new Runnable() {
@Override
public void run() {
long time = world.getFullTime() + 250;
world.setFullTime(time);
if (Math.abs(_finalTime - Math.abs(time%24000)) < 500) {
world.playSound(loc, Sound.ENTITY_LIGHTNING_BOLT_THUNDER, 100, 0);
task[0].cancel();
_changing = false;
}
}
}, 0, 5);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerToggleFlight(final PlayerToggleFlightEvent event) {
Player player = event.getPlayer();
Jumper jumper = this.jumpers.get(player);
if (jumper == null) return;
if (event.isFlying()) {
event.setCancelled(true);
this.setCharge(jumper, 0f);
this.refreshJump(player);
// calculate jump
Vector impulse = player.getLocation().getDirection();
impulse.setY(0.75 + Math.abs(impulse.getY()) * 0.5);
impulse.multiply(jumper.kit.power / 3f);
event.getPlayer().setVelocity(impulse);
player.getWorld().playSound(player.getLocation(), Sound.ZOMBIE_INFECT, 0.5f, 1.8f);
}
}
private void startAnimation(Player p, Block b, Inventory inv, ItemStack output) {
for (int j = 0; j < 4; j++) {
int current = j;
Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, () -> {
p.getWorld().playEffect(b.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
p.getWorld().playEffect(b.getLocation(), Effect.ENDER_SIGNAL, 1);
if (current < 3) {
p.getWorld().playSound(b.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1F, 1F);
}
else {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
inv.addItem(output);
}
}, j * 20L);
}
}
@EventHandler
public void onModifierFail(ModifierFailEvent event) {
Player player = event.getPlayer();
ItemStack tool = event.getTool();
Modifier mod = event.getMod();
if (MineTinker.getPlugin().getConfig().getBoolean("Sound.OnFail")) {
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_BREAK, 1.0F, 0.5F);
}
if (!event.isCommand()) {
ChatWriter.sendActionBar(player,
LanguageManager.getString("TinkerListener.ModifierFail", player)
.replace("%mod", mod.getColor() + mod.getName() + ChatColor.WHITE)
.replace("%tool", ChatWriter.getDisplayName(tool) + ChatColor.WHITE)
.replace("%cause", event.getFailCause().toString(player)));
ChatWriter.log(false, player.getDisplayName() + " failed to apply " + mod.getColor()
+ mod.getName() + ChatColor.GRAY + " " + (modManager.getModLevel(tool, mod) + 1) + " on "
+ ChatWriter.getDisplayName(tool) + ChatColor.GRAY + " (" + tool.getType().toString() + ") ("
+ event.getFailCause().toString() + ")");
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
// Glide options
if (!p.hasPermission("redprotect.bypass.glide")) {
if (!RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).player_glide.allow_elytra) {
ItemStack item = p.getInventory().getChestplate();
if (item != null && item.getType().equals(Material.ELYTRA)) {
PlayerInventory inv = p.getInventory();
inv.setChestplate(new ItemStack(Material.AIR));
if (inv.firstEmpty() == -1) {
p.getWorld().dropItem(p.getLocation(), item);
} else {
inv.setItem(inv.firstEmpty(), item);
}
p.playSound(p.getLocation(), Sound.ENTITY_ITEM_PICKUP, 10, 1);
RedProtect.get().lang.sendMessage(p, "globallistener.elytra.cantequip");
}
}
}
}
private void insertItem(Player p, Block b) {
ItemStack hand = p.getInventory().getItemInMainHand();
ItemStack stack = new CustomItem(hand, 1);
if (p.getGameMode() != GameMode.CREATIVE) {
ItemUtils.consumeItem(hand, false);
}
String nametag = ItemUtils.getItemName(stack);
Item entity = b.getWorld().dropItem(b.getLocation().add(0.5, 1.2, 0.5), new CustomItem(stack, "&5&dALTAR &3Probe - &e" + System.nanoTime()));
entity.setVelocity(new Vector(0, 0.1, 0));
SlimefunUtils.markAsNoPickup(entity, "altar_item");
entity.setCustomNameVisible(true);
entity.setCustomName(nametag);
p.playSound(b.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.3F, 0.3F);
}
public static void playSound(Location loc, String sound, float volume, float pitch) {
Sound s;
try {
s = Sound.valueOf(sound);
} catch (IllegalArgumentException ex) {
try {
//Try to resolve the 1.8 Sounds
s = Sound.valueOf(sound.substring(sound.indexOf("_") + 1, sound.length()).replace("_AMBIENT", "").replace("GENERIC_", "").replace("EXPERIENCE_", "").replace("PLAYER_", ""));
} catch (IllegalArgumentException ex2) {
return;
}
}
loc.getWorld().playSound(loc, s, volume, pitch);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
Inventory inv = e.getInventory();
InventoryView inView = e.getPlayer().getOpenInventory();
if (inView.getTitle().equals(new Messaging.MessageFormatter().format("event.crateInv"))) {
for (GameMap gMap: GameMap.getPlayableArenas(GameType.ALL)) {
for (Crate crate: gMap.getCrates()) {
if(crate.getInventory().equals(inv) && inv.getViewers().size() <= 1) {
if (SkyWarsReloaded.getNMS().getVersion() < 9) {
e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.valueOf("CHEST_CLOSE"), 1, 1);
} else {
e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.BLOCK_CHEST_CLOSE, 1, 1);
}
SkyWarsReloaded.getNMS().playEnderChestAction(e.getPlayer().getWorld().getBlockAt(crate.getLocation()), false);
return;
}
}
}
} else if (inView.getTitle().contains("chest.yml")) {
SkyWarsReloaded.getCM().save(inView.getTitle());
}
}
protected boolean priorityKick(MatchPlayer joining) {
if(!jmm.canPriorityKick(joining)) return false;
List<MatchPlayer> kickable = new ArrayList<>();
for(MatchPlayer player : getMatch().getParticipatingPlayers()) {
if(!jmm.canPriorityKick(player)) kickable.add(player);
}
if(kickable.isEmpty()) return false;
MatchPlayer kickMe = kickable.get(getMatch().getRandom().nextInt(kickable.size()));
kickMe.sendWarning(new TranslatableComponent("gameplay.ffa.kickedForPremium"), false);
kickMe.sendMessage(Links.shopPlug("shop.plug.ffa.neverKicked"));
kickMe.playSound(Sound.ENTITY_VILLAGER_HURT, kickMe.getBukkit().getLocation(), 1, 1);
getMatch().setPlayerParty(kickMe, getMatch().getDefaultParty());
return true;
}
private void runPostTask(Block block, Sound sound, int times) {
if (!(block.getBlockData() instanceof Levelled)) {
block.getWorld().playSound(block.getLocation(), Sound.BLOCK_METAL_BREAK, 1F, 1F);
return;
}
block.getWorld().playSound(block.getLocation(), sound, 1F, 1F);
int level = 8 - times;
Levelled le = (Levelled) block.getBlockData();
le.setLevel(level);
block.setBlockData(le, false);
if (times < 8) {
Slimefun.runSync(() -> runPostTask(block, sound, times + 1), 50L);
}
else {
block.getWorld().playSound(block.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerToggleFlight(final PlayerToggleFlightEvent event) {
Player player = event.getPlayer();
Jumper jumper = this.jumpers.get(player);
if(jumper == null) return;
if(event.isFlying()) {
event.setCancelled(true);
this.setCharge(jumper, 0f);
this.refreshJump(player);
// calculate jump
Vector impulse = player.getLocation().getDirection();
impulse.setY(0.75 + Math.abs(impulse.getY()) * 0.5);
impulse.multiply(jumper.kit.power / 3f);
event.getPlayer().applyImpulse(impulse, true);
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ZOMBIE_INFECT, 0.5f, 1.8f);
}
}
@Override
public void doOpen(Player player, Crate crate, Location location) {
if (chestSound) {
Sound sound = null;
try {
sound = Sound.valueOf("CHEST_OPEN");
} catch (Exception e) {
try {
sound = Sound.valueOf("BLOCK_CHEST_OPEN");
} catch (Exception ee) {
// This should never happen!
}
}
if (sound != null)
player.playSound(player.getLocation(), sound, (float) 0.5, 1);
}
crate.handleWin(player);
finish(player);
}
private void craft(Player p, Block b, ItemStack output, Inventory outputInv) {
for (int i = 0; i < 4; i++) {
int j = i;
Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, () -> {
p.getWorld().playSound(b.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1);
p.getWorld().playEffect(b.getRelative(BlockFace.UP).getLocation(), Effect.SMOKE, 4);
p.getWorld().playEffect(b.getRelative(BlockFace.UP).getLocation(), Effect.SMOKE, 4);
p.getWorld().playEffect(b.getRelative(BlockFace.UP).getLocation(), Effect.SMOKE, 4);
if (j < 3) {
p.getWorld().playSound(b.getLocation(), Sound.ENTITY_TNT_PRIMED, 1F, 1F);
}
else {
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1F, 1F);
outputInv.addItem(output);
}
}, i * 20L);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerDeath(CardinalDeathEvent event) {
TrackerDamageEvent tracker = DamageTracker.getEvent(event.getPlayer());
boolean time = tracker != null && System.currentTimeMillis() - tracker.getTime() <= 7500;
if (time) {
if (event.getTrackerDamageEvent() == null) {
event.setTrackerDamageEvent(tracker);
}
if (event.getKiller() == null && tracker.getDamager() != null) {
event.setKiller(tracker.getDamager().getPlayer());
}
}
for (Player player : Bukkit.getOnlinePlayers()) {
if (player == event.getPlayer()) {
player.playSound(player.getLocation(), Sound.ENTITY_IRONGOLEM_DEATH, 1, 1);
} else if (event.getKiller() != null && player == event.getKiller()) {
player.playSound(player.getLocation(), Sound.ENTITY_IRONGOLEM_DEATH, 1, 1.35F);
} else {
player.playSound(event.getPlayer().getLocation(), Sound.ENTITY_IRONGOLEM_HURT, 1, 1.35F);
}
}
}
@EventHandler
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
if (!event.isCancelled()) {
ItemStack item1 = event.getItem().getItemStack();
if (materials.contains(item1.getType()) && event.getPlayer().getInventory().contains(item1.getType())) {
for (ItemStack item2 : event.getPlayer().getInventory().getContents()) {
if (item2 != null && Items.toMaxDurability(item1).equals(Items.toMaxDurability(item2))) {
event.setCancelled(true);
event.getItem().remove();
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.1F, 1);
int result = item2.getDurability() - (item1.getType().getMaxDurability() - item1.getDurability());
item2.setDurability((short) (result < 0 ? 0 : result));
break;
}
}
}
}
}
private void broadcastMessageToAllTownMembers(Town town, boolean entering, String playerName) {
if (lastMessage.containsKey(playerName)) {
if (lastMessage.get(playerName) + 60000 > System.currentTimeMillis()) {
return;
}
}
lastMessage.put(playerName, System.currentTimeMillis());
for (Player p : Bukkit.getOnlinePlayers()) {
if (town.getPeople().containsKey(p.getUniqueId())) {
Civilian civilian = CivilianManager.getInstance().getCivilian(p.getUniqueId());
String message;
if (entering) {
message = LocaleManager.getInstance().getTranslation(civilian.getLocale(),
"intruder-enter").replace("$1", playerName).replace("$2", town.getName());
} else {
message = LocaleManager.getInstance().getTranslation(civilian.getLocale(),
"intruder-exit").replace("$1", playerName).replace("$2", town.getName());
}
p.sendMessage(Civs.getPrefix() + ChatColor.RED + message);
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BELL, 1, 1);
}
}
}
/**
* Writes the material and sound differences for updating purposes.
*
* @param path the path folder to save the files to.
* @since 6.0.0
*/
public static void versionDifference(Path path) {
Path materials = path.resolve("XMaterial.txt");
Path sounds = path.resolve("XSound.txt");
writeDifference(materials, Material.class, XMaterial.class);
writeDifference(sounds, Sound.class, XSound.class);
}
/**
* Recharge the shield to its maximum health. If the player has more absorption than the current
* shield strength, the excess is preserved.
*/
void recharge() {
if (shieldHealth < parameters.maxHealth) {
double delta = parameters.maxHealth - shieldHealth;
logger.fine("Recharging shield: shield=" + shieldHealth + " delta=" + delta);
shieldHealth = parameters.maxHealth;
addAbsorption(delta);
bukkit.playSound(bukkit.getLocation(), Sound.ORB_PICKUP, 1, 2);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void handleInstantActivation(BlockPlaceEvent event) {
if (this.properties.instantIgnite && event.getBlock().getType() == Material.TNT) {
World world = event.getBlock().getWorld();
TNTPrimed tnt =
world.spawn(
event.getBlock().getLocation().clone().add(new Location(world, 0.5, 0.5, 0.5)),
TNTPrimed.class);
if (this.properties.fuse != null) {
tnt.setFuseTicks(this.getFuseTicks());
}
if (this.properties.power != null) {
tnt.setYield(this.properties.power); // Note: not related to EntityExplodeEvent.yield
}
if (callPrimeEvent(tnt, event.getPlayer())) {
event.setCancelled(true); // Allow the block to be placed if priming is cancelled
world.playSound(tnt.getLocation(), Sound.FUSE, 1, 1);
ItemStack inHand = event.getPlayer().getItemInHand();
if (inHand.getAmount() == 1) {
event.getPlayer().setItemInHand(null);
} else {
inHand.setAmount(inHand.getAmount() - 1);
}
}
}
}
private void useItem(Player p, ItemStack item, Location loc) {
loc.getWorld().strikeLightning(loc);
if (p.getInventory().getItemInMainHand().getType() == Material.SHEARS) {
return;
}
if (p.getGameMode() != GameMode.CREATIVE) {
FoodLevelChangeEvent event = new FoodLevelChangeEvent(p, p.getFoodLevel() - 4);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
p.setFoodLevel(event.getFoodLevel());
}
}
ItemMeta meta = item.getItemMeta();
int usesLeft = meta.getPersistentDataContainer().getOrDefault(usageKey, PersistentDataType.INTEGER, MAX_USES);
if (usesLeft == 1) {
p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
item.setAmount(0);
}
else {
usesLeft--;
meta.getPersistentDataContainer().set(usageKey, PersistentDataType.INTEGER, usesLeft);
List<String> lore = meta.getLore();
lore.set(4, ChatColors.color("&e" + usesLeft + ' ' + (usesLeft > 1 ? "Uses" : "Use") + " &7left"));
meta.setLore(lore);
item.setItemMeta(meta);
}
}
@EventHandler(ignoreCancelled = true)
public void onCraft(CraftItemEvent event) {
if (!(event.getWhoClicked() instanceof Player)) {
return;
}
Player player = (Player) event.getWhoClicked();
FileConfiguration config = MineTinker.getPlugin().getConfig();
if (config.getBoolean("Sound.OnEveryCrafting")) {
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_USE, 1.0F, 0.5F);
return;
}
ItemStack tool = event.getInventory().getResult();
if (!(modManager.isToolViable(tool) || modManager.isArmorViable(tool) || modManager.isWandViable(tool))) {
return;
}
if (config.getBoolean("Sound.OnCrafting")) {
player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_USE, 1.0F, 0.5F);
}
if (tool != null) {
ChatWriter.log(false, player.getName() + " crafted " + ChatWriter.getDisplayName(tool)
+ "! It is now a MineTinker-Item!");
}
}
private void addLiquidLevel(Block block, boolean water) {
int level = ((Levelled) block.getBlockData()).getLevel();
if (level > 7) {
level -= 8;
}
if (level == 0) {
block.getWorld().playSound(block.getLocation(), water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, 1F, 1F);
}
else {
int finalLevel = 7 - level;
Slimefun.runSync(() -> runPostTask(block, water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, finalLevel), 50L);
}
}
private boolean consumeJuice(Player p, PlayerBackpack backpack) {
Inventory inv = backpack.getInventory();
int slot = -1;
for (int i = 0; i < inv.getSize(); i++) {
ItemStack stack = inv.getItem(i);
if (stack != null && stack.getType() == Material.POTION && stack.hasItemMeta() && stack.getItemMeta().hasDisplayName()) {
slot = i;
break;
}
}
if (slot >= 0) {
PotionMeta im = (PotionMeta) inv.getItem(slot).getItemMeta();
for (PotionEffect effect : im.getCustomEffects()) {
p.addPotionEffect(effect);
}
p.setSaturation(6F);
p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F);
inv.setItem(slot, null);
backpack.markDirty();
return true;
}
return false;
}
@Override
public void onInteract(Player p, Block b) {
Block dispBlock = b.getRelative(BlockFace.DOWN);
Dispenser disp = (Dispenser) dispBlock.getState();
Inventory inv = disp.getInventory();
for (ItemStack current : inv.getContents()) {
for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
ItemStack output = RecipeType.getRecipeOutput(this, convert);
Inventory outputInv = findOutputInventory(output, dispBlock, inv);
if (outputInv != null) {
ItemStack removing = current.clone();
removing.setAmount(1);
inv.removeItem(removing);
outputInv.addItem(output);
p.getWorld().playSound(p.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1);
}
else {
SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
return;
}
}
}
SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
@Override
public Sound getSound(CommandContext cmdCtx, String key) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.e(cmdCtx, key);
for (CraftSound sound : CraftSound.values()) {
try {
if (CommandAPIHandler.getField(CraftSound.class, "minecraftKey").get(sound)
.equals(minecraftKey.getKey())) {
return Sound.valueOf(sound.name());
}
} catch (IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
return null;
}
@Override
public Sound getSound(CommandContext cmdCtx, String key) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.c(cmdCtx, key);
for (CraftSound sound : CraftSound.values()) {
try {
if (CommandAPIHandler.getField(CraftSound.class, "minecraftKey").get(sound)
.equals(minecraftKey.getKey())) {
return Sound.valueOf(sound.name());
}
} catch (IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
return null;
}
public ChestSlimefunGuide(boolean vanillaRecipes) {
showVanillaRecipes = vanillaRecipes;
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
sound = Sound.ITEM_BOOK_PAGE_TURN;
}
else {
sound = Sound.ENTITY_BAT_TAKEOFF;
}
}
private void displayCurrent() {
Stage stage = this.tutorial.getAllStages().get(this.pos);
if (stage == this.tutorial.getPrefix() || stage == this.tutorial.getSuffix()) {
this.player.playSound(this.player.getLocation(), Sound.BLOCK_PISTON_EXTEND, 0.5f, 2);
if (stage == this.tutorial.getPrefix()) {
this.player.sendMessage("");
}
}
this.player.sendMessage("");
this.player.sendMessage(stage.getFormattedTitle());
for (String line : stage.getLines()) {
this.player.sendMessage(line);
}
this.player.sendMessage("");
if (stage.getTeleport() != null) {
Location location = stage.getTeleport().getRandomPoint().getLocation();
if (!location.getBlock().getType().equals(Material.AIR) || !location.add(0, 1, 0).getBlock().getType().equals(Material.AIR)) {
this.player.sendMessage(" " + ChatColor.YELLOW + new LocalizedChatMessage(ChatConstant.ERROR_TUTORIAL_TP).getMessage(ChatUtil.getLocale(this.player)));
} else {
this.player.setFlying(true);
this.player.teleport(stage.getTeleport().getRandomPoint().getLocation());
this.player.playSound(this.player.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 0.5f, 1);
}
}
this.updateItem(player);
}
@Override
public Sound getSound(CommandContext cmdCtx, String key) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.c(cmdCtx, key);
for(CraftSound sound : CraftSound.values()) {
try {
if(CommandAPIHandler.getField(CraftSound.class, "minecraftKey").get(sound).equals(minecraftKey.getKey())) {
return Sound.valueOf(sound.name());
}
} catch (IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
return null;
}
@Override
public Sound getSound(CommandContext cmdCtx, String key) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.c(cmdCtx, key);
for(CraftSound sound : CraftSound.values()) {
try {
if(CommandAPIHandler.getField(CraftSound.class, "minecraftKey").get(sound).equals(minecraftKey.getKey())) {
return Sound.valueOf(sound.name());
}
} catch (IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
return null;
}