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
5 changes: 2 additions & 3 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
3 changes: 1 addition & 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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);
if (runnable == null) {
return;
}
Tomut0 marked this conversation as resolved.
Show resolved Hide resolved
runnable.run();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

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 {

Expand Down Expand Up @@ -59,33 +65,47 @@ public int getSlot() {
}

public static class Builder {
private final SCComponentImpl component = new SCComponentImpl();
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());
}

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");
item = Objects.requireNonNull(XMaterial.matchXMaterial(materialName).orElse(XMaterial.STONE).parseItem());
slot = config.getInt("components." + id + ".slot");
}

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;
Expand All @@ -99,18 +119,118 @@ public Builder withLoreLine(@NotNull String line) {
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 XMaterial material;
private Function<T, ItemStack> item;
private final List<Integer> slots;
private 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<ClickType, Function<T, Runnable>>();
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");
material = XMaterial.matchXMaterial(materialName).orElse(XMaterial.STONE);
item = (t) -> Objects.requireNonNull(material.parseItem());
slots = config.getIntegerList("components." + id + ".slots");
this.elements = elements;
}

public ListBuilder<T> withItem(@NotNull Function<T, ItemStack> item) {
this.item = (t) -> {
ItemStack result = item.apply(t);
if (result == null) {
return material.parseItem();
}
return result;
RoinujNosde marked this conversation as resolved.
Show resolved Hide resolved
};
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 = new Object[args.length];
for (int i = 0; i < args.length; i++) {
processedArgs[i] = args[i].apply(t);
}
return lang(key, viewer, processedArgs);
RoinujNosde marked this conversation as resolved.
Show resolved Hide resolved
}

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.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;
}
}
}
Loading