File size: 30,704 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package net.minecraft.server.commands;

import com.google.common.collect.Lists;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceOrIdArgument;
import net.minecraft.commands.arguments.SlotArgument;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.commands.arguments.item.ItemArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.ReloadableServerRegistries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.Vec3;

public class LootCommand {
    public static final SuggestionProvider<CommandSourceStack> SUGGEST_LOOT_TABLE = (p_326304_, p_326305_) -> {
        ReloadableServerRegistries.Holder reloadableserverregistries$holder = p_326304_.getSource().getServer().reloadableRegistries();
        return SharedSuggestionProvider.suggestResource(reloadableserverregistries$holder.getKeys(Registries.LOOT_TABLE), p_326305_);
    };
    private static final DynamicCommandExceptionType ERROR_NO_HELD_ITEMS = new DynamicCommandExceptionType(
        p_308774_ -> Component.translatableEscape("commands.drop.no_held_items", p_308774_)
    );
    private static final DynamicCommandExceptionType ERROR_NO_ENTITY_LOOT_TABLE = new DynamicCommandExceptionType(
        p_308775_ -> Component.translatableEscape("commands.drop.no_loot_table.entity", p_308775_)
    );
    private static final DynamicCommandExceptionType ERROR_NO_BLOCK_LOOT_TABLE = new DynamicCommandExceptionType(
        p_358608_ -> Component.translatableEscape("commands.drop.no_loot_table.block", p_358608_)
    );

