Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable GUI #328

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions SimpleClans.iml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
15 changes: 7 additions & 8 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -93,6 +100,7 @@ public void loadAndSave() {
e.printStackTrace();
}
}
frameConfigs.clear();

save();
}
Expand Down Expand Up @@ -131,6 +139,30 @@ public Locale getLanguage() {
return new Locale(language);
}

public FileConfiguration getConfig(Class<? extends SCFrame> clazz) {
return frameConfigs.computeIfAbsent(clazz, this::readConfig);
}

private FileConfiguration readConfig(Class<? extends SCFrame> clazz) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method readConfig has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.

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) {
Tomut0 marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
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<>();
Expand All @@ -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();
Expand Down
Loading