Skip to content

Commit

Permalink
feat: Add /imagefireworkspro command
Browse files Browse the repository at this point in the history
  • Loading branch information
heyxmirko committed Apr 29, 2024
1 parent 3b8f1d6 commit 17b39c1
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.lukyn76</groupId>
<artifactId>ImageFireworksPro</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>ImageFireworksPro</name>
Expand All @@ -23,8 +23,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.lukyn76.imagefireworkspro;

import me.lukyn76.imagefireworkspro.commands.CommandHandler;
import me.lukyn76.imagefireworkspro.listeners.FireworkExplodeListener;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -15,6 +16,7 @@ public void onEnable() {
saveDefaultConfig();
createFoldersAndFiles();
registerListeners();
registerCommands();

getLogger().info("Plugin has been loaded!");
}
Expand All @@ -41,6 +43,11 @@ private void registerListeners() {
getLogger().info("Listeners registered!");
}

private void registerCommands() {
this.getCommand("imagefireworkspro").setExecutor(new CommandHandler());
this.getCommand("imagefireworkspro").setTabCompleter(new CommandHandler());
}

public static ImageFireworksPro getInstance() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package me.lukyn76.imagefireworkspro.commands;

import me.lukyn76.imagefireworkspro.ImageFireworksPro;
import me.lukyn76.imagefireworkspro.core.ImageFirework;
import me.lukyn76.imagefireworkspro.util.ConfigManager;
import org.bukkit.Bukkit;
import org.bukkit.Material;
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.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CommandHandler implements CommandExecutor, TabCompleter {
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {


switch (args[0].toLowerCase()) {
case "reload":
ConfigManager.reloadConfig();
sender.sendMessage("§aConfig reloaded!");
break;
case "give":
if (args.length < 2) {
sender.sendMessage("§cUsage: /imagefireworkspro give <player> <firework> <amount>");
break;
}

Player player = ImageFireworksPro.getInstance().getServer().getPlayer(args[1]);
if (player == null) {
sender.sendMessage("§cPlayer not found!");
break;
}

String fireworkName = args[2];
ImageFirework imageFirework = ConfigManager.getImageFirework(fireworkName);
if (imageFirework != null) {


// Give firework
int amount = 1;
if (args.length == 4) {
amount = Integer.parseInt(args[3]);
}
ItemStack firework = new ItemStack(Material.FIREWORK_ROCKET, amount);
ItemMeta fireworkMeta = firework.getItemMeta();

fireworkMeta.setDisplayName(imageFirework.getName());
fireworkMeta.setCustomModelData(imageFirework.getCustomModelData());
firework.setItemMeta(fireworkMeta);

player.getInventory().addItem(firework);
sender.sendMessage("§aFirework given!");

} else {
sender.sendMessage("§cFirework not found!");
}
break;

default:
sender.sendMessage("§cUsage: /imagefireworkspro <action>");
break;
}

return false;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String s, String[] args) {
if (args.length == 1) {
return List.of("reload", "give");
} else if (args.length == 2) {
return new ArrayList<>(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList());

} else if (args.length == 3) {
return new ArrayList<>(Objects.requireNonNull(ConfigManager.getAvailableImageFireworks()));
}else if (args.length == 4) {
return List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ImageFirework {

private String name;
private String imageName;
private int customModelData;


public String getName() {
Expand All @@ -35,6 +36,14 @@ public void setImageName(String imageName) {
this.imageName = imageName;
}

public int getCustomModelData() {
return customModelData;
}

public void setCustomModelData(int customModelData) {
this.customModelData = customModelData;
}

public void explode(Location location, double yawRotation) throws IOException {
displayImage(location, yawRotation);
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import me.lukyn76.imagefireworkspro.core.ImageFirework;
import org.bukkit.configuration.ConfigurationSection;

import java.util.List;
import java.util.Set;

public class ConfigManager {

static ImageFireworksPro plugin = ImageFireworksPro.getInstance();
Expand All @@ -20,10 +23,35 @@ public static ImageFirework getImageFirework(int customModelData) {
ImageFirework firework = new ImageFirework();
firework.setName(fireworkSection.getString("name"));
firework.setImageName(fireworkSection.getString("imageName"));
firework.setCustomModelData(fireworkSection.getInt("customModelData"));

return firework;
}
}
return null;
}

public static ImageFirework getImageFirework(String id) {
ConfigurationSection fireworksSection = plugin.getConfig().getConfigurationSection("Fireworks");
if (fireworksSection == null) return null; // No fireworks configuration found
ConfigurationSection fireworkSection = fireworksSection.getConfigurationSection(id);
if (fireworkSection == null) return null;

ImageFirework firework = new ImageFirework();
firework.setName(fireworkSection.getString("name"));
firework.setImageName(fireworkSection.getString("imageName"));
firework.setCustomModelData(fireworkSection.getInt("customModelData"));

return firework;
}

public static Set<String> getAvailableImageFireworks() {
ConfigurationSection fireworksSection = plugin.getConfig().getConfigurationSection("Fireworks");
if (fireworksSection == null) return null; // No fireworks configuration found
return fireworksSection.getKeys(false);
}

public static void reloadConfig() {
plugin.reloadConfig();
}
}
6 changes: 6 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ name: ImageFireworksPro
version: '${project.version}'
main: me.lukyn76.imagefireworkspro.ImageFireworksPro
api-version: '1.20'
commands:
imagefireworkspro:
description: Give custom image fireworks to players.
permission: imagefireworkspro.command
aliases: [ifp]
default: op

0 comments on commit 17b39c1

Please sign in to comment.