下面列出了org.bukkit.inventory.meta.FireworkEffectMeta#org.bukkit.FireworkEffect 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
if (this.iterations < ITERATION_COUNT) {
// Build this list fresh every time, because MatchPlayers can unload, but Competitors can't.
final List<MatchPlayer> players =
winners.stream().flatMap(c -> c.getPlayers().stream()).collect(Collectors.toList());
Collections.shuffle(players);
for (int i = 0; i < players.size() && i < ROCKET_COUNT; i++) {
MatchPlayer player = players.get(i);
Type type = FIREWORK_TYPES.get(match.getRandom().nextInt(FIREWORK_TYPES.size()));
FireworkEffect effect =
FireworkEffect.builder()
.with(type)
.withFlicker()
.withColor(this.colors)
.withFade(Color.BLACK)
.build();
spawnFirework(player.getBukkit().getLocation(), effect, ROCKET_POWER);
}
}
this.iterations++;
}
public void spawnFireworkDisplay(
Location center, Color color, int count, double radius, int power) {
FireworkEffect effect =
FireworkEffect.builder()
.with(Type.BURST)
.withFlicker()
.withColor(color)
.withFade(Color.BLACK)
.build();
for (int i = 0; i < count; i++) {
double angle = 2 * Math.PI / count * i;
double dx = radius * Math.cos(angle);
double dz = radius * Math.sin(angle);
Location baseLocation = center.clone().add(dx, 0, dz);
Block block = baseLocation.getBlock();
if (block == null || !block.getType().isOccluding()) {
spawnFirework(getOpenSpaceAbove(baseLocation), effect, power);
}
}
}
CraftMetaFirework(NBTTagCompound tag) {
super(tag);
if (!tag.hasKey(FIREWORKS.NBT)) {
return;
}
NBTTagCompound fireworks = tag.getCompoundTag(FIREWORKS.NBT);
power = 0xff & fireworks.getByte(FLIGHT.NBT);
if (!fireworks.hasKey(EXPLOSIONS.NBT)) {
return;
}
NBTTagList fireworkEffects = fireworks.getTagList(EXPLOSIONS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
List<FireworkEffect> effects = this.effects = new ArrayList<FireworkEffect>(fireworkEffects.tagCount());
for (int i = 0; i < fireworkEffects.tagCount(); i++) {
effects.add(getEffect((NBTTagCompound) fireworkEffects.get(i)));
}
}
static NBTTagCompound getExplosion(FireworkEffect effect) {
NBTTagCompound explosion = new NBTTagCompound();
if (effect.hasFlicker()) {
explosion.setBoolean(EXPLOSION_FLICKER.NBT, true);
}
if (effect.hasTrail()) {
explosion.setBoolean(EXPLOSION_TRAIL.NBT, true);
}
addColors(explosion, EXPLOSION_COLORS, effect.getColors());
addColors(explosion, EXPLOSION_FADE, effect.getFadeColors());
explosion.setByte(EXPLOSION_TYPE.NBT, (byte) getNBT(effect.getType()));
return explosion;
}
void safelyAddEffects(Iterable<?> collection) {
if (collection == null || (collection instanceof Collection && ((Collection<?>) collection).isEmpty())) {
return;
}
List<FireworkEffect> effects = this.effects;
if (effects == null) {
effects = this.effects = new ArrayList<FireworkEffect>();
}
for (Object obj : collection) {
if (obj instanceof FireworkEffect) {
effects.add((FireworkEffect) obj);
} else {
throw new IllegalArgumentException(obj + " in " + collection + " is not a FireworkEffect");
}
}
}
@Override
void applyToItem(NBTTagCompound itemTag) {
super.applyToItem(itemTag);
if (isFireworkEmpty()) {
return;
}
NBTTagCompound fireworks = itemTag.getCompoundTag(FIREWORKS.NBT);
itemTag.setTag(FIREWORKS.NBT, fireworks);
if (hasEffects()) {
NBTTagList effects = new NBTTagList();
for (FireworkEffect effect : this.effects) {
effects.appendTag(getExplosion(effect));
}
if (effects.tagCount() > 0) {
fireworks.setTag(EXPLOSIONS.NBT, effects);
}
}
if (hasPower()) {
fireworks.setByte(FLIGHT.NBT, (byte) power);
}
}
public static FireworkMeta getFireworkMeta(JsonObject json) {
FireworkMeta dummy = (FireworkMeta) new ItemStack(Material.FIREWORK).getItemMeta();
if (json.has("power"))
dummy.setPower(json.get("power").getAsInt());
else
dummy.setPower(1);
JsonArray effects = json.getAsJsonArray("effects");
for (int i = 0; i < effects.size() - 1; i++) {
JsonObject effectDto = effects.get(i).getAsJsonObject();
FireworkEffect effect = getFireworkEffect(effectDto);
if (effect != null)
dummy.addEffect(effect);
}
return dummy;
}
public void addEffects(FireworkEffect... effects) {
Validate.notNull(effects, "Effects cannot be null");
if (effects.length == 0) {
return;
}
List<FireworkEffect> list = this.effects;
if (list == null) {
list = this.effects = new ArrayList<FireworkEffect>();
}
for (FireworkEffect effect : effects) {
Validate.notNull(effect, "Effect cannot be null");
list.add(effect);
}
}
@Override
public void onRun() {
if (firework == null) {
Builder b = FireworkEffect.builder().with(fireworkType);
b.withColor(color).withColor(color2).withColor(color3);
b.withFade(fadeColor);
b.trail(true);
firework = b.build();
}
Location location = getLocation();
for (int i = 0; i < explosions; i++) {
Vector v = RandomUtils.getRandomVector().multiply(radius);
detonate(location, v);
if (soundInterval != 0 && step % soundInterval == 0) {
location.getWorld().playSound(location, sound, soundVolume, soundPitch);
}
}
step++;
}
public void spawnFireworkDisplay(Location center, Color color, int count, double radius, int power) {
FireworkEffect effect = FireworkEffect.builder().with(Type.BURST)
.withFlicker()
.withColor(color)
.withFade(Color.BLACK)
.build();
for(int i = 0; i < count; i++) {
double angle = 2 * Math.PI / count * i;
double dx = radius * Math.cos(angle);
double dz = radius * Math.sin(angle);
Location baseLocation = center.clone().add(dx, 0, dz);
Block block = baseLocation.getBlock();
if(block == null || !block.getType().isOccluding()) {
FireworkUtil.spawnFirework(FireworkUtil.getOpenSpaceAbove(baseLocation), effect, power);
}
}
}
@Override
public void run() {
// Build this list fresh every time, because MatchPlayers can unload, but Competitors can't.
final List<MatchPlayer> players = winners.stream()
.flatMap(c -> c.getPlayers().stream())
.collect(Collectors.toList());
Collections.shuffle(players);
for(int i = 0; i < players.size() && i < PostMatch.number(); i++) {
MatchPlayer player = players.get(i);
Type type = AVAILABLE_TYPES.get(match.getRandom().nextInt(AVAILABLE_TYPES.size()));
FireworkEffect effect = FireworkEffect.builder().with(type).withFlicker().withColor(this.colors).withFade(Color.BLACK).build();
FireworkUtil.spawnFirework(player.getBukkit().getLocation(), effect, PostMatch.power());
}
this.iterations++;
if(this.iterations >= PostMatch.iterations()) {
cancelTask();
}
}
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);
}
@SuppressWarnings("null")
@Override
protected Color[] get(Event e, Object[] source) {
if (source instanceof FireworkEffect[]) {
List<Color> colors = new ArrayList<>();
for (FireworkEffect effect : (FireworkEffect[]) source) {
effect.getColors().stream()
.map(SkriptColor::fromBukkitColor)
.forEach(colors::add);
}
if (colors.size() == 0)
return null;
return colors.toArray(new Color[0]);
}
return get(source, o -> {
Colorable colorable = getColorable(o);
if (colorable == null)
return null;
return SkriptColor.fromDyeColor(colorable.getColor());
});
}
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
Class<?> returnType = getExpr().getReturnType();
if (FireworkEffect.class.isAssignableFrom(returnType))
return CollectionUtils.array(Color[].class);
if (mode != ChangeMode.SET && !getExpr().isSingle())
return null;
if (Entity.class.isAssignableFrom(returnType))
return CollectionUtils.array(Color.class);
else if (Block.class.isAssignableFrom(returnType))
return CollectionUtils.array(Color.class);
if (ItemType.class.isAssignableFrom(returnType))
return CollectionUtils.array(Color.class);
return null;
}
public ColoredFireworkStar(Color color, String name, String... lore) {
super(Material.FIREWORK_STAR, im -> {
if (name != null) {
im.setDisplayName(ChatColors.color(name));
}
((FireworkEffectMeta) im).setEffect(FireworkEffect.builder().with(Type.BURST).withColor(color).build());
if (lore.length > 0) {
List<String> lines = new ArrayList<>();
for (String line : lore) {
lines.add(ChatColors.color(line));
}
im.setLore(lines);
}
});
}
CraftMetaFirework(net.minecraft.nbt.NBTTagCompound tag) {
super(tag);
if (!tag.hasKey(FIREWORKS.NBT)) {
return;
}
net.minecraft.nbt.NBTTagCompound fireworks = tag.getCompoundTag(FIREWORKS.NBT);
power = 0xff & fireworks.getByte(FLIGHT.NBT);
if (!fireworks.hasKey(EXPLOSIONS.NBT)) {
return;
}
net.minecraft.nbt.NBTTagList fireworkEffects = fireworks.getTagList(EXPLOSIONS.NBT, 10);
List<FireworkEffect> effects = this.effects = new ArrayList<FireworkEffect>(fireworkEffects.tagCount());
for (int i = 0; i < fireworkEffects.tagCount(); i++) {
effects.add(getEffect((net.minecraft.nbt.NBTTagCompound) fireworkEffects.getCompoundTagAt(i)));
}
}
static net.minecraft.nbt.NBTTagCompound getExplosion(FireworkEffect effect) {
net.minecraft.nbt.NBTTagCompound explosion = new net.minecraft.nbt.NBTTagCompound();
if (effect.hasFlicker()) {
explosion.setBoolean(EXPLOSION_FLICKER.NBT, true);
}
if (effect.hasTrail()) {
explosion.setBoolean(EXPLOSION_TRAIL.NBT, true);
}
addColors(explosion, EXPLOSION_COLORS, effect.getColors());
addColors(explosion, EXPLOSION_FADE, effect.getFadeColors());
explosion.setByte(EXPLOSION_TYPE.NBT, (byte) getNBT(effect.getType()));
return explosion;
}
void safelyAddEffects(Iterable<?> collection) {
if (collection == null || (collection instanceof Collection && ((Collection<?>) collection).isEmpty())) {
return;
}
List<FireworkEffect> effects = this.effects;
if (effects == null) {
effects = this.effects = new ArrayList<FireworkEffect>();
}
for (Object obj : collection) {
if (obj instanceof FireworkEffect) {
effects.add((FireworkEffect) obj);
} else {
throw new IllegalArgumentException(obj + " in " + collection + " is not a FireworkEffect");
}
}
}
@Override
void applyToItem(net.minecraft.nbt.NBTTagCompound itemTag) {
super.applyToItem(itemTag);
if (isFireworkEmpty()) {
return;
}
net.minecraft.nbt.NBTTagCompound fireworks = itemTag.getCompoundTag(FIREWORKS.NBT);
itemTag.setTag(FIREWORKS.NBT, fireworks);
if (hasEffects()) {
net.minecraft.nbt.NBTTagList effects = new net.minecraft.nbt.NBTTagList();
for (FireworkEffect effect : this.effects) {
effects.appendTag(getExplosion(effect));
}
if (effects.tagCount() > 0) {
fireworks.setTag(EXPLOSIONS.NBT, effects);
}
}
if (hasPower()) {
fireworks.setByte(FLIGHT.NBT, (byte) power);
}
}
public void addEffects(FireworkEffect...effects) {
Validate.notNull(effects, "Effects cannot be null");
if (effects.length == 0) {
return;
}
List<FireworkEffect> list = this.effects;
if (list == null) {
list = this.effects = new ArrayList<FireworkEffect>();
}
for (FireworkEffect effect : effects) {
Validate.notNull(effect, "Effect cannot be null");
list.add(effect);
}
}
public void explode() {
World world = Bukkit.getWorld(coord.getWorldname());
ItemManager.setTypeId(coord.getLocation().getBlock(), CivData.AIR);
world.playSound(coord.getLocation(), Sound.ANVIL_BREAK, 1.0f, -1.0f);
world.playSound(coord.getLocation(), Sound.EXPLODE, 1.0f, 1.0f);
FireworkEffect effect = FireworkEffect.builder().with(Type.BURST).withColor(Color.YELLOW).withColor(Color.RED).withTrail().withFlicker().build();
FireworkEffectPlayer fePlayer = new FireworkEffectPlayer();
for (int i = 0; i < 3; i++) {
try {
fePlayer.playFirework(world, coord.getLocation(), effect);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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 void addEffect(FireworkEffect effect) {
Validate.notNull(effect, "Effect cannot be null");
if (this.effects == null) {
this.effects = new ArrayList<FireworkEffect>();
}
this.effects.add(effect);
}
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 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 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);
}
}
@SuppressWarnings({"null", "unchecked"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
flicker = parseResult.mark == 2 || parseResult.mark > 3;
trail = parseResult.mark >= 3;
hasFade = matchedPattern == 1;
type = (Expression<FireworkEffect.Type>) exprs[0];
color = (Expression<Color>) exprs[1];
if (hasFade)
fade = (Expression<Color>) exprs[2];
return true;
}
@SuppressWarnings("null")
@Override
public boolean check(Event e) {
if (colors == null)
return true;
List<org.bukkit.Color> colours = Arrays.stream(colors.getArray(e))
.map(color -> color.asBukkitColor())
.collect(Collectors.toList());
FireworkMeta meta = ((FireworkExplodeEvent)e).getEntity().getFireworkMeta();
for (FireworkEffect effect : meta.getEffects()) {
if (colours.containsAll(effect.getColors()))
return true;
}
return false;
}
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
effects = (Expression<FireworkEffect>) exprs[0];
locations = (Expression<Location>) exprs[1];
lifetime = (Expression<Number>) exprs[2];
return true;
}