aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0102-LootTable-API-and-replenishable-lootables.patch
blob: b09c92c49814b56d9003bd0dc065c97171ab6322 (plain)
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 1 May 2016 21:19:14 -0400
Subject: [PATCH] LootTable API and replenishable lootables

Provides an API to control the loot table for an object.
Also provides a feature that any Lootable Inventory (Chests in Structures)
can automatically replenish after a given time.

This feature is good for long term worlds so that newer players
do not suffer with "Every chest has been looted"

== AT ==
public org.bukkit.craftbukkit.block.CraftBlockEntityState getTileEntity()Lnet/minecraft/world/level/block/entity/BlockEntity;
public org.bukkit.craftbukkit.block.CraftLootable setLootTable(Lorg/bukkit/loot/LootTable;J)V
public org.bukkit.craftbukkit.entity.CraftMinecartContainer setLootTable(Lorg/bukkit/loot/LootTable;J)V

diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperContainerEntityLootableInventory.java b/src/main/java/com/destroystokyo/paper/loottable/PaperContainerEntityLootableInventory.java
new file mode 100644
index 0000000000000000000000000000000000000000..7f5283a8ee74253e326fe994f20ae00cf3c75e1b
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperContainerEntityLootableInventory.java
@@ -0,0 +1,66 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.Optionull;
+import net.minecraft.core.registries.Registries;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.entity.vehicle.AbstractMinecartContainer;
+import net.minecraft.world.entity.vehicle.ContainerEntity;
+import net.minecraft.world.level.Level;
+import org.bukkit.Bukkit;
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
+
+public class PaperContainerEntityLootableInventory implements PaperLootableEntityInventory {
+
+    private final ContainerEntity entity;
+
+    public PaperContainerEntityLootableInventory(ContainerEntity entity) {
+        this.entity = entity;
+    }
+
+    @Override
+    public org.bukkit.loot.LootTable getLootTable() {
+        return entity.getLootTable() != null ? Optionull.map(entity.getLootTable(), rk -> Bukkit.getLootTable(CraftNamespacedKey.fromMinecraft(rk.location()))) : null;
+    }
+
+    @Override
+    public void setLootTable(org.bukkit.loot.LootTable table, long seed) {
+        setLootTable(table);
+        setSeed(seed);
+    }
+
+    @Override
+    public void setSeed(long seed) {
+        entity.setLootTableSeed(seed);
+    }
+
+    @Override
+    public long getSeed() {
+        return entity.getLootTableSeed();
+    }
+
+    @Override
+    public void setLootTable(org.bukkit.loot.LootTable table) {
+        entity.setLootTable((table == null) ? null : ResourceKey.create(Registries.LOOT_TABLE, CraftNamespacedKey.toMinecraft(table.getKey())));
+    }
+
+    @Override
+    public PaperLootableInventoryData getLootableData() {
+        return entity.getLootableData();
+    }
+
+    @Override
+    public Entity getHandle() {
+        return entity.getEntity();
+    }
+
+    @Override
+    public LootableInventory getAPILootableInventory() {
+        return (LootableInventory) entity.getEntity().getBukkitEntity();
+    }
+
+    @Override
+    public Level getNMSWorld() {
+        return entity.level();
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperLootableBlockInventory.java b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableBlockInventory.java
new file mode 100644
index 0000000000000000000000000000000000000000..24c6ff57cd25533e71f8a1d0b3c0ece2fdbbf87e
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableBlockInventory.java
@@ -0,0 +1,33 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.core.BlockPos;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
+import org.bukkit.Chunk;
+import org.bukkit.block.Block;
+
+public interface PaperLootableBlockInventory extends LootableBlockInventory, PaperLootableInventory {
+
+    RandomizableContainerBlockEntity getTileEntity();
+
+    @Override
+    default LootableInventory getAPILootableInventory() {
+        return this;
+    }
+
+    @Override
+    default Level getNMSWorld() {
+        return this.getTileEntity().getLevel();
+    }
+
+    default Block getBlock() {
+        final BlockPos position = this.getTileEntity().getBlockPos();
+        final Chunk bukkitChunk = this.getBukkitWorld().getChunkAt(org.bukkit.craftbukkit.block.CraftBlock.at(this.getNMSWorld(), position));
+        return bukkitChunk.getBlock(position.getX(), position.getY(), position.getZ());
+    }
+
+    @Override
+    default PaperLootableInventoryData getLootableData() {
+        return this.getTileEntity().lootableData;
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperLootableEntityInventory.java b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableEntityInventory.java
new file mode 100644
index 0000000000000000000000000000000000000000..2fba5bc0f982e143ad5f5bda55d768edc5f847df
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableEntityInventory.java
@@ -0,0 +1,28 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.world.level.Level;
+import org.bukkit.entity.Entity;
+
+public interface PaperLootableEntityInventory extends LootableEntityInventory, PaperLootableInventory {
+
+    net.minecraft.world.entity.Entity getHandle();
+
+    @Override
+    default LootableInventory getAPILootableInventory() {
+        return this;
+    }
+
+    default Entity getEntity() {
+        return getHandle().getBukkitEntity();
+    }
+
+    @Override
+    default Level getNMSWorld() {
+        return getHandle().getCommandSenderWorld();
+    }
+
+    @Override
+    default PaperLootableInventoryData getLootableData() {
+        return getHandle().lootableData;
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventory.java b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventory.java
new file mode 100644
index 0000000000000000000000000000000000000000..8e6dac2cef7af26ad74928eff631c1826c2980bb
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventory.java
@@ -0,0 +1,75 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.loot.Lootable;
+import java.util.UUID;
+import net.minecraft.world.level.Level;
+
+public interface PaperLootableInventory extends LootableInventory, Lootable {
+
+    PaperLootableInventoryData getLootableData();
+    LootableInventory getAPILootableInventory();
+
+    Level getNMSWorld();
+
+    default org.bukkit.World getBukkitWorld() {
+        return getNMSWorld().getWorld();
+    }
+
+    @Override
+    default boolean isRefillEnabled() {
+        return getNMSWorld().paperConfig().lootables.autoReplenish;
+    }
+
+    @Override
+    default boolean hasBeenFilled() {
+        return getLastFilled() != -1;
+    }
+
+    @Override
+    default boolean hasPlayerLooted(UUID player) {
+        return getLootableData().hasPlayerLooted(player);
+    }
+
+    @Override
+    default boolean canPlayerLoot(final UUID player) {
+        return getLootableData().canPlayerLoot(player, this.getNMSWorld().paperConfig());
+    }
+
+    @Override
+    default Long getLastLooted(UUID player) {
+        return getLootableData().getLastLooted(player);
+    }
+
+    @Override
+    default boolean setHasPlayerLooted(UUID player, boolean looted) {
+        final boolean hasLooted = hasPlayerLooted(player);
+        if (hasLooted != looted) {
+            getLootableData().setPlayerLootedState(player, looted);
+        }
+        return hasLooted;
+    }
+
+    @Override
+    default boolean hasPendingRefill() {
+        long nextRefill = getLootableData().getNextRefill();
+        return nextRefill != -1 && nextRefill > getLootableData().getLastFill();
+    }
+
+    @Override
+    default long getLastFilled() {
+        return getLootableData().getLastFill();
+    }
+
+    @Override
+    default long getNextRefill() {
+        return getLootableData().getNextRefill();
+    }
+
+    @Override
+    default long setNextRefill(long refillAt) {
+        if (refillAt < -1) {
+            refillAt = -1;
+        }
+        return getLootableData().setNextRefill(refillAt);
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventoryData.java b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventoryData.java
new file mode 100644
index 0000000000000000000000000000000000000000..6e72c43b9d3834eb91c02ce68e7d114ad907812d
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperLootableInventoryData.java
@@ -0,0 +1,188 @@
+package com.destroystokyo.paper.loottable;
+
+import io.papermc.paper.configuration.WorldConfiguration;
+import io.papermc.paper.configuration.type.DurationOrDisabled;
+import java.time.temporal.ChronoUnit;
+import java.util.concurrent.TimeUnit;
+import org.bukkit.entity.Player;
+import org.bukkit.loot.LootTable;
+import javax.annotation.Nullable;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.nbt.ListTag;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.UUID;
+
+public class PaperLootableInventoryData {
+
+    private static final Random RANDOM = new Random();
+
+    private long lastFill = -1;
+    private long nextRefill = -1;
+    private int numRefills = 0;
+    private Map<UUID, Long> lootedPlayers;
+    private final PaperLootableInventory lootable;
+
+    public PaperLootableInventoryData(PaperLootableInventory lootable) {
+        this.lootable = lootable;
+    }
+
+    long getLastFill() {
+        return this.lastFill;
+    }
+
+    long getNextRefill() {
+        return this.nextRefill;
+    }
+
+    long setNextRefill(long nextRefill) {
+        long prev = this.nextRefill;
+        this.nextRefill = nextRefill;
+        return prev;
+    }
+
+    public boolean shouldReplenish(@Nullable net.minecraft.world.entity.player.Player player) {
+        LootTable table = this.lootable.getLootTable();
+
+        // No Loot Table associated
+        if (table == null) {
+            return false;
+        }
+
+        // ALWAYS process the first fill or if the feature is disabled
+        if (this.lastFill == -1 || !this.lootable.getNMSWorld().paperConfig().lootables.autoReplenish) {
+            return true;
+        }
+
+        // Only process refills when a player is set
+        if (player == null) {
+            return false;
+        }
+
+        // Chest is not scheduled for refill
+        if (this.nextRefill == -1) {
+            return false;
+        }
+
+        final WorldConfiguration paperConfig = this.lootable.getNMSWorld().paperConfig();
+
+        // Check if max refills has been hit
+        if (paperConfig.lootables.maxRefills != -1 && this.numRefills >= paperConfig.lootables.maxRefills) {
+            return false;
+        }
+
+        // Refill has not been reached
+        if (this.nextRefill > System.currentTimeMillis()) {
+            return false;
+        }
+
+
+        final Player bukkitPlayer = (Player) player.getBukkitEntity();
+        LootableInventoryReplenishEvent event = new LootableInventoryReplenishEvent(bukkitPlayer, lootable.getAPILootableInventory());
+        event.setCancelled(!canPlayerLoot(player.getUUID(), paperConfig));
+        return event.callEvent();
+    }
+    public void processRefill(@Nullable net.minecraft.world.entity.player.Player player) {
+        this.lastFill = System.currentTimeMillis();
+        final WorldConfiguration paperConfig = this.lootable.getNMSWorld().paperConfig();
+        if (paperConfig.lootables.autoReplenish) {
+            long min = paperConfig.lootables.refreshMin.seconds();
+            long max = paperConfig.lootables.refreshMax.seconds();
+            this.nextRefill = this.lastFill + (min + RANDOM.nextLong(max - min + 1)) * 1000L;
+            this.numRefills++;
+            if (paperConfig.lootables.resetSeedOnFill) {
+                this.lootable.setSeed(0);
+            }
+            if (player != null) { // This means that numRefills can be incremented without a player being in the lootedPlayers list - Seems to be EntityMinecartChest specific
+                this.setPlayerLootedState(player.getUUID(), true);
+            }
+        } else {
+            this.lootable.clearLootTable();
+        }
+    }
+
+
+    public void loadNbt(CompoundTag base) {
+        if (!base.contains("Paper.LootableData", 10)) { // 10 = compound
+            return;
+        }
+        CompoundTag comp = base.getCompound("Paper.LootableData");
+        if (comp.contains("lastFill")) {
+            this.lastFill = comp.getLong("lastFill");
+        }
+        if (comp.contains("nextRefill")) {
+            this.nextRefill = comp.getLong("nextRefill");
+        }
+
+        if (comp.contains("numRefills")) {
+            this.numRefills = comp.getInt("numRefills");
+        }
+        if (comp.contains("lootedPlayers", net.minecraft.nbt.Tag.TAG_LIST)) {
+            ListTag list = comp.getList("lootedPlayers", net.minecraft.nbt.Tag.TAG_COMPOUND);
+            final int size = list.size();
+            if (size > 0) {
+                this.lootedPlayers = new HashMap<>(list.size());
+            }
+            for (int i = 0; i < size; i++) {
+                final CompoundTag cmp = list.getCompound(i);
+                lootedPlayers.put(cmp.getUUID("UUID"), cmp.getLong("Time"));
+            }
+        }
+    }
+    public void saveNbt(CompoundTag base) {
+        CompoundTag comp = new CompoundTag();
+        if (this.nextRefill != -1) {
+            comp.putLong("nextRefill", this.nextRefill);
+        }
+        if (this.lastFill != -1) {
+            comp.putLong("lastFill", this.lastFill);
+        }
+        if (this.numRefills != 0) {
+            comp.putInt("numRefills", this.numRefills);
+        }
+        if (this.lootedPlayers != null && !this.lootedPlayers.isEmpty()) {
+            ListTag list = new ListTag();
+            for (Map.Entry<UUID, Long> entry : this.lootedPlayers.entrySet()) {
+                CompoundTag cmp = new CompoundTag();
+                cmp.putUUID("UUID", entry.getKey());
+                cmp.putLong("Time", entry.getValue());
+                list.add(cmp);
+            }
+            comp.put("lootedPlayers", list);
+        }
+
+        if (!comp.isEmpty()) {
+            base.put("Paper.LootableData", comp);
+        }
+    }
+
+    void setPlayerLootedState(UUID player, boolean looted) {
+        if (looted && this.lootedPlayers == null) {
+            this.lootedPlayers = new HashMap<>();
+        }
+        if (looted) {
+            this.lootedPlayers.put(player, System.currentTimeMillis());
+        } else if (this.lootedPlayers != null) {
+            this.lootedPlayers.remove(player);
+        }
+    }
+
+    boolean canPlayerLoot(final UUID player, final WorldConfiguration worldConfiguration) {
+        final Long lastLooted = getLastLooted(player);
+        if (!worldConfiguration.lootables.restrictPlayerReloot || lastLooted == null) return true;
+
+        final DurationOrDisabled restrictPlayerRelootTime = worldConfiguration.lootables.restrictPlayerRelootTime;
+        if (restrictPlayerRelootTime.value().isEmpty()) return false;
+
+        return TimeUnit.SECONDS.toMillis(restrictPlayerRelootTime.value().get().seconds()) + lastLooted < System.currentTimeMillis();
+    }
+
+    boolean hasPlayerLooted(UUID player) {
+        return this.lootedPlayers != null && this.lootedPlayers.containsKey(player);
+    }
+
+    Long getLastLooted(UUID player) {
+        return lootedPlayers != null ? lootedPlayers.get(player) : null;
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/PaperTileEntityLootableInventory.java b/src/main/java/com/destroystokyo/paper/loottable/PaperTileEntityLootableInventory.java
new file mode 100644
index 0000000000000000000000000000000000000000..fb72bdea520ccc0928cfbda0569e02a1917a7e86
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/PaperTileEntityLootableInventory.java
@@ -0,0 +1,67 @@
+package com.destroystokyo.paper.loottable;
+
+import io.papermc.paper.util.MCUtil;
+import net.minecraft.core.registries.Registries;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
+import org.bukkit.Bukkit;
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
+
+public class PaperTileEntityLootableInventory implements PaperLootableBlockInventory {
+    private RandomizableContainerBlockEntity tileEntityLootable;
+
+    public PaperTileEntityLootableInventory(RandomizableContainerBlockEntity tileEntityLootable) {
+        this.tileEntityLootable = tileEntityLootable;
+    }
+
+    @Override
+    public org.bukkit.loot.LootTable getLootTable() {
+        return tileEntityLootable.lootTable != null ? Bukkit.getLootTable(CraftNamespacedKey.fromMinecraft(tileEntityLootable.lootTable.location())) : null;
+    }
+
+    @Override
+    public void setLootTable(org.bukkit.loot.LootTable table, long seed) {
+        setLootTable(table);
+        setSeed(seed);
+    }
+
+    @Override
+    public void setLootTable(org.bukkit.loot.LootTable table) {
+        tileEntityLootable.lootTable = (table == null) ? null : ResourceKey.create(Registries.LOOT_TABLE, CraftNamespacedKey.toMinecraft(table.getKey()));
+    }
+
+    @Override
+    public void setSeed(long seed) {
+        tileEntityLootable.lootTableSeed = seed;
+    }
+
+    @Override
+    public long getSeed() {
+        return tileEntityLootable.lootTableSeed;
+    }
+
+    @Override
+    public PaperLootableInventoryData getLootableData() {
+        return tileEntityLootable.lootableData;
+    }
+
+    @Override
+    public RandomizableContainerBlockEntity getTileEntity() {
+        return tileEntityLootable;
+    }
+
+    @Override
+    public LootableInventory getAPILootableInventory() {
+        Level world = tileEntityLootable.getLevel();
+        if (world == null) {
+            return null;
+        }
+        return (LootableInventory) getBukkitWorld().getBlockAt(MCUtil.toLocation(world, tileEntityLootable.getBlockPos())).getState();
+    }
+
+    @Override
+    public Level getNMSWorld() {
+        return tileEntityLootable.getLevel();
+    }
+}
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index bc098b6a9c0e3b19b9154ab3727949232bddb20a..10687787a5fd1a81ad5a625848db4649381eab67 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -242,6 +242,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
     }
     // Paper end - Share random for entities to make them more random
 
+    public com.destroystokyo.paper.loottable.PaperLootableInventoryData lootableData; // Paper
     private CraftEntity bukkitEntity;
 
     public CraftEntity getBukkitEntity() {
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecartContainer.java b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecartContainer.java
index 67840327e934b631a85cf2d64911f5cfab4402b1..2704389bc3ec6dbbf1b568a4380972f8c0d62d15 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecartContainer.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecartContainer.java
@@ -35,6 +35,20 @@ public abstract class AbstractMinecartContainer extends AbstractMinecart impleme
     public ResourceKey<LootTable> lootTable;
     public long lootTableSeed;
 
+    // Paper start
+    {
+        this.lootableData = new com.destroystokyo.paper.loottable.PaperLootableInventoryData(new com.destroystokyo.paper.loottable.PaperContainerEntityLootableInventory(this));
+    }
+    @Override
+    public Entity getEntity() {
+        return this;
+    }
+
+    @Override
+    public com.destroystokyo.paper.loottable.PaperLootableInventoryData getLootableData() {
+        return this.lootableData;
+    }
+    // Paper end
     // CraftBukkit start
     public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
     private int maxStack = MAX_STACK;
@@ -144,12 +158,14 @@ public abstract class AbstractMinecartContainer extends AbstractMinecart impleme
     @Override
     protected void addAdditionalSaveData(CompoundTag nbt) {
         super.addAdditionalSaveData(nbt);
+        this.lootableData.saveNbt(nbt); // Paper
         this.addChestVehicleSaveData(nbt, this.registryAccess());
     }
 
     @Override
     protected void readAdditionalSaveData(CompoundTag nbt) {
         super.readAdditionalSaveData(nbt);
+        this.lootableData.loadNbt(nbt); // Paper
         this.readChestVehicleSaveData(nbt, this.registryAccess());
     }
 
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/ChestBoat.java b/src/main/java/net/minecraft/world/entity/vehicle/ChestBoat.java
index 025e57993b85402c48db29d65ea3ceaf277ff27a..b04f7ce0805453f6c737fa9dc11c4129ca64e934 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/ChestBoat.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/ChestBoat.java
@@ -70,12 +70,14 @@ public class ChestBoat extends Boat implements HasCustomInventoryScreen, Contain
     @Override
     protected void addAdditionalSaveData(CompoundTag nbt) {
         super.addAdditionalSaveData(nbt);
+        this.lootableData.saveNbt(nbt); // Paper
         this.addChestVehicleSaveData(nbt, this.registryAccess());
     }
 
     @Override
     protected void readAdditionalSaveData(CompoundTag nbt) {
         super.readAdditionalSaveData(nbt);
+        this.lootableData.loadNbt(nbt); // Paper
         this.readChestVehicleSaveData(nbt, this.registryAccess());
     }
 
@@ -257,6 +259,20 @@ public class ChestBoat extends Boat implements HasCustomInventoryScreen, Contain
         this.level().gameEvent((Holder) GameEvent.CONTAINER_CLOSE, this.position(), GameEvent.Context.of((Entity) player));
     }
 
+    // Paper start
+    {
+        this.lootableData = new com.destroystokyo.paper.loottable.PaperLootableInventoryData(new com.destroystokyo.paper.loottable.PaperContainerEntityLootableInventory(this));
+    }
+    @Override
+    public Entity getEntity() {
+        return this;
+    }
+
+    @Override
+    public com.destroystokyo.paper.loottable.PaperLootableInventoryData getLootableData() {
+        return this.lootableData;
+    }
+    // Paper end
     // CraftBukkit start
     public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
     private int maxStack = MAX_STACK;
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/ContainerEntity.java b/src/main/java/net/minecraft/world/entity/vehicle/ContainerEntity.java
index dbde2402fd46b0d06e8efeb90be6fb98d7ae7798..f33e5cf6d456e615050047e924d9b24268a2c51e 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/ContainerEntity.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/ContainerEntity.java
@@ -65,9 +65,8 @@ public interface ContainerEntity extends Container, MenuProvider {
             if (this.getLootTableSeed() != 0L) {
                 nbt.putLong("LootTableSeed", this.getLootTableSeed());
             }
-        } else {
-            ContainerHelper.saveAllItems(nbt, this.getItemStacks(), registriesLookup);
         }
+        ContainerHelper.saveAllItems(nbt, this.getItemStacks(), registriesLookup); // Paper - always save the items, table may still remain
     }
 
     default void readChestVehicleSaveData(CompoundTag nbt, HolderLookup.Provider registriesLookup) {
@@ -75,9 +74,8 @@ public interface ContainerEntity extends Container, MenuProvider {
         if (nbt.contains("LootTable", 8)) {
             this.setLootTable(ResourceKey.create(Registries.LOOT_TABLE, new ResourceLocation(nbt.getString("LootTable"))));
             this.setLootTableSeed(nbt.getLong("LootTableSeed"));
-        } else {
-            ContainerHelper.loadAllItems(nbt, this.getItemStacks(), registriesLookup);
         }
+        ContainerHelper.loadAllItems(nbt, this.getItemStacks(), registriesLookup); // Paper - always save the items, table may still remain
     }
 
     default void chestVehicleDestroyed(DamageSource source, Level world, Entity vehicle) {
@@ -99,13 +97,13 @@ public interface ContainerEntity extends Container, MenuProvider {
 
     default void unpackChestVehicleLootTable(@Nullable Player player) {
         MinecraftServer minecraftServer = this.level().getServer();
-        if (this.getLootTable() != null && minecraftServer != null) {
+        if (this.getLootableData().shouldReplenish(player) && minecraftServer != null) { // Paper
             LootTable lootTable = minecraftServer.reloadableRegistries().getLootTable(this.getLootTable());
             if (player != null) {
                 CriteriaTriggers.GENERATE_LOOT.trigger((ServerPlayer)player, this.getLootTable());
             }
 
-            this.setLootTable(null);
+            this.getLootableData().processRefill(player); // Paper
             LootParams.Builder builder = new LootParams.Builder((ServerLevel)this.level()).withParameter(LootContextParams.ORIGIN, this.position());
             if (player != null) {
                 builder.withLuck(player.getLuck()).withParameter(LootContextParams.THIS_ENTITY, player);
@@ -175,4 +173,13 @@ public interface ContainerEntity extends Container, MenuProvider {
     default boolean isChestVehicleStillValid(Player player) {
         return !this.isRemoved() && player.canInteractWithEntity(this.getBoundingBox(), 4.0);
     }
+    // Paper start
+    default Entity getEntity() {
+        throw new UnsupportedOperationException();
+    }
+
+    default com.destroystokyo.paper.loottable.PaperLootableInventoryData getLootableData() {
+        throw new UnsupportedOperationException();
+    }
+    // Paper end
 }
diff --git a/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
index c2493c15d8fe4587d6ee2db100cc13303b66b39b..5b183f12717008dd6c9863938c17b2e668ebded4 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
@@ -19,6 +19,7 @@ public abstract class RandomizableContainerBlockEntity extends BaseContainerBloc
     @Nullable
     public ResourceKey<LootTable> lootTable;
     public long lootTableSeed = 0L;
+    public final com.destroystokyo.paper.loottable.PaperLootableInventoryData lootableData = new com.destroystokyo.paper.loottable.PaperLootableInventoryData(new com.destroystokyo.paper.loottable.PaperTileEntityLootableInventory(this)); // Paper
 
     protected RandomizableContainerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
         super(type, pos, state);
@@ -45,6 +46,56 @@ public abstract class RandomizableContainerBlockEntity extends BaseContainerBloc
         this.lootTableSeed = lootTableSeed;
     }
 
+    // Paper start
+    @Override
+    public boolean tryLoadLootTable(final net.minecraft.nbt.CompoundTag nbt) {
+        // Copied from super with changes, always check the original method
+        this.lootableData.loadNbt(nbt); // Paper
+        if (nbt.contains("LootTable", 8)) {
+            this.setLootTable(net.minecraft.Optionull.map(net.minecraft.resources.ResourceLocation.tryParse(nbt.getString("LootTable")), rl -> ResourceKey.create(net.minecraft.core.registries.Registries.LOOT_TABLE, rl)));
+            try { if (this.lootTable != null) org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(this.lootTable.location()); } catch (IllegalArgumentException ex) { this.lootTable = null; } // Paper - validate
+            if (nbt.contains("LootTableSeed", 4)) {
+                this.setLootTableSeed(nbt.getLong("LootTableSeed"));
+            } else {
+                this.setLootTableSeed(0L);
+            }
+            return false; // Paper - always load the items, table may still remain
+        } else {
+            return false;
+        }
+    }
+
+    @Override
+    public boolean trySaveLootTable(final net.minecraft.nbt.CompoundTag nbt) {
+        this.lootableData.saveNbt(nbt);
+        RandomizableContainer.super.trySaveLootTable(nbt);
+        return false;
+    }
+
+    @Override
+    public void unpackLootTable(@org.jetbrains.annotations.Nullable final Player player) {
+        // Copied from super with changes, always check the original method
+        net.minecraft.world.level.Level level = this.getLevel();
+        BlockPos blockPos = this.getBlockPos();
+        ResourceKey<LootTable> resourceKey = this.getLootTable();
+        if (this.lootableData.shouldReplenish(player) && resourceKey != null && level != null && level.getServer() != null) { // Paper
+            net.minecraft.world.level.storage.loot.LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(resourceKey);
+            if (player instanceof net.minecraft.server.level.ServerPlayer) {
+                net.minecraft.advancements.CriteriaTriggers.GENERATE_LOOT.trigger((net.minecraft.server.level.ServerPlayer)player, resourceKey);
+            }
+
+            this.lootableData.processRefill(player); // Paper
+            net.minecraft.world.level.storage.loot.LootParams.Builder builder = (new net.minecraft.world.level.storage.loot.LootParams.Builder((net.minecraft.server.level.ServerLevel)level)).withParameter(net.minecraft.world.level.storage.loot.parameters.LootContextParams.ORIGIN, net.minecraft.world.phys.Vec3.atCenterOf(blockPos));
+            if (player != null) {
+                builder.withLuck(player.getLuck()).withParameter(net.minecraft.world.level.storage.loot.parameters.LootContextParams.THIS_ENTITY, player);
+            }
+
+            lootTable.fill(this, builder.create(net.minecraft.world.level.storage.loot.parameters.LootContextParamSets.CHEST), this.getLootTableSeed());
+        }
+
+    }
+    // Paper end
+
     @Override
     public boolean isEmpty() {
         this.unpackLootTable(null);
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBrushableBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBrushableBlock.java
index 949e074a32b6593bd8b7405499e686a074e283e5..398ffe274bddee2b01350b9490def3d2fe854917 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBrushableBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBrushableBlock.java
@@ -58,7 +58,7 @@ public class CraftBrushableBlock extends CraftBlockEntityState<BrushableBlockEnt
         this.setLootTable(this.getLootTable(), seed);
     }
 
-    private void setLootTable(LootTable table, long seed) {
+    public void setLootTable(LootTable table, long seed) { // Paper - make public since it overrides a public method
         this.getSnapshot().setLootTable(CraftLootTable.bukkitToMinecraft(table), seed);
     }
 
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
index 2b6a93a944b27290745278957a3577772b7b8212..29df470d6706a33dad4317a9aa599456d5c7f6ee 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
@@ -13,8 +13,9 @@ import org.bukkit.craftbukkit.CraftWorld;
 import org.bukkit.craftbukkit.inventory.CraftInventory;
 import org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest;
 import org.bukkit.inventory.Inventory;
+import com.destroystokyo.paper.loottable.PaperLootableBlockInventory; // Paper
 
-public class CraftChest extends CraftLootable<ChestBlockEntity> implements Chest {
+public class CraftChest extends CraftLootable<ChestBlockEntity> implements Chest, PaperLootableBlockInventory { // Paper
 
     public CraftChest(World world, ChestBlockEntity tileEntity) {
         super(world, tileEntity);
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftLootable.java b/src/main/java/org/bukkit/craftbukkit/block/CraftLootable.java
index 74315a46f6101775321b1cf4944c124c69aed182..c3215f15b3088199dcf96f62b58d0ec7c2b4125c 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftLootable.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftLootable.java
@@ -8,7 +8,7 @@ import org.bukkit.craftbukkit.CraftLootTable;
 import org.bukkit.loot.LootTable;
 import org.bukkit.loot.Lootable;
 
-public abstract class CraftLootable<T extends RandomizableContainerBlockEntity> extends CraftContainer<T> implements Nameable, Lootable {
+public abstract class CraftLootable<T extends RandomizableContainerBlockEntity> extends CraftContainer<T> implements Nameable, Lootable, com.destroystokyo.paper.loottable.PaperLootableBlockInventory { // Paper
 
     public CraftLootable(World world, T tileEntity) {
         super(world, tileEntity);
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftChestBoat.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftChestBoat.java
index cfde210ea9d4b62fe514d3ab0dbab2f43eda0c7a..c0f6939ed782dd3151ebd7ee9d3d7e292154e76c 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftChestBoat.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftChestBoat.java
@@ -7,8 +7,7 @@ import org.bukkit.craftbukkit.inventory.CraftInventory;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.loot.LootTable;
 
-public class CraftChestBoat extends CraftBoat implements org.bukkit.entity.ChestBoat {
-
+public class CraftChestBoat extends CraftBoat implements org.bukkit.entity.ChestBoat, com.destroystokyo.paper.loottable.PaperLootableEntityInventory { // Paper
     private final Inventory inventory;
 
     public CraftChestBoat(CraftServer server, ChestBoat entity) {
@@ -51,7 +50,7 @@ public class CraftChestBoat extends CraftBoat implements org.bukkit.entity.Chest
         return this.getHandle().getLootTableSeed();
     }
 
-    private void setLootTable(LootTable table, long seed) {
+    public void setLootTable(LootTable table, long seed) { // Paper - change visibility since it overrides a public method
         this.getHandle().setLootTable(CraftLootTable.bukkitToMinecraft(table));
         this.getHandle().setLootTableSeed(seed);
     }
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
index fd42f0b20132d08039ca7735d31a61806a6b07dc..b1a708de6790bbe336202b13ab862ced78de084f 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
@@ -7,7 +7,7 @@ import org.bukkit.entity.minecart.StorageMinecart;
 import org.bukkit.inventory.Inventory;
 
 @SuppressWarnings("deprecation")
-public class CraftMinecartChest extends CraftMinecartContainer implements StorageMinecart {
+public class CraftMinecartChest extends CraftMinecartContainer implements StorageMinecart, com.destroystokyo.paper.loottable.PaperLootableEntityInventory { // Paper
     private final CraftInventory inventory;
 
     public CraftMinecartChest(CraftServer server, MinecartChest entity) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
index 39427b4f284e9402663be2b160ccb5f03f8b91da..17f5684cba9d3ed22d9925d1951520cc4751dfe2 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
@@ -6,7 +6,7 @@ import org.bukkit.craftbukkit.inventory.CraftInventory;
 import org.bukkit.entity.minecart.HopperMinecart;
 import org.bukkit.inventory.Inventory;
 
-public final class CraftMinecartHopper extends CraftMinecartContainer implements HopperMinecart {
+public final class CraftMinecartHopper extends CraftMinecartContainer implements HopperMinecart, com.destroystokyo.paper.loottable.PaperLootableEntityInventory { // Paper
     private final CraftInventory inventory;
 
     public CraftMinecartHopper(CraftServer server, MinecartHopper entity) {