PlayerParticles/src/com/esophose/playerparticles/ParticleCreator.java

153 lines
5.7 KiB
Java
Raw Normal View History

2016-05-09 09:07:15 -06:00
/**
* Copyright Esophose 2016
* 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.
*/
package com.esophose.playerparticles;
2016-09-10 21:13:13 -07:00
import java.util.ArrayList;
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.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
2016-09-10 21:13:13 -07:00
import com.esophose.playerparticles.library.ParticleEffect;
import com.esophose.playerparticles.library.ParticleEffect.ParticleProperty;
import com.esophose.playerparticles.manager.ConfigManager;
import com.esophose.playerparticles.manager.PermissionManager;
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 ParticleCreator extends BukkitRunnable implements Listener {
2016-09-10 21:13:13 -07:00
/**
2016-09-10 21:13:13 -07:00
* The list containing all the player effect info
*/
2016-09-10 21:13:13 -07:00
public static ArrayList<PPlayer> particlePlayers = new ArrayList<PPlayer>();
/**
2016-09-10 21:13:13 -07:00
* Adds the player to the array when they join
*
* @param e The event
*/
2016-05-09 09:07:15 -06:00
@EventHandler
2016-09-10 21:13:13 -07:00
public void onPlayerJoin(PlayerJoinEvent e) {
ConfigManager.getInstance().getPPlayer(e.getPlayer().getUniqueId());
2016-05-09 09:07:15 -06:00
}
2016-09-10 21:13:13 -07:00
/**
2016-09-10 21:13:13 -07:00
* Removes the player from the array when they log off
*
* @param e The event
*/
2016-05-09 09:07:15 -06:00
@EventHandler
2016-09-10 21:13:13 -07:00
public void onPlayerQuit(PlayerQuitEvent e) {
particlePlayers.remove(ConfigManager.getInstance().getPPlayer(e.getPlayer().getUniqueId()));
2016-05-09 09:07:15 -06:00
}
2016-09-10 21:13:13 -07:00
/**
2016-09-10 21:13:13 -07:00
* Clears the list then adds everybody on the server
* Used for when the server reloads and we can't rely on players rejoining
*/
2016-09-10 21:13:13 -07:00
public static void refreshPPlayers() {
particlePlayers.clear();
for (Player player : Bukkit.getOnlinePlayers()) {
ConfigManager.getInstance().getPPlayer(player.getUniqueId());
2016-05-09 09:07:15 -06:00
}
}
2016-09-10 21:13:13 -07:00
public static void updateIfContains(PPlayer pplayer) {
for (PPlayer pp : particlePlayers) {
if (pp.getUniqueId() == pplayer.getUniqueId()) {
particlePlayers.remove(pp);
particlePlayers.add(pplayer);
break;
}
2016-05-09 09:07:15 -06:00
}
}
2016-09-10 21:13:13 -07:00
/**
* Gets a particle type from a string, used for getting ParticleType's from the saved data
*
* @param particle The name of the particle to check for
* @return The ParticleType with the given name, will be null if name was not found
*/
2016-09-10 21:13:13 -07:00
public static ParticleEffect particleFromString(String particle) {
for (ParticleEffect effect : ParticleEffect.getSupportedEffects()) {
if (effect.getName().toLowerCase().replace("_", "").equals(particle.toLowerCase())) return effect;
2016-05-09 09:07:15 -06: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
*/
2016-05-09 09:07:15 -06:00
public void run() {
2016-09-10 21:13:13 -07:00
ParticleStyleManager.updateTimers();
for (Player player : Bukkit.getOnlinePlayers()) {
if (ConfigManager.getInstance().isWorldDisabled(player.getWorld().getName())) continue;
if (player.getGameMode() == GameMode.SPECTATOR) continue;
2016-09-10 21:13:13 -07:00
PPlayer pplayer = ConfigManager.getInstance().getPPlayer(player.getUniqueId());
if (PermissionManager.hasEffectPermission(player, pplayer.getParticleEffect())) {
2016-05-09 09:07:15 -06:00
Location loc = player.getLocation();
loc.setY(loc.getY() + 1);
2016-09-10 21:13:13 -07:00
displayParticles(pplayer, loc);
2016-05-09 09:07:15 -06:00
}
}
}
2016-09-10 21:13:13 -07:00
/**
2016-09-10 21:13:13 -07:00
* Displays particles at the given player location with their settings
*
2016-09-10 21:13:13 -07:00
* @param pplayer The PPlayer to use for getting particle settings
* @param location The location to display at
*/
2016-09-10 21:13:13 -07:00
private void displayParticles(PPlayer pplayer, Location location) {
if (!ParticleStyleManager.isCustomHandled(pplayer.getParticleStyle())) {
ParticleEffect effect = pplayer.getParticleEffect();
if (effect == ParticleEffect.NONE) return;
for (PParticle particle : pplayer.getParticleStyle().getParticles(pplayer, location)) {
if (effect.hasProperty(ParticleProperty.REQUIRES_DATA)) {
effect.display(pplayer.getParticleSpawnData(), particle.getXOff(), particle.getYOff(), particle.getZOff(), particle.getSpeed(), 1, particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
effect.display(pplayer.getParticleSpawnColor(), particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
} else {
effect.display(particle.getXOff(), particle.getYOff(), particle.getZOff(), particle.getSpeed(), 1, particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
}
}
2016-05-09 09:07:15 -06:00
}
}
2016-09-10 21:13:13 -07:00
/**
2016-09-10 21:13:13 -07:00
* An alternative method typically used for custom handled styles
*
2016-09-10 21:13:13 -07:00
* @param pplayer The PPlayer to use for getting particle settings
* @param location The location to display at
*/
2016-09-10 21:13:13 -07:00
public static void displayParticles(PPlayer pplayer, PParticle[] particles) {
ParticleEffect effect = pplayer.getParticleEffect();
if (effect == ParticleEffect.NONE) return;
for (PParticle particle : particles) {
if (effect.hasProperty(ParticleProperty.REQUIRES_DATA)) {
effect.display(pplayer.getParticleSpawnData(), particle.getXOff(), particle.getYOff(), particle.getZOff(), particle.getSpeed(), 1, particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
} else if (effect.hasProperty(ParticleProperty.COLORABLE)) {
effect.display(pplayer.getParticleSpawnColor(), particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
} else {
effect.display(particle.getXOff(), particle.getYOff(), particle.getZOff(), particle.getSpeed(), 1, particle.getLocation(effect.hasProperty(ParticleProperty.COLORABLE)), 256);
}
2016-05-09 09:07:15 -06:00
}
}
}