FabledSkyBlock/src/main/java/com/craftaro/skyblock/permission/BasicPermission.java

100 lines
3.3 KiB
Java
Raw Normal View History

package com.craftaro.skyblock.permission;
2020-05-27 15:57:20 +02:00
2024-01-13 16:33:16 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
import com.craftaro.core.utils.TextUtils;
import com.craftaro.skyblock.SkyBlock;
import com.craftaro.skyblock.island.Island;
import com.craftaro.skyblock.island.IslandRole;
2020-05-27 15:57:20 +02:00
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import javax.annotation.Nonnull;
2020-05-27 15:57:20 +02:00
import java.util.ArrayList;
import java.util.HashMap;
2020-05-27 15:57:20 +02:00
import java.util.List;
import java.util.Map;
2020-05-27 15:57:20 +02:00
public abstract class BasicPermission {
private final String name;
private final XMaterial icon;
2020-05-27 15:57:20 +02:00
private final PermissionType type;
//It used to write the permission's value
//to the global settings.yml which will be defaulted on all islands
private final Map<IslandRole, Boolean> defaultValues;
protected BasicPermission(@Nonnull String name, @Nonnull XMaterial icon, @Nonnull PermissionType type) {
this(name, icon, type, new HashMap<>());
}
protected BasicPermission(@Nonnull String name, @Nonnull XMaterial icon, @Nonnull PermissionType type, Map<IslandRole, Boolean> defaultValues) {
2020-05-27 15:57:20 +02:00
this.name = name;
this.icon = icon;
this.type = type;
if (defaultValues.isEmpty()) {
this.defaultValues = new HashMap<>();
this.defaultValues.put(IslandRole.VISITOR, false);
this.defaultValues.put(IslandRole.MEMBER, true);
this.defaultValues.put(IslandRole.OPERATOR, true);
this.defaultValues.put(IslandRole.COOP, true);
this.defaultValues.put(IslandRole.OWNER, true);
} else {
this.defaultValues = defaultValues;
}
2020-05-27 15:57:20 +02:00
}
public ItemStack getItem(Island island, IslandRole role) {
return getItem(island.hasPermission(role, this), role);
}
public ItemStack getItem(boolean permissionEnabled, IslandRole role) {
ItemStack is = this.icon.parseItem();
2020-09-01 21:05:37 +02:00
FileConfiguration configLoad = SkyBlock.getInstance().getLanguage();
2020-05-27 15:57:20 +02:00
List<String> itemLore = new ArrayList<>();
ItemMeta im = is.getItemMeta();
String roleName = role.getFriendlyName();
2020-05-27 15:57:20 +02:00
if (role == IslandRole.VISITOR || role == IslandRole.MEMBER || role == IslandRole.COOP) {
2020-05-27 15:57:20 +02:00
roleName = "Default";
}
2020-05-27 15:57:20 +02:00
String nameFinal = TextUtils.formatText(configLoad.getString("Menu.Settings." + roleName + ".Item.Setting." + this.name + ".Displayname", this.name));
2020-05-27 15:57:20 +02:00
if (im != null) {
2020-06-18 20:04:17 +02:00
im.setDisplayName(nameFinal);
for (String itemLoreList : configLoad
.getStringList("Menu.Settings." + roleName + ".Item.Setting.Status."
+ (permissionEnabled ? "Enabled" : "Disabled") + ".Lore")) {
2020-06-18 20:04:17 +02:00
itemLore.add(TextUtils.formatText(itemLoreList));
}
2020-05-27 15:57:20 +02:00
2020-06-18 20:04:17 +02:00
im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
im.setLore(itemLore);
is.setItemMeta(im);
}
2020-05-27 15:57:20 +02:00
return is;
}
public String getName() {
return this.name;
2020-05-27 15:57:20 +02:00
}
public XMaterial getIcon() {
return this.icon;
2020-05-27 15:57:20 +02:00
}
public PermissionType getType() {
return this.type;
2020-05-27 15:57:20 +02:00
}
public Map<IslandRole, Boolean> getDefaultValues() {
return defaultValues;
}
2020-05-27 15:57:20 +02:00
}