|
| 1 | +package net.tfminecraft.advancedcrafting.managers; |
| 2 | + |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Locale; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import java.util.logging.Level; |
| 9 | + |
| 10 | +import org.bukkit.Bukkit; |
| 11 | +import org.bukkit.command.CommandSender; |
| 12 | +import org.bukkit.entity.Player; |
| 13 | +import org.bukkit.inventory.ItemStack; |
| 14 | + |
| 15 | +import net.tfminecraft.advancedcrafting.AdvancedCrafting; |
| 16 | +import net.tfminecraft.advancedcrafting.cache.Cache; |
| 17 | +import net.tfminecraft.advancedcrafting.loaders.IngredientLoader; |
| 18 | +import net.tfminecraft.advancedcrafting.loaders.RecipeLoader; |
| 19 | +import net.tfminecraft.advancedcrafting.objects.crafting.CraftingRecipe; |
| 20 | +import net.tfminecraft.advancedcrafting.objects.crafting.CraftingStation; |
| 21 | +import net.tfminecraft.advancedcrafting.objects.ingredients.Ingredient; |
| 22 | + |
| 23 | +/** Staff grants use the normal equipment builder, without crafting rewards or station state. */ |
| 24 | +final class EquipmentGiveCommand { |
| 25 | + boolean hasPermission(CommandSender sender) { |
| 26 | + return sender.hasPermission(Cache.giveEquipmentPermission); |
| 27 | + } |
| 28 | + |
| 29 | + boolean execute(CommandSender sender, String[] args) { |
| 30 | + if (!hasPermission(sender)) { |
| 31 | + sender.sendMessage("§cNo permission."); |
| 32 | + return true; |
| 33 | + } |
| 34 | + if (args.length < 4 || args.length > 6) { |
| 35 | + sender.sendMessage("§cUsage: /ac give equipment <recipe> <ingredient.id|alloy.id> [player] [quality 0-100]"); |
| 36 | + return true; |
| 37 | + } |
| 38 | + Player target; |
| 39 | + if (args.length >= 5) { |
| 40 | + target = Bukkit.getPlayerExact(args[4]); |
| 41 | + if (target == null) { |
| 42 | + sender.sendMessage("§cPlayer not found: §f" + args[4]); |
| 43 | + return true; |
| 44 | + } |
| 45 | + } else if (sender instanceof Player player) { |
| 46 | + target = player; |
| 47 | + } else { |
| 48 | + sender.sendMessage("§cSpecify an online player from console."); |
| 49 | + return true; |
| 50 | + } |
| 51 | + double quality = 100; |
| 52 | + if (args.length == 6) { |
| 53 | + try { |
| 54 | + quality = Double.parseDouble(args[5]); |
| 55 | + } catch (NumberFormatException ex) { |
| 56 | + sender.sendMessage("§cQuality must be a finite number from 0 to 100."); |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (!Double.isFinite(quality) || quality < 0 || quality > 100) { |
| 60 | + sender.sendMessage("§cQuality must be a finite number from 0 to 100."); |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + CraftingRecipe recipe = RecipeLoader.getByString(args[2].toLowerCase(Locale.ROOT)); |
| 65 | + if (recipe == null) { |
| 66 | + sender.sendMessage("§cUnknown recipe: §f" + args[2]); |
| 67 | + return true; |
| 68 | + } |
| 69 | + String material = args[3].toLowerCase(Locale.ROOT); |
| 70 | + if (!materialsFor(recipe).contains(material)) { |
| 71 | + sender.sendMessage("§cUnknown or incompatible base material/alloy: §f" + args[3]); |
| 72 | + return true; |
| 73 | + } |
| 74 | + HashMap<String, Integer> inputs = new HashMap<>(); |
| 75 | + for (var entry : recipe.getRecipe().entrySet()) { |
| 76 | + String key = material; |
| 77 | + if (!entry.getKey().equalsIgnoreCase(recipe.getMainType())) { |
| 78 | + Ingredient filler = IngredientLoader.get().stream() |
| 79 | + .filter(i -> i.getIngredientData().getType().getId().equalsIgnoreCase(entry.getKey())) |
| 80 | + .min(Comparator.comparingInt((Ingredient i) -> i.getIngredientData().getTier()) |
| 81 | + .thenComparing(Ingredient::getId)).orElse(null); |
| 82 | + if (filler == null) { |
| 83 | + sender.sendMessage("§cNo configured ingredient for required type: §f" + entry.getKey()); |
| 84 | + return true; |
| 85 | + } |
| 86 | + key = "ingredient." + filler.getId(); |
| 87 | + } |
| 88 | + inputs.put(key, entry.getValue()); |
| 89 | + } |
| 90 | + try { |
| 91 | + CraftingStation station = new CraftingStation(target.getLocation(), recipe, inputs, new HashMap<>()); |
| 92 | + ItemStack item = station.buildCompletedItem(target, quality); |
| 93 | + for (ItemStack overflow : target.getInventory().addItem(item).values()) { |
| 94 | + target.getWorld().dropItemNaturally(target.getLocation(), overflow); |
| 95 | + } |
| 96 | + } catch (RuntimeException ex) { |
| 97 | + AdvancedCrafting.plugin.getLogger().log(Level.SEVERE, "Failed to give equipment for recipe " + recipe.getId(), ex); |
| 98 | + sender.sendMessage("§cEquipment could not be built. Check the server log and recipe/template configuration."); |
| 99 | + return true; |
| 100 | + } |
| 101 | + sender.sendMessage("§aGave §f" + recipe.getId() + "§a using §f" + material + "§a to §f" + target.getName() + "§a."); |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + private List<String> materialsFor(CraftingRecipe recipe) { |
| 106 | + if (!recipe.getRecipe().containsKey(recipe.getMainType())) return List.of(); |
| 107 | + Stream<String> ingredients = IngredientLoader.get().stream() |
| 108 | + .filter(i -> i.getIngredientData().getType().getId().equalsIgnoreCase(recipe.getMainType())) |
| 109 | + .map(i -> "ingredient." + i.getId()); |
| 110 | + Stream<String> alloys = AlloyManager.getAlloyIds().stream() |
| 111 | + .filter(id -> AlloyManager.getAlloyById(id).getData().getType().getId().equalsIgnoreCase(recipe.getMainType())) |
| 112 | + .map(id -> "alloy." + id); |
| 113 | + return Stream.concat(ingredients, alloys).sorted().toList(); |
| 114 | + } |
| 115 | + |
| 116 | + List<String> complete(CommandSender sender, String[] args) { |
| 117 | + if (!hasPermission(sender)) return List.of(); |
| 118 | + List<String> options; |
| 119 | + switch (args.length) { |
| 120 | + case 3 -> options = RecipeLoader.get().keySet().stream().sorted().toList(); |
| 121 | + case 4 -> { |
| 122 | + CraftingRecipe recipe = RecipeLoader.getByString(args[2].toLowerCase(Locale.ROOT)); |
| 123 | + options = recipe == null ? List.of() : materialsFor(recipe); |
| 124 | + } |
| 125 | + case 5 -> options = Bukkit.getOnlinePlayers().stream().map(Player::getName).sorted().toList(); |
| 126 | + case 6 -> options = List.of("0", "25", "50", "75", "90", "100"); |
| 127 | + default -> options = List.of(); |
| 128 | + } |
| 129 | + String prefix = args[args.length - 1].toLowerCase(Locale.ROOT); |
| 130 | + return options.stream().filter(o -> o.toLowerCase(Locale.ROOT).startsWith(prefix)).toList(); |
| 131 | + } |
| 132 | +} |
0 commit comments