Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
backpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Librazy committed Mar 23, 2019
1 parent 765b583 commit cdb7334
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
17 changes: 15 additions & 2 deletions src/main/java/cat/nyaa/nyaautils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,21 @@ public class Configuration extends PluginConfigure {
@Serializable(name = "sit.locations")
public Map<String, SitLocation> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Location> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ExtraBackpackLine> lines;
try (Query<ExtraBackpackLine> 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) {
Expand All @@ -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);
Expand Down Expand Up @@ -187,7 +192,7 @@ private void saveAll(Inventory inventory) {
}

private boolean saveLine(int i, List<ItemStack> 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;
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/lang/zh_CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 显示帮助信息
Expand Down

0 comments on commit cdb7334

Please sign in to comment.