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

188 lines
6.0 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;
2020-04-24 03:25:58 +02:00
import net.minestom.server.item.rule.VanillaStackingRule;
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;
2019-08-29 02:15:52 +02:00
2021-04-02 18:13:02 +02:00
import java.util.List;
import java.util.UUID;
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-04-02 18:13:02 +02:00
public class ItemStack {
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-02 18:13:02 +02:00
private final UUID uuid = UUID.randomUUID();
private final StackingRule stackingRule = new VanillaStackingRule(64);
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
private final ItemStore store;
protected ItemStack(@NotNull Material material, int amount,
@NotNull ItemMeta meta,
@NotNull ItemStore store) {
this.material = material;
this.amount = amount;
2021-04-02 18:13:02 +02:00
this.meta = meta;
2021-04-03 19:08:07 +02:00
this.store = store;
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-02 18:13:02 +02:00
@Contract(pure = true)
public @NotNull UUID getUuid() {
return uuid;
}
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)
public @NotNull ItemStack withLore(@Nullable List<@NotNull Component> lore) {
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)
public @NotNull ItemStack withLore(@NotNull UnaryOperator<@Nullable List<@NotNull Component>> loreUnaryOperator) {
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
}
2021-04-03 19:08:07 +02:00
@Contract(pure = true)
public @NotNull ItemStore getStore() {
return store;
}
@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 (uuid.equals(itemStack.uuid)) return true;
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(), store.builder())
2021-04-02 18:13:02 +02:00
.amount(amount);
2020-08-13 19:12:16 +02:00
}
}