Minestom/src/main/java/net/minestom/server/item/ItemStack.java

213 lines
7.2 KiB
Java
Raw Normal View History

2020-04-24 03:25:58 +02:00
package net.minestom.server.item;
2019-08-12 08:30:59 +02:00
2021-03-03 20:27:33 +01:00
import net.kyori.adventure.text.Component;
2021-04-10 17:01:50 +02:00
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
2020-04-24 03:25:58 +02:00
import net.minestom.server.item.rule.VanillaStackingRule;
2021-05-28 17:00:48 +02:00
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable;
2021-04-10 17:01:50 +02:00
import net.minestom.server.utils.NBTUtils;
2021-04-02 18:13:02 +02:00
import org.jetbrains.annotations.Contract;
2020-10-24 11:19:54 +02:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2021-04-13 03:27:51 +02:00
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
2019-08-29 02:15:52 +02:00
2021-04-02 18:13:02 +02:00
import java.util.List;
2021-04-13 05:50:11 +02:00
import java.util.Objects;
2021-04-02 18:13:02 +02:00
import java.util.function.Consumer;
import java.util.function.IntUnaryOperator;
import java.util.function.UnaryOperator;
2020-02-13 15:14:41 +01:00
2021-04-03 00:21:23 +02:00
/**
* Represents an immutable item to be placed inside {@link net.minestom.server.inventory.PlayerInventory},
* {@link net.minestom.server.inventory.Inventory} or even on the ground {@link net.minestom.server.entity.ItemEntity}.
* <p>
* An item stack cannot be null, {@link ItemStack#AIR} should be used instead.
*/
2021-05-28 17:00:48 +02:00
public final class ItemStack implements TagReadable, HoverEventSource<HoverEvent.ShowItem> {
2021-04-03 00:21:23 +02:00
/**
* Constant AIR item. Should be used instead of 'null'.
*/
public static final @NotNull ItemStack AIR = ItemStack.of(Material.AIR);
2019-08-12 08:30:59 +02:00
2021-04-13 05:50:11 +02:00
private final StackingRule stackingRule;
2020-05-27 12:33:12 +02:00
2021-04-02 18:13:02 +02:00
private final Material material;
private final int amount;
private final ItemMeta meta;
2021-04-03 19:08:07 +02:00
protected ItemStack(@NotNull Material material, int amount,
2021-04-13 05:50:11 +02:00
@NotNull ItemMeta meta,
@Nullable StackingRule stackingRule) {
this.material = material;
this.amount = amount;
2021-04-02 18:13:02 +02:00
this.meta = meta;
this.stackingRule = Objects.requireNonNullElseGet(stackingRule, VanillaStackingRule::new);
2020-07-23 05:36:15 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_ -> new", pure = true)
2021-04-03 19:08:07 +02:00
public static @NotNull ItemStackBuilder builder(@NotNull Material material) {
return new ItemStackBuilder(material);
2019-08-13 17:52:09 +02:00
}
2021-04-02 18:25:20 +02:00
@Contract(value = "_ ,_ -> new", pure = true)
2021-04-02 18:13:02 +02:00
public static @NotNull ItemStack of(@NotNull Material material, int amount) {
return builder(material).amount(amount).build();
2021-02-07 19:38:14 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_ -> new", pure = true)
public static @NotNull ItemStack of(@NotNull Material material) {
return of(material, 1);
}
2021-04-13 03:27:51 +02:00
@Contract(value = "_, _, _ -> new", pure = true)
public static @NotNull ItemStack fromNBT(@NotNull Material material, @Nullable NBTCompound nbtCompound, int amount) {
var itemBuilder = ItemStack.builder(material)
.amount(amount);
if (nbtCompound != null) {
itemBuilder.meta(metaBuilder -> ItemMetaBuilder.fromNBT(metaBuilder, nbtCompound));
}
return itemBuilder.build();
2021-04-13 03:27:51 +02:00
}
@Contract(value = "_, _ -> new", pure = true)
public static @NotNull ItemStack fromNBT(@NotNull Material material, @Nullable NBTCompound nbtCompound) {
2021-04-13 03:27:51 +02:00
return fromNBT(material, nbtCompound, 1);
}
2021-04-02 18:13:02 +02:00
@Contract(pure = true)
public @NotNull Material getMaterial() {
return material;
2019-09-06 16:05:36 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
2021-04-03 19:08:07 +02:00
public @NotNull ItemStack with(@NotNull Consumer<@NotNull ItemStackBuilder> builderConsumer) {
2021-04-02 18:13:02 +02:00
var builder = builder();
builderConsumer.accept(builder);
return builder.build();
2020-08-12 13:10:57 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(pure = true)
public int getAmount() {
2020-05-30 01:39:52 +02:00
return amount;
2020-04-22 02:42:58 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withAmount(int amount) {
return builder().amount(amount).build();
2020-07-23 07:36:49 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withAmount(@NotNull IntUnaryOperator intUnaryOperator) {
return withAmount(intUnaryOperator.applyAsInt(amount));
2021-03-03 20:27:33 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, _ -> new", pure = true)
public <T extends ItemMetaBuilder, U extends ItemMetaBuilder.Provider<T>> @NotNull ItemStack withMeta(Class<U> metaType, Consumer<T> metaConsumer) {
return builder().meta(metaType, metaConsumer).build();
2019-08-22 14:52:32 +02:00
}
2021-04-02 22:14:48 +02:00
@Contract(value = "_ -> new", pure = true)
public @NotNull ItemStack withMeta(@NotNull UnaryOperator<@NotNull ItemMetaBuilder> metaOperator) {
return builder().meta(metaOperator).build();
}
2021-04-02 18:13:02 +02:00
@Contract(pure = true)
public @Nullable Component getDisplayName() {
return meta.getDisplayName();
2021-03-03 20:27:33 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withDisplayName(@Nullable Component displayName) {
return builder().displayName(displayName).build();
2019-08-22 14:52:32 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
public @NotNull ItemStack withDisplayName(@NotNull UnaryOperator<@Nullable Component> componentUnaryOperator) {
return withDisplayName(componentUnaryOperator.apply(getDisplayName()));
2020-02-13 15:14:41 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(pure = true)
2021-04-03 00:21:23 +02:00
public @NotNull List<@NotNull Component> getLore() {
2021-04-02 18:13:02 +02:00
return meta.getLore();
2021-03-03 20:27:33 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
2021-04-04 20:49:21 +02:00
public @NotNull ItemStack withLore(@NotNull List<@NotNull Component> lore) {
2021-04-02 18:13:02 +02:00
return builder().lore(lore).build();
2020-02-13 15:14:41 +01:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_, -> new", pure = true)
2021-04-04 20:49:21 +02:00
public @NotNull ItemStack withLore(@NotNull UnaryOperator<@NotNull List<@NotNull Component>> loreUnaryOperator) {
2021-04-02 18:13:02 +02:00
return withLore(loreUnaryOperator.apply(getLore()));
2020-04-29 20:17:04 +02:00
}
@Contract(pure = true)
2021-04-02 18:13:02 +02:00
public @NotNull StackingRule getStackingRule() {
2020-05-30 01:39:52 +02:00
return stackingRule;
2020-02-17 17:33:53 +01:00
}
@Contract(pure = true)
2021-04-02 18:13:02 +02:00
public @NotNull ItemMeta getMeta() {
return meta;
2020-05-22 21:46:50 +02:00
}
@Contract(pure = true)
public boolean isAir() {
2021-04-03 00:03:36 +02:00
return material.equals(Material.AIR);
}
@Contract(pure = true)
2021-04-02 18:13:02 +02:00
public boolean isSimilar(@NotNull ItemStack itemStack) {
return material.equals(itemStack.material) &&
meta.equals(itemStack.meta);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemStack itemStack = (ItemStack) o;
if (amount != itemStack.amount) return false;
if (!stackingRule.equals(itemStack.stackingRule)) return false;
if (material != itemStack.material) return false;
return meta.equals(itemStack.meta);
}
@Override
public int hashCode() {
int result = stackingRule.hashCode();
result = 31 * result + material.hashCode();
result = 31 * result + amount;
result = 31 * result + meta.hashCode();
return result;
}
2020-08-13 19:12:16 +02:00
2021-04-02 18:13:02 +02:00
@Contract(value = "-> new", pure = true)
2021-04-03 19:08:07 +02:00
protected @NotNull ItemStackBuilder builder() {
return new ItemStackBuilder(material, meta.builder())
2021-04-13 09:24:37 +02:00
.amount(amount)
.stackingRule(stackingRule);
2020-08-13 19:12:16 +02:00
}
2021-04-10 17:01:50 +02:00
2021-05-28 17:00:48 +02:00
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
return meta.getTag(tag);
}
@Override
public boolean hasTag(@NotNull Tag<?> tag) {
return meta.hasTag(tag);
}
2021-04-10 17:01:50 +02:00
@Override
public @NotNull HoverEvent<HoverEvent.ShowItem> asHoverEvent(@NotNull UnaryOperator<HoverEvent.ShowItem> op) {
return HoverEvent.showItem(op.apply(HoverEvent.ShowItem.of(this.material,
this.amount,
NBTUtils.asBinaryTagHolder(this.meta.toNBT().getCompound("tag")))));
}
}