From afc34404866f39c16102b6edb6e8e8c15a5e5507 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 8 Sep 2022 20:30:36 +0200 Subject: [PATCH 1/2] Add Page Command --- .../commands/SchematicCommand.java | 15 ++++++---- .../commands/SchematicCommandUtils.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index 42a9f8a..e21ee72 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -85,7 +85,7 @@ public class SchematicCommand extends SWCommand { @Register("list") @Register({"list", "/"}) public void schemList(Player player, @OptionalValue(value = "1", onlyUINIG = true) int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), Math.max(page - 1, 0), null, SchematicListBehavior.DEFAULT); + createCachedSchemList(player, SchematicNode.getSchematicsAccessibleByUser(getUser(player).getId(), 0), Math.max(page - 1, 0), null, SchematicListBehavior.DEFAULT); } @Register({"list", "public"}) @@ -96,13 +96,13 @@ public class SchematicCommand extends SWCommand { @Register({"list", "public"}) @Register({"list", "public", "/"}) public void schemListPublic(Player player, @OptionalValue(value = "1", onlyUINIG = true) int page) { - renderSchemlist(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), Math.max(page - 1, 0), null, SchematicListBehavior.builder().setPublics(true).setPageCommandGen(integer -> "/schem list public " + integer).build()); + createCachedSchemList(player, SchematicNode.getSchematicsAccessibleByUser(0, 0), Math.max(page - 1, 0), null, SchematicListBehavior.builder().setPublics(true).setPageCommandGen(integer -> "/schem list public " + integer).build()); } @Register("list") public void schemList(Player player, @Validator("isDirValidator") @Mapper("dirMapper") SchematicNode node, @OptionalValue("1") int page) { SteamwarUser user = getUser(player); - renderSchemlist(player, SchematicNode.getSchematicNodeInNode(node), Math.max(page - 1, 0), node, SchematicListBehavior.builder().setPublics(node.getOwner() == 0).setPageCommandGen(value -> "/schem list " + (node.getOwner()==0?"public ":"") + node.generateBreadcrumbs(user) + " " + value).build()); + createCachedSchemList(player, SchematicNode.getSchematicNodeInNode(node), Math.max(page - 1, 0), node, SchematicListBehavior.builder().setPublics(node.getOwner() == 0).setPageCommandGen(value -> "/schem list " + (node.getOwner()==0?"public ":"") + node.generateBreadcrumbs(user) + " " + value).build()); } @Register({"info", "public"}) @@ -334,12 +334,12 @@ public class SchematicCommand extends SWCommand { } return true; }); - renderSchemlist(player, nodes, Math.max(page - 1, 0), null, SchematicListBehavior.builder().setPublics(userId == 0).setShowPath(false).setRenderHook(s -> { + createCachedSchemList(player, nodes, Math.max(page - 1, 0), null, SchematicListBehavior.builder().setPublics(userId == 0).setShowPath(false).setRenderHook(s -> { for (String ss : nameList) { s = s.replace(ss, "§e§l" + ss + "§7"); } return "§7" + s; - }).setPageCommandGen(integer -> "/schem search " + integer + " " + Arrays.stream(query).reduce((s, s2) -> s + " " + s2).orElse("")).build()); + }).build()); } @Register("download") @@ -547,6 +547,11 @@ public class SchematicCommand extends SWCommand { } } + @Register(value = "page", noTabComplete = true) + public void pageCommand(Player player, int page) { + cachedSchemList(player, page); + } + private final TypeMapper publicCommandTypeMapper = publicNodeTypeMapper(); @Mapper("publicMapper") diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java index 24412f0..419a8b3 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java @@ -28,8 +28,10 @@ import de.steamwar.schematicsystem.AutoCheckResult; import de.steamwar.schematicsystem.CheckSchemType; import de.steamwar.schematicsystem.SchematicSystem; import de.steamwar.sql.*; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.With; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -49,6 +51,8 @@ public class SchematicCommandUtils { public static final int CHUNK_SIZE = 15; private static final List PUBLIC_TOGGLED = new ArrayList<>(); private static final List FORBIDDEN_NAMES = Collections.unmodifiableList(Arrays.asList("public")); + private static final Map CACHED_LISTS = new HashMap<>(); + private SchematicCommandUtils() { } @@ -83,6 +87,19 @@ public class SchematicCommandUtils { return false; } + public static void createCachedSchemList(Player player, List nodes, int chunk, SchematicNode parent, SchematicListBehavior behavior) { + CachedSchematicList cachedSchematicList = new CachedSchematicList(nodes, chunk, (int) Math.ceil(nodes.size() / (double) CHUNK_SIZE), parent, behavior.withPageCommandGen((page) -> "/schematic page " + (page - 1))); + CACHED_LISTS.put(player, cachedSchematicList); + renderSchemlist(player, cachedSchematicList.nodes, chunk, parent, cachedSchematicList.behavior); + } + + public static void cachedSchemList(Player player, int chunk) { + CACHED_LISTS.computeIfPresent(player, (p, cachedSchematicList) -> { + renderSchemlist(player, cachedSchematicList.nodes, chunk, null, cachedSchematicList.behavior); + return cachedSchematicList.withPage(chunk); + }); + } + public static void renderSchemlist(Player player, List nodes, int chunk, @@ -168,6 +185,7 @@ public class SchematicCommandUtils { @Getter @Builder(setterPrefix = "set") + @With public static class SchematicListBehavior { public static final SchematicListBehavior DEFAULT = SchematicListBehavior.builder().build(); @@ -453,4 +471,14 @@ public class SchematicCommandUtils { inv.setCallback(-999, click -> player.closeInventory()); inv.open(); } + + @AllArgsConstructor + @With + static class CachedSchematicList { + private List nodes; + private int page; + private int maxPage; + private SchematicNode parent; + private SchematicListBehavior behavior; + } } From 407eb7a4689ce88128a1e3a0004952f585f66480 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 8 Sep 2022 21:44:50 +0200 Subject: [PATCH 2/2] Add Quit Listener --- .../steamwar/schematicsystem/SchematicSystem.java | 4 ++++ .../commands/SchematicCommandUtils.java | 5 +++++ .../listener/PlayerEventListener.java | 14 ++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 SchematicSystem_Core/src/de/steamwar/schematicsystem/listener/PlayerEventListener.java diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java index 844fc99..7df1177 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java @@ -22,6 +22,8 @@ package de.steamwar.schematicsystem; import de.steamwar.message.Message; import de.steamwar.schematicsystem.commands.DownloadCommand; import de.steamwar.schematicsystem.commands.SchematicCommand; +import de.steamwar.schematicsystem.listener.PlayerEventListener; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; public class SchematicSystem extends JavaPlugin { @@ -35,6 +37,8 @@ public class SchematicSystem extends JavaPlugin { new SchematicCommand(); new DownloadCommand(); + + Bukkit.getPluginManager().registerEvents(new PlayerEventListener(), this); } public static SchematicSystem getInstance() { diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java index 419a8b3..de97a90 100644 --- a/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/commands/SchematicCommandUtils.java @@ -472,6 +472,11 @@ public class SchematicCommandUtils { inv.open(); } + public static void quitPlayer(Player player) { + CACHED_LISTS.remove(player); + PUBLIC_TOGGLED.remove(player); + } + @AllArgsConstructor @With static class CachedSchematicList { diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/listener/PlayerEventListener.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/listener/PlayerEventListener.java new file mode 100644 index 0000000..7b55277 --- /dev/null +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/listener/PlayerEventListener.java @@ -0,0 +1,14 @@ +package de.steamwar.schematicsystem.listener; + +import de.steamwar.schematicsystem.commands.SchematicCommandUtils; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +public class PlayerEventListener implements Listener { + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + SchematicCommandUtils.quitPlayer(event.getPlayer()); + } +}