Skip to content

Commit

Permalink
feat: Add working logic to display image using particles when a firew…
Browse files Browse the repository at this point in the history
…ork with defined customModelData explodes
  • Loading branch information
heyxmirko committed Apr 29, 2024
1 parent 24ff12e commit c8d45dd
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/java/me/lukyn76/imagefireworkspro/ImageFireworksPro.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package me.lukyn76.imagefireworkspro;

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

import java.io.File;

public final class ImageFireworksPro extends JavaPlugin {

private static ImageFireworksPro instance;

@Override
public void onEnable() {
// Plugin startup logic
instance = this;
createFoldersAndFiles();
registerListeners();

getLogger().info("Plugin has been loaded!");
}

@Override
public void onDisable() {
// Plugin shutdown logic
getLogger().info("Plugin has been unloaded!");
}

private void createFoldersAndFiles() {
saveDefaultConfig();
if (!new File(getDataFolder(), "images").exists()) {
new File(getDataFolder(), "images").mkdir();
}
}


private void registerListeners() {
getServer().getPluginManager().registerEvents(new FireworkExplodeListener(), this);
getLogger().info("Listeners registered!");
}

public static ImageFireworksPro getInstance() {
return instance;
}
}
87 changes: 87 additions & 0 deletions src/main/java/me/lukyn76/imagefireworkspro/core/ImageFirework.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package me.lukyn76.imagefireworkspro.core;

import me.lukyn76.imagefireworkspro.ImageFireworksPro;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.scheduler.BukkitTask;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageFirework {

private String name;
private String imageName;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImageName() {
return imageName;
}

public void setImageName(String imageName) {
this.imageName = imageName;
}

public void explode(Location location, double playerViewRotation) throws IOException {
displayImage(location, playerViewRotation);
}



private void displayImage(Location explodeLocation, double playerViewRotation) throws IOException {
ImageFireworksPro plugin = ImageFireworksPro.getInstance();

File imageFile = new File(plugin.getDataFolder(), "images/"+imageName);
if (!imageFile.exists()) {
plugin.getLogger().warning("Image file not found: " + imageName);
return;
}


BufferedImage image = ImageIO.read(imageFile);
int width = image.getWidth();
int height = image.getHeight();

double yawRadians = Math.toRadians(playerViewRotation);

BukkitTask taskId = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
int color = image.getRGB(x, y);
int alpha = (color >> 24) & 0xff;
if(alpha != 0) {
int red = (color >> 16) & 0xff;
int green = (color >> 8) & 0xff;
int blue = color & 0xff;

double offsetX = x / 10.0 - width / 20.0;
double offsetY = -y / 10.0 + height / 10.0 / 4.0;
double offsetZ = 0.0;

double rotatedX = offsetX * Math.cos(yawRadians) - offsetZ * Math.sin(yawRadians);
double rotatedZ = offsetX * Math.sin(yawRadians) + offsetZ * Math.cos(yawRadians);

Location location = explodeLocation.clone().add(rotatedX, offsetY, rotatedZ);
Particle.DustOptions dust = new Particle.DustOptions(Color.fromRGB(red, green, blue), 1);
explodeLocation.getWorld().spawnParticle(Particle.REDSTONE, location, 0, 0, 0, 0, dust);
}
}
}
}, 0L, 3L);

Bukkit.getScheduler().runTaskLater(plugin, taskId::cancel, 3 * 20L);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.lukyn76.imagefireworkspro.listeners;

import me.lukyn76.imagefireworkspro.ImageFireworksPro;
import me.lukyn76.imagefireworkspro.core.ImageFirework;
import me.lukyn76.imagefireworkspro.util.ConfigManager;
import org.bukkit.Location;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.FireworkExplodeEvent;
import org.bukkit.inventory.meta.FireworkMeta;

import java.io.IOException;

public class FireworkExplodeListener implements Listener {


@EventHandler
public void onFireworkExplode(FireworkExplodeEvent event) throws IOException {
Firework firework = event.getEntity();
FireworkMeta fireworkMeta = firework.getFireworkMeta();
if (!fireworkMeta.hasCustomModelData()) return;
int customModelData = fireworkMeta.getCustomModelData();

ImageFirework imageFirework = ConfigManager.getImageFirework(customModelData);
ImageFireworksPro.getInstance().getServer().getPlayer("lukyn76").sendMessage("Firework: " + imageFirework);
if (imageFirework == null) return;

Location explodeLocation = event.getEntity().getLocation();
Player shooter = (Player) event.getEntity().getShooter();
double playerViewRotation = shooter.getLocation().getYaw();

imageFirework.explode(explodeLocation, playerViewRotation);
}
}
29 changes: 29 additions & 0 deletions src/main/java/me/lukyn76/imagefireworkspro/util/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.lukyn76.imagefireworkspro.util;

import me.lukyn76.imagefireworkspro.ImageFireworksPro;
import me.lukyn76.imagefireworkspro.core.ImageFirework;
import org.bukkit.configuration.ConfigurationSection;

public class ConfigManager {

static ImageFireworksPro plugin = ImageFireworksPro.getInstance();


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

for (String key : fireworksSection.getKeys(false)) {
ConfigurationSection fireworkSection = fireworksSection.getConfigurationSection(key);

if (fireworkSection != null && fireworkSection.getInt("customModelData") == customModelData) {
ImageFirework firework = new ImageFirework();
firework.setName(fireworkSection.getString("name"));
firework.setImageName(fireworkSection.getString("imageName"));

return firework;
}
}
return null;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fireworks:
dog:
name: "Dog"
imageName: "dog.png"
customModelData: 4444

0 comments on commit c8d45dd

Please sign in to comment.