Inventories are now DataContainer

This commit is contained in:
themode 2020-10-26 19:14:50 +01:00
parent bd620a2241
commit 3fe58b6b20
2 changed files with 35 additions and 3 deletions

View File

@ -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.
*
* <p>
* 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;
}
}

View File

@ -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<InventoryCondition> 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;
}
}