aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0153-LootTable-API-Replenishable-Lootables-Feature.patch
blob: 517455ea8f8f1a7490d350b9f00a15b10813a98c (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
From 82ffa4af43c27227820035be39705b4a233087c2 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 & Replenishable Lootables Feature

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"

diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 9a9e2eb..3612701 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -339,4 +339,26 @@ public class PaperWorldConfig {
         this.frostedIceDelayMax = this.getInt("frosted-ice.delay.max", this.frostedIceDelayMax);
         this.log("Frosted Ice: " + (this.frostedIceEnabled ? "enabled" : "disabled") + " / delay: min=" + this.frostedIceDelayMin + ", max=" + this.frostedIceDelayMax);
     }
+
+    public boolean autoReplenishLootables;
+    public boolean restrictPlayerReloot;
+    public boolean changeLootTableSeedOnFill;
+    public int maxLootableRefills;
+    public int lootableRegenMin;
+    public int lootableRegenMax;
+    private void enhancedLootables() {
+        autoReplenishLootables = getBoolean("lootables.auto-replenish", false);
+        restrictPlayerReloot = getBoolean("lootables.restrict-player-reloot", true);
+        changeLootTableSeedOnFill = getBoolean("lootables.reset-seed-on-fill", true);
+        maxLootableRefills = getInt("lootables.max-refills", -1);
+        lootableRegenMin = PaperConfig.getSeconds(getString("lootables.refresh-min", "12h"));
+        lootableRegenMax = PaperConfig.getSeconds(getString("lootables.refresh-max", "2d"));
+        if (autoReplenishLootables) {
+            log("Lootables: Replenishing every " +
+                PaperConfig.timeSummary(lootableRegenMin) + " to " +
+                PaperConfig.timeSummary(lootableRegenMax) +
+                (restrictPlayerReloot ? " (restricting reloot)" : "")
+            );
+        }
+    }
 }
