|
|
@@ -1,19 +1,29 @@
|
|
|
package me.lethunderhawk.storeMore.command;
|
|
|
|
|
|
import me.lethunderhawk.storeMore.StoreMore;
|
|
|
-import me.lethunderhawk.storeMore.abstraction.item.CAPACITY;
|
|
|
import me.lethunderhawk.storeMore.abstraction.item.ItemStorage;
|
|
|
-import me.lethunderhawk.storeMore.abstraction.item.ItemStoragePair;
|
|
|
import me.lethunderhawk.storeMore.concrete.item.StorageRegistry;
|
|
|
+import me.lethunderhawk.storeMore.itemManipulation.StorageManipulator;
|
|
|
+import net.kyori.adventure.text.Component;
|
|
|
+import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
import org.bukkit.Material;
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
import org.bukkit.entity.Player;
|
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
public class StoreMoreCommand extends CustomCommand{
|
|
|
public StoreMoreCommand(StoreMore module) {
|
|
|
super(module);
|
|
|
}
|
|
|
+ private static final List<String> MATERIALS =
|
|
|
+ Arrays.stream(Material.values())
|
|
|
+ .map(Material::name)
|
|
|
+ .toList();
|
|
|
+ private static final List<String> NUMBER_COMPLETIONS =
|
|
|
+ List.of("1", "2", "3");
|
|
|
|
|
|
@Override
|
|
|
public CommandNode getRootCommand() {
|
|
|
@@ -27,6 +37,78 @@ public class StoreMoreCommand extends CustomCommand{
|
|
|
@Override
|
|
|
public void createCommands() {
|
|
|
registerSubCommand("getAll", "Gives you all items added by this plugin", this::getAll);
|
|
|
+ registerSubCommand("addItemToCell", "Adds Items to the cell you're holding", this::addItems).setTabCompleter(this::completeItemCommand);
|
|
|
+ registerSubCommand("removeItemFromCell", "Removes Items from the cell you're holding", this::removeItems).setTabCompleter(this::completeItemCommand);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> completeItemCommand(CommandSender sender, String[] args) {
|
|
|
+ // /command <action> <material>
|
|
|
+ if (args.length == 2) {
|
|
|
+ String partial = args[1].toLowerCase(Locale.ROOT);
|
|
|
+
|
|
|
+ return MATERIALS.stream()
|
|
|
+ .filter(name -> name.toLowerCase(Locale.ROOT).startsWith(partial))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // /command <action> <material> <amount>
|
|
|
+ if (args.length == 3) {
|
|
|
+ boolean isCompleteMaterial = MATERIALS.stream()
|
|
|
+ .anyMatch(name -> name.equalsIgnoreCase(args[1]));
|
|
|
+
|
|
|
+ if (!isCompleteMaterial) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ String partial = args[2].toLowerCase(Locale.ROOT);
|
|
|
+
|
|
|
+ return NUMBER_COMPLETIONS.stream()
|
|
|
+ .filter(amount -> amount.toLowerCase(Locale.ROOT).startsWith(partial))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removeItems(CommandSender commandSender, String[] strings) {
|
|
|
+ if(strings.length < 2) return;
|
|
|
+ //commandSender.sendMessage(strings[0] + ";" + strings[1] + ";" + strings[2]);
|
|
|
+
|
|
|
+ try{
|
|
|
+ Material mat = Material.getMaterial(strings[0].toUpperCase());
|
|
|
+ if(mat == null) throw new IllegalArgumentException("Invalid material " + strings[0]);
|
|
|
+ int amount = Math.abs(Integer.parseInt(strings[1]));
|
|
|
+ commandSender.sendMessage(Component.text("Trying to remove " + amount + " " + mat.name() + " from item cell...", NamedTextColor.GOLD));
|
|
|
+ if(commandSender instanceof Player p) {
|
|
|
+ StorageManipulator.manipulateItemCellInMainHand(p, mat, -amount);
|
|
|
+ }else{
|
|
|
+ commandSender.sendMessage(Component.text("Must be a player in order to perform this command!", NamedTextColor.RED));
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e){
|
|
|
+ commandSender.sendMessage(Component.text("Invalid item amount!").color(NamedTextColor.RED));
|
|
|
+ } catch(IllegalArgumentException e){
|
|
|
+ commandSender.sendMessage(Component.text("Invalid item!").color(NamedTextColor.RED));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addItems(CommandSender commandSender, String[] strings) {
|
|
|
+ if(strings.length < 2) return;
|
|
|
+ //commandSender.sendMessage(strings[0] + ";" + strings[1] + ";" + strings[2]);
|
|
|
+ try{
|
|
|
+ Material mat = Material.getMaterial(strings[0].toUpperCase());
|
|
|
+ if(mat == null) throw new IllegalArgumentException("Invalid material " + strings[0]);
|
|
|
+ int amount = Math.abs(Integer.parseInt(strings[1]));
|
|
|
+ commandSender.sendMessage(Component.text("Trying to add " + amount + " " + mat.name() + " to item cell...", NamedTextColor.GOLD));
|
|
|
+ if(commandSender instanceof Player p) {
|
|
|
+ StorageManipulator.manipulateItemCellInMainHand(p, mat, amount);
|
|
|
+ }else{
|
|
|
+ commandSender.sendMessage(Component.text("Must be a player in order to perform this command!", NamedTextColor.RED));
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e){
|
|
|
+ commandSender.sendMessage(Component.text("Invalid item amount!").color(NamedTextColor.RED));
|
|
|
+ } catch(IllegalArgumentException e){
|
|
|
+ commandSender.sendMessage(Component.text("Invalid item!").color(NamedTextColor.RED));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void getAll(CommandSender commandSender, String[] strings) {
|
|
|
@@ -34,7 +116,6 @@ public class StoreMoreCommand extends CustomCommand{
|
|
|
for(ItemStorage storage : StorageRegistry.getRegistered()){
|
|
|
player.getInventory().addItem(storage.buildItem());
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|