    public static void register(CommandDispatcher<CommandSourceStack> p_214516_, CommandBuildContext p_214517_) {
        p_214516_.register(
            addTargets(
                Commands.literal("loot").requires(p_137937_ -> p_137937_.hasPermission(2)),
                (p_214520_, p_214521_) -> p_214520_.then(
                            Commands.literal("fish")
                                .then(
                                    Commands.argument("loot_table", ResourceOrIdArgument.lootTable(p_214517_))
                                        .suggests(SUGGEST_LOOT_TABLE)
                                        .then(
                                            Commands.argument("pos", BlockPosArgument.blockPos())
                                                .executes(
                                                    p_326309_ -> dropFishingLoot(
                                                            p_326309_,
                                                            ResourceOrIdArgument.getLootTable(p_326309_, "loot_table"),
                                                            BlockPosArgument.getLoadedBlockPos(p_326309_, "pos"),
                                                            ItemStack.EMPTY,
                                                            p_214521_
                                                        )
                                                )
                                                .then(
                                                    Commands.argument("tool", ItemArgument.item(p_214517_))
                                                        .executes(
                                                            p_326303_ -> dropFishingLoot(
                                                                    p_326303_,
                                                                    ResourceOrIdArgument.getLootTable(p_326303_, "loot_table"),
                                                                    BlockPosArgument.getLoadedBlockPos(p_326303_, "pos"),
                                                                    ItemArgument.getItem(p_326303_, "tool").createItemStack(1, false),
                                                                    p_214521_
                                                                )
                                                        )
                                                )
                                                .then(
                                                    Commands.literal("mainhand")
                                                        .executes(
                                                            p_326295_ -> dropFishingLoot(
                                                                    p_326295_,
                                                                    ResourceOrIdArgument.getLootTable(p_326295_, "loot_table"),
                                                                    BlockPosArgument.getLoadedBlockPos(p_326295_, "pos"),
                                                                    getSourceHandItem(p_326295_.getSource(), EquipmentSlot.MAINHAND),
                                                                    p_214521_
                                                                )
                                                        )
                                                )
                                                .then(
                                                    Commands.literal("offhand")
                                                        .executes(
                                                            p_326299_ -> dropFishingLoot(
                                                                    p_326299_,
                                                                    ResourceOrIdArgument.getLootTable(p_326299_, "loot_table"),
                                                                    BlockPosArgument.getLoadedBlockPos(p_326299_, "pos"),
                                                                    getSourceHandItem(p_326299_.getSource(), EquipmentSlot.OFFHAND),
                                                                    p_214521_
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                        .then(
                            Commands.literal("loot")
                                .then(
                                    Commands.argument("loot_table", ResourceOrIdArgument.lootTable(p_214517_))
                                        .suggests(SUGGEST_LOOT_TABLE)
                                        .executes(p_326301_ -> dropChestLoot(p_326301_, ResourceOrIdArgument.getLootTable(p_326301_, "loot_table"), p_214521_))
                                )
                        )
                        .then(
                            Commands.literal("kill")
                                .then(
                                    Commands.argument("target", EntityArgument.entity())
                                        .executes(p_180406_ -> dropKillLoot(p_180406_, EntityArgument.getEntity(p_180406_, "target"), p_214521_))
                                )
                        )
                        .then(
                            Commands.literal("mine")
                                .then(
                                    Commands.argument("pos", BlockPosArgument.blockPos())
                                        .executes(
                                            p_180403_ -> dropBlockLoot(p_180403_, BlockPosArgument.getLoadedBlockPos(p_180403_, "pos"), ItemStack.EMPTY, p_214521_)
                                        )
                                        .then(
                                            Commands.argument("tool", ItemArgument.item(p_214517_))
                                                .executes(
                                                    p_180400_ -> dropBlockLoot(
                                                            p_180400_,
                                                            BlockPosArgument.getLoadedBlockPos(p_180400_, "pos"),
                                                            ItemArgument.getItem(p_180400_, "tool").createItemStack(1, false),
                                                            p_214521_
                                                        )
                                                )
                                        )
                                        .then(
                                            Commands.literal("mainhand")
                                                .executes(
                                                    p_180397_ -> dropBlockLoot(
                                                            p_180397_,
                                                            BlockPosArgument.getLoadedBlockPos(p_180397_, "pos"),
                                                            getSourceHandItem(p_180397_.getSource(), EquipmentSlot.MAINHAND),
                                                            p_214521_
                                                        )
                                                )
                                        )
                                        .then(
                                            Commands.literal("offhand")
                                                .executes(
                                                    p_180394_ -> dropBlockLoot(
                                                            p_180394_,
                                                            BlockPosArgument.getLoadedBlockPos(p_180394_, "pos"),
                                                            getSourceHandItem(p_180394_.getSource(), EquipmentSlot.OFFHAND),
                                                            p_214521_
                                                        )
                                                )
                                        )
                                )
                        )
            )
        );
    }

    private static <T extends ArgumentBuilder<CommandSourceStack, T>> T addTargets(T p_137903_, LootCommand.TailProvider p_137904_) {
        return p_137903_.then(
                Commands.literal("replace")
                    .then(
                        Commands.literal("entity")
                            .then(
                                Commands.argument("entities", EntityArgument.entities())
                                    .then(
                                        p_137904_.construct(
                                                Commands.argument("slot", SlotArgument.slot()),
                                                (p_138032_, p_138033_, p_138034_) -> entityReplace(
                                                        EntityArgument.getEntities(p_138032_, "entities"),
                                                        SlotArgument.getSlot(p_138032_, "slot"),
                                                        p_138033_.size(),
                                                        p_138033_,
                                                        p_138034_
                                                    )
                                            )
                                            .then(
                                                p_137904_.construct(
                                                    Commands.argument("count", IntegerArgumentType.integer(0)),
                                                    (p_138025_, p_138026_, p_138027_) -> entityReplace(
                                                            EntityArgument.getEntities(p_138025_, "entities"),
                                                            SlotArgument.getSlot(p_138025_, "slot"),
                                                            IntegerArgumentType.getInteger(p_138025_, "count"),
                                                            p_138026_,
                                                            p_138027_
                                                        )
                                                )
                                            )
                                    )
                            )
                    )
                    .then(
                        Commands.literal("block")
                            .then(
                                Commands.argument("targetPos", BlockPosArgument.blockPos())
                                    .then(
                                        p_137904_.construct(
                                                Commands.argument("slot", SlotArgument.slot()),
                                                (p_138018_, p_138019_, p_138020_) -> blockReplace(
                                                        p_138018_.getSource(),
                                                        BlockPosArgument.getLoadedBlockPos(p_138018_, "targetPos"),
                                                        SlotArgument.getSlot(p_138018_, "slot"),
                                                        p_138019_.size(),
                                                        p_138019_,
                                                        p_138020_
                                                    )
                                            )
                                            .then(
                                                p_137904_.construct(
                                                    Commands.argument("count", IntegerArgumentType.integer(0)),
                                                    (p_138011_, p_138012_, p_138013_) -> blockReplace(
                                                            p_138011_.getSource(),
                                                            BlockPosArgument.getLoadedBlockPos(p_138011_, "targetPos"),
                                                            IntegerArgumentType.getInteger(p_138011_, "slot"),
                                                            IntegerArgumentType.getInteger(p_138011_, "count"),
                                                            p_138012_,
                                                            p_138013_
                                                        )
                                                )
                                            )
                                    )
                            )
                    )
            )
            .then(
                Commands.literal("insert")
                    .then(
                        p_137904_.construct(
                            Commands.argument("targetPos", BlockPosArgument.blockPos()),
                            (p_138004_, p_138005_, p_138006_) -> blockDistribute(
                                    p_138004_.getSource(), BlockPosArgument.getLoadedBlockPos(p_138004_, "targetPos"), p_138005_, p_138006_
                                )
                        )
                    )
            )
            .then(
                Commands.literal("give")
                    .then(
                        p_137904_.construct(
                            Commands.argument("players", EntityArgument.players()),
                            (p_137992_, p_137993_, p_137994_) -> playerGive(EntityArgument.getPlayers(p_137992_, "players"), p_137993_, p_137994_)
                        )
                    )
            )
            .then(
                Commands.literal("spawn")
                    .then(
                        p_137904_.construct(
                            Commands.argument("targetPos", Vec3Argument.vec3()),
                            (p_137918_, p_137919_, p_137920_) -> dropInWorld(
                                    p_137918_.getSource(), Vec3Argument.getVec3(p_137918_, "targetPos"), p_137919_, p_137920_
                                )
                        )
                    )
            );
    }

    private static Container getContainer(CommandSourceStack p_137951_, BlockPos p_137952_) throws CommandSyntaxException {
        BlockEntity blockentity = p_137951_.getLevel().getBlockEntity(p_137952_);
        if (!(blockentity instanceof Container)) {
            throw ItemCommands.ERROR_TARGET_NOT_A_CONTAINER.create(p_137952_.getX(), p_137952_.getY(), p_137952_.getZ());
        } else {
            return (Container)blockentity;
        }
    }

    private static int blockDistribute(CommandSourceStack p_137961_, BlockPos p_137962_, List<ItemStack> p_137963_, LootCommand.Callback p_137964_) throws CommandSyntaxException {
        Container container = getContainer(p_137961_, p_137962_);
        List<ItemStack> list = Lists.newArrayListWithCapacity(p_137963_.size());

        for (ItemStack itemstack : p_137963_) {
            if (distributeToContainer(container, itemstack.copy())) {
                container.setChanged();
                list.add(itemstack);
            }
        }

        p_137964_.accept(list);
        return list.size();
    }

    private static boolean distributeToContainer(Container p_137886_, ItemStack p_137887_) {
        boolean flag = false;

        for (int i = 0; i < p_137886_.getContainerSize() && !p_137887_.isEmpty(); i++) {
            ItemStack itemstack = p_137886_.getItem(i);
            if (p_137886_.canPlaceItem(i, p_137887_)) {
                if (itemstack.isEmpty()) {
                    p_137886_.setItem(i, p_137887_);
                    flag = true;
                    break;
                }

                if (canMergeItems(itemstack, p_137887_)) {
                    int j = p_137887_.getMaxStackSize() - itemstack.getCount();
                    int k = Math.min(p_137887_.getCount(), j);
                    p_137887_.shrink(k);
                    itemstack.grow(k);
                    flag = true;
                }
            }
        }

        return flag;
    }

    private static int blockReplace(
        CommandSourceStack p_137954_, BlockPos p_137955_, int p_137956_, int p_137957_, List<ItemStack> p_137958_, LootCommand.Callback p_137959_
    ) throws CommandSyntaxException {
        Container container = getContainer(p_137954_, p_137955_);
        int i = container.getContainerSize();
        if (p_137956_ >= 0 && p_137956_ < i) {
            List<ItemStack> list = Lists.newArrayListWithCapacity(p_137958_.size());

            for (int j = 0; j < p_137957_; j++) {
                int k = p_137956_ + j;
                ItemStack itemstack = j < p_137958_.size() ? p_137958_.get(j) : ItemStack.EMPTY;
                if (container.canPlaceItem(k, itemstack)) {
                    container.setItem(k, itemstack);
                    list.add(itemstack);
                }
            }

            p_137959_.accept(list);
            return list.size();
        } else {
            throw ItemCommands.ERROR_TARGET_INAPPLICABLE_SLOT.create(p_137956_);
        }
    }

    private static boolean canMergeItems(ItemStack p_137895_, ItemStack p_137896_) {
        return p_137895_.getCount() <= p_137895_.getMaxStackSize() && ItemStack.isSameItemSameComponents(p_137895_, p_137896_);
    }

    private static int playerGive(Collection<ServerPlayer> p_137985_, List<ItemStack> p_137986_, LootCommand.Callback p_137987_) throws CommandSyntaxException {
        List<ItemStack> list = Lists.newArrayListWithCapacity(p_137986_.size());

        for (ItemStack itemstack : p_137986_) {
            for (ServerPlayer serverplayer : p_137985_) {
                if (serverplayer.getInventory().add(itemstack.copy())) {
                    list.add(itemstack);
                }
            }
        }

        p_137987_.accept(list);
        return list.size();
    }

    private static void setSlots(Entity p_137889_, List<ItemStack> p_137890_, int p_137891_, int p_137892_, List<ItemStack> p_137893_) {
        for (int i = 0; i < p_137892_; i++) {
            ItemStack itemstack = i < p_137890_.size() ? p_137890_.get(i) : ItemStack.EMPTY;
            SlotAccess slotaccess = p_137889_.getSlot(p_137891_ + i);
            if (slotaccess != SlotAccess.NULL && slotaccess.set(itemstack.copy())) {
                p_137893_.add(itemstack);
            }
        }
    }

    private static int entityReplace(
        Collection<? extends Entity> p_137979_, int p_137980_, int p_137981_, List<ItemStack> p_137982_, LootCommand.Callback p_137983_
    ) throws CommandSyntaxException {
        List<ItemStack> list = Lists.newArrayListWithCapacity(p_137982_.size());

        for (Entity entity : p_137979_) {
            if (entity instanceof ServerPlayer serverplayer) {
                setSlots(entity, p_137982_, p_137980_, p_137981_, list);
                serverplayer.containerMenu.broadcastChanges();
            } else {
                setSlots(entity, p_137982_, p_137980_, p_137981_, list);
            }
        }

        p_137983_.accept(list);
        return list.size();
    }

    private static int dropInWorld(CommandSourceStack p_137946_, Vec3 p_137947_, List<ItemStack> p_137948_, LootCommand.Callback p_137949_) throws CommandSyntaxException {
        ServerLevel serverlevel = p_137946_.getLevel();
        p_137948_.forEach(p_137884_ -> {
            ItemEntity itementity = new ItemEntity(serverlevel, p_137947_.x, p_137947_.y, p_137947_.z, p_137884_.copy());
            itementity.setDefaultPickUpDelay();
            serverlevel.addFreshEntity(itementity);
        });
        p_137949_.accept(p_137948_);
        return p_137948_.size();
    }

    private static void callback(CommandSourceStack p_137966_, List<ItemStack> p_137967_) {
        if (p_137967_.size() == 1) {
            ItemStack itemstack = p_137967_.get(0);
            p_137966_.sendSuccess(() -> Component.translatable("commands.drop.success.single", itemstack.getCount(), itemstack.getDisplayName()), false);
        } else {
            p_137966_.sendSuccess(() -> Component.translatable("commands.drop.success.multiple", p_137967_.size()), false);
        }
    }

    private static void callback(CommandSourceStack p_137969_, List<ItemStack> p_137970_, ResourceKey<LootTable> p_327853_) {
        if (p_137970_.size() == 1) {
            ItemStack itemstack = p_137970_.get(0);
            p_137969_.sendSuccess(
                () -> Component.translatable(
                        "commands.drop.success.single_with_table", itemstack.getCount(), itemstack.getDisplayName(), Component.translationArg(p_327853_.location())
                    ),
                false
            );
        } else {
            p_137969_.sendSuccess(
                () -> Component.translatable("commands.drop.success.multiple_with_table", p_137970_.size(), Component.translationArg(p_327853_.location())), false
            );
        }
    }

    private static ItemStack getSourceHandItem(CommandSourceStack p_137939_, EquipmentSlot p_137940_) throws CommandSyntaxException {
        Entity entity = p_137939_.getEntityOrException();
        if (entity instanceof LivingEntity) {
            return ((LivingEntity)entity).getItemBySlot(p_137940_);
        } else {
            throw ERROR_NO_HELD_ITEMS.create(entity.getDisplayName());
        }
    }

    private static int dropBlockLoot(CommandContext<CommandSourceStack> p_137913_, BlockPos p_137914_, ItemStack p_137915_, LootCommand.DropConsumer p_137916_) throws CommandSyntaxException {
        CommandSourceStack commandsourcestack = p_137913_.getSource();
        ServerLevel serverlevel = commandsourcestack.getLevel();
        BlockState blockstate = serverlevel.getBlockState(p_137914_);
        BlockEntity blockentity = serverlevel.getBlockEntity(p_137914_);
        Optional<ResourceKey<LootTable>> optional = blockstate.getBlock().getLootTable();
        if (optional.isEmpty()) {
            throw ERROR_NO_BLOCK_LOOT_TABLE.create(blockstate.getBlock().getName());
        } else {
            LootParams.Builder lootparams$builder = new LootParams.Builder(serverlevel)
                .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(p_137914_))
                .withParameter(LootContextParams.BLOCK_STATE, blockstate)
                .withOptionalParameter(LootContextParams.BLOCK_ENTITY, blockentity)
                .withOptionalParameter(LootContextParams.THIS_ENTITY, commandsourcestack.getEntity())
                .withParameter(LootContextParams.TOOL, p_137915_);
            List<ItemStack> list = blockstate.getDrops(lootparams$builder);
            return p_137916_.accept(p_137913_, list, p_358604_ -> callback(commandsourcestack, p_358604_, optional.get()));
        }
    }

    private static int dropKillLoot(CommandContext<CommandSourceStack> p_137906_, Entity p_137907_, LootCommand.DropConsumer p_137908_) throws CommandSyntaxException {
        Optional<ResourceKey<LootTable>> optional = p_137907_.getLootTable();
        if (optional.isEmpty()) {
            throw ERROR_NO_ENTITY_LOOT_TABLE.create(p_137907_.getDisplayName());
        } else {
            CommandSourceStack commandsourcestack = p_137906_.getSource();
            LootParams.Builder lootparams$builder = new LootParams.Builder(commandsourcestack.getLevel());
            Entity entity = commandsourcestack.getEntity();
            if (entity instanceof Player player) {
                lootparams$builder.withParameter(LootContextParams.LAST_DAMAGE_PLAYER, player);
            }

            lootparams$builder.withParameter(LootContextParams.DAMAGE_SOURCE, p_137907_.damageSources().magic());
            lootparams$builder.withOptionalParameter(LootContextParams.DIRECT_ATTACKING_ENTITY, entity);
            lootparams$builder.withOptionalParameter(LootContextParams.ATTACKING_ENTITY, entity);
            lootparams$builder.withParameter(LootContextParams.THIS_ENTITY, p_137907_);
            lootparams$builder.withParameter(LootContextParams.ORIGIN, commandsourcestack.getPosition());
            LootParams lootparams = lootparams$builder.create(LootContextParamSets.ENTITY);
            LootTable loottable = commandsourcestack.getServer().reloadableRegistries().getLootTable(optional.get());
            List<ItemStack> list = loottable.getRandomItems(lootparams);
            return p_137908_.accept(p_137906_, list, p_358607_ -> callback(commandsourcestack, p_358607_, optional.get()));
        }
    }

    private static int dropChestLoot(CommandContext<CommandSourceStack> p_137933_, Holder<LootTable> p_333711_, LootCommand.DropConsumer p_137935_) throws CommandSyntaxException {
        CommandSourceStack commandsourcestack = p_137933_.getSource();
        LootParams lootparams = new LootParams.Builder(commandsourcestack.getLevel())
            .withOptionalParameter(LootContextParams.THIS_ENTITY, commandsourcestack.getEntity())
            .withParameter(LootContextParams.ORIGIN, commandsourcestack.getPosition())
            .create(LootContextParamSets.CHEST);
        return drop(p_137933_, p_333711_, lootparams, p_137935_);
    }

    private static int dropFishingLoot(
        CommandContext<CommandSourceStack> p_137927_, Holder<LootTable> p_334748_, BlockPos p_137929_, ItemStack p_137930_, LootCommand.DropConsumer p_137931_
    ) throws CommandSyntaxException {
        CommandSourceStack commandsourcestack = p_137927_.getSource();
        LootParams lootparams = new LootParams.Builder(commandsourcestack.getLevel())
            .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(p_137929_))
            .withParameter(LootContextParams.TOOL, p_137930_)
            .withOptionalParameter(LootContextParams.THIS_ENTITY, commandsourcestack.getEntity())
            .create(LootContextParamSets.FISHING);
        return drop(p_137927_, p_334748_, lootparams, p_137931_);
    }

    private static int drop(
        CommandContext<CommandSourceStack> p_287721_, Holder<LootTable> p_330660_, LootParams p_287728_, LootCommand.DropConsumer p_287770_
    ) throws CommandSyntaxException {
        CommandSourceStack commandsourcestack = p_287721_.getSource();
        List<ItemStack> list = p_330660_.value().getRandomItems(p_287728_);
        return p_287770_.accept(p_287721_, list, p_137997_ -> callback(commandsourcestack, p_137997_));
    }

    @FunctionalInterface
    interface Callback {
        void accept(List<ItemStack> p_138048_) throws CommandSyntaxException;
    }

    @FunctionalInterface
    interface DropConsumer {
        int accept(CommandContext<CommandSourceStack> p_138050_, List<ItemStack> p_138051_, LootCommand.Callback p_138052_) throws CommandSyntaxException;
    }

    @FunctionalInterface
    interface TailProvider {
        ArgumentBuilder<CommandSourceStack, ?> construct(ArgumentBuilder<CommandSourceStack, ?> p_138054_, LootCommand.DropConsumer p_138055_);
    }
}