2016-05-09 09:07:15 -06:00
|
|
|
/**
|
2018-02-25 20:55:34 -07:00
|
|
|
* Copyright Esophose 2018
|
2016-05-09 09:07:15 -06:00
|
|
|
* While using any of the code provided by this plugin
|
|
|
|
* you must not claim it as your own. This plugin may
|
|
|
|
* be modified and installed on a server, but may not
|
|
|
|
* be distributed to any person by any means.
|
|
|
|
*/
|
|
|
|
|
2017-02-15 22:05:50 -07:00
|
|
|
package com.esophose.playerparticles.manager;
|
2016-05-09 09:07:15 -06:00
|
|
|
|
2017-01-01 19:28:37 -07:00
|
|
|
import java.awt.Color;
|
2016-09-10 21:13:13 -07:00
|
|
|
import java.util.ArrayList;
|
2018-09-23 20:42:52 -06:00
|
|
|
import java.util.List;
|
2016-05-09 09:07:15 -06:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
2018-04-05 17:48:33 -06:00
|
|
|
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;
|
2018-04-05 17:48:33 -06:00
|
|
|
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;
|
|
|
|
|
2018-02-26 01:36:14 -07:00
|
|
|
import com.esophose.playerparticles.particles.FixedParticleEffect;
|
2018-09-23 20:42:52 -06:00
|
|
|
import com.esophose.playerparticles.particles.PPlayer;
|
2018-02-26 01:36:14 -07:00
|
|
|
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;
|
2018-09-23 20:42:52 -06:00
|
|
|
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
|
|
|
|
2017-02-15 22:05:50 -07:00
|
|
|
public class ParticleManager extends BukkitRunnable implements Listener {
|
2017-04-25 21:19:51 -06:00
|
|
|
|
2018-02-25 21:24:22 -07:00
|
|
|
/**
|
2018-09-25 19:41:00 -06:00
|
|
|
* The list containing all the loaded PPlayer info
|
2018-02-25 21:24:22 -07:00
|
|
|
*/
|
|
|
|
public static ArrayList<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
|
|
|
|
*/
|
2018-04-05 17:48:33 -06:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
2018-02-25 21:24:22 -07:00
|
|
|
public void onPlayerJoin(PlayerJoinEvent e) {
|
2018-09-25 19:41:00 -06:00
|
|
|
DataManager.getPPlayer(e.getPlayer().getUniqueId(), (pplayer) -> {}); // Loads the PPlayer from the database
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the player from the array when they log off
|
|
|
|
*
|
|
|
|
* @param e The event
|
|
|
|
*/
|
2018-04-05 17:48:33 -06:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
2018-02-25 21:24:22 -07:00
|
|
|
public void onPlayerQuit(PlayerQuitEvent e) {
|
2018-09-23 20:42:52 -06:00
|
|
|
PPlayer pplayer = DataManager.getPPlayer(e.getPlayer().getUniqueId());
|
2018-03-02 02:47:24 -07:00
|
|
|
if (pplayer != null)
|
|
|
|
particlePlayers.remove(pplayer);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-25 19:41:00 -06:00
|
|
|
* Load a PPlayer for all players on the server who have active particles or fixed effects
|
2018-02-25 21:24:22 -07:00
|
|
|
*/
|
|
|
|
public static void refreshPPlayers() {
|
|
|
|
particlePlayers.clear();
|
2018-03-02 02:47:24 -07:00
|
|
|
for (Player player : Bukkit.getOnlinePlayers())
|
2018-09-25 19:41:00 -06:00
|
|
|
DataManager.getPPlayer(player.getUniqueId(), (pplayer) -> {}); // Loads the PPlayer from the database
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides an existing PPlayer with the same UUID
|
|
|
|
*
|
|
|
|
* @param pplayer The PPlayer to override
|
|
|
|
*/
|
|
|
|
public static void updateIfContains(PPlayer pplayer) {
|
|
|
|
for (PPlayer pp : particlePlayers) {
|
|
|
|
if (pp.getUniqueId() == pplayer.getUniqueId()) {
|
|
|
|
particlePlayers.remove(pp);
|
|
|
|
particlePlayers.add(pplayer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an effect type from a string, used for getting ParticleEffects from the saved data
|
|
|
|
*
|
|
|
|
* @param effectName The name of the particle to check for
|
|
|
|
* @return The ParticleEffect with the given name, will be null if name was not found
|
|
|
|
*/
|
|
|
|
public static ParticleEffect effectFromString(String effectName) {
|
|
|
|
for (ParticleEffect effect : ParticleEffect.getSupportedEffects()) {
|
2018-09-07 02:56:59 -06:00
|
|
|
if (effect.getName().equalsIgnoreCase(effectName)) return effect;
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-02-25 21:24:22 -07:00
|
|
|
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();
|
|
|
|
|
2018-04-05 17:48:33 -06:00
|
|
|
// 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())) {
|
|
|
|
Location loc = player.getLocation();
|
|
|
|
loc.setY(loc.getY() + 1);
|
|
|
|
|
|
|
|
for (ParticlePair particles : pplayer.getActiveParticles())
|
|
|
|
displayParticles(particles, loc);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
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);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays particles at the given player location with their settings
|
|
|
|
*
|
2018-09-23 20:42:52 -06:00
|
|
|
* @param particle The ParticlePair to use for getting particle settings
|
2018-02-25 21:24:22 -07:00
|
|
|
* @param location The location to display at
|
|
|
|
*/
|
2018-09-23 20:42:52 -06:00
|
|
|
private void displayParticles(ParticlePair particle, Location location) {
|
|
|
|
if (!ParticleStyleManager.isCustomHandled(particle.getStyle())) {
|
|
|
|
ParticleEffect effect = particle.getEffect();
|
2018-02-25 21:24:22 -07:00
|
|
|
if (effect == ParticleEffect.NONE) return;
|
2018-09-23 20:42:52 -06:00
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, location)) {
|
2018-09-07 02:56:59 -06:00
|
|
|
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alternative method used for custom handled styles
|
|
|
|
*
|
2018-09-23 20:42:52 -06:00
|
|
|
* @param particle The ParticlePair to use for getting particle settings
|
2018-02-25 21:24:22 -07:00
|
|
|
* @param particles The particles to display
|
|
|
|
*/
|
2018-09-23 20:42:52 -06:00
|
|
|
public static void displayParticles(ParticlePair particle, List<PParticle> particles) {
|
|
|
|
ParticleEffect effect = particle.getEffect();
|
2018-02-25 21:24:22 -07:00
|
|
|
if (effect == ParticleEffect.NONE) return;
|
2018-09-23 20:42:52 -06:00
|
|
|
for (PParticle pparticle : particles) {
|
2018-09-07 02:56:59 -06:00
|
|
|
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays particles at the given fixed effect location
|
|
|
|
*
|
|
|
|
* @param fixedEffect The fixed effect to display
|
|
|
|
*/
|
|
|
|
private void displayFixedParticleEffect(FixedParticleEffect fixedEffect) {
|
2018-09-23 20:42:52 -06:00
|
|
|
ParticlePair particle = fixedEffect.getParticlePair();
|
|
|
|
ParticleEffect effect = particle.getEffect();
|
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, fixedEffect.getLocation())) {
|
2018-09-07 02:56:59 -06:00
|
|
|
if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(particle.getSpawnColor(), pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
} else {
|
2018-09-23 20:42:52 -06:00
|
|
|
effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)));
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|