From 3fe58b6b2076b48e8e9ebc3bc058bcae2df885bb Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 26 Oct 2020 19:14:50 +0100 Subject: [PATCH] Inventories are now DataContainer --- .../minestom/server/inventory/Inventory.java | 20 +++++++++++++++++-- .../server/inventory/PlayerInventory.java | 18 ++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index c42dea677..1d64a7511 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -1,6 +1,8 @@ package net.minestom.server.inventory; import net.minestom.server.Viewable; +import net.minestom.server.data.Data; +import net.minestom.server.data.DataContainer; import net.minestom.server.entity.Player; import net.minestom.server.inventory.click.ClickType; import net.minestom.server.inventory.click.InventoryClickLoopHandler; @@ -19,6 +21,7 @@ import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.inventory.PlayerInventoryUtils; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; @@ -35,7 +38,7 @@ import java.util.concurrent.atomic.AtomicInteger; * You can create one with {@link Inventory#Inventory(InventoryType, String)} or by making your own subclass. * It can then be opened using {@link Player#openInventory(Inventory)}. */ -public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable { +public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable, DataContainer { // incremented each time an inventory is created (used in the window packets) private static final AtomicInteger LAST_INVENTORY_ID = new AtomicInteger(); @@ -64,6 +67,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View // the click processor which process all the clicks in the inventory private final InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); + private Data data; + public Inventory(@NotNull InventoryType inventoryType, @NotNull String title) { this.id = generateId(); this.inventoryType = inventoryType; @@ -223,7 +228,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View /** * Refreshes the inventory for a specific viewer. - * + *

* The player needs to be a viewer, otherwise nothing is sent. * * @param player the player to update the inventory @@ -651,4 +656,15 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View update(player); } } + + @Nullable + @Override + public Data getData() { + return data; + } + + @Override + public void setData(@Nullable Data data) { + this.data = data; + } } diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 939e2aa33..ca2974140 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -1,5 +1,7 @@ package net.minestom.server.inventory; +import net.minestom.server.data.Data; +import net.minestom.server.data.DataContainer; import net.minestom.server.entity.Player; import net.minestom.server.event.item.ArmorEquipEvent; import net.minestom.server.event.player.PlayerAddItemStackEvent; @@ -19,6 +21,7 @@ import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.List; @@ -29,7 +32,7 @@ import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*; /** * Represents the inventory of a {@link Player}, retrieved with {@link Player#getInventory()}. */ -public class PlayerInventory implements InventoryModifier, InventoryClickHandler, EquipmentHandler { +public class PlayerInventory implements InventoryModifier, InventoryClickHandler, EquipmentHandler, DataContainer { public static final int INVENTORY_SIZE = 46; @@ -40,6 +43,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler private final List inventoryConditions = new CopyOnWriteArrayList<>(); private final InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); + private Data data; + public PlayerInventory(Player player) { this.player = player; @@ -524,4 +529,15 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler return !clickResult.isCancel(); } + + @Nullable + @Override + public Data getData() { + return data; + } + + @Override + public void setData(@Nullable Data data) { + this.data = data; + } }