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;
|
2018-11-07 20:06:08 -07:00
|
|
|
import java.util.Random;
|
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;
|
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
|
|
|
*/
|
2018-09-26 23:31:00 -06:00
|
|
|
public static List<PPlayer> particlePlayers = new ArrayList<PPlayer>();
|
2018-02-25 21:24:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2018-11-07 20:06:08 -07:00
|
|
|
private static final Random RANDOM = new Random();
|
2018-02-25 21:24:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-10-15 22:08:56 -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-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
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
2018-09-27 18:16:50 -06:00
|
|
|
|
2018-09-26 23:31:00 -06:00
|
|
|
/**
|
|
|
|
* Gets the PPlayers that are loaded
|
|
|
|
*
|
|
|
|
* @return The loaded PPlayers
|
|
|
|
*/
|
|
|
|
public static List<PPlayer> getPPlayers() {
|
2018-09-27 18:16:50 -06:00
|
|
|
return particlePlayers;
|
2018-09-26 23:31:00 -06:00
|
|
|
}
|
2018-02-25 21:24:22 -07:00
|
|
|
|
|
|
|
/**
|
2018-10-13 16:38:01 -06:00
|
|
|
* Loads all FixedParticleEffects from the database
|
|
|
|
* Loads all online PPlayers from the database
|
2018-02-25 21:24:22 -07:00
|
|
|
*/
|
2018-10-13 16:38:01 -06:00
|
|
|
public static void refreshData() {
|
2018-02-25 21:24:22 -07:00
|
|
|
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
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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++;
|
2018-11-07 03:21:46 -07:00
|
|
|
note %= 25;
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-10-30 03:17:23 -06:00
|
|
|
if (player != null && player.getGameMode() != GameMode.SPECTATOR && !PermissionManager.isWorldDisabled(player.getWorld().getName()))
|
2018-10-28 04:18:34 -06:00
|
|
|
for (ParticlePair particles : pplayer.getActiveParticles())
|
2018-10-05 21:01:28 -06:00
|
|
|
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())
|
2018-10-30 03:17:23 -06:00
|
|
|
if (!PermissionManager.isWorldDisabled(effect.getLocation().getWorld().getName()))
|
2018-10-28 04:18:34 -06:00
|
|
|
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) {
|
2018-10-28 04:18:34 -06:00
|
|
|
if (!ParticleStyleManager.isCustomHandled(particle.getStyle()))
|
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, location))
|
2018-11-01 17:55:41 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, false);
|
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) {
|
2018-10-28 04:18:34 -06:00
|
|
|
for (PParticle pparticle : particles)
|
2018-11-01 17:55:41 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, false);
|
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-27 18:16:50 -06:00
|
|
|
ParticlePair particle = fixedEffect.getParticlePair();
|
2018-10-28 04:18:34 -06:00
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, fixedEffect.getLocation()))
|
2018-11-01 17:55:41 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, true);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 20:06:08 -07:00
|
|
|
/**
|
|
|
|
* Gets the rainbow OrdinaryColor for particle spawning with data 'rainbow'
|
|
|
|
*
|
|
|
|
* @return The rainbow OrdinaryColor for particle spawning with data 'rainbow'
|
|
|
|
*/
|
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());
|
|
|
|
}
|
|
|
|
|
2018-11-07 20:06:08 -07:00
|
|
|
/**
|
|
|
|
* Gets the rainbow NoteColor for particle spawning with data 'rainbow'
|
|
|
|
*
|
|
|
|
* @return The rainbow NoteColor for particle spawning with data 'rainbow'
|
|
|
|
*/
|
2018-02-25 21:24:22 -07:00
|
|
|
public static NoteColor getRainbowNoteParticleColor() {
|
|
|
|
return new NoteColor(note);
|
|
|
|
}
|
2018-11-07 20:06:08 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a randomized OrdinaryColor for particle spawning with data 'random'
|
|
|
|
*
|
|
|
|
* @return A randomized OrdinaryColor for particle spawning with data 'random'
|
|
|
|
*/
|
|
|
|
public static OrdinaryColor getRandomParticleColor() {
|
|
|
|
Color rgb = new Color(RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256));
|
|
|
|
return new OrdinaryColor(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a randomized NoteColor for particle spawning with data 'random'
|
|
|
|
*
|
|
|
|
* @return A randomized NoteColor for particle spawning with data 'random'
|
|
|
|
*/
|
|
|
|
public static NoteColor getRandomNoteParticleColor() {
|
|
|
|
return new NoteColor(RANDOM.nextInt(25));
|
|
|
|
}
|
2016-05-09 09:07:15 -06:00
|
|
|
|
|
|
|
}
|