From bd08bf15de8cbef1f7d2bd125200a8de3f7a96c8 Mon Sep 17 00:00:00 2001 From: aleabodo Date: Sun, 13 Aug 2023 00:40:16 +0200 Subject: [PATCH] commands, show initial setup --- .../org/aleabodo/fireworksmc/Commands.java | 5 - .../fireworksmc/Commands/Fireworkshow.java | 101 ++++++++++++++ .../org/aleabodo/fireworksmc/Direction.java | 8 ++ .../aleabodo/fireworksmc/Fireworks_Mc.java | 64 ++++++++- .../org/aleabodo/fireworksmc/Message.java | 125 +++++++++++++++++ .../java/org/aleabodo/fireworksmc/Show.java | 127 ++++++++++++++++++ src/main/resources/plugin.yml | 17 +++ 7 files changed, 440 insertions(+), 7 deletions(-) delete mode 100644 src/main/java/org/aleabodo/fireworksmc/Commands.java create mode 100644 src/main/java/org/aleabodo/fireworksmc/Commands/Fireworkshow.java create mode 100644 src/main/java/org/aleabodo/fireworksmc/Direction.java create mode 100644 src/main/java/org/aleabodo/fireworksmc/Message.java create mode 100644 src/main/java/org/aleabodo/fireworksmc/Show.java diff --git a/src/main/java/org/aleabodo/fireworksmc/Commands.java b/src/main/java/org/aleabodo/fireworksmc/Commands.java deleted file mode 100644 index 2e20cfb..0000000 --- a/src/main/java/org/aleabodo/fireworksmc/Commands.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.aleabodo.fireworksmc; - -public class Commands { - -} diff --git a/src/main/java/org/aleabodo/fireworksmc/Commands/Fireworkshow.java b/src/main/java/org/aleabodo/fireworksmc/Commands/Fireworkshow.java new file mode 100644 index 0000000..397a71e --- /dev/null +++ b/src/main/java/org/aleabodo/fireworksmc/Commands/Fireworkshow.java @@ -0,0 +1,101 @@ +package org.aleabodo.fireworksmc.Commands; + +import org.aleabodo.fireworksmc.Direction; +import org.aleabodo.fireworksmc.Fireworks_Mc; +import org.aleabodo.fireworksmc.Message; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +public class Fireworkshow implements CommandExecutor, TabCompleter { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("This command is reserved for players only!"); + return false; + } + + Player player = ((Player) sender).getPlayer(); + + switch (args[0]) { + case "setlocation": + //Set location + Fireworks_Mc.show.setLocation(player.getLocation()); + Message.sendInfo(player, "Location successfully set to your position."); + break; + case "setdirection": + //Set direction + switch (args[1]) { + case "north": + Fireworks_Mc.show.setDirection(Direction.NORTH); + break; + case "east": + Fireworks_Mc.show.setDirection(Direction.EAST); + break; + case "south": + Fireworks_Mc.show.setDirection(Direction.SOUTH); + break; + case "west": + Fireworks_Mc.show.setDirection(Direction.WEST); + break; + default: + Message.sendWarning(player, "Please enter either 'north', 'east', 'south', or 'west'!"); + } + break; + case "setduration": + //Set duration + try { + int duration = Integer.parseInt(args[1]); + Fireworks_Mc.show.setDuration(duration); + Message.sendInfo(player, "Duration successfully set to " + duration); + return true; + } catch (NumberFormatException e) { + Message.sendWarning(player, "Please enter an integer for the duration (in seconds)!"); + } + case "setseed": + //Set seed + StringBuilder result = new StringBuilder(args[1]); + for (int i = 1; i onTabComplete(@NotNull CommandSender sender, @NotNull Command command, + @NotNull String label, @NotNull String[] args) { + List completions = new ArrayList<>(); + + if (args.length == 1) { + completions.add("setlocation"); + completions.add("setdirection"); + completions.add("setduration"); + completions.add("setseed"); + completions.add("start"); + } else if (args.length == 2 && args[0].equals("setdirection")) { + completions.add("north"); + completions.add("east"); + completions.add("south"); + completions.add("west"); + } + + return completions; + } +} diff --git a/src/main/java/org/aleabodo/fireworksmc/Direction.java b/src/main/java/org/aleabodo/fireworksmc/Direction.java new file mode 100644 index 0000000..3a806ae --- /dev/null +++ b/src/main/java/org/aleabodo/fireworksmc/Direction.java @@ -0,0 +1,8 @@ +package org.aleabodo.fireworksmc; + +public enum Direction { + NORTH, + EAST, + SOUTH, + WEST +} diff --git a/src/main/java/org/aleabodo/fireworksmc/Fireworks_Mc.java b/src/main/java/org/aleabodo/fireworksmc/Fireworks_Mc.java index e9fc804..2008f37 100644 --- a/src/main/java/org/aleabodo/fireworksmc/Fireworks_Mc.java +++ b/src/main/java/org/aleabodo/fireworksmc/Fireworks_Mc.java @@ -1,21 +1,81 @@ package org.aleabodo.fireworksmc; +import com.github.fierioziy.particlenativeapi.api.ParticleNativeAPI; +import com.github.fierioziy.particlenativeapi.plugin.ParticleNativePlugin; +import org.aleabodo.fireworksmc.Commands.Fireworkshow; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import java.io.Console; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Objects; +import java.util.Random; +import java.util.UUID; import java.util.logging.Level; public final class Fireworks_Mc extends JavaPlugin { + private static ParticleNativeAPI particlesAPI; + private static Fireworks_Mc plugin; - + public static Show show; @Override public void onEnable() { - getLogger().log(Level.INFO, "awdawawdawawdawawdawawdawawdawawdawawdawawdaw"); + Fireworks_Mc.plugin = this; + + getLogger().info("Starting Fireworks-MC plugin ..."); + + /* + * Initialize Particles API + * Requires the ParticlesNativeAPI plugin + */ + if (!ParticleNativePlugin.isValid()) { + getLogger().log(Level.SEVERE, "Error occurred while loading Particles API dependency."); + this.setEnabled(false); + return; + } + particlesAPI = ParticleNativePlugin.getAPI(); + + //Initialize show with default values + Fireworks_Mc.show = new Show("Survival-Pi", + new Location(Fireworks_Mc.getWorld("world"), 0, 80, 0), + Direction.NORTH, + 60); + + /* + * Initialize command executors + */ + Objects.requireNonNull(getCommand("fireworkshow")).setExecutor(new Fireworkshow()); } @Override public void onDisable() { } + + /* + * ################### GETTER ################### + */ + + public static World getWorld(UUID worldUUID) { + return Fireworks_Mc.plugin.getServer().getWorld(worldUUID); + } + + public static World getWorld(String worldName) { + return Fireworks_Mc.plugin.getServer().getWorld(worldName); + } + + public static Fireworks_Mc getPlugin() { + return plugin; + } + + /* + * ################### SETTER ################### + */ + + } diff --git a/src/main/java/org/aleabodo/fireworksmc/Message.java b/src/main/java/org/aleabodo/fireworksmc/Message.java new file mode 100644 index 0000000..0535dbe --- /dev/null +++ b/src/main/java/org/aleabodo/fireworksmc/Message.java @@ -0,0 +1,125 @@ +package org.aleabodo.fireworksmc; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +public class Message { + + public static void sendInfo(@NotNull Player player, Component ... content) { + Component textComponent = Component.text().color(NamedTextColor.GRAY).build() + .append(Component.text("[Fireworks] ") + .color(TextColor.color(0,213,255)) + .decoration(TextDecoration.BOLD, true)); + for (Component comp : content) { + textComponent = textComponent.append(comp); + } + + player.sendMessage(textComponent); + } + + public static void sendInfo(@NotNull Player player, String content) { + sendInfo(player, Component.text(content)); + } + + public static void sendInfo(Collection players, Component ... content) { + for (Player p : players) { + sendInfo(p, content); + } + } + + public static void sendInfo(Collection players, String content) { + for (Player p : players) { + sendInfo(p, content); + } + } + + /* + * ############################################################################# + * Warning + */ + + public static void sendWarning(@NotNull Player player, Component ... content) { + Component textComponent = Component.text().color(NamedTextColor.GRAY).build() + .append(Component.text("[Fireworks] ") + .color(TextColor.color(255, 3, 4)) + .decoration(TextDecoration.BOLD, true)); + + for (Component comp : content) { + textComponent = textComponent.append(comp); + } + + player.sendMessage(textComponent); + } + + public static void sendWarning(@NotNull Player player, String content) { + sendWarning(player, Component.text(content)); + } + + public static void sendWarning(Collection players, Component ... content) { + for (Player p : players) { + sendWarning(p, content); + } + } + + public static void sendWarning(Collection players, String content) { + for (Player p : players) { + sendWarning(p, content); + } + } + + /* + * ############################################################################# + * Broadcast + */ + + public static void sendBroadcast(Component ... content) { + Component textComponent = Component.text().color(NamedTextColor.GRAY).build() + .append(Component.text("[Fireworks] ") + .color(TextColor.color(255, 153, 0)) + .decoration(TextDecoration.BOLD, true)); + + for (Component comp : content) { + textComponent = textComponent.append(comp); + } + + for (Player p : Bukkit.getOnlinePlayers()) { + p.sendMessage(textComponent); + } + } + + public static void sendBroadcast(String content) { + sendBroadcast(Component.text(content)); + } + + /* + * ############################################################################# + * Debug + */ + + public static void sendDebug(Component ... content) { + Component textComponent = Component.text().color(NamedTextColor.GRAY).build() + .append(Component.text("[Fireworks] ") + .color(TextColor.color(255, 0, 245)) + .decoration(TextDecoration.BOLD, true)); + + for (Component comp : content) { + textComponent = textComponent.append(comp); + } + + for (Player p : Bukkit.getOnlinePlayers()) { + if (p.hasPermission("beacon.debug")) + p.sendMessage(textComponent); + } + } + + public static void sendDebug(String content) { + sendDebug(Component.text(content)); + } +} \ No newline at end of file diff --git a/src/main/java/org/aleabodo/fireworksmc/Show.java b/src/main/java/org/aleabodo/fireworksmc/Show.java new file mode 100644 index 0000000..2469bcd --- /dev/null +++ b/src/main/java/org/aleabodo/fireworksmc/Show.java @@ -0,0 +1,127 @@ +package org.aleabodo.fireworksmc; + +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.FireworkEffect; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Firework; +import org.bukkit.inventory.meta.FireworkMeta; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Random; + +public class Show { + private String seed; + private Location location; + private Direction direction; + private int duration; //in seconds + + public Show(String seed, Location location, Direction direction, int duration) { + this.seed = seed; + this.location = location; + this.direction = direction; + this.duration = duration; + } + + /** + * Executes the firework show + */ + public void start() { + spawnRocket(this.location); + } + + public void spawnRocket(Location location) { + Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK); + FireworkMeta fireworkMeta = firework.getFireworkMeta(); + + // Create a firework effect + FireworkEffect effect = FireworkEffect.builder() + .flicker(true) + .trail(true) + .withColor(Color.RED) + .withFade(Color.YELLOW) + .with(FireworkEffect.Type.BALL_LARGE) + .build(); + + fireworkMeta.addEffect(effect); + fireworkMeta.setPower(1); // Number of times the firework will explode + firework.setFireworkMeta(fireworkMeta); + + // Schedule the firework to be removed after a delay + new BukkitRunnable() { + @Override + public void run() { + firework.detonate(); + } + }.runTaskLater(Fireworks_Mc.getPlugin(), 20); // 20 ticks = 1 second + } + + /** + * + * @param seedString Random seed as string + * @return Random number between 0 and based on the seed + */ + private double generateRandomValueFromSeed(String seedString) { + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + byte[] hashBytes = md.digest(seedString.getBytes()); + + // Convert the first 8 bytes of the hash to a long value + long hashValue = 0; + for (int i = 0; i < 8; i++) { + hashValue = (hashValue << 8) | (hashBytes[i]); + } + + //Convert long (hastValue) into random number between 0 and 1. + return new Random(hashValue).nextDouble(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Hashing algorithm not available. (Have to check or Intellij complains.", e); + } + } + + /* + * ################### GETTER ################### + */ + + public String getSeed() { + return seed; + } + + public Location getLocation() { + return location; + } + + public Direction getDirection() { + return direction; + } + + public int getDuration() { + return duration; + } + + + + /* + * ################### SETTER ################### + */ + + public void setSeed(String seed) { + this.seed = seed; + } + + public void setLocation(Location location) { + this.location = location; + } + + public void setDirection(Direction direction) { + this.direction = direction; + } + + public void setDuration(int duration) { + this.duration = duration; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 84c8206..050cd68 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,3 +2,20 @@ name: Fireworks-Mc version: '${project.version}' main: org.aleabodo.fireworksmc.Fireworks_Mc api-version: '1.20' +commands: + fireworkshow: + description: All fireworks show related commands +depend: [ParticleNativeAPI] +permissions: + fireworksmc.setseed: + description: Set the random seed for generating the fireworks show. + default: op + fireworksmc.setlocation: + description: Set the location of the fireworks show. + default: op + fireworksmc.setdirection: + description: Set the direction/rotation of the firework (north,east). + default: op + fireworksmc.debug: + description: Get debug messages + default: op \ No newline at end of file