| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.function.ToIntFunction;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.commands.CommandSourceStack;
|
| 12 | import net.minecraft.commands.Commands;
|
| 13 | import net.minecraft.commands.arguments.ResourceKeyArgument;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.HolderLookup;
|
| 17 | import net.minecraft.core.component.DataComponents;
|
| 18 | import net.minecraft.core.registries.Registries;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.resources.ResourceKey;
|
| 21 | import net.minecraft.server.level.ServerLevel;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.world.entity.EquipmentSlot;
|
| 24 | import net.minecraft.world.entity.decoration.ArmorStand;
|
| 25 | import net.minecraft.world.entity.player.Player;
|
| 26 | import net.minecraft.world.item.Item;
|
| 27 | import net.minecraft.world.item.ItemStack;
|
| 28 | import net.minecraft.world.item.equipment.Equippable;
|
| 29 | import net.minecraft.world.item.equipment.trim.ArmorTrim;
|
| 30 | import net.minecraft.world.item.equipment.trim.TrimMaterial;
|
| 31 | import net.minecraft.world.item.equipment.trim.TrimMaterials;
|
| 32 | import net.minecraft.world.item.equipment.trim.TrimPattern;
|
| 33 | import net.minecraft.world.item.equipment.trim.TrimPatterns;
|
| 34 |
|
| 35 | public class SpawnArmorTrimsCommand {
|
| 36 | private static final List<ResourceKey<TrimPattern>> VANILLA_TRIM_PATTERNS = List.of(
|
| 37 | TrimPatterns.SENTRY,
|
| 38 | TrimPatterns.DUNE,
|
| 39 | TrimPatterns.COAST,
|
| 40 | TrimPatterns.WILD,
|
| 41 | TrimPatterns.WARD,
|
| 42 | TrimPatterns.EYE,
|
| 43 | TrimPatterns.VEX,
|
| 44 | TrimPatterns.TIDE,
|
| 45 | TrimPatterns.SNOUT,
|
| 46 | TrimPatterns.RIB,
|
| 47 | TrimPatterns.SPIRE,
|
| 48 | TrimPatterns.WAYFINDER,
|
| 49 | TrimPatterns.SHAPER,
|
| 50 | TrimPatterns.SILENCE,
|
| 51 | TrimPatterns.RAISER,
|
| 52 | TrimPatterns.HOST,
|
| 53 | TrimPatterns.FLOW,
|
| 54 | TrimPatterns.BOLT
|
| 55 | );
|
| 56 | private static final List<ResourceKey<TrimMaterial>> VANILLA_TRIM_MATERIALS = List.of(
|
| 57 | TrimMaterials.QUARTZ,
|
| 58 | TrimMaterials.IRON,
|
| 59 | TrimMaterials.NETHERITE,
|
| 60 | TrimMaterials.REDSTONE,
|
| 61 | TrimMaterials.COPPER,
|
| 62 | TrimMaterials.GOLD,
|
| 63 | TrimMaterials.EMERALD,
|
| 64 | TrimMaterials.DIAMOND,
|
| 65 | TrimMaterials.LAPIS,
|
| 66 | TrimMaterials.AMETHYST,
|
| 67 | TrimMaterials.RESIN
|
| 68 | );
|
| 69 | private static final ToIntFunction<ResourceKey<TrimPattern>> TRIM_PATTERN_ORDER = Util.createIndexLookup(VANILLA_TRIM_PATTERNS);
|
| 70 | private static final ToIntFunction<ResourceKey<TrimMaterial>> TRIM_MATERIAL_ORDER = Util.createIndexLookup(VANILLA_TRIM_MATERIALS);
|
| 71 | private static final DynamicCommandExceptionType ERROR_INVALID_PATTERN = new DynamicCommandExceptionType(
|
| 72 | value -> Component.translatableEscape("Invalid pattern", value)
|
| 73 | );
|
| 74 |
|
| 75 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 76 | dispatcher.register(
|
| 77 | Commands.literal("spawn_armor_trims")
|
| 78 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 79 | .then(Commands.literal("*_lag_my_game").executes(c -> spawnAllArmorTrims(c.getSource(), c.getSource().getPlayerOrException())))
|
| 80 | .then(
|
| 81 | Commands.argument("pattern", ResourceKeyArgument.key(Registries.TRIM_PATTERN))
|
| 82 | .executes(
|
| 83 | c -> spawnArmorTrim(
|
| 84 | c.getSource(), c.getSource().getPlayerOrException(), ResourceKeyArgument.getRegistryKey(c, "pattern", Registries.TRIM_PATTERN, ERROR_INVALID_PATTERN)
|
| 85 | )
|
| 86 | )
|
| 87 | )
|
| 88 | );
|
| 89 | }
|
| 90 |
|
| 91 | private static int spawnAllArmorTrims(final CommandSourceStack source, final Player player) {
|
| 92 | return spawnArmorTrims(source, player, source.getServer().registryAccess().lookupOrThrow(Registries.TRIM_PATTERN).listElements());
|
| 93 | }
|
| 94 |
|
| 95 | private static int spawnArmorTrim(final CommandSourceStack source, final Player player, final ResourceKey<TrimPattern> pattern) {
|
| 96 | return spawnArmorTrims(source, player, Stream.of(source.getServer().registryAccess().lookupOrThrow(Registries.TRIM_PATTERN).get(pattern).orElseThrow()));
|
| 97 | }
|
| 98 |
|
| 99 | private static int spawnArmorTrims(final CommandSourceStack source, final Player player, final Stream<Holder.Reference<TrimPattern>> patterns) {
|
| 100 | ServerLevel level = source.getLevel();
|
| 101 | List<Holder.Reference<TrimPattern>> sortedPatterns = patterns.sorted(Comparator.comparing(h -> TRIM_PATTERN_ORDER.applyAsInt(h.key()))).toList();
|
| 102 | List<Holder.Reference<TrimMaterial>> sortedMaterials = level.registryAccess()
|
| 103 | .lookupOrThrow(Registries.TRIM_MATERIAL)
|
| 104 | .listElements()
|
| 105 | .sorted(Comparator.comparing(h -> TRIM_MATERIAL_ORDER.applyAsInt(h.key())))
|
| 106 | .toList();
|
| 107 | List<Holder.Reference<Item>> equippableItems = findEquippableItemsWithAssets(level.registryAccess().lookupOrThrow(Registries.ITEM));
|
| 108 | BlockPos origin = player.blockPosition().relative(player.getDirection(), 5);
|
| 109 | double padding = 3.0;
|
| 110 |
|
| 111 | for (int materialIndex = 0; materialIndex < sortedMaterials.size(); materialIndex++) {
|
| 112 | Holder.Reference<TrimMaterial> material = sortedMaterials.get(materialIndex);
|
| 113 |
|
| 114 | for (int patternIndex = 0; patternIndex < sortedPatterns.size(); patternIndex++) {
|
| 115 | Holder.Reference<TrimPattern> pattern = sortedPatterns.get(patternIndex);
|
| 116 | ArmorTrim trim = new ArmorTrim(material, pattern);
|
| 117 |
|
| 118 | for (int itemIndex = 0; itemIndex < equippableItems.size(); itemIndex++) {
|
| 119 | Holder.Reference<Item> equippableItem = equippableItems.get(itemIndex);
|
| 120 | double x = origin.getX() + 0.5 - itemIndex * 3.0;
|
| 121 | double y = origin.getY() + 0.5 + materialIndex * 3.0;
|
| 122 | double z = origin.getZ() + 0.5 + patternIndex * 10;
|
| 123 | ArmorStand armorStand = new ArmorStand(level, x, y, z);
|
| 124 | armorStand.setYRot(180.0F);
|
| 125 | armorStand.setNoGravity(true);
|
| 126 | ItemStack stack = new ItemStack(equippableItem);
|
| 127 | Equippable equippable = Objects.requireNonNull(stack.get(DataComponents.EQUIPPABLE));
|
| 128 | stack.set(DataComponents.TRIM, trim);
|
| 129 | armorStand.setItemSlot(equippable.slot(), stack);
|
| 130 | if (itemIndex == 0) {
|
| 131 | armorStand.setCustomName(trim.pattern().value().copyWithStyle(trim.material()).copy().append(" & ").append(trim.material().value().description()));
|
| 132 | armorStand.setCustomNameVisible(true);
|
| 133 | } else {
|
| 134 | armorStand.setInvisible(true);
|
| 135 | }
|
| 136 |
|
| 137 | level.addFreshEntity(armorStand);
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | source.sendSuccess(() -> Component.literal("Armorstands with trimmed armor spawned around you"), true);
|
| 143 | return 1;
|
| 144 | }
|
| 145 |
|
| 146 | private static List<Holder.Reference<Item>> findEquippableItemsWithAssets(final HolderLookup<Item> items) {
|
| 147 | List<Holder.Reference<Item>> result = new ArrayList<>();
|
| 148 | items.listElements().forEach(item -> {
|
| 149 | Equippable equippable = item.components().get(DataComponents.EQUIPPABLE);
|
| 150 | if (equippable != null && equippable.slot().getType() == EquipmentSlot.Type.HUMANOID_ARMOR && equippable.assetId().isPresent()) {
|
| 151 | result.add((Holder.Reference<Item>)item);
|
| 152 | }
|
| 153 | });
|
| 154 | return result;
|
| 155 | }
|
| 156 | }
|
| 157 |
|