2018-07-31 18:03:32 +02:00
|
|
|
package world.bentobox.bentobox.api.flags;
|
2018-02-20 23:06:09 +01:00
|
|
|
|
2019-07-05 01:54:05 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2018-02-25 17:09:46 +01:00
|
|
|
import org.bukkit.Material;
|
2018-06-04 05:54:37 +02:00
|
|
|
import org.bukkit.World;
|
2018-02-20 23:06:09 +01:00
|
|
|
import org.bukkit.event.Listener;
|
2018-02-25 17:09:46 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-01-23 19:58:31 +01:00
|
|
|
import org.eclipse.jdt.annotation.NonNull;
|
2019-07-05 01:54:05 +02:00
|
|
|
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.BentoBox;
|
2019-05-11 00:18:13 +02:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
2019-02-13 07:58:33 +01:00
|
|
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
2018-12-23 16:33:10 +01:00
|
|
|
import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick;
|
|
|
|
import world.bentobox.bentobox.api.flags.clicklisteners.IslandToggleClick;
|
|
|
|
import world.bentobox.bentobox.api.flags.clicklisteners.WorldToggleClick;
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.api.localization.TextVariables;
|
|
|
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
|
|
|
import world.bentobox.bentobox.api.user.User;
|
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
|
|
|
import world.bentobox.bentobox.managers.RanksManager;
|
2019-01-15 20:45:49 +01:00
|
|
|
import world.bentobox.bentobox.util.Util;
|
2018-02-20 23:06:09 +01:00
|
|
|
|
2018-02-26 04:48:01 +01:00
|
|
|
public class Flag implements Comparable<Flag> {
|
2018-02-20 23:06:09 +01:00
|
|
|
|
2019-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Defines the behavior and operation of the flag, as well as its category in the {@link world.bentobox.bentobox.panels.SettingsPanel}.
|
|
|
|
*/
|
2018-02-20 23:06:09 +01:00
|
|
|
public enum Type {
|
2019-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Flag protecting an island.
|
|
|
|
* It can be modified by the players (island owner).
|
|
|
|
* It applies differently depending on the rank of the player who performs the action protected by the flag.
|
|
|
|
*/
|
2018-06-16 17:10:00 +02:00
|
|
|
PROTECTION(Material.SHIELD),
|
2019-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Flag modifying parameters of the island.
|
|
|
|
* It can be modified by the players (island owner).
|
|
|
|
* This is usually an on/off setting.
|
|
|
|
*/
|
2018-08-01 10:18:37 +02:00
|
|
|
SETTING(Material.COMMAND_BLOCK),
|
2019-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Flag applying to the world.
|
|
|
|
* It can only be modified by administrators (permission or operator).
|
|
|
|
* This is usually an on/off setting.
|
|
|
|
*/
|
2018-08-31 21:04:24 +02:00
|
|
|
WORLD_SETTING(Material.GRASS_BLOCK);
|
2018-06-16 17:10:00 +02:00
|
|
|
|
2019-01-23 19:58:31 +01:00
|
|
|
private @NonNull Material icon;
|
2018-06-16 17:10:00 +02:00
|
|
|
|
2019-01-23 19:58:31 +01:00
|
|
|
Type(@NonNull Material icon) {
|
2018-06-16 17:10:00 +02:00
|
|
|
this.icon = icon;
|
|
|
|
}
|
|
|
|
|
2019-01-23 19:58:31 +01:00
|
|
|
@NonNull
|
2018-06-16 17:10:00 +02:00
|
|
|
public Material getIcon() {
|
|
|
|
return icon;
|
|
|
|
}
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 02:06:17 +02:00
|
|
|
private static final String PROTECTION_FLAGS = "protection.flags.";
|
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
private final String id;
|
2018-02-25 17:09:46 +01:00
|
|
|
private final Material icon;
|
2018-02-20 23:06:09 +01:00
|
|
|
private final Listener listener;
|
|
|
|
private final Type type;
|
2018-06-04 05:54:37 +02:00
|
|
|
private boolean setting;
|
2019-01-15 20:45:49 +01:00
|
|
|
private Map<World, Boolean> defaultWorldSettings = new HashMap<>();
|
2018-04-30 10:12:32 +02:00
|
|
|
private final int defaultRank;
|
2018-05-02 07:56:10 +02:00
|
|
|
private final PanelItem.ClickHandler clickHandler;
|
2018-06-16 17:10:00 +02:00
|
|
|
private final boolean subPanel;
|
2019-02-13 07:58:33 +01:00
|
|
|
private Set<GameModeAddon> gameModes = new HashSet<>();
|
2019-05-11 00:18:13 +02:00
|
|
|
private final Addon addon;
|
2018-02-20 23:06:09 +01:00
|
|
|
|
2019-05-11 14:28:57 +02:00
|
|
|
private Flag(Builder builder) {
|
2018-12-23 16:33:10 +01:00
|
|
|
this.id = builder.id;
|
|
|
|
this.icon = builder.icon;
|
|
|
|
this.listener = builder.listener;
|
|
|
|
this.type = builder.type;
|
|
|
|
this.setting = builder.defaultSetting;
|
|
|
|
this.defaultRank = builder.defaultRank;
|
|
|
|
this.clickHandler = builder.clickHandler;
|
|
|
|
this.subPanel = builder.usePanel;
|
2019-02-13 07:58:33 +01:00
|
|
|
if (builder.gameModeAddon != null) {
|
|
|
|
this.gameModes.add(builder.gameModeAddon);
|
|
|
|
}
|
2019-05-11 00:18:13 +02:00
|
|
|
this.addon = builder.addon;
|
2018-12-23 16:33:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
public String getID() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-02-25 17:09:46 +01:00
|
|
|
public Material getIcon() {
|
2018-02-20 23:06:09 +01:00
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<Listener> getListener() {
|
|
|
|
return Optional.ofNullable(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-04 05:54:37 +02:00
|
|
|
* Check if a setting is set in this world
|
|
|
|
* @param world - world
|
2018-06-11 07:34:01 +02:00
|
|
|
* @return world setting or default flag setting if a specific world setting is not set.
|
|
|
|
* If world is not a game world, then the result will always be false!
|
2018-02-20 23:06:09 +01:00
|
|
|
*/
|
2018-06-08 17:20:16 +02:00
|
|
|
public boolean isSetForWorld(World world) {
|
2018-06-18 06:37:50 +02:00
|
|
|
if (type.equals(Type.WORLD_SETTING)) {
|
2018-07-29 22:21:46 +02:00
|
|
|
WorldSettings ws = BentoBox.getInstance().getIWM().getWorldSettings(world);
|
2018-06-18 06:37:50 +02:00
|
|
|
if (ws != null) {
|
|
|
|
ws.getWorldFlags().putIfAbsent(getID(), setting);
|
|
|
|
return ws.getWorldFlags().get(getID());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Setting
|
2019-01-15 20:45:49 +01:00
|
|
|
return defaultWorldSettings.getOrDefault(Util.getWorld(world), setting);
|
2018-06-18 06:37:50 +02:00
|
|
|
}
|
2018-06-04 05:54:37 +02:00
|
|
|
}
|
2018-06-08 17:20:16 +02:00
|
|
|
|
2018-06-04 05:54:37 +02:00
|
|
|
/**
|
2018-06-10 07:09:20 +02:00
|
|
|
* Set a world setting
|
2018-06-04 05:54:37 +02:00
|
|
|
* @param world - world
|
|
|
|
* @param setting - true or false
|
|
|
|
*/
|
|
|
|
public void setSetting(World world, boolean setting) {
|
2018-06-10 07:09:20 +02:00
|
|
|
if (getType().equals(Type.WORLD_SETTING)) {
|
2018-07-29 22:21:46 +02:00
|
|
|
BentoBox.getInstance().getIWM().getWorldSettings(world).getWorldFlags().put(getID(), setting);
|
2018-06-10 07:09:20 +02:00
|
|
|
}
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status of this flag for locations outside of island spaces
|
|
|
|
* @param defaultSetting - true means it is allowed. false means it is not allowed
|
|
|
|
*/
|
|
|
|
public void setDefaultSetting(boolean defaultSetting) {
|
2018-06-04 05:54:37 +02:00
|
|
|
this.setting = defaultSetting;
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:45:49 +01:00
|
|
|
/**
|
|
|
|
* Set the status of this flag for locations outside of island spaces for a specific world
|
|
|
|
* @param defaultSetting - true means it is allowed. false means it is not allowed
|
|
|
|
*/
|
|
|
|
public void setDefaultSetting(World world, boolean defaultSetting) {
|
|
|
|
this.defaultWorldSettings.put(world, defaultSetting);
|
|
|
|
}
|
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
/**
|
|
|
|
* @return the type
|
|
|
|
*/
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2018-04-30 10:12:32 +02:00
|
|
|
/**
|
|
|
|
* @return the defaultRank
|
|
|
|
*/
|
|
|
|
public int getDefaultRank() {
|
|
|
|
return defaultRank;
|
|
|
|
}
|
|
|
|
|
2018-06-16 17:10:00 +02:00
|
|
|
/**
|
|
|
|
* @return whether the flag uses a subpanel or not
|
|
|
|
*/
|
|
|
|
public boolean hasSubPanel() {
|
|
|
|
return subPanel;
|
|
|
|
}
|
2019-05-11 14:28:57 +02:00
|
|
|
|
2019-05-11 00:18:13 +02:00
|
|
|
/**
|
|
|
|
* Get the addon that made this flag
|
|
|
|
* @return the addon
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
public Addon getAddon() {
|
2019-05-11 14:28:57 +02:00
|
|
|
return addon;
|
2019-05-11 00:18:13 +02:00
|
|
|
}
|
2018-06-16 17:10:00 +02:00
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#hashCode()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
final int prime = 31;
|
|
|
|
int result = 1;
|
|
|
|
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
|
|
|
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#equals(java.lang.Object)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-25 16:01:30 +01:00
|
|
|
if (!(obj instanceof Flag)) {
|
2018-02-20 23:06:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-25 16:01:30 +01:00
|
|
|
Flag other = (Flag) obj;
|
2018-02-20 23:06:09 +01:00
|
|
|
if (id == null) {
|
|
|
|
if (other.id != null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!id.equals(other.id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-25 13:48:58 +02:00
|
|
|
|
|
|
|
return type == other.type;
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 05:55:54 +01:00
|
|
|
/**
|
|
|
|
* @return a locale reference for the name of this protection flag
|
|
|
|
*/
|
2018-06-11 15:55:01 +02:00
|
|
|
public String getNameReference() {
|
2018-06-24 02:06:17 +02:00
|
|
|
return PROTECTION_FLAGS + this.id + ".name";
|
2018-06-11 15:55:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 05:55:54 +01:00
|
|
|
/**
|
|
|
|
* @return a locale reference for the description of this protection flag
|
|
|
|
*/
|
2018-06-11 15:55:01 +02:00
|
|
|
public String getDescriptionReference() {
|
2018-06-24 02:06:17 +02:00
|
|
|
return PROTECTION_FLAGS + this.id + ".description";
|
2018-06-11 15:55:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 05:55:54 +01:00
|
|
|
/**
|
|
|
|
* @return a locale reference for the hint of this protection flag
|
|
|
|
*/
|
2018-06-16 17:15:51 +02:00
|
|
|
public String getHintReference() {
|
2018-06-24 02:06:17 +02:00
|
|
|
return PROTECTION_FLAGS + this.id + ".hint";
|
2018-06-16 17:15:51 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
2019-03-10 05:55:54 +01:00
|
|
|
* A set of game mode addons that use this flag. If empty, flag applies to all.
|
2019-02-13 07:58:33 +01:00
|
|
|
* @return the gameModeAddon
|
|
|
|
*/
|
|
|
|
public Set<GameModeAddon> getGameModes() {
|
|
|
|
return gameModes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param gameModeAddon the gameModeAddon to set
|
|
|
|
*/
|
|
|
|
public void setGameModes(Set<GameModeAddon> gameModeAddon) {
|
|
|
|
this.gameModes = gameModeAddon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a gameModeAddon to this flag
|
|
|
|
* @param gameModeAddon - game mode addon
|
|
|
|
*/
|
|
|
|
public void addGameModeAddon(GameModeAddon gameModeAddon) {
|
|
|
|
this.gameModes.add(gameModeAddon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a gameModeAddon to this flag
|
|
|
|
* @param gameModeAddon - game mode addon
|
|
|
|
* @return <tt>true</tt> if this set contained the specified element
|
|
|
|
*/
|
|
|
|
public boolean removeGameModeAddon(GameModeAddon gameModeAddon) {
|
|
|
|
return this.gameModes.remove(gameModeAddon);
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:56:10 +02:00
|
|
|
/**
|
|
|
|
* Converts a flag to a panel item. The content of the flag will change depending on who the user is and where they are.
|
2018-06-11 02:23:48 +02:00
|
|
|
* @param plugin - plugin
|
2018-05-02 07:56:10 +02:00
|
|
|
* @param user - user that will see this flag
|
2019-03-23 23:38:06 +01:00
|
|
|
* @param invisible - true if this flag is not visible to players
|
|
|
|
* @return - PanelItem for this flag or null if item is inivisible to user
|
2018-05-02 07:56:10 +02:00
|
|
|
*/
|
2019-03-23 23:38:06 +01:00
|
|
|
public PanelItem toPanelItem(BentoBox plugin, User user, boolean invisible) {
|
|
|
|
// Invisibility
|
|
|
|
if (!user.isOp() && invisible) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
// Start the flag conversion
|
|
|
|
PanelItemBuilder pib = new PanelItemBuilder()
|
|
|
|
.icon(new ItemStack(icon))
|
2018-06-11 15:55:01 +02:00
|
|
|
.name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, user.getTranslation(getNameReference())))
|
2019-03-23 23:38:06 +01:00
|
|
|
.clickHandler(clickHandler)
|
|
|
|
.invisible(invisible);
|
2018-06-16 17:10:00 +02:00
|
|
|
if (hasSubPanel()) {
|
2018-06-11 15:55:01 +02:00
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.menu-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())));
|
2018-06-10 02:22:38 +02:00
|
|
|
return pib.build();
|
|
|
|
}
|
2019-03-24 02:59:30 +01:00
|
|
|
Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId()));
|
|
|
|
switch(getType()) {
|
2019-06-09 04:14:25 +02:00
|
|
|
case PROTECTION:
|
|
|
|
return createProtectionFlag(plugin, user, island, pib).build();
|
|
|
|
case SETTING:
|
|
|
|
return createSettingFlag(user, island, pib).build();
|
|
|
|
case WORLD_SETTING:
|
|
|
|
return createWorldSettingFlag(user, pib).build();
|
|
|
|
default:
|
|
|
|
return pib.build();
|
2018-07-29 22:21:46 +02:00
|
|
|
}
|
2019-03-24 02:59:30 +01:00
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
|
2019-03-24 15:47:25 +01:00
|
|
|
private PanelItemBuilder createWorldSettingFlag(User user, PanelItemBuilder pib) {
|
2019-03-24 02:59:30 +01:00
|
|
|
String worldSetting = this.isSetForWorld(user.getWorld()) ? user.getTranslation("protection.panel.flag-item.setting-active")
|
|
|
|
: user.getTranslation("protection.panel.flag-item.setting-disabled");
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())
|
|
|
|
, "[setting]", worldSetting));
|
|
|
|
return pib;
|
|
|
|
}
|
|
|
|
|
2019-03-24 15:47:25 +01:00
|
|
|
private PanelItemBuilder createSettingFlag(User user, Island island, PanelItemBuilder pib) {
|
2018-05-02 07:56:10 +02:00
|
|
|
if (island != null) {
|
2019-03-24 02:59:30 +01:00
|
|
|
String islandSetting = island.isAllowed(this) ? user.getTranslation("protection.panel.flag-item.setting-active")
|
|
|
|
: user.getTranslation("protection.panel.flag-item.setting-disabled");
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())
|
|
|
|
, "[setting]", islandSetting));
|
2018-05-02 07:56:10 +02:00
|
|
|
}
|
2019-03-24 02:59:30 +01:00
|
|
|
return pib;
|
2018-02-25 17:09:46 +01:00
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
|
2019-03-24 02:59:30 +01:00
|
|
|
private PanelItemBuilder createProtectionFlag(BentoBox plugin, User user, Island island, PanelItemBuilder pib) {
|
|
|
|
if (island != null) {
|
|
|
|
// Protection flag
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.description-layout",
|
|
|
|
TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())));
|
|
|
|
plugin.getRanksManager().getRanks().forEach((reference, score) -> {
|
|
|
|
if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) {
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.blocked-rank") + user.getTranslation(reference));
|
|
|
|
} else if (score <= RanksManager.OWNER_RANK && score > island.getFlag(this)) {
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.allowed-rank") + user.getTranslation(reference));
|
|
|
|
} else if (score == island.getFlag(this)) {
|
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.minimal-rank") + user.getTranslation(reference));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pib;
|
|
|
|
}
|
2018-02-26 04:48:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-05-11 14:28:57 +02:00
|
|
|
public String toString() {
|
|
|
|
return "Flag [id=" + id + "]";
|
|
|
|
}
|
2019-05-11 00:18:13 +02:00
|
|
|
|
2019-05-11 14:28:57 +02:00
|
|
|
@Override
|
2018-02-26 04:48:01 +01:00
|
|
|
public int compareTo(Flag o) {
|
|
|
|
return getID().compareTo(o.getID());
|
|
|
|
}
|
2018-12-23 16:33:10 +01:00
|
|
|
|
|
|
|
/**
|
2019-02-13 07:58:33 +01:00
|
|
|
* Builder for making flags
|
2018-12-23 16:33:10 +01:00
|
|
|
* @author tastybento, Poslovitch
|
|
|
|
*/
|
|
|
|
public static class Builder {
|
|
|
|
// Mandatory fields
|
|
|
|
private String id;
|
|
|
|
private Material icon;
|
|
|
|
|
|
|
|
// Listener
|
|
|
|
private Listener listener;
|
|
|
|
|
|
|
|
// Type - is defaulted to PROTECTION
|
|
|
|
private Type type = Type.PROTECTION;
|
|
|
|
|
|
|
|
// Default settings
|
2018-12-27 11:24:53 +01:00
|
|
|
private boolean defaultSetting = false;
|
2018-12-24 07:15:37 +01:00
|
|
|
private int defaultRank = RanksManager.MEMBER_RANK;
|
2018-12-23 16:33:10 +01:00
|
|
|
|
|
|
|
// ClickHandler - default depends on the type
|
|
|
|
private PanelItem.ClickHandler clickHandler;
|
|
|
|
|
|
|
|
// Whether there is a sub-panel or not
|
|
|
|
private boolean usePanel = false;
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
// GameModeAddon
|
|
|
|
private GameModeAddon gameModeAddon;
|
2019-05-11 14:28:57 +02:00
|
|
|
private Addon addon;
|
2019-02-13 07:58:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builder for making flags
|
|
|
|
* @param id - a unique id that MUST be the same as the enum of the flag
|
|
|
|
* @param icon - a material that will be used as the icon in the GUI
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder(String id, Material icon) {
|
|
|
|
this.id = id;
|
|
|
|
this.icon = icon;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* The listener that should be instantiated to handle events this flag cares about.
|
|
|
|
* If the listener class already exists, then do not create it again in another flag.
|
|
|
|
* @param listener - Bukkit Listener
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder listener(Listener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* The type of flag.
|
|
|
|
* @param type {@link Type#PROTECTION}, {@link Type#SETTING} or {@link Type#WORLD_SETTING}
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder type(Type type) {
|
|
|
|
this.type = type;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* The click handler to use when this icon is clicked
|
|
|
|
* @param clickHandler - click handler
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder clickHandler(PanelItem.ClickHandler clickHandler) {
|
|
|
|
this.clickHandler = clickHandler;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* Set the default setting for {@link Type#SETTING} or {@link Type#WORLD_SETTING} flags
|
|
|
|
* @param defaultSetting - true or false
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder defaultSetting(boolean defaultSetting) {
|
|
|
|
this.defaultSetting = defaultSetting;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* Set the default rank for {@link Type#PROTECTION} flags
|
|
|
|
* @param defaultRank - default rank
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Builder defaultRank(int defaultRank) {
|
|
|
|
this.defaultRank = defaultRank;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* Set that this flag icon will open up a sub-panel
|
|
|
|
* @param usePanel - true or false
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-23 16:39:31 +01:00
|
|
|
public Builder usePanel(boolean usePanel) {
|
2018-12-23 16:33:10 +01:00
|
|
|
this.usePanel = usePanel;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:58:33 +01:00
|
|
|
/**
|
|
|
|
* Make this flag specific to this gameMode
|
|
|
|
* @param gameModeAddon
|
2019-05-11 00:18:13 +02:00
|
|
|
* @return Builder
|
2019-02-13 07:58:33 +01:00
|
|
|
*/
|
|
|
|
public Builder setGameMode(GameModeAddon gameModeAddon) {
|
|
|
|
this.gameModeAddon = gameModeAddon;
|
|
|
|
return this;
|
|
|
|
}
|
2019-05-11 14:28:57 +02:00
|
|
|
|
2019-05-11 00:18:13 +02:00
|
|
|
/**
|
|
|
|
* The addon registering this flag. Ensure this is set to enable the addon to be reloaded.
|
|
|
|
* @param addon
|
|
|
|
* @return Builder
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
public Builder addon(Addon addon) {
|
2019-05-11 14:28:57 +02:00
|
|
|
this.addon = addon;
|
|
|
|
return this;
|
2019-05-11 00:18:13 +02:00
|
|
|
}
|
2019-02-13 07:58:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the flag
|
|
|
|
* @return Flag
|
|
|
|
*/
|
2018-12-23 16:33:10 +01:00
|
|
|
public Flag build() {
|
|
|
|
// If no clickHandler has been set, then apply default ones
|
|
|
|
if (clickHandler == null) {
|
|
|
|
switch (type){
|
2019-06-09 04:14:25 +02:00
|
|
|
case PROTECTION:
|
|
|
|
clickHandler = new CycleClick(id);
|
|
|
|
break;
|
|
|
|
case SETTING:
|
|
|
|
clickHandler = new IslandToggleClick(id);
|
|
|
|
break;
|
|
|
|
case WORLD_SETTING:
|
|
|
|
clickHandler = new WorldToggleClick(id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
clickHandler = new CycleClick(id);
|
|
|
|
break;
|
2018-12-23 16:33:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Flag(this);
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 23:38:06 +01:00
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|