mirror of
https://github.com/TotalFreedomMC/PlayerParticles.git
synced 2024-12-28 02:14:15 +00:00
Use maps for ParticleStyle lookups
This commit is contained in:
parent
ffbe03f792
commit
32d0995289
3 changed files with 49 additions and 27 deletions
|
@ -7,7 +7,9 @@ import dev.esophose.playerparticles.styles.ParticleStyle;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
|
@ -16,13 +18,15 @@ public class ParticleStyleManager extends Manager {
|
|||
/**
|
||||
* Arrays that contain all registered styles
|
||||
*/
|
||||
private final List<ParticleStyle> styles;
|
||||
private final Map<String, ParticleStyle> stylesByName;
|
||||
private final Map<String, ParticleStyle> stylesByInternalName;
|
||||
private final List<ParticleStyle> eventStyles;
|
||||
|
||||
public ParticleStyleManager(PlayerParticles playerParticles) {
|
||||
super(playerParticles);
|
||||
|
||||
this.styles = new ArrayList<>();
|
||||
this.stylesByName = new HashMap<>();
|
||||
this.stylesByInternalName = new HashMap<>();
|
||||
this.eventStyles = new ArrayList<>();
|
||||
|
||||
DefaultStyles.initStyles();
|
||||
|
@ -30,7 +34,8 @@ public class ParticleStyleManager extends Manager {
|
|||
|
||||
@Override
|
||||
public void reload() {
|
||||
this.styles.clear();
|
||||
this.stylesByName.clear();
|
||||
this.stylesByInternalName.clear();
|
||||
this.eventStyles.clear();
|
||||
|
||||
// Run task a tick later to allow other plugins to finish registering to the event
|
||||
|
@ -53,15 +58,14 @@ public class ParticleStyleManager extends Manager {
|
|||
if (style.getInternalName() == null || style.getInternalName().trim().isEmpty())
|
||||
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() + "'");
|
||||
}
|
||||
}
|
||||
if (this.stylesByName.containsValue(style))
|
||||
throw new IllegalArgumentException("Tried to register the same style twice: '" + style.getInternalName() + "'");
|
||||
|
||||
this.styles.add(style);
|
||||
if (this.stylesByInternalName.containsKey(style.getInternalName().toLowerCase()))
|
||||
throw new IllegalArgumentException("Tried to register two styles with the same internal name spelling: '" + style.getInternalName() + "'");
|
||||
|
||||
this.stylesByName.put(style.getName().toLowerCase(), style);
|
||||
this.stylesByInternalName.put(style.getInternalName().toLowerCase(), style);
|
||||
|
||||
if (eventStyles.contains(style))
|
||||
this.eventStyles.add(style);
|
||||
|
@ -90,15 +94,41 @@ public class ParticleStyleManager extends Manager {
|
|||
/**
|
||||
* @return A List of styles that are registered and enabled
|
||||
*/
|
||||
public List<ParticleStyle> getStyles() {
|
||||
return this.styles.stream().filter(ParticleStyle::isEnabled).collect(Collectors.toList());
|
||||
public Collection<ParticleStyle> getStyles() {
|
||||
return this.stylesByName.values().stream().filter(ParticleStyle::isEnabled).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all registered styles, regardless if they are enabled or not
|
||||
*/
|
||||
public List<ParticleStyle> getStylesWithDisabled() {
|
||||
return this.styles;
|
||||
public Collection<ParticleStyle> getStylesWithDisabled() {
|
||||
return this.stylesByName.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a registered ParticleStyle by its name
|
||||
*
|
||||
* @param name The name of the ParticleStyle
|
||||
* @return The ParticleStyle, or null if not found
|
||||
*/
|
||||
public ParticleStyle getStyleByName(String name) {
|
||||
ParticleStyle style = this.stylesByName.get(name.toLowerCase());
|
||||
if (style != null && !style.isEnabled())
|
||||
style = null;
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a registered ParticleStyle by its internal name
|
||||
*
|
||||
* @param internalName The internal name of the ParticleStyle
|
||||
* @return The ParticleStyle, or null if not found
|
||||
*/
|
||||
public ParticleStyle getStyleByInternalName(String internalName) {
|
||||
ParticleStyle style = this.stylesByInternalName.get(internalName.toLowerCase());
|
||||
if (style != null && !style.isEnabled())
|
||||
style = null;
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,8 +137,7 @@ public class ParticleStyleManager extends Manager {
|
|||
* Do not call this in your plugin, it will mess with other styles
|
||||
*/
|
||||
public void updateTimers() {
|
||||
for (ParticleStyle style : this.styles)
|
||||
style.updateTimers();
|
||||
this.stylesByName.values().forEach(ParticleStyle::updateTimers);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -107,9 +107,8 @@ public class PermissionManager extends Manager {
|
|||
allPermissions.add(new Permission("playerparticles.effect.*", effectPermissions));
|
||||
|
||||
// Styles
|
||||
List<ParticleStyle> styles = this.playerParticles.getManager(ParticleStyleManager.class).getStylesWithDisabled();
|
||||
Map<String, Boolean> stylePermissions = new HashMap<>();
|
||||
for (ParticleStyle style : styles) {
|
||||
for (ParticleStyle style : this.playerParticles.getManager(ParticleStyleManager.class).getStylesWithDisabled()) {
|
||||
Permission permission = new Permission("playerparticles.style." + style.getInternalName());
|
||||
pluginManager.addPermission(permission);
|
||||
stylePermissions.put(permission.getName(), true);
|
||||
|
|
|
@ -99,10 +99,7 @@ public interface ParticleStyle {
|
|||
* @return The ParticleStyle with a matching name
|
||||
*/
|
||||
static ParticleStyle fromName(String styleName) {
|
||||
for (ParticleStyle style : PlayerParticles.getInstance().getManager(ParticleStyleManager.class).getStyles())
|
||||
if (style.getName().equalsIgnoreCase(styleName))
|
||||
return style;
|
||||
return null;
|
||||
return PlayerParticles.getInstance().getManager(ParticleStyleManager.class).getStyleByName(styleName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,10 +109,7 @@ public interface ParticleStyle {
|
|||
* @return The ParticleStyle with a matching name
|
||||
*/
|
||||
static ParticleStyle fromInternalName(String styleName) {
|
||||
for (ParticleStyle style : PlayerParticles.getInstance().getManager(ParticleStyleManager.class).getStyles())
|
||||
if (style.getInternalName().equalsIgnoreCase(styleName))
|
||||
return style;
|
||||
return null;
|
||||
return PlayerParticles.getInstance().getManager(ParticleStyleManager.class).getStyleByInternalName(styleName);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue