Added check stock command

This commit is contained in:
Teriuihi 2022-11-11 04:21:15 +01:00
parent dccbaea32b
commit 515613c36d
3 changed files with 121 additions and 4 deletions

View File

@ -0,0 +1,94 @@
package com.alttd.playershops.commands;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.shop.PlayerShop;
import com.alttd.playershops.utils.ShopUtil;
import com.alttd.playershops.utils.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.stream.Collectors;
public class CheckStockCommand {
private int playerX;
private int playerZ;
private int radius;
private int minimumStock = -1;
private final PlayerShops plugin;
public CheckStockCommand(PlayerShops plugin, Player player, String[] args) {
this.plugin = plugin;
if (args.length != 2 && args.length != 3) {
player.sendMessage(Util.parseMiniMessage("<red>Invalid command syntax, use /checkstock <radius> [minimum stock]"));
return;
}
try {
radius = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
player.sendMessage(Util.parseMiniMessage("<red>radius has to be a valid number, use /checkstock <radius> [minimum stock]"));
return;
}
if (radius > 100 || radius <= 0) {
player.sendMessage(Util.parseMiniMessage("<red>Please keep the radius between 1 and 100"));
return;
}
if (args.length == 3) {
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]"));
return;
}
}
playerX = player.getLocation().getBlockX();
playerZ = player.getLocation().getBlockZ();
List<Stock> stockList = checkStock();
sendStockMessage(player, stockList);
}
private List<Stock> checkStock() {
List<PlayerShop> shops = plugin.getShopHandler().getShopsInRadius(playerX, playerZ, radius);
if (minimumStock != -1)
shops = shops.stream().filter(shop -> shop.getRemainingStock() < minimumStock).collect(Collectors.toList());
return shops.stream().map(shop -> {
Location signLocation = shop.getSignLocation();
return new Stock(shop.getRemainingStock(),
signLocation.getBlockX(),
signLocation.getBlockY(),
signLocation.getBlockZ(),
ShopUtil.itemNameComponent(shop.getItemStack()));
}).collect(Collectors.toList());
}
private static final String putInConfig = "<yellow><click:run_command:'/cmi tppos <x> <y> <z>'>[<aqua><item_component></aqua>] <amount> left in stock</click></yellow>";
private void sendStockMessage(Player player, List<Stock> stockList) {
Component component = null;
for (Stock stock : stockList) {
TagResolver resolver = TagResolver.resolver(
Placeholder.component("item_component", stock.itemNameComponent),
Placeholder.parsed("x", stock.x + ""),
Placeholder.parsed("y", stock.y + ""),
Placeholder.parsed("z", stock.z + ""),
Placeholder.parsed("amount", stock.stock + "")
);
if (component == null)
component = Util.parseMiniMessage(putInConfig, resolver);
else
component = component.append(Component.newline()).append(Util.parseMiniMessage(putInConfig, resolver));
}
if (component == null)
player.sendMessage(Util.parseMiniMessage(
"<yellow>No shops with less than <minimum_stock> stock found.</yellow>",
TagResolver.resolver(Placeholder.parsed("minimum_stock", minimumStock + ""))));
else
player.sendMessage(component);
}
private record Stock(int stock, int x, int y, int z, Component itemNameComponent) {}
}

View File

@ -1,11 +1,8 @@
package com.alttd.playershops.commands;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.config.Config;
import com.alttd.playershops.config.MessageConfig;
import com.alttd.playershops.gui.HomeGui;
import com.alttd.playershops.gui.ShopManagementGui;
import com.alttd.playershops.shop.PlayerShop;
import com.alttd.playershops.utils.Util;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -33,6 +30,13 @@ public class ShopCommand implements CommandExecutor, TabCompleter {
HomeGui gui = new HomeGui(player.getUniqueId());
gui.open();
break;
case "checkstock":
if (!player.hasPermission("playershops.command.playershop.checkstock")) {
sender.sendMessage(Util.parseMiniMessage("<red><hover:show_text:'<red>playershops.command.playershop.checkstock'>You do not have permission for this command</hover></red>"));
break;
}
new CheckStockCommand(PlayerShops.getInstance(), player, args);
break;
default:
sender.sendMessage("invalid command useage");
break;

View File

@ -129,6 +129,25 @@ public class ShopHandler {
}
return null;
}
public List<PlayerShop> getShopsInRadius(int x, int z, int radius) {
ArrayList<PlayerShop> shops = new ArrayList<>();
int bottomX = x - radius;
int topX = x + radius;
int bottomZ = z - radius;
int topZ = z + radius;
for (PlayerShop playerShop : shopLocation.values()) {
if (inRange(bottomX, topX, bottomZ, topZ, playerShop.getShopLocation()))
shops.add(playerShop);
}
return shops;
}
private boolean inRange(int bottomX, int topX, int bottomZ, int topZ, Location location) {
int x = location.getBlockX();
int z = location.getBlockZ();
return (x > bottomX && x < topX && z > bottomZ && z < topZ);
}
public void addShop(PlayerShop shop) {
shopLocation.put(shop.getShopLocation(), shop);