Flag registry
About 643 wordsAbout 2 min
Info
From NoBuildPlus 1.5.50, you are able to register flags from a new class in NoBuildPlus or in the class of your own plugin.
Interface Flag
Before creating a class, there are the methods that you will need to know.
public interface Flag {
/**
* @return name of the flag
*/
String getName();
/**
* This can refer to your own flag setting file
* @return type of the flag
*/
String getType();
/**
* This can refer to your own flag setting file
* @return list of the flag
*/
List<String> getList();
/**
* This is recommended to initialize in the constructor
* @return type of the flag
*/
String getDefaultType();
/**
* This is recommended to initialize in the constructor
* @return list of the flag
*/
List<String> getDefaultList();
/**
* True means the plugin is protecting
* @param world name of the world
* @return boolean
*/
default boolean isEnabled(String world) {
if (!Settings.canExecute(world, getName())) {
return false;
}
return !Worlds.getFlag(world, getName());
}
/**
* Should the flag stay at flags.yml
* Otherwise, you can let the flags settings staying in your own file.
* @return boolean
*/
boolean inLocalFile();
/**
* List Map
*/
void refreshMap();
// For Menu
/**
* Item on menu
* @return material.toString()
*/
String getShowItem();
/**
* To get language of NoBuildPlus, see NBPAPI.getLang()
* @param language language of config
* @return type of the flag
*/
String getDescription(String language);
}In NoBuildPlus FlagRegistry, it allows you to make the flag settings staying in your own file, but not "flags.yml" in NoBuildPlus folder! Please take a look at the comments in the below methods to see how to enable the function.
Create an enum class
You can create the class in your plugin, or in p1xel.nobuildplus.flag package in the fork of NoBuildPlus.
public enum ExampleFlags implements Flag {
custom_flag_one("custom-flag-one", "TNT", null, null),
custom_flag_two("custom-flag-two", "GRASS_BLOCK", null, null);
private final String flag_name;
private final String show_item;
private final String type;
private final List<String> list;
private final HashMap<ExampleFlags, List<String>> LISTMAP = new HashMap<>();
ExampleFlags(String flag_name, String show_item, String type, List<String> list) {
this.flag_name = flag_name;
this.show_item = show_item;
this.type = type;
this.list = list;
}
@Override
public String getName() {
return flag_name;
}
@Override
public String getType() {
// Don't change it if you the file settings in flags.yml of NoBuildPlus.
// Otherwise, change it to your own method.
return FlagsManager.getFlagsType(getName());
}
@Override
public List<String> getList() {
// Don't change it if you the file settings in flags.yml of NoBuildPlus.
// Otherwise, change it to your own method.
return FlagsManager.getFlagsList(flag_name);
}
@Override
public String getDefaultType() {
return type;
}
@Override
public List<String> getDefaultList() {
return list;
}
@Override
public boolean inLocalFile() {
// Set the return boolean to FALSE,
// if you want the flag settings in your own file.
return true;
}
@Override
public void refreshMap() {
for (ExampleFlags flag : values()) {
List<String> list = new ArrayList<>();
// Don't change it if you the file settings in flags.yml of NoBuildPlus.
// Otherwise, change it to your own method.
if (FlagsManager.yaml.isSet("flags." + flag + ".list")) {
list = FlagsManager.getFlagsList(flag.flag_name);
}
LISTMAP.put(flag, list);
}
}
@Override
public String getShowItem() {
return show_item;
}
@Override
public String getDescription(String language) {
// Don't change it if you want the description message in language file of NoBuildPlus
// Otherwise, change it to your own method.
return Locale.getMessage("flag.description." + flag_name);
}
}Register flags
After setting up the enum, please make sure to register it to let NoBuildPlus knows!
public class FlagRegistrationLoader {
private static final Map<String, Flag[]> FLAG_REGISTRATION_HOOKS = new HashMap<String, Flag[]>() {{
put("ItemsAdder", ItemsAdderFlags.values());
// Add your plugin here
}};
private static final Map<String, List<Listener>> LISTENER_REGISTRATION = new HashMap<String, List<Listener>>() {{
put("ItemsAdder", Collections.singletonList(new ItemsAdderListener()));
// Add your plugin here
}};
}For adding in NoBuildPlus
If you want to pull the new commit to NoBuildPlus repository,
please make sure the descriptions of new created flags have been added.
flag:
description:
custom_flag: 'What does it protect'