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

137 lines
4.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;
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-02 18:13:02 +02:00
public class ItemStack {
2021-04-02 18:13:02 +02:00
public static final ItemStack AIR = ItemStack.builder(Material.AIR).build();
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-02 18:13:02 +02:00
protected ItemStack(@NotNull Material material, int amount, ItemMeta meta) {
this.material = material;
this.amount = amount;
2021-04-02 18:13:02 +02:00
this.meta = meta;
2020-07-23 05:36:15 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_ -> new", pure = true)
public static @NotNull ItemBuilder builder(@NotNull Material material) {
return new ItemBuilder(material);
2019-08-13 17:52:09 +02:00
}
2021-04-02 18:13:02 +02:00
@Contract(value = "_ -> new", pure = true)
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)
public @NotNull ItemStack with(@NotNull Consumer<@NotNull ItemBuilder> builderConsumer) {
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 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)
public @Nullable List<@NotNull Component> getLore() {
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
}
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
}
2021-04-02 18:13:02 +02:00
public @NotNull ItemMeta getMeta() {
return meta;
2020-05-22 21:46:50 +02:00
}
2021-04-02 18:13:02 +02:00
public boolean isSimilar(@NotNull ItemStack itemStack) {
return material.equals(itemStack.material) &&
meta.equals(itemStack.meta);
}
2021-04-02 18:13:02 +02:00
public boolean isAir() {
return equals(AIR);
}
2020-08-13 19:12:16 +02:00
2021-04-02 18:13:02 +02:00
@Contract(value = "-> new", pure = true)
protected @NotNull ItemBuilder builder() {
return new ItemBuilder(material, meta.builder())
.amount(amount);
2020-08-13 19:12:16 +02:00
}
}