added /irepair

This commit is contained in:
ImproperIssues
2023-04-15 18:09:04 -07:00
parent 1a06aa228c
commit f5254cbb1c
4 changed files with 87 additions and 9 deletions

View File

@@ -1,14 +1,17 @@
package io.github.itzispyder.ogredupealias.commands.commands;
import io.github.itzispyder.ogredupealias.commands.CmdExHandler;
import io.github.itzispyder.ogredupealias.commands.TabComplBuilder;
import io.github.itzispyder.ogredupealias.utils.ArrayUtils;
import io.github.itzispyder.ogredupealias.utils.StringUtils;
import io.github.itzispyder.ogredupealias.utils.Text;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -16,15 +19,48 @@ import java.util.Objects;
public class IRepairCommand implements TabExecutor {
@Override
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
try {
Player p = (Player) sender;
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).forEach(item -> {
item.setDurability(new ItemStack(item.getType()).getDurability());
item.setAmount(item.getMaxStackSize());
});
sender.sendMessage(Text.builder("&3Repaired and restocked your inventory.").prefix().color().build());
if (args.length == 0) {
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).forEach(this::repair);
sender.sendMessage(Text.builder("&3Repaired and restocked your inventory.").prefix().color().build());
return true;
}
switch (args[0]) {
case "all" -> {
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).forEach(this::repair);
sender.sendMessage(Text.builder("&3Repaired and restocked your inventory.").prefix().color().build());
}
case "hand" -> {
ItemStack hand = p.getInventory().getItemInMainHand();
if (hand.getType().isAir()) {
sender.sendMessage(Text.builder("&cCannot repair air.").prefix().color().build());
return true;
}
repair(hand);
sender.sendMessage(Text.builder("&3Repaired and restocked your hand item.").prefix().color().build());
}
case "fillrest" -> {
Material type = Material.valueOf(args[1].toUpperCase());
ItemStack item = new ItemStack(type, type.getMaxStackSize());
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).forEach(this::repair);
while (p.getInventory().firstEmpty() != -1) p.getInventory().setItem(p.getInventory().firstEmpty(),item);
sender.sendMessage(Text.builder("&3Repaired and restocked your inventory, filled the rest with &7" + StringUtils.capitalizeWords(type.name()) + "&b.").prefix().color().build());
}
case "only" -> {
Material type = Material.valueOf(args[1].toUpperCase());
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).filter(item -> item.getType() == type).forEach(this::repair);
sender.sendMessage(Text.builder("&3Repaired and restocked your &7" + StringUtils.capitalizeWords(type.name()) + "&b.").prefix().color().build());
}
case "exclude" -> {
Material type = Material.valueOf(args[1].toUpperCase());
Arrays.stream(p.getInventory().getContents()).filter(Objects::nonNull).filter(item -> item.getType() != type).forEach(this::repair);
sender.sendMessage(Text.builder("&3Repaired and restocked everything but your &7" + StringUtils.capitalizeWords(type.name()) + "&b.").prefix().color().build());
}
}
}
catch (Exception ex) {
CmdExHandler handler = new CmdExHandler(ex,command);
@@ -33,8 +69,23 @@ public class IRepairCommand implements TabExecutor {
return true;
}
@SuppressWarnings("deprecation")
private void repair(ItemStack item) {
item.setDurability(new ItemStack(item.getType()).getDurability());
item.setAmount(item.getMaxStackSize());
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return new ArrayList<>();
return new TabComplBuilder(sender, command, alias, args)
.add(1, new String[] {
"fillrest",
"exclude",
"only",
"hand",
"all"
})
.add(2, ArrayUtils.Constants.MATERIAL_NAMES, !args[0].equals("all") && !args[0].equals("hand"))
.build();
}
}

View File

@@ -1,5 +1,8 @@
package io.github.itzispyder.ogredupealias.utils;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -30,4 +33,9 @@ public abstract class ArrayUtils {
tList.forEach(list::add);
return list;
}
public static class Constants {
public static final List<String> MATERIAL_NAMES = toNewList(Arrays.stream(Material.values()).toList(),m -> m.name().toLowerCase());
public static final List<String> ENTITY_NAMES = toNewList(Arrays.stream(EntityType.values()).toList(),e -> e.name().toLowerCase());
}
}

View File

@@ -0,0 +1,18 @@
package io.github.itzispyder.ogredupealias.utils;
public abstract class StringUtils {
public static String capitalize(String s) {
if (s.length() == 1) return s.toUpperCase();
s = s.toLowerCase();
return String.valueOf(s.charAt(0)).toUpperCase() + s.substring(1);
}
public static String capitalizeWords(String s) {
s = s.replaceAll("[_-]"," ");
String[] sArray = s.split(" ");
StringBuilder sb = new StringBuilder();
for (String str : sArray) sb.append(capitalize(str)).append(" ");
return sb.toString().trim();
}
}

View File

@@ -83,4 +83,5 @@ commands:
usage: /irepair
permission: oda.commands.irepair
aliases:
- ipearlrepair
- ipearlrepair
- irestock