2018-02-20 23:06:09 +01:00
|
|
|
package us.tastybento.bskyblock.api.flags;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
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;
|
2018-02-26 03:21:42 +01:00
|
|
|
|
2018-05-02 07:56:10 +02:00
|
|
|
import us.tastybento.bskyblock.BSkyBlock;
|
2018-06-11 07:34:01 +02:00
|
|
|
import us.tastybento.bskyblock.api.configuration.WorldSettings;
|
2018-06-03 10:07:58 +02:00
|
|
|
import us.tastybento.bskyblock.api.localization.TextVariables;
|
2018-02-20 23:06:09 +01:00
|
|
|
import us.tastybento.bskyblock.api.panels.PanelItem;
|
2018-02-25 17:09:46 +01:00
|
|
|
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
2018-03-11 23:59:03 +01:00
|
|
|
import us.tastybento.bskyblock.api.user.User;
|
2018-05-02 07:56:10 +02:00
|
|
|
import us.tastybento.bskyblock.database.objects.Island;
|
|
|
|
import us.tastybento.bskyblock.managers.RanksManager;
|
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
|
|
|
|
|
|
|
public enum Type {
|
|
|
|
PROTECTION,
|
2018-06-08 17:20:16 +02:00
|
|
|
SETTING,
|
2018-06-10 02:22:38 +02:00
|
|
|
MENU,
|
2018-06-08 17:20:16 +02:00
|
|
|
WORLD_SETTING
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 00:42:21 +02:00
|
|
|
private static final String PRO_FLAGS = "protection.flags.";
|
|
|
|
private static final String DOT_DESC = ".description";
|
|
|
|
private static final String DESC_PLACEHOLDER = "[description]";
|
|
|
|
|
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;
|
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-02-20 23:06:09 +01:00
|
|
|
|
2018-05-02 07:56:10 +02:00
|
|
|
Flag(String id, Material icon, Listener listener, boolean defaultSetting, Type type, int defaultRank, PanelItem.ClickHandler clickListener) {
|
2018-02-25 17:09:46 +01:00
|
|
|
this.id = id;
|
2018-02-20 23:06:09 +01:00
|
|
|
this.icon = icon;
|
|
|
|
this.listener = listener;
|
2018-06-04 05:54:37 +02:00
|
|
|
this.setting = defaultSetting;
|
2018-02-20 23:06:09 +01:00
|
|
|
this.type = type;
|
2018-04-30 10:12:32 +02:00
|
|
|
this.defaultRank = defaultRank;
|
2018-05-02 07:56:10 +02:00
|
|
|
this.clickHandler = clickListener;
|
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-11 07:34:01 +02:00
|
|
|
WorldSettings ws = BSkyBlock.getInstance().getIWM().getWorldSettings(world);
|
|
|
|
return ws != null ? ws.getWorldFlags().getOrDefault(getID(), setting) : false;
|
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)) {
|
|
|
|
BSkyBlock.getInstance().getIWM().getWorldSettings(world).getWorldFlags().put(getID(), setting);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return - PanelItem for this flag
|
|
|
|
*/
|
|
|
|
public PanelItem toPanelItem(BSkyBlock plugin, User user) {
|
2018-06-02 20:27:51 +02:00
|
|
|
// Start the flag conversion
|
|
|
|
PanelItemBuilder pib = new PanelItemBuilder()
|
|
|
|
.icon(new ItemStack(icon))
|
2018-06-11 00:42:21 +02:00
|
|
|
.name(user.getTranslation("protection.panel.flag-item.name-layout", TextVariables.NAME, user.getTranslation(PRO_FLAGS + id + ".name")))
|
2018-06-02 20:27:51 +02:00
|
|
|
.clickHandler(clickHandler);
|
2018-06-10 02:22:38 +02:00
|
|
|
if (getType().equals(Type.MENU)) {
|
2018-06-11 00:42:21 +02:00
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.menu-layout", DESC_PLACEHOLDER, user.getTranslation(PRO_FLAGS + id + DOT_DESC)));
|
2018-06-10 02:22:38 +02:00
|
|
|
return pib.build();
|
|
|
|
}
|
2018-06-08 17:20:16 +02:00
|
|
|
// Check if this is a setting or world setting
|
2018-06-10 02:22:38 +02:00
|
|
|
if (getType().equals(Type.WORLD_SETTING)) {
|
2018-06-11 00:42:21 +02:00
|
|
|
String worldDetting = this.isSetForWorld(user.getWorld()) ? user.getTranslation("protection.panel.flag-item.setting-active")
|
2018-06-04 05:54:37 +02:00
|
|
|
: user.getTranslation("protection.panel.flag-item.setting-disabled");
|
2018-06-11 00:42:21 +02:00
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", DESC_PLACEHOLDER, user.getTranslation(PRO_FLAGS + id + DOT_DESC)
|
|
|
|
, "[setting]", worldDetting));
|
2018-06-04 05:54:37 +02:00
|
|
|
return pib.build();
|
2018-06-08 17:20:16 +02:00
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
|
2018-05-02 07:56:10 +02:00
|
|
|
// Get the island this user is on or their own
|
2018-05-18 06:25:12 +02:00
|
|
|
Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId()));
|
2018-05-02 07:56:10 +02:00
|
|
|
if (island != null) {
|
2018-06-10 02:22:38 +02:00
|
|
|
if (getType().equals(Type.SETTING)) {
|
2018-06-11 00:42:21 +02:00
|
|
|
String islandSetting = island.isAllowed(this) ? user.getTranslation("protection.panel.flag-item.setting-active")
|
2018-06-08 17:20:16 +02:00
|
|
|
: user.getTranslation("protection.panel.flag-item.setting-disabled");
|
2018-06-11 00:42:21 +02:00
|
|
|
pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", DESC_PLACEHOLDER, user.getTranslation(PRO_FLAGS + id + DOT_DESC)
|
|
|
|
, "[setting]", islandSetting));
|
2018-06-08 17:20:16 +02:00
|
|
|
return pib.build();
|
|
|
|
}
|
2018-05-02 07:56:10 +02:00
|
|
|
// TODO: Get the world settings - the player has no island and is not in an island location
|
2018-06-02 20:27:51 +02:00
|
|
|
// Dynamic rank list
|
2018-06-10 02:22:38 +02:00
|
|
|
if (getType().equals(Type.PROTECTION)) {
|
2018-06-08 17:20:16 +02:00
|
|
|
// Protection flag
|
2018-06-11 00:42:21 +02:00
|
|
|
String d = user.getTranslation(PRO_FLAGS + id + DOT_DESC);
|
|
|
|
d = user.getTranslation("protection.panel.flag-item.description-layout", DESC_PLACEHOLDER, d);
|
2018-06-10 07:09:20 +02:00
|
|
|
pib.description(d);
|
2018-06-08 17:20:16 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-02 07:56:10 +02:00
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
return pib.build();
|
2018-02-25 17:09:46 +01:00
|
|
|
}
|
2018-06-02 20:27:51 +02:00
|
|
|
|
2018-02-26 04:48:01 +01:00
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see java.lang.Object#toString()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2018-05-02 07:56:10 +02:00
|
|
|
return "Flag [id=" + id + ", icon=" + icon + ", listener=" + listener + ", type=" + type + ", defaultSetting="
|
2018-06-04 05:54:37 +02:00
|
|
|
+ setting + ", defaultRank=" + defaultRank + ", clickHandler=" + clickHandler + "]";
|
2018-02-26 04:48:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(Flag o) {
|
|
|
|
return getID().compareTo(o.getID());
|
|
|
|
}
|
2018-04-30 10:12:32 +02:00
|
|
|
|
2018-06-11 02:23:48 +02:00
|
|
|
|
2018-02-20 23:06:09 +01:00
|
|
|
}
|