Made stock checking work for everyone

This commit is contained in:
Teriuihi 2023-01-22 01:47:35 +01:00
parent c04142cf2a
commit d3ba5be040
3 changed files with 21 additions and 14 deletions

View File

@ -19,6 +19,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@DefaultQualifier(NonNull.class)
public class CheckStockCommand implements Subcommand {
@ -44,7 +45,7 @@ public class CheckStockCommand implements Subcommand {
public boolean doStockCheck(final CommandSender sender, final String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("<red>Only players can use this command.");
sender.sendMessage(Util.parseMiniMessage("<red>Only players can use this command."));
return false;
}
if (args.length != 2 && args.length != 3) {
@ -69,20 +70,30 @@ public class CheckStockCommand implements Subcommand {
try {
minimumStock = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
player.sendMessage(Util.parseMiniMessage("<red>minium stock has to be a valid number, use /checkstock <radius> [minimum stock]"));
player.sendMessage(Util.parseMiniMessage("<red>minimum stock has to be a valid number, use /checkstock <radius> [minimum stock]"));
return false;
}
}
List<Stock> stockList = checkStock(player.getLocation().getBlockX(), player.getLocation().getBlockZ(), radius);
List<Stock> stockList = checkStock(player.getLocation().getBlockX(), player.getLocation().getBlockZ(), radius, player);
sendStockMessage(player, stockList);
return true;
}
private List<Stock> checkStock(int x, int z, int radius) {
/**
* @param x Coordinate to center search on
* @param z Coordinate to center search on
* @param radius Range to check in
* @param caller Player who is checking for stock, can only see the stock from their shops unless they have base_perm.checkstock.bypass
* @return A list of Stock that the player is allowed to see
*/
private List<Stock> checkStock(int x, int z, int radius, Player caller) {
List<PlayerShop> shops = PlayerShops.getInstance().getShopHandler().getShopsInRadius(x, z, radius);
Stream<PlayerShop> playerShopStream = shops.stream();
if (minimumStock != -1)
shops = shops.stream().filter(shop -> shop.getRemainingStock() < minimumStock).collect(Collectors.toList());
return shops.stream().map(shop -> {
playerShopStream = playerShopStream.filter(shop -> shop.getRemainingStock() < minimumStock);
if (!caller.hasPermission(PlayerShopCommand.BASE_PERM + ".checkstock.any"))
playerShopStream = playerShopStream.filter(shop -> shop.getOwnerUUID().equals(caller.getUniqueId()));
return playerShopStream.map(shop -> {
Location signLocation = shop.getSignLocation();
return new Stock(shop.getRemainingStock(),
signLocation.getBlockX(),
@ -110,7 +121,7 @@ public class CheckStockCommand implements Subcommand {
}
if (component == null)
if (minimumStock == -1)
player.sendMessage(Util.parseMiniMessage("<yellow>No shops found in specified radius.<yellow>"));
player.sendMessage(Util.parseMiniMessage("<yellow>No shops that you have permission to view found in specified radius.<yellow>"));
else
player.sendMessage(Util.parseMiniMessage(
"<yellow>No shops with less than <minimum_stock> stock found.</yellow>",

View File

@ -2,6 +2,7 @@ package com.alttd.playershops.commands.subcommands;
import com.alttd.playershops.commands.Subcommand;
import com.alttd.playershops.gui.HomeGui;
import com.alttd.playershops.utils.Util;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -13,7 +14,7 @@ public class OpenCommand implements Subcommand {
@Override
public boolean execute(final CommandSender sender, final String subCommand, final String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("<red>Only players can use this command.");
sender.sendMessage(Util.parseMiniMessage("<red>Only players can use this command."));
return false;
}
HomeGui gui = new HomeGui(player.getUniqueId());

View File

@ -11,14 +11,10 @@ import com.alttd.playershops.shop.TransactionError;
import com.alttd.playershops.utils.Logger;
import com.alttd.playershops.utils.ShopUtil;
import com.alttd.playershops.utils.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
@ -26,7 +22,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
/**
* Dedicated class to listen to transactions for shops.
@ -85,7 +80,7 @@ public class TransactionListener extends EventListener {
}
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
if (event.getPlayer().isSneaking() && player.hasPermission("playershop.shop.check-stock"))
if (event.getPlayer().isSneaking() && (player.hasPermission("playershop.shop.check-stock") || playerShop.getOwnerUUID().equals(player.getUniqueId())))
giveStockInfo(playerShop, player);
else
giveInfo(playerShop, player);