diff --git a/src/main/java/com/destroystokyo/paper/loottable/CraftLootable.java b/src/main/java/com/destroystokyo/paper/loottable/CraftLootable.java
new file mode 100644
index 0000000..36c36d1
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/CraftLootable.java
@@ -0,0 +1,12 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.server.World;
+
+interface CraftLootable extends Lootable {
+
+    World getNMSWorld();
+
+    default org.bukkit.World getBukkitWorld() {
+        return getNMSWorld().getWorld();
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/CraftLootableBlockInventory.java b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableBlockInventory.java
new file mode 100644
index 0000000..042fc6f
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableBlockInventory.java
@@ -0,0 +1,28 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.server.BlockPosition;
+import net.minecraft.server.TileEntityLootable;
+import net.minecraft.server.World;
+import org.bukkit.Chunk;
+import org.bukkit.block.Block;
+
+public interface CraftLootableBlockInventory extends LootableBlockInventory, CraftLootableInventory {
+
+    TileEntityLootable getTileEntity();
+
+    @Override
+    default World getNMSWorld() {
+        return getTileEntity().getWorld();
+    }
+
+    default Block getBlock() {
+        final BlockPosition position = getTileEntity().getPosition();
+        final Chunk bukkitChunk = getTileEntity().getWorld().getChunkAtWorldCoords(position).bukkitChunk;
+        return bukkitChunk.getBlock(position.getX(), position.getY(), position.getZ());
+    }
+
+    @Override
+    default CraftLootableInventoryData getLootableData() {
+        return getTileEntity().getLootableData();
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/CraftLootableEntityInventory.java b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableEntityInventory.java
new file mode 100644
index 0000000..6407c58
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableEntityInventory.java
@@ -0,0 +1,26 @@
+package com.destroystokyo.paper.loottable;
+
+import net.minecraft.server.World;
+import org.bukkit.entity.Entity;
+
+public interface CraftLootableEntityInventory extends LootableEntityInventory, CraftLootableInventory {
+
+    net.minecraft.server.Entity getHandle();
+
+    default Entity getEntity() {
+        return getHandle().getBukkitEntity();
+    }
+
+    @Override
+    default World getNMSWorld() {
+        return getHandle().getWorld();
+    }
+
+    @Override
+    default CraftLootableInventoryData getLootableData() {
+        if (getHandle() instanceof CraftLootableInventory) {
+            return ((CraftLootableInventory) getHandle()).getLootableData();
+        }
+        return null;
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/CraftLootableInventory.java b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableInventory.java
new file mode 100644
index 0000000..87917d5
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableInventory.java
@@ -0,0 +1,87 @@
+package com.destroystokyo.paper.loottable;
+
+import org.apache.commons.lang.Validate;
+
+import java.util.UUID;
+
+public interface CraftLootableInventory extends CraftLootable, LootableInventory {
+
+    CraftLootableInventoryData getLootableData();
+
+    @Override
+    default boolean isRefillEnabled() {
+        return getNMSWorld().paperConfig.autoReplenishLootables;
+    }
+
+    @Override
+    default boolean hasBeenFilled() {
+        return getLastFilled() != -1;
+    }
+
+    @Override
+    default String getLootTableName() {
+        return getLootableData().getLootable().getLootTableName();
+    }
+
+    @Override
+    default String setLootTable(String name, long seed) {
+        Validate.notNull(name);
+
+        String prevLootTable = getLootTableName();
+        getLootableData().getLootable().setLootTable(name, seed);
+        return prevLootTable;
+    }
+
+    @Override
+    default long getLootTableSeed() {
+        return getLootableData().getLootable().getLootTableSeed();
+    }
+
+    @Override
+    default void clearLootTable() {
+        getLootableData().getLootable().clearLootTable();
+    }
+
+    @Override
+    default boolean hasPlayerLooted(UUID player) {
+        return getLootableData().hasPlayerLooted(player);
+    }
+
+    @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/CraftLootableInventoryData.java b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableInventoryData.java
new file mode 100644
index 0000000..4b2c3fd
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/CraftLootableInventoryData.java
@@ -0,0 +1,179 @@
+package com.destroystokyo.paper.loottable;
+
+import com.destroystokyo.paper.PaperWorldConfig;
+import net.minecraft.server.*;
+import org.bukkit.entity.Player;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.UUID;
+
+public class CraftLootableInventoryData {
+
+    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 CraftLootableInventory lootable;
+
+    public CraftLootableInventoryData(CraftLootableInventory 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;
+    }
+
+    CraftLootableInventory getLootable() {
+        return lootable;
+    }
+
+    public boolean shouldReplenish(EntityHuman player) {
+        String tableName = this.lootable.getLootTableName();
+
+        // No Loot Table associated
+        if (tableName == null) {
+            return false;
+        }
+
+        // ALWAYS process the first fill
+        if (this.lastFill == -1) {
+            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 PaperWorldConfig paperConfig = this.lootable.getNMSWorld().paperConfig;
+
+        // Check if max refills has been hit
+        if (paperConfig.maxLootableRefills != -1 && this.numRefills >= paperConfig.maxLootableRefills) {
+            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);
+        if (paperConfig.restrictPlayerReloot && hasPlayerLooted(player.getUniqueID())) {
+            event.setCancelled(true);
+        }
+        return event.callEvent();
+    }
+    public void processRefill(EntityHuman player) {
+        this.lastFill = System.currentTimeMillis();
+        final PaperWorldConfig paperConfig = this.lootable.getNMSWorld().paperConfig;
+        if (paperConfig.autoReplenishLootables) {
+            int min = paperConfig.lootableRegenMin * 1000;
+            int max = paperConfig.lootableRegenMax * 1000;
+            this.nextRefill = this.lastFill + min + RANDOM.nextInt(max - min + 1);
+            this.numRefills++;
+            if (paperConfig.changeLootTableSeedOnFill) {
+                this.lootable.setLootTableSeed(0);
+            }
+            this.setPlayerLootedState(player.getUniqueID(), true);
+        } else {
+            this.lootable.clearLootTable();
+        }
+    }
+
+
+    public void loadNbt(NBTTagCompound base) {
+        if (!base.hasKeyOfType("Paper.LootableData", 10)) { // 10 = compound
+            return;
+        }
+        NBTTagCompound comp = base.getCompound("Paper.LootableData");
+        if (comp.hasKey("lastFill")) {
+            this.lastFill = comp.getLong("lastFill");
+        }
+        if (comp.hasKey("nextRefill")) {
+            this.nextRefill = comp.getLong("nextRefill");
+        }
+
+        if (comp.hasKey("numRefills")) {
+            this.numRefills = comp.getInt("numRefills");
+        }
+        if (comp.hasKeyOfType("lootedPlayers", 9)) { // 9 = list
+            NBTTagList list = comp.getList("lootedPlayers", 10); // 10 = compound
+            final int size = list.size();
+            if (size > 0) {
+                this.lootedPlayers = new HashMap<>(list.size());
+            }
+            for (int i = 0; i < size; i++) {
+                final NBTTagCompound cmp = list.get(i);
+                lootedPlayers.put(cmp.getUUID("UUID"), cmp.getLong("Time"));
+            }
+        }
+    }
+    public void saveNbt(NBTTagCompound base) {
+        NBTTagCompound comp = new NBTTagCompound();
+        if (this.nextRefill != -1) {
+            comp.setLong("nextRefill", this.nextRefill);
+        }
+        if (this.lastFill != -1) {
+            comp.setLong("lastFill", this.lastFill);
+        }
+        if (this.numRefills != 0) {
+            comp.setInt("numRefills", this.numRefills);
+        }
+        if (this.lootedPlayers != null && !this.lootedPlayers.isEmpty()) {
+            NBTTagList list = new NBTTagList();
+            for (Map.Entry<UUID, Long> entry : this.lootedPlayers.entrySet()) {
+                NBTTagCompound cmp = new NBTTagCompound();
+                cmp.setUUID("UUID", entry.getKey());
+                cmp.setLong("Time", entry.getValue());
+                list.add(cmp);
+            }
+            comp.set("lootedPlayers", list);
+        }
+
+        if (!comp.isEmpty()) {
+            base.set("Paper.LootableData", comp);
+        }
+    }
+
+    void setPlayerLootedState(UUID player, boolean looted) {
+        if (looted && this.lootedPlayers == null) {
+            this.lootedPlayers = new HashMap<>();
+        }
+        if (looted) {
+            if (!this.lootedPlayers.containsKey(player)) {
+                this.lootedPlayers.put(player, System.currentTimeMillis());
+            }
+        } else if (this.lootedPlayers != null) {
+            this.lootedPlayers.remove(player);
+        }
+    }
+
+    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/net/minecraft/server/EntityMinecartContainer.java b/src/main/java/net/minecraft/server/EntityMinecartContainer.java
index 56e7de8..51b29c8 100644
--- a/src/main/java/net/minecraft/server/EntityMinecartContainer.java
+++ b/src/main/java/net/minecraft/server/EntityMinecartContainer.java
@@ -3,17 +3,20 @@ package net.minecraft.server;
 import java.util.Random;
 // CraftBukkit start
 import java.util.List;
+
+import com.destroystokyo.paper.loottable.CraftLootableInventoryData; // Paper
+import com.destroystokyo.paper.loottable.CraftLootableInventory; // Paper
 import org.bukkit.craftbukkit.entity.CraftHumanEntity;
 import org.bukkit.entity.HumanEntity;
 import org.bukkit.inventory.InventoryHolder;
 // CraftBukkit end
 
-public abstract class EntityMinecartContainer extends EntityMinecartAbstract implements ITileInventory, ILootable {
+public abstract class EntityMinecartContainer extends EntityMinecartAbstract implements ITileInventory, ILootable, CraftLootableInventory { // Paper
 
     private ItemStack[] items = new ItemStack[27]; // CraftBukkit - 36 -> 27
     private boolean b = true;
     private MinecraftKey c;
-    private long d;
+    private long d;public long getLootTableSeed() { return d; } // Paper // OBFHELPER
 
     // CraftBukkit start
     public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
@@ -130,6 +133,7 @@ public abstract class EntityMinecartContainer extends EntityMinecartAbstract imp
 
     protected void b(NBTTagCompound nbttagcompound) {
         super.b(nbttagcompound);
+        lootableData.saveNbt(nbttagcompound); // Paper
         if (this.c != null) {
             nbttagcompound.setString("LootTable", this.c.toString());
             if (this.d != 0L) {
@@ -156,6 +160,7 @@ public abstract class EntityMinecartContainer extends EntityMinecartAbstract imp
     protected void a(NBTTagCompound nbttagcompound) {
         super.a(nbttagcompound);
         this.items = new ItemStack[this.getSize()];
+        lootableData.loadNbt(nbttagcompound); // Paper
         if (nbttagcompound.hasKeyOfType("LootTable", 8)) {
             this.c = new MinecraftKey(nbttagcompound.getString("LootTable"));
             this.d = nbttagcompound.getLong("LootTableSeed");
@@ -217,10 +222,10 @@ public abstract class EntityMinecartContainer extends EntityMinecartAbstract imp
     }
 
     public void f(EntityHuman entityhuman) {
-        if (this.c != null) {
+        if (lootableData.shouldReplenish(entityhuman)) { // Paper
             LootTable loottable = this.world.ak().a(this.c);
 
-            this.c = null;
+            lootableData.processRefill(entityhuman); // Paper
             Random random;
 
             if (this.d == 0L) {
@@ -249,12 +254,42 @@ public abstract class EntityMinecartContainer extends EntityMinecartAbstract imp
 
     }
 
+    public void setLootTable(MinecraftKey key, long seed) { a(key, seed);} // Paper // OBFHELPER
     public void a(MinecraftKey minecraftkey, long i) {
         this.c = minecraftkey;
         this.d = i;
     }
 
+
+    public MinecraftKey getLootTableKey() { return b(); } // Paper // OBFHELPER
     public MinecraftKey b() {
         return this.c;
     }
+
+    // Paper start
+    private CraftLootableInventoryData lootableData = new CraftLootableInventoryData(this);
+
+    @Override
+    public CraftLootableInventoryData getLootableData() {
+        return lootableData;
+    }
+
+    public String getLootTableName() {
+        final MinecraftKey key = getLootTableKey();
+        return key != null ? key.toString() : null;
+    }
+
+    @Override
+    public String setLootTable(String name, long seed) {
+        String prev = getLootTableName();
+        setLootTable(new MinecraftKey(name), seed);
+        return prev;
+    }
+
+    @Override
+    public void clearLootTable() {
+        //noinspection RedundantCast
+        this.c = (MinecraftKey) null;
+    }
+    // Paper end
 }
diff --git a/src/main/java/net/minecraft/server/TileEntityLootable.java b/src/main/java/net/minecraft/server/TileEntityLootable.java
index 437ac1e..2df98ff 100644
--- a/src/main/java/net/minecraft/server/TileEntityLootable.java
+++ b/src/main/java/net/minecraft/server/TileEntityLootable.java
@@ -1,15 +1,19 @@
 package net.minecraft.server;
 
+import com.destroystokyo.paper.loottable.CraftLootableInventoryData; // Paper
+import com.destroystokyo.paper.loottable.CraftLootableInventory; // Paper
+
 import java.util.Random;
 
-public abstract class TileEntityLootable extends TileEntityContainer implements ILootable {
+public abstract class TileEntityLootable extends TileEntityContainer implements ILootable, CraftLootableInventory { // Paper
 
     protected MinecraftKey m;
-    protected long n;
+    protected long n; public long getLootTableSeed() { return n; } // Paper // OBFHELPER
 
     public TileEntityLootable() {}
 
     protected boolean c(NBTTagCompound nbttagcompound) {
+        lootableData.loadNbt(nbttagcompound); // Paper
         if (nbttagcompound.hasKeyOfType("LootTable", 8)) {
             this.m = new MinecraftKey(nbttagcompound.getString("LootTable"));
             this.n = nbttagcompound.getLong("LootTableSeed");
@@ -20,6 +24,7 @@ public abstract class TileEntityLootable extends TileEntityContainer implements
     }
 
     protected boolean d(NBTTagCompound nbttagcompound) {
+        lootableData.saveNbt(nbttagcompound); // Paper
         if (this.m != null) {
             nbttagcompound.setString("LootTable", this.m.toString());
             if (this.n != 0L) {
@@ -33,10 +38,10 @@ public abstract class TileEntityLootable extends TileEntityContainer implements
     }
 
     protected void d(EntityHuman entityhuman) {
-        if (this.m != null) {
+        if (lootableData.shouldReplenish(entityhuman)) { // Paper
             LootTable loottable = this.world.ak().a(this.m);
 
-            this.m = null;
+            lootableData.processRefill(entityhuman); // Paper
             Random random;
 
             if (this.n == 0L) {
@@ -56,12 +61,46 @@ public abstract class TileEntityLootable extends TileEntityContainer implements
 
     }
 
+    public MinecraftKey getLootTableKey() { return b(); } // Paper // OBFHELPER
     public MinecraftKey b() {
         return this.m;
     }
 
+    public void setLootTable(MinecraftKey key, long seed) { a(key, seed);} // Paper // OBFHELPER
     public void a(MinecraftKey minecraftkey, long i) {
         this.m = minecraftkey;
         this.n = i;
     }
+
+    // Paper start - LootTable API
+    private CraftLootableInventoryData lootableData = new CraftLootableInventoryData(this);
+
+    @Override
+    public CraftLootableInventoryData getLootableData() {
+        return lootableData;
+    }
+
+    @Override
+    public World getNMSWorld() {
+        return world;
+    }
+
+    public String getLootTableName() {
+        final MinecraftKey key = getLootTableKey();
+        return key != null ? key.toString() : null;
+    }
+
+    @Override
+    public String setLootTable(String name, long seed) {
+        String prev = getLootTableName();
+        setLootTable(new MinecraftKey(name), seed);
+        return prev;
+    }
+
+    @Override
+    public void clearLootTable() {
+        //noinspection RedundantCast
+        this.m = (MinecraftKey) null;
+    }
+    // Paper end
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
index ce36ee4..99b039b 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java
@@ -1,5 +1,6 @@
 package org.bukkit.craftbukkit.block;
 
+import com.destroystokyo.paper.loottable.CraftLootableBlockInventory; // Paper
 import net.minecraft.server.BlockPosition;
 import net.minecraft.server.TileEntityChest;
 
@@ -11,7 +12,7 @@ import org.bukkit.craftbukkit.inventory.CraftInventory;
 import org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest;
 import org.bukkit.inventory.Inventory;
 
-public class CraftChest extends CraftBlockState implements Chest {
+public class CraftChest extends CraftBlockState implements Chest, CraftLootableBlockInventory { // Paper
     private final CraftWorld world;
     private final TileEntityChest chest;
 
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftDispenser.java b/src/main/java/org/bukkit/craftbukkit/block/CraftDispenser.java
index a3ca3a5..cd7e65d 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftDispenser.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftDispenser.java
@@ -1,5 +1,6 @@
 package org.bukkit.craftbukkit.block;
 
+import com.destroystokyo.paper.loottable.CraftLootableBlockInventory; // Paper
 import net.minecraft.server.BlockDispenser;
 import net.minecraft.server.BlockPosition;
 import net.minecraft.server.Blocks;
@@ -14,7 +15,7 @@ import org.bukkit.craftbukkit.projectiles.CraftBlockProjectileSource;
 import org.bukkit.inventory.Inventory;
 import org.bukkit.projectiles.BlockProjectileSource;
 
-public class CraftDispenser extends CraftBlockState implements Dispenser {
+public class CraftDispenser extends CraftBlockState implements Dispenser, CraftLootableBlockInventory { // Paper
     private final CraftWorld world;
     private final TileEntityDispenser dispenser;
 
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftHopper.java b/src/main/java/org/bukkit/craftbukkit/block/CraftHopper.java
index db844a3..1e805a7 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftHopper.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftHopper.java
@@ -1,5 +1,6 @@
 package org.bukkit.craftbukkit.block;
 
+import com.destroystokyo.paper.loottable.CraftLootableBlockInventory; // Paper
 import net.minecraft.server.TileEntityHopper;
 import org.bukkit.Material;
 import org.bukkit.block.Block;
@@ -8,7 +9,7 @@ import org.bukkit.craftbukkit.CraftWorld;
 import org.bukkit.craftbukkit.inventory.CraftInventory;
 import org.bukkit.inventory.Inventory;
 
-public class CraftHopper extends CraftBlockState implements Hopper {
+public class CraftHopper extends CraftBlockState implements Hopper, CraftLootableBlockInventory { // Paper
     private final TileEntityHopper hopper;
 
     public CraftHopper(final Block block) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
index f5a1875..b2c5679 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartChest.java
@@ -1,5 +1,6 @@
 package org.bukkit.craftbukkit.entity;
 
+import com.destroystokyo.paper.loottable.CraftLootableEntityInventory; // Paper
 import net.minecraft.server.EntityMinecartChest;
 
 import org.bukkit.craftbukkit.CraftServer;
@@ -9,7 +10,7 @@ import org.bukkit.entity.StorageMinecart;
 import org.bukkit.inventory.Inventory;
 
 @SuppressWarnings("deprecation")
-public class CraftMinecartChest extends CraftMinecart implements StorageMinecart {
+public class CraftMinecartChest extends CraftMinecart implements StorageMinecart, CraftLootableEntityInventory { // Paper
     private final CraftInventory inventory;
 
     public CraftMinecartChest(CraftServer server, EntityMinecartChest entity) {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
index 38a5e02..0ccc616 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMinecartHopper.java
@@ -1,5 +1,6 @@
 package org.bukkit.craftbukkit.entity;
 
+import com.destroystokyo.paper.loottable.CraftLootableEntityInventory; // Paper
 import net.minecraft.server.EntityMinecartHopper;
 
 import org.bukkit.craftbukkit.CraftServer;
@@ -8,7 +9,7 @@ import org.bukkit.entity.EntityType;
 import org.bukkit.entity.minecart.HopperMinecart;
 import org.bukkit.inventory.Inventory;
 
-final class CraftMinecartHopper extends CraftMinecart implements HopperMinecart {
+final class CraftMinecartHopper extends CraftMinecart implements HopperMinecart, CraftLootableEntityInventory { // Paper
     private final CraftInventory inventory;
 
     CraftMinecartHopper(CraftServer server, EntityMinecartHopper entity) {
-- 
2.8.2