From 4a960d9f2c71fc2631ef5e8750d8e42a7281a36a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 6 Aug 2020 12:23:47 +0100 Subject: [PATCH] update pagination to new json --- .../com/plotsquared/core/command/Area.java | 47 +++++++------- .../com/plotsquared/core/command/Command.java | 61 +++++++++++-------- .../plotsquared/core/command/DebugExec.java | 20 +++--- .../com/plotsquared/core/command/ListCmd.java | 20 +++--- .../configuration/caption/CaptionHolder.java | 41 +++++++++++++ .../plot/flag/types/BlockTypeListFlag.java | 2 +- Core/src/main/resources/lang/messages_en.json | 10 ++- 7 files changed, 129 insertions(+), 72 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionHolder.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Area.java b/Core/src/main/java/com/plotsquared/core/command/Area.java index f480a41f8..eef58e03a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Area.java +++ b/Core/src/main/java/com/plotsquared/core/command/Area.java @@ -31,6 +31,8 @@ import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.ConfigurationSection; import com.plotsquared.core.configuration.ConfigurationUtil; import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.configuration.caption.CaptionHolder; +import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.file.YamlConfiguration; @@ -73,6 +75,7 @@ import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.Template; import javax.annotation.Nonnull; @@ -596,7 +599,7 @@ public class Area extends SubCommand { Template regionTemplate = Template.of("name", region); Template generatorTemplate = Template.of("name", generator); Template footerTemplate = Template.of("name", TranslatableCaption.of("info.plot_info_footer").getComponent(player)); - player.sendMessage(TranslatableCaption.of("info.area_format"), headerTemplate, nameTemplate, typeTemplate, terrainTemplate, + player.sendMessage(TranslatableCaption.of("info.area_info_format"), headerTemplate, nameTemplate, typeTemplate, terrainTemplate, usageTemplate, claimedTemplate, clustersTemplate, regionTemplate, generatorTemplate, footerTemplate); return true; } @@ -627,8 +630,8 @@ public class Area extends SubCommand { } final List areas = new ArrayList<>(Arrays.asList(this.plotAreaManager.getAllPlotAreas())); paginate(player, areas, 8, page, - new RunnableVal3() { - @Override public void run(Integer i, PlotArea area, Caption message) { + new RunnableVal3() { + @Override public void run(Integer i, PlotArea area, CaptionHolder caption) { String name; double percent; int claimed = area.getPlotCount(); @@ -645,27 +648,29 @@ public class Area extends SubCommand { region = area.getRegion().toString(); } else { name = area.getWorldName(); - percent = claimed == 0 ? - 0 : - Short.MAX_VALUE * Short.MAX_VALUE / (double) claimed; + percent = claimed == 0 ? 0 : Short.MAX_VALUE * Short.MAX_VALUE / (double) claimed; region = "N/A"; } - PlotMessage tooltip = new PlotMessage().text("Claimed=").color("$1") - .text(String.valueOf(claimed)).color("$2").text("\nUsage=") - .color("$1").text(String.format("%.2f", percent) + '%').color("$2") - .text("\nClusters=").color("$1").text(String.valueOf(clusters)) - .color("$2").text("\nRegion=").color("$1").text(region).color("$2") - .text("\nGenerator=").color("$1").text(generator).color("$2"); - - // type / terrain - String visit = "/plot area tp " + area.toString(); - message.text("[").color("$3").text(String.valueOf(i)).command(visit) - .tooltip(visit).color("$1").text("]").color("$3").text(' ' + name) - .tooltip(tooltip).command(getCommandString() + " info " + area) - .color("$1").text(" - ").color("$2") - .text(area.getType() + ":" + area.getTerrain()).color("$3"); + Template claimedTemplate = Template.of("claimed", String.valueOf(claimed)); + Template usageTemplate = Template.of("usage", String.format("%.2f", percent) + "%"); + Template clustersTemplate = Template.of("clusters", String.valueOf(clusters)); + Template regionTemplate = Template.of("region", region); + Template generatorTemplate = Template.of("generator", generator); + String tooltip = MINI_MESSAGE.serialize(MINI_MESSAGE + .parse(TranslatableCaption.of("info.area_list_tooltip").getComponent(player), claimedTemplate, usageTemplate, + clustersTemplate, regionTemplate, generatorTemplate)); + Template tooltipTemplate = Template.of("hover_info", tooltip); + Template visitcmdTemplate = Template.of("command_tp", "/plot area tp " + area.toString()); + Template numberTemplate = Template.of("number", String.valueOf(i)); + Template nameTemplate = Template.of("area_name", name); + Template typeTemplate = Template.of("area_type", area.getType().name()); + Template terrainTemplate = Template.of("area_terrain", area.getTerrain().name()); + Caption item = TranslatableCaption.of("info.area_list_item"); + caption.set(StaticCaption.of(MINI_MESSAGE.serialize(MINI_MESSAGE + .parse(item.getComponent(player), tooltipTemplate, visitcmdTemplate, numberTemplate, nameTemplate, typeTemplate, + terrainTemplate)))); } - }, "/plot area list", Captions.AREA_LIST_HEADER_PAGED.getTranslated()); + }, "/plot area list", TranslatableCaption.of("list.area_list_header_paged")); return true; case "regen": case "clear": diff --git a/Core/src/main/java/com/plotsquared/core/command/Command.java b/Core/src/main/java/com/plotsquared/core/command/Command.java index 3f4adbd97..dbb872e24 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Command.java +++ b/Core/src/main/java/com/plotsquared/core/command/Command.java @@ -26,9 +26,10 @@ package com.plotsquared.core.command; import com.plotsquared.core.configuration.caption.Caption; -import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.caption.CaptionHolder; import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.permissions.PermissionHolder; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.Permissions; @@ -36,7 +37,7 @@ import com.plotsquared.core.util.StringComparison; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; -import com.plotsquared.core.permissions.PermissionHolder; +import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.Template; import javax.annotation.Nullable; @@ -55,6 +56,8 @@ import java.util.concurrent.CompletableFuture; public abstract class Command { + static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); + // May be none private final ArrayList allCommands = new ArrayList<>(); private final ArrayList dynamicCommands = new ArrayList<>(); @@ -82,7 +85,7 @@ public abstract class Command { this.permission = permission; this.required = required; this.category = category; - this.aliases = Arrays.asList(id); + this.aliases = Collections.singletonList(id); if (this.parent != null) { this.parent.register(this); } @@ -98,7 +101,7 @@ public abstract class Command { for (final Method method : getClass().getDeclaredMethods()) { if (method.isAnnotationPresent(CommandDeclaration.class)) { Class[] types = method.getParameterTypes(); - // final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 + // final PlotPlayer player, String[] args, RunnableVal3 confirm, RunnableVal2 // whenDone if (types.length == 5 && types[0] == Command.class && types[1] == PlotPlayer.class && types[2] == String[].class && types[3] == RunnableVal3.class @@ -138,7 +141,7 @@ public abstract class Command { return this.id; } - public List getCommands(PlotPlayer player) { + public List getCommands(PlotPlayer player) { List commands = new ArrayList<>(); for (Command cmd : this.allCommands) { if (cmd.canExecute(player, false)) { @@ -148,7 +151,7 @@ public abstract class Command { return commands; } - public List getCommands(CommandCategory category, PlotPlayer player) { + public List getCommands(CommandCategory category, PlotPlayer player) { List commands = getCommands(player); if (category != null) { commands.removeIf(command -> command.category != category); @@ -226,12 +229,12 @@ public abstract class Command { } public void paginate(PlotPlayer player, List c, int size, int page, - RunnableVal3 add, String baseCommand, String header) { + RunnableVal3 add, String baseCommand, Caption header) { // Calculate pages & index if (page < 0) { page = 0; } - int totalPages = (int) Math.ceil(c.size() / size); + int totalPages = (int) Math.ceil((double) c.size() / size); if (page > totalPages) { page = totalPages; } @@ -240,35 +243,39 @@ public abstract class Command { max = c.size(); } // Send the header - header = header.replaceAll("%cur", page + 1 + "").replaceAll("%max", totalPages + 1 + "") - .replaceAll("%amount%", c.size() + "").replaceAll("%word%", "all"); - player.sendMessage(StaticCaption.of(header)); + Template curTemplate = Template.of("cur", String.valueOf(page + 1)); + Template maxTemplate = Template.of("max", String.valueOf(totalPages + 1)); + Template amountTemplate = Template.of("amount", String.valueOf(c.size())); + player.sendMessage(header, curTemplate, maxTemplate, amountTemplate); // Send the page content List subList = c.subList(page * size, max); int i = page * size; for (T obj : subList) { i++; - PlotMessage msg = new PlotMessage(); + CaptionHolder msg = new CaptionHolder(); add.run(i, obj, msg); - msg.send(player); + player.sendMessage(msg.get()); } // Send the footer if (page < totalPages && page > 0) { // Back | Next - new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ") - .color("$3").text("->").color("$1").command(baseCommand + " " + (page + 2)) - .text(Captions.CLICKABLE.getTranslated()).color("$2").send(player); + Template command1 = Template.of("command1", baseCommand + " " + page); + Template command2 = Template.of("command2", baseCommand + " " + (page + 2)); + Template clickable = Template.of("clickable", TranslatableCaption.of("list.clickable").getComponent(player)); + player.sendMessage(TranslatableCaption.of("list.page_turn"), command1, command2, clickable); return; } if (page == 0 && totalPages != 0) { // Next - new PlotMessage().text("<-").color("$3").text(" | ").color("$3").text("->").color("$1") - .command(baseCommand + " " + 2).text(Captions.CLICKABLE.getTranslated()).color("$2") - .send(player); + Template command1 = Template.of("command1", ""); + Template command2 = Template.of("command2", baseCommand + " " + (page + 2)); + Template clickable = Template.of("clickable", TranslatableCaption.of("list.clickable").getComponent(player)); + player.sendMessage(TranslatableCaption.of("list.page_turn"), command1, command2, clickable); return; } if (totalPages != 0) { // Back - new PlotMessage().text("<-").color("$1").command(baseCommand + " " + page).text(" | ") - .color("$3").text("->").color("$3").text(Captions.CLICKABLE.getTranslated()) - .color("$2").send(player); + Template command1 = Template.of("command1", ""); + Template command2 = Template.of("command2", ""); + Template clickable = Template.of("clickable", TranslatableCaption.of("list.clickable").getComponent(player)); + player.sendMessage(TranslatableCaption.of("list.page_turn"), command1, command2, clickable); } } @@ -351,7 +358,7 @@ public abstract class Command { return CompletableFuture.completedFuture(true); } - public boolean checkArgs(PlotPlayer player, String[] args) { + public boolean checkArgs(PlotPlayer player, String[] args) { Argument[] reqArgs = getRequiredArguments(); if (reqArgs != null && reqArgs.length > 0) { boolean failed = args.length < reqArgs.length; @@ -446,7 +453,7 @@ public abstract class Command { return null; } - public boolean canExecute(PlotPlayer player, boolean message) { + public boolean canExecute(PlotPlayer player, boolean message) { if (player == null) { return true; } @@ -502,7 +509,7 @@ public abstract class Command { return getCommandString() + " " + args + "]"; } - public Collection tabOf(PlotPlayer player, String[] input, boolean space, + public Collection tabOf(PlotPlayer player, String[] input, boolean space, String... args) { if (!space) { return null; @@ -523,7 +530,7 @@ public abstract class Command { return result; } - public Collection tab(PlotPlayer player, String[] args, boolean space) { + public Collection tab(PlotPlayer player, String[] args, boolean space) { switch (args.length) { case 0: return this.allCommands; @@ -537,7 +544,7 @@ public abstract class Command { return null; } } else { - Set commands = new HashSet(); + Set commands = new HashSet<>(); for (Map.Entry entry : this.staticCommands.entrySet()) { if (entry.getKey().startsWith(arg) && entry.getValue() .canExecute(player, false)) { diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java index 926aa776a..e5ee52e6d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java @@ -27,10 +27,10 @@ package com.plotsquared.core.command; import com.google.common.io.Files; import com.google.inject.Inject; -import com.google.inject.internal.cglib.transform.$ClassTransformer; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.CaptionHolder; import com.plotsquared.core.configuration.caption.StaticCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; @@ -414,15 +414,15 @@ public class DebugExec extends SubCommand { } List allFiles = Arrays.asList(filesArray); - paginate(player, allFiles, 8, page, - new RunnableVal3() { - - @Override public void run(Integer i, File file, PlotMessage message) { - String name = file.getName(); - message.text("[").color("$3").text(String.valueOf(i)).color("$1") - .text("]").color("$3").text(' ' + name).color("$1"); - } - }, "/plot debugexec list-scripts", "List of scripts"); + paginate(player, allFiles, 8, page, new RunnableVal3() { + @Override public void run(Integer i, File file, CaptionHolder message) { + String name = file.getName(); + Template numTemplate = Template.of("number", String.valueOf(i)); + Template nameTemplate = Template.of("name", name); + message.set(StaticCaption.of(MINI_MESSAGE.serialize(MINI_MESSAGE + .parse(TranslatableCaption.of("debugexec.script_list_item").getComponent(player), numTemplate, nameTemplate)))); + } + }, "/plot debugexec list-scripts", StaticCaption.of("List of scripts")); return true; case "all": if (args.length < 3) { diff --git a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java index 592ca5c65..eab416c8f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java @@ -29,6 +29,7 @@ import com.google.inject.Inject; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.CaptionHolder; import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.player.PlotPlayer; @@ -47,6 +48,7 @@ import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.TabCompletions; import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.query.SortingStrategy; +import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.uuid.UUIDMapping; import net.kyori.adventure.text.minimessage.Template; @@ -356,12 +358,12 @@ public class ListCmd extends SubCommand { return true; } - public void displayPlots(final PlotPlayer player, List plots, int pageSize, int page, String[] args) { + public void displayPlots(final PlotPlayer player, List plots, int pageSize, int page, String[] args) { // Header plots.removeIf(plot -> !plot.isBasePlot()); this.paginate(player, plots, pageSize, page, - new RunnableVal3() { - @Override public void run(Integer i, Plot plot, PlotMessage message) { + new RunnableVal3() { + @Override public void run(Integer i, Plot plot, CaptionHolder caption) { String color; if (plot.getOwner() == null) { color = "$3"; @@ -420,10 +422,10 @@ public class ListCmd extends SubCommand { player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout")); } } - }, "/plot list " + args[0], Captions.PLOT_LIST_HEADER_PAGED.getTranslated()); + }, "/plot list " + args[0], TranslatableCaption.of("list.plot_list_header_paged")); } - @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { + @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { final List completions = new LinkedList<>(); if (this.econHandler != null && Permissions .hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) { @@ -454,11 +456,9 @@ public class ListCmd extends SubCommand { completions.add("expired"); } - final List commands = new LinkedList<>(); - commands.addAll(completions.stream() - .filter(completion -> completion.toLowerCase().startsWith(args[0].toLowerCase())) - .map(completion -> new Command(null, true, completion, "", RequiredType.NONE, CommandCategory.TELEPORT) {}) - .collect(Collectors.toList())); + final List commands = completions.stream().filter(completion -> completion.toLowerCase().startsWith(args[0].toLowerCase())) + .map(completion -> new Command(null, true, completion, "", RequiredType.NONE, CommandCategory.TELEPORT) { + }).collect(Collectors.toCollection(LinkedList::new)); if (Permissions.hasPermission(player, Captions.PERMISSION_LIST_PLAYER) && args[0].length() > 0) { commands.addAll(TabCompletions.completePlayers(args[0], Collections.emptyList())); diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionHolder.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionHolder.java new file mode 100644 index 000000000..bf6477235 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionHolder.java @@ -0,0 +1,41 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) ${year} IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.plotsquared.core.configuration.caption; + +public class CaptionHolder { + + private Caption caption = StaticCaption.of(""); + + public void set(Caption caption) { + this.caption = caption; + } + + public Caption get() { + return this.caption; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeListFlag.java b/Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeListFlag.java index 196a80909..f51b9d4bc 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeListFlag.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/types/BlockTypeListFlag.java @@ -46,7 +46,7 @@ public abstract class BlockTypeListFlag> public static boolean skipCategoryVerification = false; protected BlockTypeListFlag(List blockTypeList, Caption description) { - super(blockTypeList, Captions.FLAG_CATEGORY_BLOCK_LIST, description); + super(blockTypeList, TranslatableCaption.of("flags.flag_category_block_list"), description); } @Override public F parse(@Nonnull String input) throws FlagParseException { diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index a674b879f..18d7591e6 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -280,6 +280,7 @@ "debugexec.task_not_running": "Task not running.", "debugexec.expiry_started": "Started plot expiry task.", "debugexec.expiry_already_started": "Plot expiry task already started.", + "debugexec.script_list_item": "[] ", "debugimportworlds.single_plot_area": "Must be a single plot area.", "debugimportworlds.world_container": "World container must be configured to be a separate directory to your base files.", @@ -375,8 +376,10 @@ "info.plot_no_description": "No description set.", "info.plot_caps_header": "--------- CAPS ---------", "info.plot_caps_format": "- Cap Type: | Status: / (%)", - "info.area_format": "
\nNAME: \nType: \nTerrain: \nUsage: %\nClaimed: \nClusters: \nRegion: \nGenerator: \n