Many improvements to captive items and menus

This commit is contained in:
Auxilor
2022-10-01 14:36:53 +01:00
parent 25b6a15c9c
commit b845001467
12 changed files with 157 additions and 118 deletions

View File

@@ -2,6 +2,7 @@ package com.willfp.eco.core.gui.menu;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.gui.slot.Slot;
import com.willfp.eco.util.NamespacedKeyUtils;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
@@ -12,7 +13,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* GUI version of {@link Inventory}.
@@ -182,10 +185,12 @@ public interface Menu {
* @deprecated Use addState instead.
*/
@Deprecated(since = "6.35.0", forRemoval = true)
<T, Z> void writeData(@NotNull Player player,
@NotNull NamespacedKey key,
@NotNull PersistentDataType<T, Z> type,
@NotNull Z value);
default <T, Z> void writeData(@NotNull final Player player,
@NotNull final NamespacedKey key,
@NotNull final PersistentDataType<T, Z> type,
@NotNull final Z value) {
this.addState(player, key.toString(), value);
}
/**
* Read data.
@@ -199,9 +204,11 @@ public interface Menu {
* @deprecated Use getState instead.
*/
@Deprecated(since = "6.35.0", forRemoval = true)
@Nullable <T, Z> T readData(@NotNull Player player,
@NotNull NamespacedKey key,
@NotNull PersistentDataType<T, Z> type);
default @Nullable <T, Z> T readData(@NotNull final Player player,
@NotNull final NamespacedKey key,
@NotNull final PersistentDataType<T, Z> type) {
return this.getState(player, key.toString());
}
/**
* Get all data keys for a player.
@@ -211,7 +218,12 @@ public interface Menu {
* @deprecated Use getState instead.
*/
@Deprecated(since = "6.35.0", forRemoval = true)
Set<NamespacedKey> getKeys(@NotNull Player player);
default Set<NamespacedKey> getKeys(@NotNull final Player player) {
return this.getState(player).keySet().stream()
.map(NamespacedKeyUtils::fromStringOrNull)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
/**
* Create a builder with a given amount of rows.

View File

@@ -4,6 +4,7 @@ import com.willfp.eco.core.gui.menu.Menu;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Interface to run on slot display.
@@ -17,6 +18,7 @@ public interface SlotProvider {
* @param menu The menu.
* @return The ItemStack.
*/
@Nullable
ItemStack provide(@NotNull Player player,
@NotNull Menu menu);
}

View File

@@ -4,6 +4,7 @@ import com.willfp.eco.core.gui.menu.Menu;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Interface to run on slot update.
@@ -18,6 +19,7 @@ public interface SlotUpdater {
* @param previous The previous ItemStack.
* @return The new ItemStack.
*/
@Nullable
ItemStack update(@NotNull Player player,
@NotNull Menu menu,
@NotNull ItemStack previous);

View File

@@ -81,6 +81,11 @@ public final class Items {
*/
private static final ItemsLookupHandler ITEMS_LOOKUP_HANDLER = new ItemsLookupHandler(Items::doParse);
/**
* Instance of EmptyTestableItem.
*/
private static final TestableItem EMPTY_TESTABLE_ITEM = new EmptyTestableItem();
/**
* Register a new custom item.
*
@@ -547,6 +552,16 @@ public final class Items {
return Eco.getHandler().getSNBTHandler().fromSNBT(snbt);
}
/**
* Get if an item is empty.
*
* @param itemStack The item.
* @return If empty.
*/
public static boolean isEmpty(@Nullable final ItemStack itemStack) {
return EMPTY_TESTABLE_ITEM.matches(itemStack);
}
private Items() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -35,3 +35,7 @@ fun ItemStack.clearNBT() =
/** @see Items.toSNBT */
fun ItemStack.toSNBT() =
Items.toSNBT(this)
/** @see Items.isEmpty */
val ItemStack?.isEmpty: Boolean
get() = Items.isEmpty(this)