| 1 | package net.minecraft.server.commands.item;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import com.mojang.brigadier.builder.ArgumentBuilder;
|
| 6 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.Dynamic3CommandExceptionType;
|
| 9 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 10 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.function.UnaryOperator;
|
| 15 | import net.minecraft.commands.CommandBuildContext;
|
| 16 | import net.minecraft.commands.CommandSourceStack;
|
| 17 | import net.minecraft.commands.Commands;
|
| 18 | import net.minecraft.commands.arguments.ResourceOrIdArgument;
|
| 19 | import net.minecraft.commands.arguments.SlotSourceArgument;
|
| 20 | import net.minecraft.commands.arguments.item.ItemArgument;
|
| 21 | import net.minecraft.core.Holder;
|
| 22 | import net.minecraft.network.chat.Component;
|
| 23 | import net.minecraft.server.commands.ArgProvider;
|
| 24 | import net.minecraft.server.commands.CommandResponseTracker;
|
| 25 | import net.minecraft.world.entity.SlotProvider;
|
| 26 | import net.minecraft.world.item.ItemProvider;
|
| 27 | import net.minecraft.world.item.ItemStack;
|
| 28 | import net.minecraft.world.item.slot.CountingModifier;
|
| 29 | import net.minecraft.world.item.slot.SlotCollection;
|
| 30 | import net.minecraft.world.item.slot.SlotSelector;
|
| 31 | import net.minecraft.world.item.slot.SlotSource;
|
| 32 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 33 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 34 | import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
|
| 35 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 36 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 37 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | public class ItemCommands {
|
| 41 | public static final Dynamic3CommandExceptionType ERROR_TARGET_NOT_A_CONTAINER = new Dynamic3CommandExceptionType(
|
| 42 | (x, y, z) -> Component.translatableEscape("commands.item.target.not_a_container", x, y, z)
|
| 43 | );
|
| 44 | public static final Dynamic3CommandExceptionType ERROR_SOURCE_NOT_A_CONTAINER = new Dynamic3CommandExceptionType(
|
| 45 | (x, y, z) -> Component.translatableEscape("commands.item.source.not_a_container", x, y, z)
|
| 46 | );
|
| 47 | public static final DynamicCommandExceptionType ERROR_TARGET_INAPPLICABLE_SLOT = new DynamicCommandExceptionType(
|
| 48 | slot -> Component.translatableEscape("commands.item.target.no_such_slot", slot)
|
| 49 | );
|
| 50 | private static final SimpleCommandExceptionType ERROR_TARGET_INAPPLICABLE_UNNAMED_SLOT = new SimpleCommandExceptionType(
|
| 51 | Component.translatable("commands.item.target.no_such_slot.unnamed")
|
| 52 | );
|
| 53 | private static final DynamicCommandExceptionType ERROR_SOURCE_INAPPLICABLE_SLOT = new DynamicCommandExceptionType(
|
| 54 | slot -> Component.translatableEscape("commands.item.source.no_such_slot", slot)
|
| 55 | );
|
| 56 | private static final SimpleCommandExceptionType ERROR_SOURCE_INAPPLICABLE_UNNAMED_SLOT = new SimpleCommandExceptionType(
|
| 57 | Component.translatable("commands.item.source.no_such_slot.unnamed")
|
| 58 | );
|
| 59 | public static final SimpleCommandExceptionType ERROR_TARGET_NO_CHANGES = new SimpleCommandExceptionType(Component.translatable("commands.item.target.failed"));
|
| 60 | public static final DynamicCommandExceptionType ERROR_TARGET_NO_CHANGES_KNOWN_ITEM = new DynamicCommandExceptionType(
|
| 61 | item -> Component.translatableEscape("commands.item.target.failed.known_item", item)
|
| 62 | );
|
| 63 | public static final List<ArgProvider.Factory<ItemAccessor<?>>> ALL_PROVIDERS = List.of(EntityItemAccessor.PROVIDER, BlockItemAccessor.PROVIDER);
|
| 64 | public static final List<ArgProvider<ItemAccessor<?>>> TARGET_PROVIDERS = ArgProvider.buildList("target", ALL_PROVIDERS);
|
| 65 | public static final List<ArgProvider<ItemAccessor<?>>> SOURCE_PROVIDERS = ArgProvider.buildList("source", ALL_PROVIDERS);
|
| 66 |
|
| 67 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 68 | LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal("item").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
|
| 69 |
|
| 70 | for (ArgProvider<ItemAccessor<?>> targetProvider : TARGET_PROVIDERS) {
|
| 71 | root.then(wrapSetItems(context, Commands.literal("replace"), targetProvider, ItemCommands::replaceItemProvider))
|
| 72 | .then(wrapSetItems(context, Commands.literal("fill"), targetProvider, ItemCommands::fillItemProvider))
|
| 73 | .then(wrapSetItems(context, Commands.literal("override"), targetProvider, ItemCommands::overrideItemProvider))
|
| 74 | .then(
|
| 75 | targetProvider.wrap(
|
| 76 | Commands.literal("modify"),
|
| 77 | p -> p.then(
|
| 78 | Commands.argument("slots", SlotSourceArgument.slotSource(context))
|
| 79 | .then(
|
| 80 | Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
|
| 81 | .executes(
|
| 82 | c -> modifyItems(
|
| 83 | (CommandSourceStack)c.getSource(),
|
| 84 | targetProvider.access(c),
|
| 85 | SlotSourceArgument.getSlotSource(c, "slots"),
|
| 86 | ResourceOrIdArgument.getLootModifier(c, "modifier")
|
| 87 | )
|
| 88 | )
|
| 89 | )
|
| 90 | )
|
| 91 | )
|
| 92 | );
|
| 93 | }
|
| 94 |
|
| 95 | dispatcher.register(root);
|
| 96 | }
|
| 97 |
|
| 98 | private static ArgumentBuilder<CommandSourceStack, ?> wrapSetItems(
|
| 99 | final CommandBuildContext context,
|
| 100 | final ArgumentBuilder<CommandSourceStack, ?> builder,
|
| 101 | final ArgProvider<ItemAccessor<?>> targetProvider,
|
| 102 | final ItemCommands.ItemDistributor distributor
|
| 103 | ) {
|
| 104 | return targetProvider.wrap(
|
| 105 | builder,
|
| 106 | t -> {
|
| 107 | ArgumentBuilder<CommandSourceStack, ?> slotsNode = Commands.argument("slots", SlotSourceArgument.slotSource(context));
|
| 108 |
|
| 109 | for (ArgProvider<ItemAccessor<?>> sourceProvider : SOURCE_PROVIDERS) {
|
| 110 | slotsNode.then(
|
| 111 | sourceProvider.wrap(
|
| 112 | Commands.literal("from"),
|
| 113 | p -> p.then(
|
| 114 | Commands.argument("sourceSlots", SlotSourceArgument.slotSource(context))
|
| 115 | .executes(
|
| 116 | c -> setItems(
|
| 117 | c.getSource(),
|
| 118 | targetProvider.access(c),
|
| 119 | SlotSourceArgument.getSlotSource(c, "slots"),
|
| 120 | distributor,
|
| 121 | getItems(c.getSource(), sourceProvider.access(c), SlotSourceArgument.getSlotSource(c, "sourceSlots"), null)
|
| 122 | )
|
| 123 | )
|
| 124 | .then(
|
| 125 | Commands.argument("modifier", ResourceOrIdArgument.lootModifier(context))
|
| 126 | .executes(
|
| 127 | c -> setItems(
|
| 128 | (CommandSourceStack)c.getSource(),
|
| 129 | targetProvider.access(c),
|
| 130 | SlotSourceArgument.getSlotSource(c, "slots"),
|
| 131 | distributor,
|
| 132 | getItems(
|
| 133 | (CommandSourceStack)c.getSource(),
|
| 134 | sourceProvider.access(c),
|
| 135 | SlotSourceArgument.getSlotSource(c, "sourceSlots"),
|
| 136 | ResourceOrIdArgument.getLootModifier(c, "modifier")
|
| 137 | )
|
| 138 | )
|
| 139 | )
|
| 140 | )
|
| 141 | )
|
| 142 | )
|
| 143 | );
|
| 144 | }
|
| 145 |
|
| 146 | slotsNode.then(
|
| 147 | Commands.literal("with")
|
| 148 | .then(
|
| 149 | Commands.argument("item", ItemArgument.item(context))
|
| 150 | .executes(
|
| 151 | c -> setItem(
|
| 152 | c.getSource(),
|
| 153 | targetProvider.access(c),
|
| 154 | SlotSourceArgument.getSlotSource(c, "slots"),
|
| 155 | distributor,
|
| 156 | ItemArgument.getItem(c, "item").createItemStack(1)
|
| 157 | )
|
| 158 | )
|
| 159 | .then(
|
| 160 | Commands.argument("count", IntegerArgumentType.integer(1, 99))
|
| 161 | .executes(
|
| 162 | c -> setItem(
|
| 163 | c.getSource(),
|
| 164 | targetProvider.access(c),
|
| 165 | SlotSourceArgument.getSlotSource(c, "slots"),
|
| 166 | distributor,
|
| 167 | ItemArgument.getItem(c, "item").createItemStack(IntegerArgumentType.getInteger(c, "count"))
|
| 168 | )
|
| 169 | )
|
| 170 | )
|
| 171 | )
|
| 172 | );
|
| 173 | return t.then(slotsNode);
|
| 174 | }
|
| 175 | );
|
| 176 | }
|
| 177 |
|
| 178 | public static ItemProvider replaceItemProvider(final Collection<ItemStack> items) {
|
| 179 | return ItemProvider.of(items);
|
| 180 | }
|
| 181 |
|
| 182 | public static ItemProvider fillItemProvider(final Collection<ItemStack> items) {
|
| 183 | return ItemProvider.cycle(items);
|
| 184 | }
|
| 185 |
|
| 186 | public static ItemProvider overrideItemProvider(final Collection<ItemStack> items) {
|
| 187 | return ItemProvider.of(items).orElseProvide(ItemStack.EMPTY);
|
| 188 | }
|
| 189 |
|
| 190 | private static <Target> int setItems(
|
| 191 | final CommandSourceStack source,
|
| 192 | final ItemAccessor<Target> accessor,
|
| 193 | final SlotSourceArgument.Result slotSource,
|
| 194 | final ItemProvider items,
|
| 195 | final @Nullable ItemStack knownItem
|
| 196 | ) throws CommandSyntaxException {
|
| 197 | MutableInt selectedCount = new MutableInt();
|
| 198 | SlotSelector slotSelector = SlotSelector.tracking(selectedCount);
|
| 199 | CommandResponseTracker<Target> tracker = CommandResponseTracker.create();
|
| 200 | accessor.setItems(source, slotSource.value(), (target, slots) -> {
|
| 201 | items.restart();
|
| 202 | int count = slots.replaceSlotItems(items, slotSelector);
|
| 203 | tracker.track(target, count);
|
| 204 | return count;
|
| 205 | });
|
| 206 | if (selectedCount.intValue() == 0) {
|
| 207 | throw (CommandSyntaxException)slotSource.name().map(ERROR_TARGET_INAPPLICABLE_SLOT::create).orElseGet(ERROR_TARGET_INAPPLICABLE_UNNAMED_SLOT::create);
|
| 208 | } else {
|
| 209 | return accessor.getReplaceSuccess(source, tracker, knownItem);
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | private static int setItems(
|
| 214 | final CommandSourceStack source,
|
| 215 | final ItemAccessor<?> accessor,
|
| 216 | final SlotSourceArgument.Result slotSource,
|
| 217 | final ItemCommands.ItemDistributor distributor,
|
| 218 | final Collection<ItemStack> items
|
| 219 | ) throws CommandSyntaxException {
|
| 220 | return setItems(source, accessor, slotSource, distributor.apply(items), null);
|
| 221 | }
|
| 222 |
|
| 223 | private static int setItem(
|
| 224 | final CommandSourceStack source,
|
| 225 | final ItemAccessor<?> accessor,
|
| 226 | final SlotSourceArgument.Result slotSource,
|
| 227 | final ItemCommands.ItemDistributor distributor,
|
| 228 | final ItemStack item
|
| 229 | ) throws CommandSyntaxException {
|
| 230 | return setItems(source, accessor, slotSource, distributor.apply(List.of(item)), item);
|
| 231 | }
|
| 232 |
|
| 233 | private static <Target> int modifyItems(
|
| 234 | final CommandSourceStack source, final ItemAccessor<Target> accessor, final SlotSourceArgument.Result slotSource, final Holder<LootItemFunction> modifier
|
| 235 | ) throws CommandSyntaxException {
|
| 236 | MutableInt selectedCount = new MutableInt();
|
| 237 | SlotSelector slotSelector = SlotSelector.tracking(SlotSelector.NON_EMPTY_SLOTS, selectedCount);
|
| 238 | CountingModifier countingModifier = new CountingModifier(applyModifier(source, modifier.value()));
|
| 239 | CommandResponseTracker<Target> tracker = CommandResponseTracker.create();
|
| 240 | accessor.setItems(source, slotSource.value(), (target, slots) -> {
|
| 241 | int lastUpdated = countingModifier.updatedCount();
|
| 242 | slots.modifySlots(countingModifier, slotSelector);
|
| 243 | int count = countingModifier.updatedCount() - lastUpdated;
|
| 244 | tracker.track(target, count);
|
| 245 | return count;
|
| 246 | });
|
| 247 | if (selectedCount.intValue() == 0) {
|
| 248 | throw (CommandSyntaxException)slotSource.name().map(ERROR_TARGET_INAPPLICABLE_SLOT::create).orElseGet(ERROR_TARGET_INAPPLICABLE_UNNAMED_SLOT::create);
|
| 249 | } else {
|
| 250 | return accessor.getModifySuccess(source, tracker);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | private static Collection<ItemStack> getItems(
|
| 255 | final CommandSourceStack source,
|
| 256 | final ItemAccessor<?> accessor,
|
| 257 | final SlotSourceArgument.Result slotSource,
|
| 258 | final @Nullable Holder<LootItemFunction> modifier
|
| 259 | ) throws CommandSyntaxException {
|
| 260 | SlotCollection slots = accessor.getSlots(source, slotSource.value());
|
| 261 | Collection<ItemStack> items = modifier != null ? slots.itemCopies().map(applyModifier(source, modifier.value())).toList() : slots.itemCopies().toList();
|
| 262 | if (items.isEmpty()) {
|
| 263 | throw (CommandSyntaxException)slotSource.name().map(ERROR_SOURCE_INAPPLICABLE_SLOT::create).orElseGet(ERROR_SOURCE_INAPPLICABLE_UNNAMED_SLOT::create);
|
| 264 | } else {
|
| 265 | return items;
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | private static UnaryOperator<ItemStack> applyModifier(final CommandSourceStack source, final LootItemFunction modifier) {
|
| 270 | LootParams lootParams = new LootParams.Builder(source.getLevel())
|
| 271 | .withParameter(LootContextParams.ORIGIN, source.getPosition())
|
| 272 | .withOptionalParameter(LootContextParams.THIS_ENTITY, source.getEntity())
|
| 273 | .create(LootContextParamSets.COMMAND);
|
| 274 | LootContext context = new LootContext.Builder(lootParams).create(Optional.empty());
|
| 275 | context.pushVisitedElement(LootContext.createVisitedEntry(modifier));
|
| 276 | return item -> {
|
| 277 | ItemStack newItem = modifier.apply(item, context);
|
| 278 | newItem.limitSize(newItem.getMaxStackSize());
|
| 279 | return newItem;
|
| 280 | };
|
| 281 | }
|
| 282 |
|
| 283 | static SlotCollection getSlotsFromProvider(final CommandSourceStack source, final SlotProvider container, final SlotSource slotSource) {
|
| 284 | LootParams lootParams = new LootParams.Builder(source.getLevel())
|
| 285 | .withParameter(LootContextParams.ORIGIN, source.getPosition())
|
| 286 | .withParameter(LootContextParams.CONTAINER, container)
|
| 287 | .withOptionalParameter(LootContextParams.THIS_ENTITY, source.getEntity())
|
| 288 | .create(LootContextParamSets.COMMAND_SLOT_SOURCE);
|
| 289 | LootContext context = new LootContext.Builder(lootParams).create(Optional.empty());
|
| 290 | context.pushVisitedElement(LootContext.createVisitedEntry(slotSource));
|
| 291 | return slotSource.provide(context);
|
| 292 | }
|
| 293 |
|
| 294 | @FunctionalInterface
|
| 295 | private interface ItemDistributor {
|
| 296 | ItemProvider apply(Collection<ItemStack> items);
|
| 297 | }
|
| 298 | }
|
| 299 |
|