bentobox/src/main/java/us/tastybento/bskyblock/managers/FlagsManager.java
Tastybento ace6294e23 Came full circle - kept Flags as final statics
This approach simulates an enum, but one that can be extended by others
to add custom flags. I added a handy values() method that uses
reflection to provide a list of all the flags in the class.

See TestBSkyBlock.java test classes for the tests of the default flag
registration and the custom flag registration.
2018-02-05 23:53:07 -08:00

60 lines
1.5 KiB
Java

package us.tastybento.bskyblock.managers;
import java.util.HashMap;
import org.bukkit.Bukkit;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.lists.Flags;
public class FlagsManager {
private BSkyBlock plugin;
private HashMap<String, Flag> flags = new HashMap<>();
public FlagsManager(BSkyBlock plugin) {
this.plugin = plugin;
// Register default flags
for (Flag flag : Flags.values()) {
registerFlag(flag);
}
}
/**
* Register a new flag with BSkyBlock
* @param flag
*/
public void registerFlag(Flag flag) {
//Bukkit.getLogger().info("DEBUG: registering flag " + flag.getID());
flags.put(flag.getID(), flag);
// If there is a listener, register it into Bukkit.
flag.getListener().ifPresent(l -> Bukkit.getServer().getPluginManager().registerEvents(l, plugin));
}
public HashMap<String, Flag> getFlags() {
return flags;
}
/**
* Get flag by string
* @param key - string name same as the enum
* @return Flag or null if not known
*/
public Flag getFlagByID(String id) {
//Bukkit.getLogger().info("DEBUG: requesting " + id + " flags size = " + flags.size());
return flags.get(id);
}
public Flag getFlagByIcon(PanelItem item) {
for (Flag flag : flags.values()) {
if (flag.getIcon().equals(item)) return flag;
}
return null;
}
}