Add ParticleStyleRegistrationEvent, deprecate registration API methods

This commit is contained in:
Esophose 2020-04-13 20:35:03 -06:00
parent f2a3263b41
commit 60a171e540
5 changed files with 184 additions and 53 deletions

View file

@ -24,7 +24,6 @@ import dev.esophose.playerparticles.manager.PluginUpdateManager;
import dev.esophose.playerparticles.particles.listener.PPlayerCombatListener;
import dev.esophose.playerparticles.particles.listener.PPlayerMovementListener;
import dev.esophose.playerparticles.util.Metrics;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import org.bukkit.Bukkit;
@ -100,16 +99,6 @@ public class PlayerParticles extends JavaPlugin {
return null;
}
}
/**
* Returns the file which contains this plugin
* Exposes the JavaPlugin.getFile() method
*
* @return File containing this plugin
*/
public File getJarFile() {
return this.getFile();
}
/**
* Reloads the plugin

View file

@ -1,6 +1,7 @@
package dev.esophose.playerparticles.api;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.event.ParticleStyleRegistrationEvent;
import dev.esophose.playerparticles.manager.DataManager;
import dev.esophose.playerparticles.manager.GuiManager;
import dev.esophose.playerparticles.manager.ParticleManager;
@ -1039,7 +1040,9 @@ public final class PlayerParticlesAPI {
* Registers a particle style with the plugin
*
* @param particleStyle The particle style to register
* @deprecated Use {@link ParticleStyleRegistrationEvent} instead
*/
@Deprecated
public void registerParticleStyle(@NotNull ParticleStyle particleStyle) {
Objects.requireNonNull(particleStyle);
@ -1050,7 +1053,9 @@ public final class PlayerParticlesAPI {
* Registers an event-based particle style with the plugin
*
* @param particleStyle The particle style to register
* @deprecated Use {@link ParticleStyleRegistrationEvent} instead
*/
@Deprecated
public void registerEventParticleStyle(@NotNull ParticleStyle particleStyle) {
Objects.requireNonNull(particleStyle);

View file

@ -0,0 +1,91 @@
package dev.esophose.playerparticles.event;
import dev.esophose.playerparticles.styles.ParticleStyle;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* An event that gets called during the PlayerParticles style registration
*/
public class ParticleStyleRegistrationEvent extends Event {
private static final HandlerList HANDLER_LIST = new HandlerList();
private Map<String, ParticleStyle> registeredStyles;
private Map<String, ParticleStyle> registeredEventStyles;
public ParticleStyleRegistrationEvent() {
this.registeredStyles = new HashMap<>();
this.registeredEventStyles = new HashMap<>();
}
/**
* @return An unmodifiable map of registered styles keyed by the style internal name
*/
public Map<String, ParticleStyle> getRegisteredStyles() {
return Collections.unmodifiableMap(this.registeredStyles);
}
/**
* @return An unmodifiable map of registered event styles keyed by the style internal name
*/
public Map<String, ParticleStyle> getRegisteredEventStyles() {
return Collections.unmodifiableMap(this.registeredEventStyles);
}
/**
* Registers a ParticleStyle, overwriting any existing styles with the same name
*
* @param style The ParticleStyle to register
* @return true if registered without replacing an existing style, false if an existing style was replaced
*/
public boolean registerStyle(ParticleStyle style) {
if (this.registeredEventStyles.containsKey(style.getInternalName())) {
this.registeredEventStyles.remove(style.getInternalName());
this.registeredStyles.put(style.getInternalName(), style);
return false;
}
return this.registeredStyles.put(style.getInternalName(), style) == null;
}
/**
* Registers an event-based ParticleStyle, overwriting any existing styles with the same name.
* Styles registered with this method bypass the normal update loop, and must instead be spawned manually.
*
* @param style The ParticleStyle to register
* @return true if registered without replacing an existing style, false if an existing style was replaced
*/
public boolean registerEventStyle(ParticleStyle style) {
if (this.registeredStyles.containsKey(style.getInternalName())) {
this.registeredStyles.remove(style.getInternalName());
this.registeredEventStyles.put(style.getInternalName(), style);
return false;
}
return this.registeredEventStyles.put(style.getInternalName(), style) == null;
}
/**
* Unregisters a ParticleStyle
*
* @param internalName The internal name of the ParticleStyle to unregister
* @return true if a style was unregistered, false otherwise
*/
public boolean unregisterStyle(String internalName) {
return this.registeredStyles.remove(internalName) != null || this.registeredEventStyles.remove(internalName) != null;
}
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}

View file

@ -1,11 +1,14 @@
package dev.esophose.playerparticles.manager;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.event.ParticleStyleRegistrationEvent;
import dev.esophose.playerparticles.styles.DefaultStyles;
import dev.esophose.playerparticles.styles.ParticleStyle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
public class ParticleStyleManager extends Manager {
@ -20,12 +23,47 @@ public class ParticleStyleManager extends Manager {
this.styles = new ArrayList<>();
this.eventStyles = new ArrayList<>();
DefaultStyles.registerStyles(this);
DefaultStyles.initStyles();
}
@Override
public void reload() {
// Styles List is never reset so you don't need to re-register styles each time the plugin reloads
this.styles.clear();
this.eventStyles.clear();
// Call registration event
// We use this event internally, so no other action needs to be done for us to register the default styles
ParticleStyleRegistrationEvent event = new ParticleStyleRegistrationEvent();
Bukkit.getPluginManager().callEvent(event);
Collection<ParticleStyle> eventStyles = event.getRegisteredEventStyles().values();
for (ParticleStyle style : event.getRegisteredStyles().values()) {
try {
if (style == null) {
throw new IllegalArgumentException("Tried to register a null style");
}
if (style.getInternalName() == null || style.getInternalName().trim().equals("")) {
throw new IllegalArgumentException("Tried to register a style with a null or empty name: '" + style.getInternalName() + "'");
}
for (ParticleStyle testAgainst : this.styles) {
if (testAgainst.equals(style)) {
throw new IllegalArgumentException("Tried to register the same style twice: '" + style.getInternalName() + "'");
} else if (testAgainst.getInternalName().equalsIgnoreCase(style.getInternalName())) {
throw new IllegalArgumentException("Tried to register two styles with the same internal name spelling: '" + style.getInternalName() + "'");
}
}
this.styles.add(style);
if (eventStyles.contains(style))
this.eventStyles.add(style);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
}
@Override
@ -38,6 +76,7 @@ public class ParticleStyleManager extends Manager {
*
* @param style The style to add
*/
@Deprecated
public void registerStyle(ParticleStyle style) {
if (style == null) {
throw new IllegalArgumentException("Tried to register a null style");
@ -63,6 +102,7 @@ public class ParticleStyleManager extends Manager {
*
* @param style The style to register
*/
@Deprecated
public void registerEventStyle(ParticleStyle style) {
this.registerStyle(style);
this.eventStyles.add(style);

View file

@ -1,12 +1,15 @@
package dev.esophose.playerparticles.styles;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.event.ParticleStyleRegistrationEvent;
import dev.esophose.playerparticles.manager.ParticleStyleManager;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
public class DefaultStyles {
public class DefaultStyles implements Listener {
/**
* All the styles that are available by default from this plugin
@ -47,46 +50,11 @@ public class DefaultStyles {
public static final ParticleStyle WINGS = new ParticleStyleWings();
/**
* Registers all the default styles to the ParticleStyleManager
* Registered in alphabetical order
*
* @param particleStyleManager The ParticleStyleManager instance
* Initializes all the default styles
*/
public static void registerStyles(ParticleStyleManager particleStyleManager) {
particleStyleManager.registerStyle(ARROWS);
particleStyleManager.registerStyle(BATMAN);
particleStyleManager.registerStyle(BEAM);
particleStyleManager.registerEventStyle(BLOCKBREAK);
particleStyleManager.registerEventStyle(BLOCKPLACE);
particleStyleManager.registerStyle(CELEBRATION);
particleStyleManager.registerStyle(CHAINS);
particleStyleManager.registerStyle(COMPANION);
particleStyleManager.registerStyle(CUBE);
particleStyleManager.registerStyle(FEET);
particleStyleManager.registerStyle(HALO);
particleStyleManager.registerEventStyle(HURT);
particleStyleManager.registerStyle(INVOCATION);
particleStyleManager.registerEventStyle(MOVE);
particleStyleManager.registerStyle(NORMAL);
particleStyleManager.registerStyle(ORBIT);
particleStyleManager.registerStyle(OVERHEAD);
particleStyleManager.registerStyle(POINT);
particleStyleManager.registerStyle(POPPER);
particleStyleManager.registerStyle(PULSE);
particleStyleManager.registerStyle(QUADHELIX);
particleStyleManager.registerStyle(RINGS);
particleStyleManager.registerStyle(SPHERE);
particleStyleManager.registerStyle(SPIN);
particleStyleManager.registerStyle(SPIRAL);
particleStyleManager.registerEventStyle(SWORDS);
particleStyleManager.registerEventStyle(TELEPORT);
particleStyleManager.registerStyle(THICK);
particleStyleManager.registerEventStyle(TRAIL);
particleStyleManager.registerStyle(TWINS);
particleStyleManager.registerStyle(VORTEX);
particleStyleManager.registerStyle(WHIRL);
particleStyleManager.registerStyle(WHIRLWIND);
particleStyleManager.registerStyle(WINGS);
public static void initStyles() {
// Register event
Bukkit.getPluginManager().registerEvents(new DefaultStyles(), PlayerParticles.getInstance());
// Register their events
PluginManager pluginManager = Bukkit.getPluginManager();
@ -101,6 +69,44 @@ public class DefaultStyles {
pluginManager.registerEvents((Listener) TRAIL, playerParticles);
}
@EventHandler(priority = EventPriority.LOWEST)
public void onParticleStyleRegistration(ParticleStyleRegistrationEvent event) {
event.registerStyle(ARROWS);
event.registerStyle(BATMAN);
event.registerStyle(BEAM);
event.registerEventStyle(BLOCKBREAK);
event.registerEventStyle(BLOCKPLACE);
event.registerStyle(CELEBRATION);
event.registerStyle(CHAINS);
event.registerStyle(COMPANION);
event.registerStyle(CUBE);
event.registerStyle(FEET);
event.registerStyle(HALO);
event.registerEventStyle(HURT);
event.registerStyle(INVOCATION);
event.registerEventStyle(MOVE);
event.registerStyle(NORMAL);
event.registerStyle(ORBIT);
event.registerStyle(OVERHEAD);
event.registerStyle(POINT);
event.registerStyle(POPPER);
event.registerStyle(PULSE);
event.registerStyle(QUADHELIX);
event.registerStyle(RINGS);
event.registerStyle(SPHERE);
event.registerStyle(SPIN);
event.registerStyle(SPIRAL);
event.registerEventStyle(SWORDS);
event.registerEventStyle(TELEPORT);
event.registerStyle(THICK);
event.registerEventStyle(TRAIL);
event.registerStyle(TWINS);
event.registerStyle(VORTEX);
event.registerStyle(WHIRL);
event.registerStyle(WHIRLWIND);
event.registerStyle(WINGS);
}
/**
* Reloads the settings for all default styles
*