papermc/Spigot-API-Patches/0035-LootTable-API.patch
Aikar 3a70bed5f4
Updated Upstream (Bukkit/CraftBukkit) for LootTable API
I have tested that the Replenishing Feature still works as expected.
Lootable API's that now have Bukkit equivalents are now deprecated.

Bukkit Changes:
f0f33981 SPIGOT-1936: LootTable API

CraftBukkit Changes:
c0df4b82 SPIGOT-1936: LootTable API
2018-08-12 13:11:13 -04:00

489 lines
17 KiB
Diff

From 74dcc240e66bd353b4e94b30769b8dec97ef41ea Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 1 May 2016 15:19:49 -0400
Subject: [PATCH] LootTable API
Provides API to control what Loot Table an object uses.
Also provides an Event to control if a lootable inventory should
auto replenish for a player.
Provides methods to determine players looted state for an object
diff --git a/src/main/java/com/destroystokyo/paper/loottable/Lootable.java b/src/main/java/com/destroystokyo/paper/loottable/Lootable.java
new file mode 100644
index 000000000..7dd3955b5
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/Lootable.java
@@ -0,0 +1,93 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.Bukkit;
+import org.bukkit.NamespacedKey;
+import org.bukkit.loot.LootTable;
+
+/**
+ * Defines an object that has a Loot Table and seed associated with it.
+ * <p>
+ * How the Loot Table and seed are used may vary based on Minecraft Versions
+ * and what type of object is using the Loot Table
+ *
+ * @deprecated Use {@link org.bukkit.loot.Lootable}
+ */
+@Deprecated
+public interface Lootable extends org.bukkit.loot.Lootable {
+
+ /**
+ * Gets the name of the Loot Table to be used in the World Folder
+ *
+ * @return The name, or null if no loot table exists
+ * @deprecated Use {@link org.bukkit.loot.Lootable#getLootTable()}
+ */
+ @Deprecated
+ default String getLootTableName() {
+ LootTable lootTable = getLootTable();
+ return lootTable != null && lootTable.getKey() != null ? lootTable.getKey().toString() : null;
+ }
+
+ /**
+ * Sets the name of the Loot Table to be used in the World Folder
+ * Will use a random seed (0)
+ *
+ * @param name name in either foo or minecraft:foo format
+ * @return The previous Loot Table before the change
+ * @deprecated Use {@link org.bukkit.loot.Lootable#setLootTable(LootTable)}
+ */
+ @Deprecated
+ default String setLootTable(String name) {
+ return setLootTable(name, 0);
+ }
+
+ /**
+ * Sets the name of the Loot Table to be used in the World Folder
+ * Uses supplied Seed
+ *
+ * @param name name in either foo or minecraft:foo format
+ * @param seed seed for the loot table. If 0, seed will be random
+ * @return The previous Loot Table before the change
+ * @deprecated Use {@link org.bukkit.loot.Lootable#setSeed(long)}
+ */
+ @Deprecated
+ default String setLootTable(String name, long seed) {
+ String prev = getLootTableName();
+ LootTable lootTable = Bukkit.getLootTable(NamespacedKey.minecraft(name));
+ setLootTable(lootTable, seed);
+ return prev;
+ }
+
+ /**
+ * Gets the current seed associated to the Loot Table on this object
+ *
+ * @return The seed, or 0 for random
+ * @deprecated Use {@link org.bukkit.loot.Lootable#getSeed()}
+ */
+ @Deprecated
+ default long getLootTableSeed() {
+ return getSeed();
+ }
+
+ /**
+ * Changes the current seed associated with the Loot Table on this object.
+ * <p>
+ * The seed will have no affect if this object does not have a Loot Table
+ * associated with it.
+ *
+ * @param seed The seed to use, or 0 for random
+ * @return The previous seed
+ * @throws IllegalStateException If called when this object does not have a loot table
+ * @deprecated Use {@link org.bukkit.loot.Lootable#setSeed(long)}
+ */
+ @Deprecated
+ default long setLootTableSeed(long seed) {
+ final String lootTableName = getLootTableName();
+ if (lootTableName == null) {
+ throw new IllegalStateException("This object does not currently have a Loot Table.");
+ }
+
+ long prev = getSeed();
+ setSeed(seed);
+ return prev;
+ }
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/LootableBlockInventory.java b/src/main/java/com/destroystokyo/paper/loottable/LootableBlockInventory.java
new file mode 100644
index 000000000..9095df616
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/LootableBlockInventory.java
@@ -0,0 +1,15 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.block.Block;
+
+/**
+ * Represents an Inventory that can generate loot, such as Chests inside of Fortresses and Mineshafts
+ */
+public interface LootableBlockInventory extends LootableInventory {
+
+ /**
+ * Gets the block that is lootable
+ * @return The Block
+ */
+ Block getBlock();
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/LootableEntityInventory.java b/src/main/java/com/destroystokyo/paper/loottable/LootableEntityInventory.java
new file mode 100644
index 000000000..a389f74c6
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/LootableEntityInventory.java
@@ -0,0 +1,15 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.entity.Entity;
+
+/**
+ * Represents an Inventory that can generate loot, such as Minecarts inside of Mineshafts
+ */
+public interface LootableEntityInventory extends LootableInventory {
+
+ /**
+ * Gets the entity that is lootable
+ * @return The Entity
+ */
+ Entity getEntity();
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/LootableInventory.java b/src/main/java/com/destroystokyo/paper/loottable/LootableInventory.java
new file mode 100644
index 000000000..cde999ef4
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/LootableInventory.java
@@ -0,0 +1,111 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.entity.Player;
+
+import java.util.UUID;
+
+/**
+ * Represents an Inventory that contains a Loot Table associated to it that will
+ * automatically fill on first open.
+ *
+ * A new feature and API is provided to support automatically refreshing the contents
+ * of the inventory based on that Loot Table after a configurable amount of time has passed.
+ *
+ * The behavior of how the Inventory is filled based on the loot table may vary based
+ * on Minecraft versions and the Loot Table feature.
+ */
+public interface LootableInventory extends Lootable {
+
+ /**
+ * Server owners have to enable whether or not an object in a world should refill
+ *
+ * @return If the world this inventory is currently in has Replenishable Lootables enabled
+ */
+ boolean isRefillEnabled();
+
+ /**
+ * Whether or not this object has ever been filled
+ * @return Has ever been filled
+ */
+ boolean hasBeenFilled();
+
+ /**
+ * Has this player ever looted this block
+ * @param player The player to check
+ * @return Whether or not this player has looted this block
+ */
+ default boolean hasPlayerLooted(Player player) {
+ return hasPlayerLooted(player.getUniqueId());
+ }
+
+ /**
+ * Has this player ever looted this block
+ * @param player The player to check
+ * @return Whether or not this player has looted this block
+ */
+ boolean hasPlayerLooted(UUID player);
+
+ /**
+ * Gets the timestamp, in milliseconds, of when the player last looted this object
+ *
+ * @param player The player to check
+ * @return Timestamp last looted, or null if player has not looted this object
+ */
+ default Long getLastLooted(Player player) {
+ return getLastLooted(player.getUniqueId());
+ }
+
+ /**
+ * Gets the timestamp, in milliseconds, of when the player last looted this object
+ *
+ * @param player The player to check
+ * @return Timestamp last looted, or null if player has not looted this object
+ */
+ Long getLastLooted(UUID player);
+
+ /**
+ * Change the state of whether or not a player has looted this block
+ * @param player The player to change state for
+ * @param looted true to add player to looted list, false to remove
+ * @return The previous state of whether the player had looted this or not
+ */
+ default boolean setHasPlayerLooted(Player player, boolean looted) {
+ return setHasPlayerLooted(player.getUniqueId(), looted);
+ }
+
+ /**
+ * Change the state of whether or not a player has looted this block
+ * @param player The player to change state for
+ * @param looted true to add player to looted list, false to remove
+ * @return The previous state of whether the player had looted this or not
+ */
+ boolean setHasPlayerLooted(UUID player, boolean looted);
+
+ /**
+ * Returns Whether or not this object has been filled and now has a pending refill
+ * @return Has pending refill
+ */
+ boolean hasPendingRefill();
+
+ /**
+ * Gets the timestamp in milliseconds that the Lootable object was last refilled
+ *
+ * @return -1 if it was never refilled, or timestamp in milliseconds
+ */
+ long getLastFilled();
+
+ /**
+ * Gets the timestamp in milliseconds that the Lootable object will refill
+ *
+ * @return -1 if it is not scheduled for refill, or timestamp in milliseconds
+ */
+ long getNextRefill();
+
+ /**
+ * Sets the timestamp in milliseconds of the next refill for this object
+ *
+ * @param refillAt timestamp in milliseconds. -1 to clear next refill
+ * @return The previous scheduled time to refill, or -1 if was not scheduled
+ */
+ long setNextRefill(long refillAt);
+}
diff --git a/src/main/java/com/destroystokyo/paper/loottable/LootableInventoryReplenishEvent.java b/src/main/java/com/destroystokyo/paper/loottable/LootableInventoryReplenishEvent.java
new file mode 100644
index 000000000..2169493d9
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/loottable/LootableInventoryReplenishEvent.java
@@ -0,0 +1,41 @@
+package com.destroystokyo.paper.loottable;
+
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+
+public class LootableInventoryReplenishEvent extends PlayerEvent implements Cancellable {
+ private final LootableInventory inventory;
+
+ public LootableInventoryReplenishEvent(Player player, LootableInventory inventory) {
+ super(player);
+ this.inventory = inventory;
+ }
+
+ public LootableInventory getInventory() {
+ return inventory;
+ }
+
+ private static final HandlerList handlers = new HandlerList();
+
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+
+ private boolean cancelled = false;
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ cancelled = cancel;
+ }
+}
diff --git a/src/main/java/org/bukkit/block/Chest.java b/src/main/java/org/bukkit/block/Chest.java
index 815d79a53..b68ab881d 100644
--- a/src/main/java/org/bukkit/block/Chest.java
+++ b/src/main/java/org/bukkit/block/Chest.java
@@ -1,5 +1,6 @@
package org.bukkit.block;
+import com.destroystokyo.paper.loottable.LootableBlockInventory;
import org.bukkit.Nameable;
import org.bukkit.inventory.Inventory;
import org.bukkit.loot.Lootable;
@@ -7,7 +8,7 @@ import org.bukkit.loot.Lootable;
/**
* Represents a captured state of a chest.
*/
-public interface Chest extends Container, Nameable, Lootable {
+public interface Chest extends Container, Nameable, LootableBlockInventory { // Paper
/**
* Gets the inventory of the chest block represented by this block state.
diff --git a/src/main/java/org/bukkit/block/Dispenser.java b/src/main/java/org/bukkit/block/Dispenser.java
index 2741625db..6d180117e 100644
--- a/src/main/java/org/bukkit/block/Dispenser.java
+++ b/src/main/java/org/bukkit/block/Dispenser.java
@@ -1,5 +1,6 @@
package org.bukkit.block;
+import com.destroystokyo.paper.loottable.LootableBlockInventory;
import org.bukkit.Nameable;
import org.bukkit.loot.Lootable;
import org.bukkit.projectiles.BlockProjectileSource;
@@ -7,7 +8,7 @@ import org.bukkit.projectiles.BlockProjectileSource;
/**
* Represents a captured state of a dispenser.
*/
-public interface Dispenser extends Container, Nameable, Lootable {
+public interface Dispenser extends Container, Nameable, LootableBlockInventory { // Paper
/**
* Gets the BlockProjectileSource object for the dispenser.
diff --git a/src/main/java/org/bukkit/block/Dropper.java b/src/main/java/org/bukkit/block/Dropper.java
index 2e8c3f711..47737b590 100644
--- a/src/main/java/org/bukkit/block/Dropper.java
+++ b/src/main/java/org/bukkit/block/Dropper.java
@@ -1,12 +1,13 @@
package org.bukkit.block;
+import com.destroystokyo.paper.loottable.LootableBlockInventory;
import org.bukkit.Nameable;
import org.bukkit.loot.Lootable;
/**
* Represents a captured state of a dropper.
*/
-public interface Dropper extends Container, Nameable, Lootable {
+public interface Dropper extends Container, Nameable, LootableBlockInventory { // Paper
/**
* Tries to drop a randomly selected item from the dropper's inventory,
diff --git a/src/main/java/org/bukkit/block/Hopper.java b/src/main/java/org/bukkit/block/Hopper.java
index 73fce5f33..221123e8c 100644
--- a/src/main/java/org/bukkit/block/Hopper.java
+++ b/src/main/java/org/bukkit/block/Hopper.java
@@ -1,9 +1,10 @@
package org.bukkit.block;
+import com.destroystokyo.paper.loottable.LootableBlockInventory;
import org.bukkit.Nameable;
import org.bukkit.loot.Lootable;
/**
* Represents a captured state of a hopper.
*/
-public interface Hopper extends Container, Nameable, Lootable { }
+public interface Hopper extends Container, Nameable, LootableBlockInventory { } // Paper
diff --git a/src/main/java/org/bukkit/block/ShulkerBox.java b/src/main/java/org/bukkit/block/ShulkerBox.java
index 8e061e4a4..1c02c6e1c 100644
--- a/src/main/java/org/bukkit/block/ShulkerBox.java
+++ b/src/main/java/org/bukkit/block/ShulkerBox.java
@@ -1,5 +1,6 @@
package org.bukkit.block;
+import com.destroystokyo.paper.loottable.LootableBlockInventory;
import org.bukkit.DyeColor;
import org.bukkit.Nameable;
import org.bukkit.loot.Lootable;
@@ -7,7 +8,7 @@ import org.bukkit.loot.Lootable;
/**
* Represents a captured state of a ShulkerBox.
*/
-public interface ShulkerBox extends Container, Nameable, Lootable {
+public interface ShulkerBox extends Container, Nameable, LootableBlockInventory { // Paper
/**
* Get the {@link DyeColor} corresponding to this ShulkerBox
diff --git a/src/main/java/org/bukkit/entity/minecart/HopperMinecart.java b/src/main/java/org/bukkit/entity/minecart/HopperMinecart.java
index 8ced54039..865885501 100644
--- a/src/main/java/org/bukkit/entity/minecart/HopperMinecart.java
+++ b/src/main/java/org/bukkit/entity/minecart/HopperMinecart.java
@@ -1,5 +1,6 @@
package org.bukkit.entity.minecart;
+import com.destroystokyo.paper.loottable.LootableEntityInventory;
import org.bukkit.entity.Minecart;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.loot.Lootable;
@@ -7,7 +8,7 @@ import org.bukkit.loot.Lootable;
/**
* Represents a Minecart with a Hopper inside it
*/
-public interface HopperMinecart extends Minecart, InventoryHolder, Lootable {
+public interface HopperMinecart extends Minecart, InventoryHolder, LootableEntityInventory {
/**
* Checks whether or not this Minecart will pick up
diff --git a/src/main/java/org/bukkit/entity/minecart/StorageMinecart.java b/src/main/java/org/bukkit/entity/minecart/StorageMinecart.java
index 9ea403e6f..238d118f7 100644
--- a/src/main/java/org/bukkit/entity/minecart/StorageMinecart.java
+++ b/src/main/java/org/bukkit/entity/minecart/StorageMinecart.java
@@ -1,5 +1,6 @@
package org.bukkit.entity.minecart;
+import com.destroystokyo.paper.loottable.LootableEntityInventory;
import org.bukkit.entity.Minecart;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.loot.Lootable;
@@ -9,5 +10,5 @@ import org.bukkit.loot.Lootable;
* minecarts} have their own inventory that can be accessed using methods
* from the {@link InventoryHolder} interface.
*/
-public interface StorageMinecart extends Minecart, InventoryHolder, Lootable {
+public interface StorageMinecart extends Minecart, InventoryHolder, LootableEntityInventory { // Paper
}
diff --git a/src/main/java/org/bukkit/loot/Lootable.java b/src/main/java/org/bukkit/loot/Lootable.java
index f4b3d0215..98a472055 100644
--- a/src/main/java/org/bukkit/loot/Lootable.java
+++ b/src/main/java/org/bukkit/loot/Lootable.java
@@ -33,6 +33,31 @@ public interface Lootable {
*/
LootTable getLootTable();
+ // Paper start
+ /**
+ * Set the loot table and seed for a container or entity at the same time.
+ *
+ * @param table the Loot Table this {@link org.bukkit.block.Container} or {@link org.bukkit.entity.Mob} will have.
+ * @param seed the seed to used to generate loot. Default is 0.
+ */
+ void setLootTable(LootTable table, long seed);
+
+ /**
+ * Returns whether or not this object has a Loot Table
+ * @return Has a loot table
+ */
+ default boolean hasLootTable() {
+ return getLootTable() != null;
+ }
+
+ /**
+ * Clears the associated Loot Table to this object
+ */
+ default void clearLootTable() {
+ setLootTable(null);
+ }
+ // Paper end
+
/**
* Set the seed used when this Loot Table generates loot.
*
--
2.18.0