WordFireworkShow.java v1.0
支持文字烟花、跨年倒计时、烟花秀等,输入文字即可绽放烟花图案,支持多种预设秀。
命令列表
- wordfirework在空中绽放输入文字的烟花图案
- fireworkshow触发预设烟花秀(如新年秀、倒计时)
package com.example.fireworkshow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import java.util.*;
/**
* @pluginName WordFireworkShow
* @author Scriptirc
* @version 1.0
* @description 支持文字烟花、跨年倒计时、烟花秀等,输入文字即可绽放烟花图案,支持多种预设秀。
* [command]wordfirework|在空中绽放输入文字的烟花图案[/command]
* [command]fireworkshow|触发预设烟花秀(如新年秀、倒计时)[/command]
* [Permission]fireworkshow.use|允许使用烟花秀相关命令[/Permission]
* [Permission]wordfirework.use|允许使用文字烟花命令[/Permission]
*/
public class WordFireworkShow extends JavaPlugin {
@Override
public void onEnable() {
getLogger().info("WordFireworkShow 插件已启用!");
}
@Override
public void onDisable() {
getLogger().info("WordFireworkShow 插件已关闭。");
}
// 处理命令
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("§c此命令只能由玩家执行。");
return true;
}
Player player = (Player) sender;
if (command.getName().equalsIgnoreCase("wordfirework")) {
if (!player.hasPermission("wordfirework.use")) {
player.sendMessage("§c你没有权限使用此命令。");
return true;
}
if (args.length == 0) {
player.sendMessage("§e用法: /wordfirework <文字>");
return true;
}
String text = String.join(" ", args);
Location loc = player.getLocation().add(player.getLocation().getDirection().multiply(5)).add(0, 5, 0);
WordFireworkUtil.spawnTextFirework(text, loc, player.getWorld());
player.sendMessage("§a已在空中绽放文字烟花: " + text);
return true;
}
if (command.getName().equalsIgnoreCase("fireworkshow")) {
if (!player.hasPermission("fireworkshow.use")) {
player.sendMessage("§c你没有权限使用此命令。");
return true;
}
if (args.length == 0) {
player.sendMessage("§e用法: /fireworkshow <newyear|countdown>");
return true;
}
String type = args[0].toLowerCase();
if (type.equals("newyear")) {
player.sendMessage("§b新年烟花秀即将开始!");
WordFireworkUtil.startNewYearShow(player.getLocation(), player.getWorld());
return true;
} else if (type.equals("countdown")) {
player.sendMessage("§b跨年倒计时烟花秀即将开始!");
WordFireworkUtil.startCountdownShow(player.getLocation(), player.getWorld());
return true;
} else {
player.sendMessage("§c未知类型: " + type);
return true;
}
}
return false;
}
}
// 工具类:文字转点阵、烟花渲染、预设秀
class WordFireworkUtil {
// 简易5x7像素字体,仅支持大写字母和数字
private static final Map<Character, boolean[][]> FONT = new HashMap<>();
static {
FONT.put('A', new boolean[][]{
{false,true,true,true,false},
{true,false,false,false,true},
{true,false,false,false,true},
{true,true,true,true,true},
{true,false,false,false,true},
{true,false,false,false,true},
{true,false,false,false,true},
});
FONT.put('B', new boolean[][]{
{true,true,true,true,false},
{true,false,false,false,true},
{true,true,true,true,false},
{true,false,false,false,true},
{true,false,false,false,true},
{true,true,true,true,false},
{false,false,false,false,false},
});
// ... 你可以继续补充更多字母和数字 ...
FONT.put('0', new boolean[][]{
{false,true,true,true,false},
{true,false,false,false,true},
{true,false,true,false,true},
{true,false,false,false,true},
{true,false,false,false,true},
{false,true,true,true,false},
{false,false,false,false,false},
});
FONT.put('1', new boolean[][]{
{false,false,true,false,false},
{false,true,true,false,false},
{true,false,true,false,false},
{false,false,true,false,false},
{false,false,true,false,false},
{true,true,true,true,false},
{false,false,false,false,false},
});
// ... 可根据需要补充 ...
}
// 生成文字烟花
public static void spawnTextFirework(String text, Location base, World world) {
text = text.toUpperCase();
int charWidth = 6; // 每个字符宽度(含间隔)
int charHeight = 7;
int startX = -(text.length() * charWidth) / 2;
int yOffset = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
boolean[][] matrix = FONT.getOrDefault(c, FONT.get('A'));
for (int y = 0; y < charHeight; y++) {
for (int x = 0; x < 5; x++) {
if (matrix[y][x]) {
Location fwLoc = base.clone().add(startX + i * charWidth + x, yOffset - y, 0);
spawnFirework(fwLoc, world, randomColor());
}
}
}
}
}
// 生成单个烟花
private static void spawnFirework(Location loc, World world, Color color) {
Firework fw = world.spawn(loc, Firework.class);
FireworkMeta meta = fw.getFireworkMeta();
meta.addEffect(FireworkEffect.builder()
.with(FireworkEffect.Type.BALL_LARGE)
.withColor(color)
.withFade(Color.WHITE)
.flicker(true)
.trail(true)
.build());
meta.setPower(1);
fw.setFireworkMeta(meta);
// 立刻引爆
Bukkit.getScheduler().runTaskLater(Bukkit.getPluginManager().getPlugin("WordFireworkShow"), fw::detonate, 1L);
}
// 随机颜色
private static Color randomColor() {
Random r = new Random();
return Color.fromRGB(r.nextInt(256), r.nextInt(256), r.nextInt(256));
}
// 新年烟花秀
public static void startNewYearShow(Location base, World world) {
// 连续5轮烟花,每轮间隔1秒
new BukkitRunnable() {
int round = 0;
@Override
public void run() {
if (round >= 5) {
cancel();
return;
}
for (int i = 0; i < 10; i++) {
Location loc = base.clone().add((Math.random() - 0.5) * 10, 5 + Math.random() * 3, (Math.random() - 0.5) * 10);
spawnFirework(loc, world, randomColor());
}
round++;
}
}.runTaskTimer(Bukkit.getPluginManager().getPlugin("WordFireworkShow"), 0L, 20L);
}
// 跨年倒计时烟花秀
public static void startCountdownShow(Location base, World world) {
// 5秒倒计时,每秒一个数字烟花,最后NEW YEAR烟花
new BukkitRunnable() {
int count = 5;
@Override
public void run() {
if (count > 0) {
spawnTextFirework(String.valueOf(count), base.clone().add(0, 10, 0), world);
count--;
} else if (count == 0) {
spawnTextFirework("NEW YEAR", base.clone().add(0, 10, 0), world);
startNewYearShow(base, world);
cancel();
}
}
}.runTaskTimer(Bukkit.getPluginManager().getPlugin("WordFireworkShow"), 0L, 20L);
}
}