Compare commits

...
16 changed files with 474 additions and 177 deletions

No files matched your search

+4 -4
View File
@@ -27,11 +27,11 @@
<orderEntry type="library" name="Maven: org.jetbrains:annotations:22.0.0" level="project" />
<orderEntry type="library" name="Maven: io.papermc:paperlib:1.0.8-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: co.aikar:acf-paper:0.5.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.github.cryptomorin:XSeries:8.3.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" name="Maven: com.github.cryptomorin:XSeries:9.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.7.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-bukkit:7.0.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-bukkit:7.2.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-core:7.0.4" level="project" />
+7 -8
View File
@@ -4,7 +4,7 @@
<groupId>net.sacredlabyrinth.phaed.simpleclans</groupId>
<artifactId>SimpleClans</artifactId>
<name>SimpleClans</name>
<version>2.18.1-SNAPSHOT</version>
<version>2.18.2-SNAPSHOT</version>
<url>https://www.spigotmc.org/resources/simpleclans.71242/</url>
<build>
<resources>
@@ -13,8 +13,7 @@
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>**/*.yml</include>
<include>*.properties</include>
</includes>
</resource>
@@ -235,13 +234,9 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<version>5.9.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>apiguardian-api</artifactId>
<groupId>org.apiguardian</groupId>
</exclusion>
<exclusion>
<artifactId>opentest4j</artifactId>
<groupId>org.opentest4j</groupId>
@@ -250,6 +245,10 @@
<artifactId>junit-platform-commons</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>apiguardian-api</artifactId>
<groupId>org.apiguardian</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
+3 -4
View File
@@ -16,8 +16,7 @@
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>**/*.yml</include>
<include>*.properties</include>
</includes>
</resource>
@@ -211,12 +210,12 @@
<dependency>
<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>8.3.0</version>
<version>9.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -3,15 +3,21 @@ package net.sacredlabyrinth.phaed.simpleclans.managers;
import com.cryptomorin.xseries.XMaterial;
import net.sacredlabyrinth.phaed.simpleclans.Rank;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCFrame;
import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.*;
import static net.sacredlabyrinth.phaed.simpleclans.utils.RankingNumberResolver.RankingType;
@@ -28,6 +34,7 @@ public final class SettingsManager {
private final FileConfiguration config;
private final File configFile;
private final Map<Class<? extends SCFrame>, FileConfiguration> frameConfigs = new ConcurrentHashMap<>();
public SettingsManager(SimpleClans plugin) {
this.plugin = plugin;
@@ -93,6 +100,7 @@ public final class SettingsManager {
e.printStackTrace();
}
}
frameConfigs.clear();
save();
}
@@ -131,6 +139,30 @@ public final class SettingsManager {
return new Locale(language);
}
public FileConfiguration getConfig(Class<? extends SCFrame> clazz) {
return frameConfigs.computeIfAbsent(clazz, this::readConfig);
}
private FileConfiguration readConfig(Class<? extends SCFrame> clazz) {
String configPath = clazz.getName().replace("net.sacredlabyrinth.phaed.simpleclans.ui.", "")
.replace(".", File.separator) + ".yml";
File externalFile = new File(plugin.getDataFolder(), configPath);
InputStream resource = plugin.getResource(configPath);
YamlConfiguration config = YamlConfiguration.loadConfiguration(externalFile);
if (resource != null) {
YamlConfiguration defaults = YamlConfiguration.loadConfiguration(new InputStreamReader(resource));
config.setDefaults(defaults);
if (!externalFile.exists()) {
try {
defaults.save(externalFile);
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, String.format("Error saving defaults to %s", configPath), e);
}
}
}
return config;
}
public List<Material> getItemList() {
List<Material> itemsList = new ArrayList<>();
for (String material : getStringList(ITEM_LIST)) {
@@ -0,0 +1,60 @@
package net.sacredlabyrinth.phaed.simpleclans.ui;
import net.sacredlabyrinth.phaed.simpleclans.utils.Paginator;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
public abstract class PageableFrame<T> extends SCFrame {
public PageableFrame(@Nullable SCFrame parent, @NotNull Player viewer) {
super(parent, viewer);
}
@Override
@OverridingMethodsMustInvokeSuper
public void createComponents() {
super.createComponents();
SCComponent nextPage = new SCComponentImpl.Builder(getConfig(), "next_page")
.withDisplayNameKey("gui.next.page.title").build();
setOneTimeListener(nextPage, this::nextPage, getPaginator().hasNextPage());
add(nextPage);
SCComponent previousPage = new SCComponentImpl.Builder(getConfig(), "previous_page")
.withDisplayNameKey("gui.previous.page.title").build();
setOneTimeListener(previousPage, this::previousPage, getPaginator().hasPreviousPage());
add(previousPage);
}
public abstract Paginator<T> getPaginator();
public int getPageSize() {
return getConfig().getIntegerList("components.list.slots").size();
}
private void nextPage() {
if (getPaginator().nextPage()) {
update();
}
}
private void previousPage() {
if (getPaginator().previousPage()) {
update();
}
}
private void setOneTimeListener(final SCComponent component, final Runnable runnable, boolean pageCheck) {
if (!pageCheck) {
return;
}
component.setListener(ClickType.LEFT, () -> {
component.setListener(ClickType.LEFT, null);
runnable.run();
});
}
}
@@ -20,6 +20,7 @@ import net.sacredlabyrinth.phaed.simpleclans.RankPermission;
*/
public abstract class SCComponent {
private boolean enabled = true;
private final HashMap<ClickType, Runnable> listeners = new HashMap<>();
private final HashMap<ClickType, Object> permissions = new HashMap<>();
private final Set<ClickType> verified = new HashSet<>();
@@ -31,6 +32,14 @@ public abstract class SCComponent {
public abstract int getSlot();
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Nullable
public ItemMeta getItemMeta() {
return getItem().getItemMeta();
@@ -2,14 +2,20 @@ package net.sacredlabyrinth.phaed.simpleclans.ui;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.sacredlabyrinth.phaed.simpleclans.SimpleClans.lang;
public class SCComponentImpl extends SCComponent {
@@ -59,33 +65,51 @@ public class SCComponentImpl extends SCComponent {
}
public static class Builder {
private final SCComponentImpl component = new SCComponentImpl();
private boolean enabled;
private final ItemStack item;
private String displayName;
private int slot;
private @Nullable List<String> lore;
private Player viewer;
public Builder(@NotNull XMaterial material) {
this(material.parseItem());
}
@SuppressWarnings("unused")
public Builder(@NotNull Material material) {
component.item = new ItemStack(material);
this(new ItemStack(material));
}
public Builder(@Nullable ItemStack item) {
if (item == null) {
item = new ItemStack(Material.STONE);
}
component.item = item;
this.item = item;
}
public Builder(FileConfiguration config, String id) {
String materialName = config.getString("components." + id + ".material");
if (materialName == null) materialName = "";
item = Objects.requireNonNull(XMaterial.matchXMaterial(materialName).orElse(XMaterial.STONE).parseItem());
slot = config.getInt("components." + id + ".slot");
enabled = config.getBoolean("components." + id + ".enabled");
}
public Builder withViewer(@NotNull Player player) {
this.viewer = player;
return this;
}
public Builder withDisplayName(@Nullable String displayName) {
ItemMeta itemMeta = component.getItemMeta();
if (itemMeta != null) {
itemMeta.setDisplayName(displayName);
component.setItemMeta(itemMeta);
}
this.displayName = displayName;
return this;
}
public Builder withDisplayNameKey(@NotNull String key, Object... args) {
return withDisplayName(lang(key, viewer, args));
}
public Builder withLore(@Nullable List<String> lore) {
this.lore = lore;
return this;
@@ -99,18 +123,120 @@ public class SCComponentImpl extends SCComponent {
return this;
}
public Builder withLoreKey(@NotNull String key, Object... args) {
return withLoreLine(lang(key, viewer, args));
}
public Builder withSlot(int slot) {
component.slot = slot;
this.slot = slot;
return this;
}
public SCComponent build() {
ItemMeta itemMeta = component.getItemMeta();
SCComponentImpl component = new SCComponentImpl();
component.item = item;
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta != null) {
itemMeta.setLore(lore);
itemMeta.setDisplayName(displayName);
component.setItemMeta(itemMeta);
}
component.slot = slot;
return component;
}
}
public static class ListBuilder<T> {
private final boolean enabled;
private final XMaterial material;
private Function<T, ItemStack> item;
private final List<Integer> slots;
private final List<T> elements;
private Player viewer;
private Function<T, String> displayName = (t) -> null;
private final List<Function<T, String>> lore = new ArrayList<>();
private final Map<ClickType, Function<T, Runnable>> listeners = new HashMap<>();
private final Map<ClickType, String> permissions = new HashMap<>();
private String lorePermission;
public ListBuilder(@NotNull FileConfiguration config, @NotNull String id, @NotNull List<T> elements) {
String materialName = config.getString("components." + id + ".material");
if (materialName == null) materialName = "";
material = XMaterial.matchXMaterial(materialName).orElse(XMaterial.STONE);
item = (t) -> Objects.requireNonNull(material.parseItem());
slots = config.getIntegerList("components." + id + ".slots");
this.elements = elements;
enabled = config.getBoolean("components." + id + ".enabled");
}
public ListBuilder(@NotNull FileConfiguration config, @NotNull String id) {
this(config, id, new ArrayList<>());
slots.forEach(e -> elements.add(null));
}
public ListBuilder<T> withItem(@NotNull Function<T, ItemStack> item) {
this.item = (t) -> {
ItemStack result = item.apply(t);
return result == null ? material.parseItem() : result;
};
return this;
}
public ListBuilder<T> withViewer(@NotNull Player player) {
viewer = player;
return this;
}
@SafeVarargs
public final ListBuilder<T> withDisplayNameKey(@NotNull String key, Function<T, Object>... args) {
displayName = (t) -> processLang(key, t, args);
return this;
}
@SafeVarargs
public final ListBuilder<T> withLoreKey(@NotNull String key, Function<T, Object>... args) {
lore.add((t) -> processLang(key, t, args));
return this;
}
@NotNull
private String processLang(@NotNull String key, T t, Function<T, Object>[] args) {
Object[] processedArgs = Arrays.stream(args).map(func -> func.apply(t)).toArray();
return lang(key, viewer, processedArgs);
}
public ListBuilder<T> withLorePermission(@Nullable String permission) {
lorePermission = permission;
return this;
}
public ListBuilder<T> withListener(@NotNull ClickType click, @Nullable Function<T, Runnable> runnable, @Nullable String permission) {
permissions.put(click, permission);
listeners.put(click, runnable);
return this;
}
public List<SCComponent> build() {
List<SCComponent> components = new ArrayList<>();
for (int i = 0; i < elements.size() && i < slots.size(); i++) {
T t = elements.get(i);
SCComponentImpl component = new SCComponentImpl();
component.setEnabled(enabled);
component.item = item.apply(t);
component.slot = slots.get(i);
ItemMeta itemMeta = component.getItemMeta();
if (itemMeta != null) {
itemMeta.setDisplayName(displayName.apply(t));
List<String> processedLore = lore.stream().map(f -> f.apply(t)).collect(Collectors.toList());
itemMeta.setLore(processedLore);
component.setItemMeta(itemMeta);
}
component.setLorePermission(lorePermission);
listeners.forEach((click, fn) -> component.setListener(click, fn.apply(t)));
permissions.forEach(component::setPermission);
components.add(component);
}
return components;
}
}
}
@@ -1,12 +1,18 @@
package net.sacredlabyrinth.phaed.simpleclans.ui;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author RoinujNosde
@@ -17,7 +23,7 @@ public abstract class SCFrame {
private final SCFrame parent;
private final Player viewer;
private final Set<SCComponent> components = ConcurrentHashMap.newKeySet();
public SCFrame(@Nullable SCFrame parent, @NotNull Player viewer) {
this.parent = parent;
this.viewer = viewer;
@@ -36,9 +42,25 @@ public abstract class SCFrame {
return parent;
}
public abstract int getSize();
public int getSize() {
return getConfig().getInt("rows") * 9;
}
public abstract void createComponents();
@OverridingMethodsMustInvokeSuper
public void createComponents() {
SCComponent back = new SCComponentImpl.Builder(getConfig(), "back")
.withDisplayNameKey("gui.back.title").build();
back.setListener(ClickType.LEFT, () -> InventoryDrawer.open(getParent()));
add(back);
ConfigurationSection decorSection = getConfig().getConfigurationSection("components.decorations");
if (decorSection != null) {
for (String key : decorSection.getKeys(false)) {
addAll(new SCComponentImpl.ListBuilder<>(getConfig(), "decorations." + key)
.withDisplayNameKey(" ").build());
}
}
}
@Nullable
public SCComponent getComponent(int slot) {
@@ -51,18 +73,37 @@ public abstract class SCFrame {
}
public void add(@NotNull SCComponent c) {
if (!c.isEnabled()) {
return;
}
SCComponent old = getComponent(c.getSlot());
if (old != null) {
components.remove(old);
}
components.add(c);
}
public void addAll(@NotNull Collection<SCComponent> collection) {
collection.forEach(this::add);
}
public void clear() {
components.clear();
}
public void update() {
InventoryDrawer.open(this);
}
@NotNull
public Set<SCComponent> getComponents() {
return components;
}
protected FileConfiguration getConfig() {
return SimpleClans.getInstance().getSettingsManager().getConfig(getClass());
}
@Override
public boolean equals(Object other) {
if (other instanceof SCFrame) {
@@ -1,23 +1,27 @@
package net.sacredlabyrinth.phaed.simpleclans.ui.frames;
import com.cryptomorin.xseries.XMaterial;
import net.sacredlabyrinth.phaed.simpleclans.Helper;
import net.sacredlabyrinth.phaed.simpleclans.Rank;
import net.sacredlabyrinth.phaed.simpleclans.ui.*;
import net.sacredlabyrinth.phaed.simpleclans.ui.InventoryController;
import net.sacredlabyrinth.phaed.simpleclans.ui.PageableFrame;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCComponent;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCComponentImpl.ListBuilder;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCFrame;
import net.sacredlabyrinth.phaed.simpleclans.utils.Paginator;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static net.sacredlabyrinth.phaed.simpleclans.SimpleClans.lang;
public class AddPermissionFrame extends SCFrame {
private final String[] availablePermissions;
private final Paginator paginator;
public class AddPermissionFrame extends PageableFrame<String> {
private final List<String> availablePermissions;
private final Paginator<String> paginator;
private final Rank rank;
public AddPermissionFrame(SCFrame parent, Player viewer, Rank rank) {
@@ -25,63 +29,38 @@ public class AddPermissionFrame extends SCFrame {
this.rank = rank;
Set<String> rankPerms = rank.getPermissions();
availablePermissions = Arrays.stream(Helper.fromPermissionArray()).filter(p -> !rankPerms.contains(p))
.toArray(String[]::new);
paginator = new Paginator(getSize() - 9, availablePermissions.length);
.collect(Collectors.toList());
paginator = new Paginator<>(getPageSize(), availablePermissions);
}
@Override
public void createComponents() {
for (int slot = 0; slot < 9; slot++) {
if (slot == 2 || slot == 6 || slot == 7)
continue;
add(Components.getPanelComponent(slot));
}
add(Components.getBackComponent(getParent(), 2, getViewer()));
add(Components.getPreviousPageComponent(6, this::previousPage, paginator, getViewer()));
add(Components.getNextPageComponent(7, this::nextPage, paginator, getViewer()));
int slot = 9;
for (int i = paginator.getMinIndex(); paginator.isValidIndex(i); i++) {
String permission = availablePermissions[i];
SCComponent c = new SCComponentImpl(
lang("gui.add.permission.permission.title",getViewer(), permission),
Collections.singletonList(lang("gui.add.permission.permission.lore",getViewer())),
XMaterial.PAPER, slot);
c.setListener(ClickType.LEFT, () -> InventoryController.runSubcommand(getViewer(),
"rank permissions add", true, rank.getName(), permission));
c.setPermission(ClickType.LEFT, "simpleclans.leader.rank.permissions.add");
add(c);
slot++;
}
super.createComponents();
List<SCComponent> list = new ListBuilder<>(getConfig(), "list", paginator.getCurrentElements())
.withViewer(getViewer())
.withDisplayNameKey("gui.add.permission.permission.title", p -> p)
.withLoreKey("gui.add.permission.permission.lore")
.withListener(ClickType.LEFT, this::addPermission, "simpleclans.leader.rank.permissions.add")
.build();
addAll(list);
}
private void previousPage() {
if (paginator.previousPage()) {
updateFrame();
}
private Runnable addPermission(String permission) {
return () -> {
availablePermissions.remove(permission);
InventoryController.runSubcommand(getViewer(), "rank permissions add", true,
rank.getName(), permission);
};
}
private void nextPage() {
if (paginator.nextPage()) {
updateFrame();
}
}
private void updateFrame() {
InventoryDrawer.open(this);
@Override
public Paginator<String> getPaginator() {
return paginator;
}
@Override
public @NotNull String getTitle() {
return lang("gui.add.permission.title",getViewer());
}
@Override
public int getSize() {
return 3 * 9;
return lang("gui.add.permission.title", getViewer());
}
}
@@ -1,31 +1,28 @@
package net.sacredlabyrinth.phaed.simpleclans.ui.frames;
import com.cryptomorin.xseries.XMaterial;
import net.sacredlabyrinth.phaed.simpleclans.Clan;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager;
import net.sacredlabyrinth.phaed.simpleclans.ui.InventoryDrawer;
import net.sacredlabyrinth.phaed.simpleclans.ui.PageableFrame;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCComponent;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCComponentImpl;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCComponentImpl.ListBuilder;
import net.sacredlabyrinth.phaed.simpleclans.ui.SCFrame;
import net.sacredlabyrinth.phaed.simpleclans.utils.KDRFormat;
import net.sacredlabyrinth.phaed.simpleclans.utils.Paginator;
import net.sacredlabyrinth.phaed.simpleclans.utils.RankingNumberResolver;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static net.sacredlabyrinth.phaed.simpleclans.SimpleClans.lang;
import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.SHOW_UNVERIFIED_ON_LIST;
public class ClanListFrame extends SCFrame {
public class ClanListFrame extends PageableFrame<Clan> {
private final List<Clan> clans;
private final Paginator paginator;
private final Paginator<Clan> paginator;
private final RankingNumberResolver<Clan, BigDecimal> rankingResolver;
public ClanListFrame(SCFrame parent, Player viewer) {
@@ -34,8 +31,8 @@ public class ClanListFrame extends SCFrame {
SettingsManager sm = plugin.getSettingsManager();
clans = plugin.getClanManager().getClans().stream()
.filter(clan -> clan.isVerified() || sm.is(SHOW_UNVERIFIED_ON_LIST)).collect(Collectors.toList());
paginator = new Paginator(getSize() - 9, clans);
plugin.getClanManager().sortClansByKDR(clans);
paginator = new Paginator<>(getPageSize(), clans);
rankingResolver = new RankingNumberResolver<>(clans, c -> KDRFormat.toBigDecimal(c.getTotalKDR()), false,
sm.getRankingType());
@@ -43,57 +40,32 @@ public class ClanListFrame extends SCFrame {
@Override
public void createComponents() {
super.createComponents();
for (int slot = 0; slot < 9; slot++) {
if (slot == 2 || slot == 6 || slot == 7)
continue;
add(Components.getPanelComponent(slot));
}
add(Components.getBackComponent(getParent(), 2, getViewer()));
add(Components.getPreviousPageComponent(6, this::previousPage, paginator, getViewer()));
add(Components.getNextPageComponent(7, this::nextPage, paginator, getViewer()));
int slot = 9;
for (int i = paginator.getMinIndex(); paginator.isValidIndex(i); i++) {
Clan clan = clans.get(i);
ItemStack banner = clan.getBanner() != null ? clan.getBanner() : XMaterial.BLACK_BANNER.parseItem();
SCComponent c = new SCComponentImpl(
lang("gui.clanlist.clan.title", getViewer(), clan.getColorTag(), clan.getName()),
Arrays.asList(lang("gui.clanlist.clan.lore.position", getViewer(),
rankingResolver.getRankingNumber(clan)),
lang("gui.clanlist.clan.lore.kdr", getViewer(), KDRFormat.format(clan.getTotalKDR())),
lang("gui.clanlist.clan.lore.members", getViewer(), clan.getMembers().size())),
banner, slot);
c.setLorePermission("simpleclans.anyone.list");
add(c);
slot++;
}
List<SCComponent> list = new ListBuilder<>(getConfig(), "list", paginator.getCurrentElements())
.withItem(Clan::getBanner)
.withViewer(getViewer())
.withDisplayNameKey("gui.clanlist.clan.title", Clan::getColorTag, Clan::getName)
.withLoreKey("gui.clanlist.clan.lore.position", rankingResolver::getRankingNumber)
.withLoreKey("gui.clanlist.clan.lore.kdr", clan -> KDRFormat.format(clan.getTotalKDR()))
.withLoreKey("gui.clanlist.clan.lore.members", clan -> clan.getMembers().size())
.withLorePermission("simpleclans.anyone.list").build();
addAll(list);
}
private void previousPage() {
if (paginator.previousPage()) {
updateFrame();
}
}
private void nextPage() {
if (paginator.nextPage()) {
updateFrame();
}
}
private void updateFrame() {
InventoryDrawer.open(this);
@Override
public Paginator<Clan> getPaginator() {
return paginator;
}
@Override
public @NotNull String getTitle() {
return lang("gui.clanlist.title",getViewer(), clans.size());
}
@Override
public int getSize() {
return 6 * 9;
return lang("gui.clanlist.title", getViewer(), clans.size());
}
}
@@ -161,7 +161,7 @@ public class Components {
return new SCComponentImpl(" ", null, XMaterial.GRAY_STAINED_GLASS_PANE, slot);
}
public static @NotNull SCComponent getPreviousPageComponent(int slot, @Nullable Runnable listener, @NotNull Paginator paginator, @NotNull Player viewer) {
public static @NotNull SCComponent getPreviousPageComponent(int slot, @Nullable Runnable listener, @NotNull Paginator<?> paginator, @NotNull Player viewer) {
if (!paginator.hasPreviousPage()) {
return getPanelComponent(slot);
}
@@ -171,7 +171,7 @@ public class Components {
return c;
}
public static @NotNull SCComponent getNextPageComponent(int slot, @Nullable Runnable listener, @NotNull Paginator paginator, @NotNull Player viewer) {
public static @NotNull SCComponent getNextPageComponent(int slot, @Nullable Runnable listener, @NotNull Paginator<?> paginator, @NotNull Player viewer) {
if (!paginator.hasNextPage()) {
return getPanelComponent(slot);
}
@@ -1,10 +1,8 @@
package net.sacredlabyrinth.phaed.simpleclans.ui.frames.staff;
import com.cryptomorin.xseries.XMaterial;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager;
import net.sacredlabyrinth.phaed.simpleclans.ui.*;
import net.sacredlabyrinth.phaed.simpleclans.ui.frames.Components;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
@@ -32,13 +30,7 @@ public class StaffFrame extends SCFrame {
@Override
public void createComponents() {
for (int slot = 0; slot < 9; slot++) {
if (slot == 4)
continue;
add(Components.getPanelComponent(slot));
}
add(Components.getBackComponent(getParent(), 4, getViewer()));
super.createComponents();
addClans();
addPlayers();
addGlobalFf();
@@ -46,10 +38,10 @@ public class StaffFrame extends SCFrame {
}
private void addClans() {
SCComponent clanList = new SCComponentImpl.Builder(XMaterial.PURPLE_BANNER)
.withDisplayName(lang("gui.main.clan.list.title", getViewer())).withSlot(9)
.withLoreLine(lang("gui.staff.clan.list.lore.left.click", getViewer()))
.withLoreLine(lang("gui.staff.clan.list.lore.right.click", getViewer())).build();
SCComponent clanList = new SCComponentImpl.Builder(getConfig(), "clans").withViewer(getViewer())
.withDisplayNameKey("gui.main.clan.list.title")
.withLoreKey("gui.staff.clan.list.lore.left.click")
.withLoreKey("gui.staff.clan.list.lore.right.click").build();
clanList.setListener(ClickType.LEFT, () -> InventoryDrawer.open(new ClanListFrame(this, getViewer(),
Type.ALL, null)));
clanList.setListener(ClickType.RIGHT, () -> InventoryDrawer.open(new ClanListFrame(this, getViewer(),
@@ -58,19 +50,18 @@ public class StaffFrame extends SCFrame {
}
private void addPlayers() {
SCComponent players = new SCComponentImpl.Builder(XMaterial.WHITE_BANNER)
.withDisplayName(lang("gui.staff.player.list.title", getViewer())).withSlot(10)
.withLoreLine(lang("gui.staff.player.list.lore.left.click", getViewer()))
.withLoreLine(lang("gui.staff.player.list.lore.right.click", getViewer())).build();
SCComponent players = new SCComponentImpl.Builder(getConfig(), "players").withViewer(getViewer())
.withDisplayNameKey("gui.staff.player.list.title")
.withLoreKey("gui.staff.player.list.lore.left.click")
.withLoreKey("gui.staff.player.list.lore.right.click").build();
players.setListener(ClickType.LEFT, () -> InventoryDrawer.open(new PlayerListFrame(getViewer(), this, false)));
players.setListener(ClickType.RIGHT, () -> InventoryDrawer.open(new PlayerListFrame(getViewer(), this, true)));
add(players);
}
private void addReload() {
SCComponent reload = new SCComponentImpl.Builder(XMaterial.SPAWNER)
.withDisplayName(lang("gui.staff.reload.title", getViewer())).withSlot(17)
.withLoreLine(lang("gui.staff.reload.lore", getViewer())).build();
SCComponent reload = new SCComponentImpl.Builder(getConfig(), "reload").withViewer(getViewer())
.withDisplayNameKey("gui.staff.reload.title").withLoreKey("gui.staff.reload.lore").build();
reload.setPermission(ClickType.LEFT, "simpleclans.admin.reload");
reload.setConfirmationRequired(ClickType.LEFT);
reload.setListener(ClickType.LEFT, () ->
@@ -82,18 +73,14 @@ public class StaffFrame extends SCFrame {
SettingsManager sm = SimpleClans.getInstance().getSettingsManager();
boolean globalffAllowed = sm.is(GLOBAL_FRIENDLY_FIRE);
String status = globalffAllowed ? lang("allowed", getViewer()) : lang("auto", getViewer());
SCComponent globalFf = new SCComponentImpl.Builder(XMaterial.DIAMOND_SWORD).withSlot(12)
.withDisplayName(lang("gui.staff.global.ff.title", getViewer()))
.withLoreLine(lang("gui.staff.global.ff.lore.status", getViewer(), status))
.withLoreLine(lang("gui.staff.global.ff.lore.toggle", getViewer())).build();
SCComponent globalFf = new SCComponentImpl.Builder(getConfig(), "global_ff").withViewer(getViewer())
.withDisplayNameKey("gui.staff.global.ff.title")
.withLoreKey("gui.staff.global.ff.lore.status", status)
.withLoreKey("gui.staff.global.ff.lore.toggle").build();
globalFf.setPermission(ClickType.LEFT, "simpleclans.mod.globalff");
globalFf.setListener(ClickType.LEFT, () -> {
String arg;
if (globalffAllowed) {
arg = "auto";
} else {
arg = "allow";
}
String arg = globalffAllowed ? "auto" : "allow";
InventoryController.runSubcommand(getViewer(), "mod globalff", true, arg);
});
add(globalFf);
@@ -2,19 +2,21 @@ package net.sacredlabyrinth.phaed.simpleclans.utils;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
* @author RoinujNosde
*
*/
public class Paginator {
public class Paginator<T> {
private int currentPage;
private final int sizePerPage;
private int totalElements;
private Collection<?> collection;
private List<T> collection;
public Paginator(int sizePerPage, int totalElements) {
if (sizePerPage < 1) {
@@ -27,12 +29,17 @@ public class Paginator {
this.totalElements = totalElements;
}
public Paginator(int sizePerPage, @NotNull Collection<?> collection) {
@Deprecated
public Paginator(int sizePerPage, @NotNull Collection<T> collection) {
this(sizePerPage, new ArrayList<>(collection));
}
public Paginator(int sizePerPage, @NotNull List<T> list) {
if (sizePerPage < 1) {
throw new IllegalArgumentException("sizePerPage cannot be less than 1");
}
this.sizePerPage = sizePerPage;
this.collection = collection;
this.collection = list;
}
/**
@@ -87,18 +94,32 @@ public class Paginator {
public int getCurrentPage() {
return currentPage;
}
/**
*
* @return the elements in the current page
*
* @throws IllegalStateException if the underlining collection is null
*/
public @NotNull List<T> getCurrentElements() throws IllegalStateException {
if (collection == null) {
throw new IllegalStateException("the collection is null");
}
int maxIndex = Math.min(getMaxIndex(), collection.size());
return collection.subList(getMinIndex(), maxIndex);
}
/**
* Increases the page number if there are elements to display
*
* @author RoinujNosde
*/
public boolean nextPage() {
if (!hasNextPage()) {
return false;
boolean hasNext = hasNextPage();
if (hasNext) {
currentPage++;
}
currentPage++;
return true;
return hasNext;
}
/**
@@ -115,11 +136,11 @@ public class Paginator {
* @author RoinujNosde
*/
public boolean previousPage() {
if (hasPreviousPage()) {
boolean hasPrevious = hasPreviousPage();
if (hasPrevious) {
currentPage--;
return true;
}
return false;
return hasPrevious;
}
/**
@@ -0,0 +1,24 @@
rows: 3
components:
list:
enabled: true
slots: [ 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26 ]
material: PAPER
next_page:
enabled: true
slot: 7
material: STONE_BUTTON
previous_page:
enabled: true
slot: 6
material: STONE_BUTTON
back:
enabled: true
slot: 2
material: ARROW
decorations:
panels:
enabled: true
slots: [ 0, 1, 3, 4, 5, 8 ]
material: GRAY_STAINED_GLASS_PANE
@@ -0,0 +1,21 @@
rows: 6
components:
list:
slots: [ 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53 ]
material: BLACK_BANNER
next_page:
enabled: true
slot: 7
material: STONE_BUTTON
previous_page:
enabled: true
slot: 6
material: STONE_BUTTON
back:
enabled: true
slot: 2
material: ARROW
@@ -0,0 +1,27 @@
rows: 3
components:
decorations:
panels:
enabled: true
slots: [ 0, 1, 2, 3, 5, 6, 7, 8 ]
material: GRAY_STAINED_GLASS_PANE
clans:
enabled: true
material: PURPLE_BANNER
slot: 9
players:
enabled: true
material: WHITE_BANNER
slot: 10
reload:
enabled: true
material: SPAWNER
slot: 17
global_ff:
enabled: true
material: DIAMOND_SWORD
slot: 12
back:
enabled: true
slot: 4
material: ARROW