More methods & annotation

This commit is contained in:
themode 2021-04-01 00:09:02 +02:00
parent cba09e6a26
commit e66fdee4b1
2 changed files with 35 additions and 3 deletions

View File

@ -2,10 +2,12 @@ package net.minestom.server.item;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntUnaryOperator;
import java.util.function.UnaryOperator;
public class Item {
@ -14,7 +16,8 @@ public class Item {
private final Component displayName;
private final List<Component> lore;
protected Item(Material material, int amount, Component displayName, List<Component> lore) {
protected Item(@NotNull Material material, int amount,
@Nullable Component displayName, @Nullable List<Component> lore) {
this.material = material;
this.amount = amount;
this.displayName = displayName;
@ -26,6 +29,7 @@ public class Item {
return new ItemBuilder(material);
}
@NotNull
public ItemBuilder builder() {
return new ItemBuilder(material)
.amount(amount)
@ -54,11 +58,33 @@ public class Item {
return withAmount(intUnaryOperator.applyAsInt(amount));
}
@Nullable
public Component getDisplayName() {
return displayName;
}
@NotNull
public Item withDisplayName(@Nullable Component displayName) {
return builder().displayName(displayName).build();
}
@NotNull
public Item withDisplayName(@NotNull UnaryOperator<@Nullable Component> componentUnaryOperator) {
return withDisplayName(componentUnaryOperator.apply(displayName));
}
@Nullable
public List<Component> getLore() {
return lore;
}
@NotNull
public Item withLore(@Nullable List<@NotNull Component> lore) {
return builder().lore(lore).build();
}
@NotNull
public Item withLore(@NotNull UnaryOperator<@Nullable List<Component>> loreUnaryOperator) {
return withLore(loreUnaryOperator.apply(lore));
}
}

View File

@ -9,36 +9,42 @@ import java.util.List;
public class ItemBuilder {
private final Material material;
private int amount;
private Component displayName;
private List<Component> lore;
protected ItemBuilder(@NotNull Material material) {
this.material = material;
this.amount = 0;
}
@NotNull
public ItemBuilder amount(int amount) {
this.amount = amount;
return this;
}
@NotNull
public ItemBuilder displayName(Component displayName) {
this.displayName = displayName;
return this;
}
@NotNull
public ItemBuilder lore(List<Component> lore) {
this.lore = Collections.unmodifiableList(lore);
return this;
}
@NotNull
public ItemBuilder lore(Component... lore) {
return lore(Arrays.asList(lore));
}
@NotNull
public Item build() {
return null; // TODO
return new Item(material, amount, displayName, lore);
}
}