下面列出了怎么用org.bukkit.entity.Firework的API类实例代码及写法,或者点击链接到github查看源代码。
private void showFlare() {
float angle = (float) (this.random.nextFloat() * Math.PI * 2);
Location location = this.definition.detectRegion.getBounds().center()
.plus(
new Vector(
Math.sin(angle) * this.definition.flareRadius,
0,
Math.cos(angle) * this.definition.flareRadius
)
).toLocation(this.match.getWorld());
Set<Color> colors = new HashSet<>();
for(MatchPlayer player : this.playersInside) {
colors.add(player.getParty().getFullColor());
}
Firework firework = FireworkUtil.spawnFirework(location,
FireworkEffect.builder()
.with(FireworkEffect.Type.BALL)
.withColor(colors)
.build(),
0);
NMSHacks.skipFireworksLaunch(firework);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (!(Gizmos.gizmoMap.get(player) instanceof LauncherGizmo)) return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR) return;
if (player.getItemInHand().getType() != this.getIcon()) return;
Firework oldFirework = this.launchedPlayers.get(player);
if (oldFirework == null || oldFirework.isDead()) {
Firework firework = this.buildFirework(player.getLocation());
firework.setPassenger(player);
this.launchedPlayers.put(player, firework);
}
}
public void spawnFirework(Location location) {
Firework fw = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta fwm = fw.getFireworkMeta();
Random r = new Random();
int rt = r.nextInt(4) + 1;
FireworkEffect.Type type = FireworkEffect.Type.BALL;
if (rt == 1) type = FireworkEffect.Type.BALL;
if (rt == 2) type = FireworkEffect.Type.BALL_LARGE;
if (rt == 3) type = FireworkEffect.Type.BURST;
if (rt == 4) type = FireworkEffect.Type.CREEPER;
if (rt == 5) type = FireworkEffect.Type.STAR;
int r1i = r.nextInt(17) + 1;
int r2i = r.nextInt(17) + 1;
Color c1 = getColor(r1i);
Color c2 = getColor(r2i);
FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
fwm.addEffect(effect);
int rp = r.nextInt(2) + 1;
fwm.setPower(rp);
fw.setFireworkMeta(fwm);
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Optional<TeamModule> team = Teams.getTeamByPlayer(event.getPlayer());
if (region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector()) && team.isPresent() && !team.get().isObserver() && GameHandler.getGameHandler().getMatch().isRunning()) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (notify == null) {
if (detect.evaluate(player).equals(FilterState.DENY)) {
player.sendMessage(ChatColor.RED + message);
}
} else if (notify.evaluate(player).equals(FilterState.ALLOW)) {
player.sendMessage(ChatColor.RED + message);
}
}
RegionModule radius = new CylinderRegion("radius", region.getCenterBlock().getVector(), flareRadius, 1);
int flareAmount = new Random().nextInt(6);
for (int f = 0; flareAmount > f; f++) {
Firework firework = Fireworks.spawnFirework(radius.getRandomPoint().getLocation(),
Fireworks.getFireworkEffect(MiscUtil.convertChatColorToColor(Teams.getTeamColorByPlayer(event.getPlayer()))), 1);
Fireworks.explodeFirework(firework);
}
}
}
protected final Firework fire(Location location, IConsumableDetails details, Object userObject) {
final Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta meta = firework.getFireworkMeta();
final FireworkPlayerDetails fDetails = FireworkPlayerDetails.fromConsumableDetails(details, firework, userObject);
if (!onFire(fDetails, meta)) {
firework.remove();
return null;
}
firework.setFireworkMeta(meta);
final BukkitTask[] task = new BukkitTask[1];
task[0] = Bukkit.getScheduler().runTaskTimer(getPlugin(), new Runnable() {
@Override
public void run() {
if (firework.isDead()) {
onExplode(fDetails);
task[0].cancel();
}
firework.setTicksLived(Integer.MAX_VALUE);
}
}, 10 * (1 + meta.getPower()), 2);
return firework;
}
public static Firework spawnFirework(Location location, FireworkEffect effect, int power) {
Preconditions.checkNotNull(location, "location");
Preconditions.checkNotNull(effect, "firework effect");
Preconditions.checkArgument(power >= 0, "power must be positive");
FireworkMeta meta = (FireworkMeta) Bukkit.getItemFactory().getItemMeta(Material.FIREWORK);
meta.setPower(power);
meta.addEffect(effect);
Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
firework.setFireworkMeta(meta);
return firework;
}
public static @Nonnull Firework spawnFirework(@Nonnull Location location, @Nonnull FireworkEffect effect, int power) {
Preconditions.checkNotNull(location, "location");
Preconditions.checkNotNull(effect, "firework effect");
Preconditions.checkArgument(power >= 0, "power must be positive");
FireworkMeta meta = (FireworkMeta) Bukkit.getItemFactory().getItemMeta(Material.FIREWORK);
meta.setPower(power);
meta.addEffect(effect);
Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
firework.setFireworkMeta(meta);
return firework;
}
public void playFlareEffect() {
if(isCurrent(Spawned.class)) {
Location location = ((Spawned) this.state).getLocation();
if(location == null) return;
FireworkEffect effect = FireworkEffect.builder().with(FireworkEffect.Type.BURST).withColor(this.getDyeColor().getColor()).build();
Firework firework = FireworkUtil.spawnFirework(location, effect, 0);
NMSHacks.skipFireworksLaunch(firework);
}
}
public Rocket(Player observer, Player victim, List<Firework> fireworks) {
this.observer = observer;
this.victim = victim;
this.fireworks = Lists.newArrayList(fireworks);
this.previousCenter = this.getCenter();
}
public boolean allFireworksAlive() {
for(Firework firework : this.fireworks) {
if(firework.isDead()) return false;
}
return true;
}
public Vector getCenter() {
int num = this.fireworks.size();
double totalX = 0;
double totalY = 0;
double totalZ = 0;
for(Firework firework : this.fireworks) {
totalX += firework.getLocation().getX();
totalY += firework.getLocation().getY();
totalZ += firework.getLocation().getZ();
}
return new Vector(totalX / num, totalY / num, totalZ / num);
}
public static Firework getRandomFirework(Location loc) {
FireworkMeta fireworkMeta = (FireworkMeta) (new ItemStack(Material.FIREWORK)).getItemMeta();
Firework firework = (Firework) loc.getWorld().spawnEntity(loc, EntityType.FIREWORK);
fireworkMeta.setPower(GizmoConfig.FIREWORK_POWER);
fireworkMeta.addEffect(FireworkEffect.builder()
.with(RocketUtils.randomFireworkType())
.withColor(RocketUtils.randomColor())
.trail(GizmoConfig.FIREWORK_TRAIL)
.build());
firework.setFireworkMeta(fireworkMeta);
return firework;
}
private Firework buildFirework(Location loc) {
Firework firework = (Firework) loc.getWorld().spawnEntity(loc, EntityType.FIREWORK);
FireworkMeta fwm = firework.getFireworkMeta();
fwm.addEffect(FireworkEffect.builder()
.withColor(AMERICA_COLORS)
.with(FireworkEffect.Type.STAR)
.withTrail()
.withFade(AMERICA_COLORS)
.build());
firework.setFireworkMeta(fwm);
firework.setVelocity(loc.getDirection().divide(new Vector(2, 1, 2)));
return firework;
}
public void launch(Player player, int fireworkCount) {
for (int i = 0; i < fireworkCount; i++) {
Firework fw = player.getWorld().spawn(player
.getLocation(), Firework.class);
FireworkMeta fwMeta = fw.getFireworkMeta();
fwMeta.addEffect(FireworkEffect.builder().flicker(random
.nextBoolean()).withColor(colors[random.nextInt(14)]).withFade(
colors[random.nextInt(14)]).with(types[random.nextInt(3)])
.trail(random.nextBoolean()).build());
fwMeta.setPower(random.nextInt(3));
fw.setFireworkMeta(fwMeta);
}
}
@Override
protected void execute(Event e) {
Number power = lifetime.getSingle(e);
if (power == null)
power = 1;
for (Location location : locations.getArray(e)) {
Firework firework = location.getWorld().spawn(location, Firework.class);
FireworkMeta meta = firework.getFireworkMeta();
meta.addEffects(effects.getArray(e));
meta.setPower(power.intValue());
firework.setFireworkMeta(meta);
}
}
public static void fireWork(Location loc) {
final Firework fw = loc.getWorld().spawn(loc, Firework.class);
FireworkMeta fm = fw.getFireworkMeta();
fm.addEffects(FireworkEffect.builder().with(FireworkEffect.Type.BALL_LARGE).withColor(Color.RED).withColor(Color.AQUA).withColor(Color.ORANGE).withColor(Color.YELLOW).trail(false).flicker(false).build());
fm.setPower(0);
fw.setFireworkMeta(fm);
FireworkDamageEvent.addFirework(fw);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, fw :: detonate, 2);
}
private static void fireWork(Location loc, Color color) {
final Firework fw = (Firework) loc.getWorld().spawnEntity(loc, EntityType.FIREWORK);
FireworkMeta fm = fw.getFireworkMeta();
fm.addEffects(FireworkEffect.builder().with(FireworkEffect.Type.BALL).withColor(color).withColor(color).trail(false).flicker(false).build());
fm.setPower(0);
fw.setFireworkMeta(fm);
FireworkDamageEvent.addFirework(fw);
new BukkitRunnable() {
public void run() {
fw.detonate();
}
}.runTaskLaterAsynchronously(cc.getPlugin(), 1);
}
public static void launchFirework(Location l, Color color) {
Firework fw = (Firework)l.getWorld().spawnEntity(l, EntityType.FIREWORK);
FireworkMeta meta = fw.getFireworkMeta();
FireworkEffect effect = FireworkEffect.builder().flicker(CSCoreLib.randomizer().nextBoolean()).withColor(color).with(CSCoreLib.randomizer().nextInt(3) + 1 == 1 ? Type.BALL: Type.BALL_LARGE).trail(CSCoreLib.randomizer().nextBoolean()).build();
meta.addEffect(effect);
meta.setPower(CSCoreLib.randomizer().nextInt(2) + 1);
fw.setFireworkMeta(meta);
}
public static Firework createFirework(Location l, Color color) {
Firework fw = (Firework)l.getWorld().spawnEntity(l, EntityType.FIREWORK);
FireworkMeta meta = fw.getFireworkMeta();
FireworkEffect effect = FireworkEffect.builder().flicker(CSCoreLib.randomizer().nextBoolean()).withColor(color).with(CSCoreLib.randomizer().nextInt(3) + 1 == 1 ? Type.BALL: Type.BALL_LARGE).trail(CSCoreLib.randomizer().nextBoolean()).build();
meta.addEffect(effect);
meta.setPower(CSCoreLib.randomizer().nextInt(2) + 1);
fw.setFireworkMeta(meta);
return fw;
}
public static void playEffect(Location l, Color color) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Firework fw = l.getWorld().spawn(l, Firework.class);
Object worldObject = ReflectionUtils.getMethod(l.getWorld().getClass(), "getHandle").invoke(l.getWorld(),(Object[]) null);
FireworkMeta meta = fw.getFireworkMeta();
meta.addEffect(FireworkEffect.builder().with(Type.BURST).flicker(false).trail(false).withColor(color).withFade(Color.WHITE).build());
fw.setFireworkMeta(meta);
ReflectionUtils.getMethod(worldObject.getClass(), "broadcastEntityEffect").invoke(worldObject, new Object[] {ReflectionUtils.getMethod(fw.getClass(), "getHandle").invoke(fw, (Object[]) null), (byte) 17});
fw.remove();
}
/**
* Spawns a randomized firework.
*
* @param location the location where the firework is fired
* @return the Firework
*/
public static Firework spawnRandom(Location location) {
Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta meta = firework.getFireworkMeta();
Random r = new Random();
int rt = r.nextInt(4) + 1;
FireworkEffect.Type type = FireworkEffect.Type.BALL;
if (rt == 1) {
type = FireworkEffect.Type.BALL;
}
if (rt == 2) {
type = FireworkEffect.Type.BALL_LARGE;
}
if (rt == 3) {
type = FireworkEffect.Type.BURST;
}
if (rt == 4) {
type = FireworkEffect.Type.CREEPER;
}
if (rt == 5) {
type = FireworkEffect.Type.STAR;
}
FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(randomColor()).withFade(randomColor()).with(type).trail(r.nextBoolean()).build();
meta.addEffect(effect);
int rp = r.nextInt(2) + 1;
meta.setPower(rp);
firework.setFireworkMeta(meta);
return firework;
}
public void fireworks(final Player player, final int length, final int fireworksPer5Tick) {
final List<FireworkEffect.Type> type = new ArrayList<>(Arrays.asList(FireworkEffect.Type.BALL, FireworkEffect.Type.BALL_LARGE, FireworkEffect.Type.BURST, FireworkEffect.Type.STAR, FireworkEffect.Type.CREEPER));
final List<Color> colors = new ArrayList<>(Arrays.asList(Color.AQUA, Color.BLACK, Color.BLUE, Color.FUCHSIA, Color.GRAY, Color.GREEN, Color.LIME, Color.MAROON, Color.NAVY, Color.OLIVE, Color.ORANGE, Color.PURPLE, Color.RED, Color.SILVER, Color.TEAL, Color.WHITE, Color.YELLOW));
final long currentTime = System.currentTimeMillis();
Random rand = new Random();
if (SkyWarsReloaded.get().isEnabled()) {
new BukkitRunnable() {
public void run() {
if (System.currentTimeMillis() >= currentTime + length * 1000 || SkyWarsReloaded.get().getServer().getPlayer(player.getUniqueId()) == null) {
this.cancel();
}
else {
for (int i = 0; i < fireworksPer5Tick; ++i) {
final Location loc = player.getLocation();
@SuppressWarnings({ "unchecked", "rawtypes" })
final Firework firework = (Firework)player.getLocation().getWorld().spawn(loc, (Class)Firework.class);
final FireworkMeta fMeta = firework.getFireworkMeta();
FireworkEffect fe = FireworkEffect.builder().withColor(colors.get(rand.nextInt(17))).withColor(colors.get(rand.nextInt(17)))
.withColor(colors.get(rand.nextInt(17))).with(type.get(rand.nextInt(5))).trail(rand.nextBoolean())
.flicker(rand.nextBoolean()).build();
fMeta.addEffects(fe);
fMeta.setPower(new Random().nextInt(2) + 2);
firework.setFireworkMeta(fMeta);
}
}
}
}.runTaskTimer(SkyWarsReloaded.get(), 0L, 5L);
}
}
public static void launchFirework(Location l, Color color) {
Firework fw = (Firework) l.getWorld().spawnEntity(l, EntityType.FIREWORK);
FireworkMeta meta = fw.getFireworkMeta();
meta.setDisplayName(ChatColor.GREEN + "Slimefun Research");
FireworkEffect effect = getRandomEffect(ThreadLocalRandom.current(), color);
meta.addEffect(effect);
meta.setPower(ThreadLocalRandom.current().nextInt(2) + 1);
fw.setFireworkMeta(meta);
}
public static Firework createFirework(Location l, Color color) {
Firework fw = (Firework) l.getWorld().spawnEntity(l, EntityType.FIREWORK);
FireworkMeta meta = fw.getFireworkMeta();
meta.setDisplayName(ChatColor.GREEN + "Slimefun Research");
FireworkEffect effect = FireworkEffect.builder().flicker(ThreadLocalRandom.current().nextBoolean()).withColor(color).with(ThreadLocalRandom.current().nextInt(3) + 1 == 1 ? Type.BALL : Type.BALL_LARGE).trail(ThreadLocalRandom.current().nextBoolean()).build();
meta.addEffect(effect);
meta.setPower(ThreadLocalRandom.current().nextInt(2) + 1);
fw.setFireworkMeta(meta);
return fw;
}
@EventHandler
public void onResearchFireworkDamage(EntityDamageByEntityEvent e) {
if (e.getDamager().getType() == EntityType.FIREWORK) {
Firework firework = (Firework) e.getDamager();
FireworkMeta meta = firework.getFireworkMeta();
if (meta.hasDisplayName() && meta.getDisplayName().equals(ChatColor.GREEN + "Slimefun Research")) {
e.setCancelled(true);
}
}
}
@Test
public void testFireworkDamage() {
Player player = server.addPlayer();
Firework firework = Mockito.mock(Firework.class);
FireworkMeta meta = new FireworkMetaMock();
meta.setDisplayName(ChatColor.GREEN + "Slimefun Research");
Mockito.when(firework.getType()).thenReturn(EntityType.FIREWORK);
Mockito.when(firework.getFireworkMeta()).thenReturn(meta);
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(firework, player, DamageCause.ENTITY_EXPLOSION, 6.0);
server.getPluginManager().callEvent(event);
Assertions.assertTrue(event.isCancelled());
}
public static Firework shootFirework(Location loc, Random rand) {
int type = rand.nextInt(5) + 1;
Firework firework = loc.getWorld().spawn(loc, Firework.class);
FireworkMeta meta = firework.getFireworkMeta();
Type ft = null;
switch (type) {
case 1:
ft = Type.BALL;
break;
case 2:
ft = Type.BALL_LARGE;
break;
case 3:
ft = Type.BURST;
break;
case 4:
ft = Type.CREEPER;
break;
case 5:
ft = Type.STAR;
break;
}
FireworkEffect effect = FireworkEffect.builder().flicker(rand.nextBoolean()).withColor(fireworkColor(rand.nextInt(16) + 1)).withFade(fireworkColor(rand.nextInt(16) + 1))
.trail(rand.nextBoolean()).with(ft).trail(rand.nextBoolean()).build();
meta.addEffect(effect);
firework.setFireworkMeta(meta);
return firework;
}
protected void detonate(Location location, Vector v) {
final Firework fw = (Firework) location.getWorld().spawnEntity(location.add(v), EntityType.FIREWORK);
location.subtract(v);
FireworkMeta meta = fw.getFireworkMeta();
meta.setPower(0);
for (int i = 0; i < intensity; i++) {
meta.addEffect(firework);
}
fw.setFireworkMeta(meta);
fw.detonate();
}
public static Firework spawnFirework(Location loc, FireworkEffect effect, int power) {
Firework firework = loc.getWorld().spawn(loc, Firework.class);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.addEffect(effect);
fireworkMeta.setPower(power);
firework.setFireworkMeta(fireworkMeta);
firework.setMetadata(FIREWORK_METADATA, new FixedMetadataValue(Cardinal.getInstance(), true));
return firework;
}
public FireworkExplodeEvent(final Firework what) {
super(what);
}