| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.builder.ArgumentBuilder;
|
| 4 | import com.mojang.brigadier.context.CommandContext;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.minecraft.commands.CommandSourceStack;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.arguments.EntityArgument;
|
| 11 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 18 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 19 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 20 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 21 |
|
| 22 | public class LootContextSources {
|
| 23 | private static ArgumentBuilder<CommandSourceStack, ?> decorate(
|
| 24 | final ArgumentBuilder<CommandSourceStack, ?> node, final LootContextSources.NodeVisitor nodeVisitor, final LootContextSources.ContextDecorator decorator
|
| 25 | ) {
|
| 26 | nodeVisitor.visit(decorator, node::then);
|
| 27 | return node;
|
| 28 | }
|
| 29 |
|
| 30 | public static <T extends ArgumentBuilder<CommandSourceStack, T>> T addContextSources(final T node, final LootContextSources.NodeVisitor factory) {
|
| 31 | return node.then(decorate(Commands.literal("default"), factory, (var0, params) -> params.create(LootContextParamSets.COMMAND_COMPUTE_DEFAULT)))
|
| 32 | .then(
|
| 33 | Commands.literal("block")
|
| 34 | .then(
|
| 35 | decorate(
|
| 36 | Commands.argument("computePos", BlockPosArgument.blockPos()),
|
| 37 | factory,
|
| 38 | (context, params) -> {
|
| 39 | BlockPos pos = BlockPosArgument.getLoadedBlockPos(context, "computePos");
|
| 40 | CommandSourceStack source = context.getSource();
|
| 41 | ServerLevel level = source.getLevel();
|
| 42 | BlockState blockState = level.getBlockState(pos);
|
| 43 | BlockEntity blockEntity = level.getBlockEntity(pos);
|
| 44 | return params.withParameter(LootContextParams.BLOCK_STATE, blockState)
|
| 45 | .withOptionalParameter(LootContextParams.BLOCK_ENTITY, blockEntity)
|
| 46 | .create(LootContextParamSets.COMMAND_COMPUTE_POSITION);
|
| 47 | }
|
| 48 | )
|
| 49 | )
|
| 50 | )
|
| 51 | .then(Commands.literal("entity").then(decorate(Commands.argument("computeTarget", EntityArgument.entity()), factory, (context, params) -> {
|
| 52 | Entity target = EntityArgument.getEntity(context, "computeTarget");
|
| 53 | return params.withParameter(LootContextParams.TARGET_ENTITY, target).create(LootContextParamSets.COMMAND_COMPUTE_ENTITY);
|
| 54 | })));
|
| 55 | }
|
| 56 |
|
| 57 | @FunctionalInterface
|
| 58 | public interface ContextDecorator {
|
| 59 | LootParams customize(CommandContext<CommandSourceStack> context, LootParams.Builder params) throws CommandSyntaxException;
|
| 60 |
|
| 61 | default LootParams createParams(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 62 | CommandSourceStack source = context.getSource();
|
| 63 | ServerLevel level = source.getLevel();
|
| 64 | LootParams.Builder commonContext = new LootParams.Builder(level)
|
| 65 | .withOptionalParameter(LootContextParams.THIS_ENTITY, source.getEntity())
|
| 66 | .withParameter(LootContextParams.ORIGIN, source.getPosition());
|
| 67 | return this.customize(context, commonContext);
|
| 68 | }
|
| 69 |
|
| 70 | default LootContext createContext(final CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
| 71 | LootParams params = this.createParams(context);
|
| 72 | return new LootContext.Builder(params).create(Optional.empty());
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | @FunctionalInterface
|
| 77 | public interface NodeVisitor {
|
| 78 | void visit(LootContextSources.ContextDecorator contextDecorator, Consumer<ArgumentBuilder<CommandSourceStack, ?>> output);
|
| 79 | }
|
| 80 | }
|
| 81 |
|