PlayerParticles/src/com/esophose/playerparticles/manager/ParticleManager.java

188 lines
8.2 KiB
Java
Raw Normal View History

package com.esophose.playerparticles.manager;
2016-05-09 09:07:15 -06:00
import java.awt.Color;
2016-09-10 21:13:13 -07:00
import java.util.ArrayList;
import java.util.List;
2016-05-09 09:07:15 -06:00
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
2016-05-09 09:07:15 -06:00
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
2016-05-09 09:07:15 -06:00
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import com.esophose.playerparticles.particles.FixedParticleEffect;
import com.esophose.playerparticles.particles.PPlayer;
import com.esophose.playerparticles.particles.ParticleEffect;
import com.esophose.playerparticles.particles.ParticleEffect.NoteColor;
import com.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor;
import com.esophose.playerparticles.particles.ParticleEffect.ParticleProperty;
import com.esophose.playerparticles.particles.ParticlePair;
2016-09-10 21:13:13 -07:00
import com.esophose.playerparticles.styles.api.PParticle;
import com.esophose.playerparticles.styles.api.ParticleStyleManager;
2016-05-09 09:07:15 -06:00
public class ParticleManager extends BukkitRunnable implements Listener {
/**
2018-09-25 19:41:00 -06:00
* The list containing all the loaded PPlayer info
*/
public static List<PPlayer> particlePlayers = new ArrayList<PPlayer>();
/**
* Rainbow particle effect hue and note color used for rainbow colorable effects
* These should be moved to a more appropriate place later
*/
private static int hue = 0;
private static int note = 0;
/**
* Adds the player to the array when they join
*
* @param e The event
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent e) {
DataManager.getPPlayer(e.getPlayer().getUniqueId(), (pplayer) -> { }); // Loads the PPlayer from the database
}
/**
* Removes the player from the array when they log off
*
* @param e The event
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent e) {
PPlayer pplayer = DataManager.getPPlayer(e.getPlayer().getUniqueId());
2018-10-17 02:00:11 -06:00
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty()) particlePlayers.remove(pplayer); // Unload the PPlayer if they don't have any fixed effects
}
/**
* Gets the PPlayers that are loaded
*
* @return The loaded PPlayers
*/
public static List<PPlayer> getPPlayers() {
return particlePlayers;
}
/**
2018-10-13 16:38:01 -06:00
* Loads all FixedParticleEffects from the database
* Loads all online PPlayers from the database
*/
2018-10-13 16:38:01 -06:00
public static void refreshData() {
particlePlayers.clear();
2018-10-13 16:38:01 -06:00
DataManager.loadFixedEffects();
for (Player player : Bukkit.getOnlinePlayers()) {
DataManager.getPPlayer(player.getUniqueId(), (pplayer) -> { }); // Loads the PPlayer from the database
}
}
/**
* The main loop to display all the particles
* Does not display particles if the world is disabled or if the player is in spectator mode
*/
public void run() {
ParticleStyleManager.updateTimers();
hue++;
hue %= 360;
2018-09-25 19:41:00 -06:00
if (hue % 5 == 0) { // Only increment note by 4 notes per second
note++;
note %= 24;
}
// Loop over backwards so we can remove pplayers if need be
for (int i = particlePlayers.size() - 1; i >= 0; i--) {
PPlayer pplayer = particlePlayers.get(i);
Player player = pplayer.getPlayer();
// Don't show their particles if they are in spectator mode
2018-09-25 19:41:00 -06:00
// Don't spawn particles if the world doesn't allow it
if (player != null && player.getGameMode() != GameMode.SPECTATOR && !DataManager.isWorldDisabled(player.getWorld().getName())) {
for (ParticlePair particles : pplayer.getActiveParticles()) {
displayParticles(particles, player.getLocation().clone().add(0, 1, 0));
}
}
2018-09-25 19:41:00 -06:00
// Loop for FixedParticleEffects
// Don't spawn particles if the world doesn't allow it
for (FixedParticleEffect effect : pplayer.getFixedParticles())
if (!DataManager.isWorldDisabled(effect.getLocation().getWorld().getName())) displayFixedParticleEffect(effect);
}
}
/**
* Displays particles at the given player location with their settings
*
* @param particle The ParticlePair to use for getting particle settings
* @param location The location to display at
*/
private void displayParticles(ParticlePair particle, Location location) {
if (!ParticleStyleManager.isCustomHandled(particle.getStyle())) {
ParticleEffect effect = particle.getEffect();
for (PParticle pparticle : particle.getStyle().getParticles(particle, location)) {
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else {
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
}
}
}
}
/**
* An alternative method used for custom handled styles
*
* @param particle The ParticlePair to use for getting particle settings
* @param particles The particles to display
*/
public static void displayParticles(ParticlePair particle, List<PParticle> particles) {
ParticleEffect effect = particle.getEffect();
for (PParticle pparticle : particles) {
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else {
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
}
}
}
/**
* Displays particles at the given fixed effect location
*
* @param fixedEffect The fixed effect to display
*/
private void displayFixedParticleEffect(FixedParticleEffect fixedEffect) {
ParticlePair particle = fixedEffect.getParticlePair();
ParticleEffect effect = particle.getEffect();
for (PParticle pparticle : particle.getStyle().getParticles(particle, fixedEffect.getLocation())) {
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
} else {
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
}
}
}
public static OrdinaryColor getRainbowParticleColor() {
Color rgb = Color.getHSBColor(hue / 360F, 1.0F, 1.0F);
return new OrdinaryColor(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
}
public static NoteColor getRainbowNoteParticleColor() {
return new NoteColor(note);
}
2016-05-09 09:07:15 -06:00
}