From cdb7334c613997d1bcd04d9e6cde02bc5a29d37e Mon Sep 17 00:00:00 2001 From: Liqueur Librazy Date: Sat, 23 Mar 2019 20:42:15 +0800 Subject: [PATCH] backpack --- .../cat/nyaa/nyaautils/Configuration.java | 17 +++++++- .../extrabackpack/ExtraBackpackCommands.java | 43 +++++++++++++++++-- .../extrabackpack/ExtraBackpackGUI.java | 23 +++++++--- src/main/resources/lang/en_US.yml | 3 +- src/main/resources/lang/zh_CN.yml | 3 +- 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/main/java/cat/nyaa/nyaautils/Configuration.java b/src/main/java/cat/nyaa/nyaautils/Configuration.java index 3ae7916..47ae14e 100644 --- a/src/main/java/cat/nyaa/nyaautils/Configuration.java +++ b/src/main/java/cat/nyaa/nyaautils/Configuration.java @@ -249,8 +249,21 @@ public class Configuration extends PluginConfigure { @Serializable(name = "sit.locations") public Map sit_locations = new HashMap<>(); - @Serializable(name = "extrabackpack.default_lines") - public int bp_default_lines = 6; + + @Serializable(name = "extra_backpack.enable") + public boolean bp_enable = false; + + @Serializable(name = "extra_backpack.start_line") + public int bp_default_lines = 0; + + @Serializable(name = "extra_backpack.max_line") + public int bp_max_lines = 6; + + @Serializable(name = "extra_backpack.require_nearby_block") + public Material bp_require_nearby_block = Material.AIR; + + @Serializable(name = "extra_backpack.require_nearby_distance") + public int bp_require_nearby_distance = 5; @StandaloneConfig public final MailboxLocations mailbox; diff --git a/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackCommands.java b/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackCommands.java index 59eec50..33c4396 100644 --- a/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackCommands.java +++ b/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackCommands.java @@ -2,19 +2,27 @@ import cat.nyaa.nyaacore.CommandReceiver; import cat.nyaa.nyaacore.LanguageRepository; +import cat.nyaa.nyaacore.Message; +import cat.nyaa.nyaacore.Pair; import cat.nyaa.nyaacore.database.DatabaseUtils; import cat.nyaa.nyaacore.database.relational.Query; import cat.nyaa.nyaacore.database.relational.RelationalDB; +import cat.nyaa.nyaacore.utils.LocaleUtils; +import cat.nyaa.nyaautils.I18n; import cat.nyaa.nyaautils.NyaaUtils; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import java.util.List; import java.util.Objects; import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class ExtraBackpackCommands extends CommandReceiver { private NyaaUtils plugin; @@ -156,18 +164,47 @@ public void openBackpack(CommandSender sender, Arguments args) { } @DefaultCommand(permission = "nu.bp.use") - private void open(CommandSender sender, Arguments args) { + private void open(CommandSender commandSender, Arguments args) { Player player = args.nextPlayerOrSender(); + Player sender = asPlayer(commandSender); + if (plugin.cfg.bp_require_nearby_block != Material.AIR && !sender.hasPermission("nu.bp.admin")) { + Location location = player.getLocation(); + List nearbyBlock = IntStream.rangeClosed(-plugin.cfg.bp_require_nearby_distance, plugin.cfg.bp_require_nearby_distance) + .parallel() + .boxed() + .flatMap(x -> + IntStream.rangeClosed(-plugin.cfg.bp_require_nearby_distance, plugin.cfg.bp_require_nearby_distance) + .parallel() + .boxed() + .map(y -> Pair.of(x, y)) + ) + .flatMap(p -> + IntStream.rangeClosed(-plugin.cfg.bp_require_nearby_distance, plugin.cfg.bp_require_nearby_distance) + .parallel() + .boxed() + .map(z -> location.clone().add(p.getKey(), p.getValue(), z)) + ).collect(Collectors.toList()); + boolean match = nearbyBlock.parallelStream().anyMatch(loc -> loc.getBlock().getType() == plugin.cfg.bp_require_nearby_block); + if (!match) { + new Message(I18n.format("user.backpack.no_required_block")) + .append(LocaleUtils.getNameComponent(new ItemStack(plugin.cfg.bp_require_nearby_block))) + .send(sender); + return; + } + } int page = 0; if (args.top() != null) { page = args.nextInt(); } + if (page < 0) { + msg(sender, "user.error.bad_int"); + } + if (!player.equals(sender) && !sender.hasPermission("nu.bp.admin")) { msg(sender, "user.error.no_required_permission", "nu.bp.admin"); return; } - ExtraBackpackGUI extraBackpackGUI = new ExtraBackpackGUI(plugin, database, player.getUniqueId(), asPlayer(sender)); + ExtraBackpackGUI extraBackpackGUI = new ExtraBackpackGUI(plugin, database, player.getUniqueId(), sender); extraBackpackGUI.open(page); } - } diff --git a/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackGUI.java b/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackGUI.java index b6c5f72..7c911b0 100644 --- a/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackGUI.java +++ b/src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackGUI.java @@ -6,8 +6,6 @@ import cat.nyaa.nyaacore.utils.ItemStackUtils; import cat.nyaa.nyaautils.I18n; import cat.nyaa.nyaautils.NyaaUtils; -import com.google.common.collect.MultimapBuilder; -import com.google.common.collect.SetMultimap; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -47,7 +45,7 @@ public class ExtraBackpackGUI implements InventoryHolder { void open(int page) { Player currentOpener; if ((currentOpener = opened.putIfAbsent(owner, opener)) != null) { - if (!currentOpener.equals(opener)) { + if (!currentOpener.equals(opener) && opener.getOpenInventory().getTopInventory().getHolder() instanceof ExtraBackpackGUI) { if (opener.hasPermission("nu.bp.admin") && !currentOpener.hasPermission("nu.bp.admin")) { Inventory inventory = currentOpener.getOpenInventory().getTopInventory(); if (inventory.getHolder() instanceof ExtraBackpackGUI) { @@ -77,10 +75,16 @@ void open(int page) { query.commit(); } } + if (maxLine <= 0) { + new Message(I18n.format("user.backpack.disabled")).send(opener); + opened.remove(owner); + return; + } List lines; try (Query query = database.queryTransactional(ExtraBackpackLine.class).whereEq("player_id", ownerId)) { lines = query.select(); if (lines.size() > maxLine) { + opened.remove(owner); throw new IllegalStateException("Too many lines"); } else if (lines.size() != maxLine) { for (int cur = lines.size(); cur < maxLine; ++cur) { @@ -100,10 +104,11 @@ void open(int page) { int viewSize = view.size(); if (viewSize == 0) { new Message(I18n.format("user.backpack.invalid_page", page, pageCount)).send(opener); + opened.remove(owner); return; } int size = viewSize * 9; - Inventory inventory = Bukkit.createInventory(this, size, I18n.format("user.backpack.title", Bukkit.getOfflinePlayer(owner).getName(), page, pageCount)); + Inventory inventory = Bukkit.createInventory(this, size, I18n.format("user.backpack.title", Bukkit.getOfflinePlayer(owner).getName(), page, pageCount - 1)); ItemStack[] itemStacks = view.stream().flatMap(l -> l.getItemStacks().stream()).toArray(ItemStack[]::new); inventory.setContents(itemStacks); opener.openInventory(inventory); @@ -187,7 +192,7 @@ private void saveAll(Inventory inventory) { } private boolean saveLine(int i, List line) { - int lineNo = currentPage * 5 + i; + int lineNo = currentPage * 6 + i; String lineLastState = lastState.get(owner).get(lineNo); String desiredState = ItemStackUtils.itemsToBase64(line); if (lineLastState.equals(desiredState)) return true; @@ -225,7 +230,13 @@ private void close() { } static boolean isOpened(UUID owner) { - return opened.containsKey(owner); + Player opener = opened.get(owner); + if (opener != null && opener.getOpenInventory().getTopInventory().getHolder() instanceof ExtraBackpackGUI) { + return true; + } else if (opener != null) { + opened.remove(owner); + } + return false; } public static void closeAll() { diff --git a/src/main/resources/lang/en_US.yml b/src/main/resources/lang/en_US.yml index b12ed9e..48b60ca 100644 --- a/src/main/resources/lang/en_US.yml +++ b/src/main/resources/lang/en_US.yml @@ -352,10 +352,9 @@ user: force_opened: Your backpack is being opened by admin invalid_page: 'Invalid page %d. Expected: [0, %d)' title: 'Backpack of %s. Page %d/%d' - next: Next - back: Back error_saving: Error saving backpack. Please concat operators for help. error_state: 'Backpack of %s line %d failed to save when opened by %s' + disabled: Current backpack is disabled manual: help: description: Show help message diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index e44f690..6bf6636 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -383,10 +383,9 @@ user: force_opened: 您的背包被管理员打开了 invalid_page: '无效的页码 %d。有效页码:[0, %d)' title: '%s 的背包 第 %d/%d 页' - next: 下一页 - back: 上一页 error_saving: 背包保存失败。请联系管理员获取帮助。 error_state: '%s 的背包第 %d 行保存失败(由 %s 打开)。' + disabled: 当前背包已被禁用 manual: help: description: 显示帮助信息