Add style 'rings', code cleanup

This commit is contained in:
Esophose 2018-10-13 16:38:01 -06:00
parent 36803440c0
commit 200103a5c9
16 changed files with 196 additions and 80 deletions

View file

@ -3,7 +3,6 @@
* + Add new style 'tornado'
* + Add new style 'companion'
* + Add new style 'atom'
* + Add new style 'rings'
*/
package com.esophose.playerparticles;
@ -42,7 +41,7 @@ public class PlayerParticles extends JavaPlugin {
/**
* The database connection manager
*/
public static DatabaseConnector databaseConnector = null;
private static DatabaseConnector databaseConnector = null;
/**
* Registers all the styles available by default
@ -81,6 +80,7 @@ public class PlayerParticles extends JavaPlugin {
LangManager.setup();
configureDatabase(getConfig().getBoolean("database-enable"));
ParticleManager.refreshData();
startParticleTask();
if (shouldCheckUpdates()) {
@ -117,6 +117,15 @@ public class PlayerParticles extends JavaPlugin {
public static Plugin getPlugin() {
return pluginInstance;
}
/**
* Gets the DatabaseConnector that allows querying the database
*
* @return The DatabaseConnector
*/
public static DatabaseConnector getDBConnector() {
return databaseConnector;
}
/**
* Checks the config if the plugin can look for updates
@ -214,7 +223,6 @@ public class PlayerParticles extends JavaPlugin {
final Plugin playerParticles = this;
new BukkitRunnable() {
public void run() {
ParticleManager.refreshPPlayers(); // Add any online players who have particles
PlayerParticlesGui.setup();
long ticks = getConfig().getLong("ticks-per-particle");

View file

@ -10,7 +10,6 @@ import org.bukkit.util.StringUtil;
import com.esophose.playerparticles.manager.DataManager;
import com.esophose.playerparticles.manager.LangManager;
import com.esophose.playerparticles.manager.LangManager.Lang;
import com.esophose.playerparticles.manager.ParticleManager;
import com.esophose.playerparticles.manager.PermissionManager;
import com.esophose.playerparticles.particles.PPlayer;
import com.esophose.playerparticles.particles.ParticleEffect;
@ -31,7 +30,7 @@ public class AddCommandModule implements CommandModule {
return;
}
ParticleEffect effect = ParticleManager.effectFromString(args[0]);
ParticleEffect effect = ParticleEffect.fromName(args[0]);
if (effect == null) {
LangManager.sendMessage(pplayer, Lang.EFFECT_INVALID, args[0]);
return;

View file

@ -133,7 +133,7 @@ public class FixedCommandModule implements CommandModule {
return;
}
ParticleEffect effect = ParticleManager.effectFromString(args[3]);
ParticleEffect effect = ParticleEffect.fromName(args[3]);
if (effect == null) {
LangManager.sendMessage(p, Lang.FIXED_CREATE_EFFECT_INVALID, args[3]);
return;

View file

@ -519,7 +519,7 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
List<String> effectsUserHasPermissionFor = PermissionManager.getEffectsUserHasPermissionFor(player);
for (int i = 0; i < effectsUserHasPermissionFor.size(); i++) {
String s = effectsUserHasPermissionFor.get(i);
ParticleEffect effect = ParticleManager.effectFromString(s);
ParticleEffect effect = ParticleEffect.fromName(s);
inventory.setItem(i, getItemForEffect(effect, effect == getPPlayerEffect(pplayer)));
}
@ -706,7 +706,7 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener {
}
break;
case EFFECT:
setPPlayerEffect(pplayer, ParticleManager.effectFromString(name));
setPPlayerEffect(pplayer, ParticleEffect.fromName(name));
changeState(pplayer, GuiState.DEFAULT);
break;
case STYLE:

View file

@ -81,7 +81,7 @@ public class DataManager {
List<ParticleGroup> groups = new ArrayList<ParticleGroup>();
List<FixedParticleEffect> fixedParticles = new ArrayList<FixedParticleEffect>();
PlayerParticles.databaseConnector.connect((connection) -> {
PlayerParticles.getDBConnector().connect((connection) -> {
// Load particle groups
String groupQuery = "SELECT * FROM pp_group g " + // @formatter:off
"JOIN pp_particle p ON g.uuid = p.group_uuid " +
@ -167,6 +167,24 @@ public class DataManager {
});
});
}
/**
* Loads all PPlayers from the database that own FixedParticleEffects
*/
public static void loadFixedEffects() {
async(() -> {
PlayerParticles.getDBConnector().connect((connection) -> {
String query = "SELECT DISTINCT owner_uuid FROM pp_fixed";
try (PreparedStatement statement = connection.prepareStatement(query)) {
ResultSet result = statement.executeQuery();
while (result.next()) {
UUID playerUUID = UUID.fromString(result.getString("owner_uuid"));
getPPlayer(playerUUID, (pplayer) -> { });
}
}
});
});
}
/**
* Saves a ParticleGroup. If it already exists, update it.
@ -176,7 +194,7 @@ public class DataManager {
*/
public static void saveParticleGroup(UUID playerUUID, ParticleGroup group) {
async(() -> {
PlayerParticles.databaseConnector.connect((connection) -> {
PlayerParticles.getDBConnector().connect((connection) -> {
String groupUUIDQuery = "SELECT uuid FROM pp_group WHERE owner_uuid = ? AND name = ?";
try (PreparedStatement statement = connection.prepareStatement(groupUUIDQuery)) {
statement.setString(1, playerUUID.toString());
@ -247,7 +265,7 @@ public class DataManager {
*/
public static void removeParticleGroup(UUID playerUUID, ParticleGroup group) {
async(() -> {
PlayerParticles.databaseConnector.connect((connection) -> {
PlayerParticles.getDBConnector().connect((connection) -> {
String groupQuery = "SELECT * FROM pp_group WHERE owner_uuid = ? AND name = ?";
String particleDeleteQuery = "DELETE FROM pp_particle WHERE group_uuid = ?";
String groupDeleteQuery = "DELETE FROM pp_group WHERE uuid = ?";
@ -291,7 +309,7 @@ public class DataManager {
*/
public static void saveFixedEffect(FixedParticleEffect fixedEffect) {
async(() -> {
PlayerParticles.databaseConnector.connect((connection) -> {
PlayerParticles.getDBConnector().connect((connection) -> {
String particleUUID = UUID.randomUUID().toString();
String particleQuery = "INSERT INTO pp_particle (uuid, group_uuid, id, effect, style, item_material, block_material, note, r, g, b) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
@ -339,7 +357,7 @@ public class DataManager {
*/
public static void removeFixedEffect(UUID playerUUID, int id) {
async(() -> {
PlayerParticles.databaseConnector.connect((connection) -> {
PlayerParticles.getDBConnector().connect((connection) -> {
String particleUUID = null;
String particleUUIDQuery = "SELECT particle_uuid FROM pp_fixed WHERE owner_uuid = ? AND id = ?";

View file

@ -47,6 +47,7 @@ public class ParticleManager extends BukkitRunnable implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent e) {
DataManager.getPPlayer(e.getPlayer().getUniqueId(), (pplayer) -> {
System.out.println("Loaded");
}); // Loads the PPlayer from the database
}
@ -58,7 +59,7 @@ public class ParticleManager extends BukkitRunnable implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent e) {
PPlayer pplayer = DataManager.getPPlayer(e.getPlayer().getUniqueId());
if (pplayer != null) particlePlayers.remove(pplayer);
if (pplayer != null && pplayer.getFixedEffectIds().isEmpty()) particlePlayers.remove(pplayer);
}
/**
@ -71,43 +72,17 @@ public class ParticleManager extends BukkitRunnable implements Listener {
}
/**
* Load a PPlayer for all players on the server who have active particles or fixed effects
* Loads all FixedParticleEffects from the database
* Loads all online PPlayers from the database
*/
public static void refreshPPlayers() {
public static void refreshData() {
particlePlayers.clear();
for (Player player : Bukkit.getOnlinePlayers())
DataManager.getPPlayer(player.getUniqueId(), (pplayer) -> {
}); // Loads the PPlayer from the database
}
/**
* 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;
}
DataManager.loadFixedEffects();
for (Player player : Bukkit.getOnlinePlayers()) {
DataManager.getPPlayer(player.getUniqueId(), (pplayer) -> { }); // Loads the PPlayer from the database
}
}
/**
* 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()) {
if (effect.getName().equalsIgnoreCase(effectName)) return effect;
}
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

View file

@ -206,7 +206,6 @@ public enum ParticleEffect {
for (Player player : getPlayersInRange(center)) {
player.spawnParticle(internalEnum, center.getX(), center.getY(), center.getZ(), amount, offsetX, offsetY, offsetZ, speed);
}
}
/**
@ -272,7 +271,6 @@ public enum ParticleEffect {
} else if (internalEnum.getDataType() == MaterialData.class) {
extraData = new MaterialData(spawnMaterial); // Deprecated, only used in versions < 1.13
} else {
System.out.println(internalEnum.getDataType());
extraData = null;
}

View file

@ -28,6 +28,7 @@ public class DefaultStyles {
public static final ParticleStyle ORBIT = new ParticleStyleOrbit();
public static final ParticleStyle POINT = new ParticleStylePoint();
public static final ParticleStyle QUADHELIX = new ParticleStyleQuadhelix();
public static final ParticleStyle RINGS = new ParticleStyleRings();
public static final ParticleStyle SPHERE = new ParticleStyleSphere();
public static final ParticleStyle SPIN = new ParticleStyleSpin();
public static final ParticleStyle SPIRAL = new ParticleStyleSpiral();
@ -54,6 +55,7 @@ public class DefaultStyles {
ParticleStyleManager.registerStyle(ORBIT);
ParticleStyleManager.registerStyle(POINT);
ParticleStyleManager.registerStyle(QUADHELIX);
ParticleStyleManager.registerStyle(RINGS);
ParticleStyleManager.registerStyle(SPHERE);
ParticleStyleManager.registerStyle(SPIN);
ParticleStyleManager.registerStyle(SPIRAL);

View file

@ -11,19 +11,30 @@ import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleBeam implements ParticleStyle {
private static double[] cos, sin;
private static final int points = 16;
private int step = 0;
private boolean reversed = false;
static {
cos = new double[points];
sin = new double[points];
int i = 0;
for (double n = 0; n < Math.PI * 2; n += Math.PI * 2 / points) {
cos[i] = Math.cos(n);
sin[i] = Math.sin(n);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
int points = 16;
double radius = 1;
double slice = 2 * Math.PI / points;
List<PParticle> particles = new ArrayList<PParticle>();
for (int i = 0; i < points; i++) {
double angle = slice * i;
double newX = location.getX() + radius * Math.cos(angle);
double newX = location.getX() + radius * cos[i];
double newY = location.getY() + (step / 10D) - 1;
double newZ = location.getZ() + radius * Math.sin(angle);
double newZ = location.getZ() + radius * sin[i];
particles.add(new PParticle(new Location(location.getWorld(), newX, newY, newZ)));
}
return particles;

View file

@ -52,7 +52,7 @@ public class ParticleStyleCube implements ParticleStyle {
public List<PParticle> getParticles(ParticlePair particle, Location location) {
List<PParticle> pparticles = new ArrayList<PParticle>();
if (!skipNextStep) {
if (!skipNextStep) { // TODO: relative position lookup tables
double xRotation = 0, yRotation = 0, zRotation = 0;
xRotation = step * angularVelocityX;
yRotation = step * angularVelocityY;

View file

@ -11,21 +11,33 @@ import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleHalo implements ParticleStyle {
private static double[] cos, sin;
private static final int points = 16;
private int step = 0;
static {
cos = new double[points];
sin = new double[points];
int i = 0;
for (double n = 0; n < Math.PI * 2; n += Math.PI * 2 / points) {
cos[i] = Math.cos(n);
sin[i] = Math.sin(n);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
if (step % 2 == 0) return new ArrayList<PParticle>();
int points = 16;
double radius = .65;
double slice = 2 * Math.PI / points;
List<PParticle> particles = new ArrayList<PParticle>();
for (int i = 0; i < points; i++) {
double angle = slice * i;
double newX = location.getX() + radius * Math.cos(angle);
double newY = location.getY() + 1.5;
double newZ = location.getZ() + radius * Math.sin(angle);
particles.add(new PParticle(new Location(location.getWorld(), newX, newY, newZ)));
double dx = location.getX() + radius * cos[i];
double dy = location.getY() + 1.5;
double dz = location.getZ() + radius * sin[i];
particles.add(new PParticle(location.clone().add(dx, dy, dz)));
}
return particles;
}

View file

@ -11,22 +11,36 @@ import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleOrbit implements ParticleStyle {
private static double[] cos, sin;
private static final int orbs = 3;
private static final int numSteps = 120;
private int step = 0;
static {
cos = new double[120];
sin = new double[120];
int i = 0;
for (double n = 0; n < numSteps; n++) {
cos[i] = -Math.cos(n / numSteps * Math.PI * 2);
sin[i] = -Math.sin(n / numSteps * Math.PI * 2);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
int orbs = 3;
List<PParticle> particles = new ArrayList<PParticle>();
for (int i = 0; i < orbs; i++) {
double dx = -(Math.cos((step / 120D) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i)));
double dz = -(Math.sin((step / 120D) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i)));
particles.add(new PParticle(new Location(location.getWorld(), location.getX() + dx, location.getY(), location.getZ() + dz)));
double dx = cos[(step + (numSteps / orbs * i)) % numSteps];
double dz = sin[(step + (numSteps / orbs * i)) % numSteps];
particles.add(new PParticle(location.clone().add(dx, 0, dz)));
}
return particles;
}
public void updateTimers() {
step++;
if (step > 120) {
if (step > numSteps) {
step = 0;
}
}

View file

@ -11,18 +11,33 @@ import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleQuadhelix implements ParticleStyle {
private static double[] cos, sin;
private static final int orbs = 4;
private static int maxStepX = 80;
private static int maxStepY = 60;
private int stepX = 0;
private int maxStepX = 90;
private int stepY = 0;
private int maxStepY = 60;
private boolean reverse = false;
static {
cos = new double[maxStepX];
sin = new double[maxStepX];
int i = 0;
for (double n = 0; n < maxStepX; n++) {
cos[i] = -Math.cos(n / maxStepX * Math.PI * 2);
sin[i] = -Math.sin(n / maxStepX * Math.PI * 2);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
List<PParticle> particles = new ArrayList<PParticle>();
for (int i = 0; i < 4; i++) {
double dx = -(Math.cos((stepX / (double)maxStepX) * (Math.PI * 2) + ((Math.PI / 2) * i))) * ((60 - Math.abs(stepY)) / (double)maxStepY);
for (int i = 0; i < orbs; i++) {
int step = (stepX + (maxStepX / orbs) * i) % maxStepX;
double dx = cos[step] * ((60 - Math.abs(stepY)) / (double)maxStepY);
double dy = (stepY / (double)maxStepY) * 1.5;
double dz = -(Math.sin((stepX / (double)maxStepX) * (Math.PI * 2) + ((Math.PI / 2) * i))) * ((60 - Math.abs(stepY)) / (double)maxStepY);
double dz = sin[step] * ((60 - Math.abs(stepY)) / (double)maxStepY);
particles.add(new PParticle(location.clone().add(dx, dy, dz)));
}
return particles;

View file

@ -0,0 +1,56 @@
package com.esophose.playerparticles.styles;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import com.esophose.playerparticles.particles.ParticlePair;
import com.esophose.playerparticles.styles.api.PParticle;
import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleRings implements ParticleStyle {
private static double[] cos, sin;
private int index = 0;
static {
cos = new double[32];
sin = new double[32];
int i = 0;
for (double n = 0; n < Math.PI * 2; n += Math.PI / 16) {
cos[i] = Math.sin(n);
sin[i] = Math.cos(n);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
List<PParticle> particles = new ArrayList<PParticle>();
particles.add(new PParticle(location.clone().add(cos[index], sin[index], sin[index])));
particles.add(new PParticle(location.clone().add(cos[wrap(index + 16)], sin[wrap(index + 16)], sin[wrap(index + 16)])));
particles.add(new PParticle(location.clone().add(cos[wrap(index + 16)], sin[index], sin[wrap(index + 16)])));
particles.add(new PParticle(location.clone().add(cos[index], sin[wrap(index + 16)], sin[index])));
return particles;
}
private int wrap(int index) {
return index % cos.length;
}
public void updateTimers() {
index = (index + 1) % cos.length;
}
public String getName() {
return "rings";
}
public boolean canBeFixed() {
return true;
}
}

View file

@ -11,24 +11,32 @@ import com.esophose.playerparticles.styles.api.ParticleStyle;
public class ParticleStyleSpin implements ParticleStyle {
private static double[] cos, sin;
private static final int maxSteps = 30;
private int step = 0;
static {
cos = new double[maxSteps];
sin = new double[maxSteps];
int i = 0;
for (double n = 0; n < Math.PI * 2; n += Math.PI * 2 / maxSteps) {
cos[i] = Math.cos(n);
sin[i] = Math.sin(n);
i++;
}
}
public List<PParticle> getParticles(ParticlePair particle, Location location) {
int points = 15;
double radius = .5;
double slice = 2 * Math.PI / points;
double angle = slice * (step % 15);
double newX = location.getX() + radius * Math.cos(angle);
double newX = location.getX() + radius * cos[step];
double newY = location.getY() + 1.5;
double newZ = location.getZ() + radius * Math.sin(angle);
double newZ = location.getZ() + radius * sin[step];
return Collections.singletonList(new PParticle(new Location(location.getWorld(), newX, newY, newZ)));
}
public void updateTimers() {
step++;
if (step > 30) {
step = 0;
}
step = (step + 1) % maxSteps;
}
public String getName() {

View file

@ -68,7 +68,7 @@ rainbow: '&cr&6a&ei&an&bb&9o&dw'
effect-no-permission: '&cYou do not have permission to use the effect &b{0} &c!'
effect-invalid: '&cThe effect &b{0} &cdoes not exist! Use &b/pp effects &cfor a list of effects you can use.'
effect-list: '&eYou can use the following effects: &b{0}'
effect-list-empoty: '&cYou do not have permission to use any effects!'
effect-list-empty: '&cYou do not have permission to use any effects!'
# Styles
style-no-permission: '&cYou do not have permission to use the style &b{0} &c!'