3
0
Mirror von https://github.com/TheSilentPro/HeadDB.git synchronisiert 2024-12-27 11:20:05 +01:00

update anvilgui methods

Dieser Commit ist enthalten in:
Lennart Lösche 2023-07-18 12:11:14 +02:00
Ursprung 97aaefcc43
Commit 815febd79d

Datei anzeigen

@ -1,6 +1,8 @@
package tsp.headdb.core.command; package tsp.headdb.core.command;
import java.util.Arrays;
import net.wesjd.anvilgui.AnvilGUI; import net.wesjd.anvilgui.AnvilGUI;
import net.wesjd.anvilgui.AnvilGUI.Builder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -61,25 +63,24 @@ public class CommandMain extends HeadDBCommand implements CommandExecutor, TabCo
if (e.isLeftClick()) { if (e.isLeftClick()) {
Bukkit.dispatchCommand(e.getWhoClicked(), "hdb open " + category.getName()); Bukkit.dispatchCommand(e.getWhoClicked(), "hdb open " + category.getName());
} else if (e.isRightClick()) { } else if (e.isRightClick()) {
new AnvilGUI.Builder() new AnvilGUI.Builder().onClick((slot, stateSnapshot) -> {
.onComplete((p, text) -> { try {
try { int page = Integer.parseInt(stateSnapshot.getText());
int page = Integer.parseInt(text); // to be replaced with own version of anvil-gui
// to be replaced with own version of anvil-gui List<Head> heads = HeadAPI.getHeads(category);
List<Head> heads = HeadAPI.getHeads(category); PagedPane main = Utils.createPaged(player, Utils.translateTitle(getLocalization().getMessage(player.getUniqueId(), "menu.category.name").orElse(category.getName()), heads.size(), category.getName()));
PagedPane main = Utils.createPaged(player, Utils.translateTitle(getLocalization().getMessage(player.getUniqueId(), "menu.category.name").orElse(category.getName()), heads.size(), category.getName())); Utils.addHeads(player, category, main, heads);
Utils.addHeads(player, category, main, heads); main.selectPage(page);
main.selectPage(page); main.reRender();
main.reRender(); return Arrays.asList(AnvilGUI.ResponseAction.openInventory(main.getInventory()));
return AnvilGUI.Response.openInventory(main.getInventory()); } catch (NumberFormatException nfe) {
} catch (NumberFormatException nfe) { return Arrays.asList(AnvilGUI.ResponseAction.replaceInputText("Invalid number!"));
return AnvilGUI.Response.text("Invalid number!"); }
} })
}) .text("Query")
.text("Query") .title(StringUtils.colorize(getLocalization().getMessage(player.getUniqueId(), "menu.main.category.page.name").orElse("Enter page")))
.title(StringUtils.colorize(getLocalization().getMessage(player.getUniqueId(), "menu.main.category.page.name").orElse("Enter page"))) .plugin(getInstance())
.plugin(getInstance()) .open(player);
.open(player);
} }
})); }));
} }
@ -100,28 +101,28 @@ public class CommandMain extends HeadDBCommand implements CommandExecutor, TabCo
pane.setButton(getInstance().getConfig().getInt("gui.main.meta.search.slot"), new Button(Utils.getItemFromConfig("gui.main.meta.search.item", Material.DARK_OAK_SIGN), e -> { pane.setButton(getInstance().getConfig().getInt("gui.main.meta.search.slot"), new Button(Utils.getItemFromConfig("gui.main.meta.search.item", Material.DARK_OAK_SIGN), e -> {
e.setCancelled(true); e.setCancelled(true);
new AnvilGUI.Builder() new AnvilGUI.Builder()
.onComplete((p, query) -> { .onClick((slot, stateSnapshot) -> {
// Copied from CommandSearch // Copied from CommandSearch
List<Head> heads = new ArrayList<>(); List<Head> heads = new ArrayList<>();
List<Head> headList = HeadAPI.getHeads(); List<Head> headList = HeadAPI.getHeads();
if (query.length() > 3) { if (stateSnapshot.getText().length() > 3) {
if (query.startsWith("id:")) { if (stateSnapshot.getText().startsWith("id:")) {
try { try {
HeadAPI.getHeadById(Integer.parseInt(query.substring(3))).ifPresent(heads::add); HeadAPI.getHeadById(Integer.parseInt(stateSnapshot.getText().substring(3))).ifPresent(heads::add);
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} else if (query.startsWith("tg:")) { } else if (stateSnapshot.getText().startsWith("tg:")) {
heads.addAll(headList.stream().filter(head -> Utils.matches(head.getTags(), query.substring(3))).toList()); heads.addAll(headList.stream().filter(head -> Utils.matches(head.getTags(), stateSnapshot.getText().substring(3))).toList());
} else { } else {
// no query prefix // no query prefix
heads.addAll(headList.stream().filter(head -> Utils.matches(head.getName(), query)).toList()); heads.addAll(headList.stream().filter(head -> Utils.matches(head.getName(), stateSnapshot.getText())).toList());
} }
} else { } else {
// query is <=3, no point in looking for prefixes // query is <=3, no point in looking for prefixes
heads.addAll(headList.stream().filter(head -> Utils.matches(head.getName(), query)).toList()); heads.addAll(headList.stream().filter(head -> Utils.matches(head.getName(), stateSnapshot.getText())).toList());
} }
PagedPane main = Utils.createPaged(player, Utils.translateTitle(getLocalization().getMessage(player.getUniqueId(), "menu.search.name").orElse("&cHeadDB - &eSearch Results"), heads.size(), "None", query)); PagedPane main = Utils.createPaged(player, Utils.translateTitle(getLocalization().getMessage(player.getUniqueId(), "menu.search.name").orElse("&cHeadDB - &eSearch Results"), heads.size(), "None", stateSnapshot.getText()));
Utils.addHeads(player, null, main, heads); Utils.addHeads(player, null, main, heads);
main.reRender(); main.reRender();
return AnvilGUI.Response.openInventory(main.getInventory()); return AnvilGUI.Response.openInventory(main.getInventory());