2019-10-17 16:04:43 -06:00
|
|
|
package dev.esophose.playerparticles.manager;
|
2016-05-09 09:07:15 -06:00
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
import dev.esophose.playerparticles.PlayerParticles;
|
2019-12-12 19:32:53 -07:00
|
|
|
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
|
2020-01-26 00:02:27 -07:00
|
|
|
import dev.esophose.playerparticles.particles.ConsolePPlayer;
|
2019-12-09 12:04:06 -07:00
|
|
|
import dev.esophose.playerparticles.particles.FixedParticleEffect;
|
2020-01-05 04:39:27 -07:00
|
|
|
import dev.esophose.playerparticles.particles.PParticle;
|
2019-12-09 12:04:06 -07:00
|
|
|
import dev.esophose.playerparticles.particles.PPlayer;
|
|
|
|
import dev.esophose.playerparticles.particles.ParticleEffect;
|
|
|
|
import dev.esophose.playerparticles.particles.ParticleEffect.NoteColor;
|
|
|
|
import dev.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor;
|
|
|
|
import dev.esophose.playerparticles.particles.ParticlePair;
|
|
|
|
import dev.esophose.playerparticles.styles.DefaultStyles;
|
2017-01-01 19:28:37 -07:00
|
|
|
import java.awt.Color;
|
2020-01-05 04:39:27 -07:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2018-09-23 20:42:52 -06:00
|
|
|
import java.util.List;
|
2020-01-05 04:39:27 -07:00
|
|
|
import java.util.Map;
|
2018-11-07 20:06:08 -07:00
|
|
|
import java.util.Random;
|
2020-01-05 04:39:27 -07:00
|
|
|
import java.util.UUID;
|
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;
|
2019-12-09 12:04:06 -07:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2016-05-09 09:07:15 -06:00
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
public class ParticleManager extends Manager implements Listener, Runnable {
|
2017-04-25 21:19:51 -06:00
|
|
|
|
2018-02-25 21:24:22 -07:00
|
|
|
/**
|
2020-01-05 04:39:27 -07:00
|
|
|
* The map containing all the loaded PPlayer info
|
2018-02-25 21:24:22 -07:00
|
|
|
*/
|
2020-01-05 04:39:27 -07:00
|
|
|
private Map<UUID, PPlayer> particlePlayers = new HashMap<>();
|
2019-12-09 12:04:06 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The task that spawns the particles
|
|
|
|
*/
|
|
|
|
private BukkitTask particleTask = null;
|
2018-02-25 21:24:22 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rainbow particle effect hue and note color used for rainbow colorable effects
|
|
|
|
*/
|
2019-12-09 12:04:06 -07:00
|
|
|
private int hue = 0;
|
|
|
|
private int note = 0;
|
2020-01-07 21:32:35 -07:00
|
|
|
private final Random random = new Random();
|
2019-12-09 12:04:06 -07:00
|
|
|
|
|
|
|
public ParticleManager(PlayerParticles playerParticles) {
|
|
|
|
super(playerParticles);
|
|
|
|
|
|
|
|
Bukkit.getPluginManager().registerEvents(this, this.playerParticles);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reload() {
|
|
|
|
if (this.particleTask != null)
|
|
|
|
this.particleTask.cancel();
|
|
|
|
|
|
|
|
Bukkit.getScheduler().runTaskLater(this.playerParticles, () -> {
|
|
|
|
long ticks = Setting.TICKS_PER_PARTICLE.getLong();
|
|
|
|
this.particleTask = Bukkit.getScheduler().runTaskTimer(this.playerParticles, this, 5, ticks);
|
|
|
|
}, 1);
|
|
|
|
|
|
|
|
this.particlePlayers.clear();
|
|
|
|
DataManager dataManager = this.playerParticles.getManager(DataManager.class);
|
|
|
|
dataManager.loadFixedEffects();
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers())
|
|
|
|
dataManager.getPPlayer(player.getUniqueId(), (pplayer) -> { }); // Loads the PPlayer from the database
|
2020-01-26 00:02:27 -07:00
|
|
|
dataManager.getPPlayer(ConsolePPlayer.getUUID(), (pplayer) -> { }); // Load the console PPlayer
|
2019-12-09 12:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disable() {
|
2019-12-20 18:15:13 -07:00
|
|
|
if (this.particleTask != null)
|
|
|
|
this.particleTask.cancel();
|
2019-12-09 12:04:06 -07:00
|
|
|
}
|
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) {
|
2019-12-09 12:04:06 -07:00
|
|
|
this.playerParticles.getManager(DataManager.class).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) {
|
2019-12-09 12:04:06 -07:00
|
|
|
PPlayer pplayer = this.playerParticles.getManager(DataManager.class).getPPlayer(e.getPlayer().getUniqueId());
|
|
|
|
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty())
|
2020-01-05 04:39:27 -07:00
|
|
|
this.particlePlayers.remove(pplayer.getUniqueId()); // 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
|
|
|
|
*/
|
2020-01-05 04:39:27 -07:00
|
|
|
public Collection<PPlayer> getPPlayers() {
|
|
|
|
return this.particlePlayers.values();
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
2020-01-08 17:15:55 -07:00
|
|
|
/**
|
|
|
|
* Adds a PPlayer to the loaded map
|
|
|
|
*
|
|
|
|
* @param pplayer The PPlayer to add
|
|
|
|
*/
|
|
|
|
public void addPPlayer(PPlayer pplayer) {
|
|
|
|
this.particlePlayers.put(pplayer.getUniqueId(), pplayer);
|
|
|
|
}
|
|
|
|
|
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() {
|
2019-12-09 12:04:06 -07:00
|
|
|
this.playerParticles.getManager(ParticleStyleManager.class).updateTimers();
|
2018-02-25 21:24:22 -07:00
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
this.hue += Setting.RAINBOW_CYCLE_SPEED.getInt();
|
|
|
|
this.hue %= 360;
|
2018-02-25 21:24:22 -07:00
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
if (this.hue % 4 == 0) { // Only increment note by 5 notes per second
|
|
|
|
this.note++;
|
|
|
|
this.note %= 25;
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
PermissionManager permissionManager = this.playerParticles.getManager(PermissionManager.class);
|
|
|
|
|
2020-01-05 04:39:27 -07:00
|
|
|
// Spawn particles for each player
|
|
|
|
for (PPlayer pplayer : this.particlePlayers.values()) {
|
2018-02-25 21:24:22 -07:00
|
|
|
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
|
2019-12-09 12:04:06 -07:00
|
|
|
if (player != null && player.getGameMode() != GameMode.SPECTATOR && permissionManager.isWorldEnabled(player.getWorld().getName()))
|
2018-10-28 04:18:34 -06:00
|
|
|
for (ParticlePair particles : pplayer.getActiveParticles())
|
2019-04-28 00:17:08 -06:00
|
|
|
this.displayParticles(pplayer, particles, player.getLocation().clone().add(0, 1, 0));
|
2018-10-05 21:01:28 -06: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())
|
2019-12-09 12:04:06 -07:00
|
|
|
if (effect.getLocation().getWorld() != null && permissionManager.isWorldEnabled(effect.getLocation().getWorld().getName()))
|
2019-04-28 00:17:08 -06:00
|
|
|
this.displayFixedParticleEffect(effect);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays particles at the given player location with their settings
|
|
|
|
*
|
2018-11-16 03:26:08 -07:00
|
|
|
* @param pplayer The PPlayer to spawn the particles for
|
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-11-16 03:26:08 -07:00
|
|
|
private void displayParticles(PPlayer pplayer, ParticlePair particle, Location location) {
|
2020-01-07 21:32:35 -07:00
|
|
|
if (!this.playerParticles.getManager(ParticleStyleManager.class).isEventHandled(particle.getStyle())) {
|
2020-01-16 15:28:45 -07:00
|
|
|
if (Setting.TOGGLE_ON_COMBAT.getBoolean() && pplayer.isInCombat())
|
|
|
|
return;
|
|
|
|
|
2019-12-09 12:04:06 -07:00
|
|
|
if (Setting.TOGGLE_ON_MOVE.getBoolean() && particle.getStyle().canToggleWithMovement() && pplayer.isMoving()) {
|
2018-11-16 03:26:08 -07:00
|
|
|
for (PParticle pparticle : DefaultStyles.FEET.getParticles(particle, location))
|
2019-04-28 03:58:01 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, false, pplayer.getPlayer());
|
2018-11-16 03:26:08 -07:00
|
|
|
} else {
|
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, location))
|
2019-04-28 03:58:01 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, false, pplayer.getPlayer());
|
2018-11-16 03:26:08 -07:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-07 21:32:35 -07:00
|
|
|
* An alternative method used for event styles
|
2020-01-14 13:37:22 -07:00
|
|
|
*
|
|
|
|
* @param player The player the particles are spawning from
|
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
|
|
|
|
*/
|
2020-01-14 13:37:22 -07:00
|
|
|
public void displayParticles(Player player, ParticlePair particle, List<PParticle> particles) {
|
|
|
|
PermissionManager permissionManager = this.playerParticles.getManager(PermissionManager.class);
|
|
|
|
if (player.getGameMode() == GameMode.SPECTATOR || !permissionManager.isWorldEnabled(player.getWorld().getName()))
|
|
|
|
return;
|
|
|
|
|
2018-10-28 04:18:34 -06:00
|
|
|
for (PParticle pparticle : particles)
|
2019-04-28 03:58:01 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, false, Bukkit.getPlayer(particle.getOwnerUniqueId()));
|
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-12-01 19:34:01 -07:00
|
|
|
for (PParticle pparticle : particle.getStyle().getParticles(particle, fixedEffect.getLocation().clone().add(0, particle.getStyle().getFixedEffectOffset(), 0)))
|
2019-04-28 03:58:01 -06:00
|
|
|
ParticleEffect.display(particle, pparticle, true, null);
|
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'
|
|
|
|
*/
|
2019-12-09 12:04:06 -07:00
|
|
|
public OrdinaryColor getRainbowParticleColor() {
|
|
|
|
Color rgb = Color.getHSBColor(this.hue / 360F, 1.0F, 1.0F);
|
2018-02-25 21:24:22 -07:00
|
|
|
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'
|
|
|
|
*/
|
2019-12-09 12:04:06 -07:00
|
|
|
public NoteColor getRainbowNoteParticleColor() {
|
|
|
|
return new NoteColor(this.note);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
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'
|
|
|
|
*/
|
2019-12-09 12:04:06 -07:00
|
|
|
public OrdinaryColor getRandomParticleColor() {
|
2020-01-07 21:32:35 -07:00
|
|
|
Color rgb = new Color(this.random.nextInt(256), this.random.nextInt(256), this.random.nextInt(256));
|
2018-11-07 20:06:08 -07:00
|
|
|
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'
|
|
|
|
*/
|
2019-12-09 12:04:06 -07:00
|
|
|
public NoteColor getRandomNoteParticleColor() {
|
2020-01-07 21:32:35 -07:00
|
|
|
return new NoteColor(this.random.nextInt(25));
|
2018-11-07 20:06:08 -07:00
|
|
|
}
|
2016-05-09 09:07:15 -06:00
|
|
|
}
|