Improve creation of item builder

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-14 00:50:19 +02:00
parent 83baabcbba
commit e026a841ab
3 changed files with 9 additions and 9 deletions

View File

@ -11,7 +11,7 @@ import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
record ItemMetaImpl(TagHandler tagHandler) implements ItemMeta { record ItemMetaImpl(TagHandler tagHandler) implements ItemMeta {
static final ItemMeta EMPTY = new ItemMetaImpl(TagHandler.newHandler()); static final ItemMetaImpl EMPTY = new ItemMetaImpl(TagHandler.newHandler());
@Override @Override
public <T> @UnknownNullability T getTag(@NotNull Tag<T> tag) { public <T> @UnknownNullability T getTag(@NotNull Tag<T> tag) {

View File

@ -129,7 +129,7 @@ public sealed interface ItemStack extends TagReadable, HoverEventSource<HoverEve
@ApiStatus.Experimental @ApiStatus.Experimental
@Contract(value = "_ -> new", pure = true) @Contract(value = "_ -> new", pure = true)
default @NotNull ItemStack withMeta(@NotNull ItemMeta meta) { default @NotNull ItemStack withMeta(@NotNull ItemMeta meta) {
return new ItemStackImpl(material(), amount(), meta); return ItemStackImpl.create(material(), amount(), meta);
} }
@Contract(value = "_, -> new", pure = true) @Contract(value = "_, -> new", pure = true)

View File

@ -13,7 +13,7 @@ import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
record ItemStackImpl(Material material, int amount, ItemMeta meta) implements ItemStack { record ItemStackImpl(Material material, int amount, ItemMetaImpl meta) implements ItemStack {
static final @NotNull StackingRule DEFAULT_STACKING_RULE; static final @NotNull StackingRule DEFAULT_STACKING_RULE;
static { static {
@ -32,7 +32,7 @@ record ItemStackImpl(Material material, int amount, ItemMeta meta) implements It
static ItemStack create(Material material, int amount, ItemMeta meta) { static ItemStack create(Material material, int amount, ItemMeta meta) {
if (amount <= 0) return AIR; if (amount <= 0) return AIR;
return new ItemStackImpl(material, amount, meta); return new ItemStackImpl(material, amount, (ItemMetaImpl) meta);
} }
static ItemStack create(Material material, int amount) { static ItemStack create(Material material, int amount) {
@ -83,15 +83,15 @@ record ItemStackImpl(Material material, int amount, ItemMeta meta) implements It
@Contract(value = "-> new", pure = true) @Contract(value = "-> new", pure = true)
private @NotNull ItemStack.Builder builder() { private @NotNull ItemStack.Builder builder() {
return new Builder(material, amount, new ItemMetaImpl.Builder(TagHandler.fromCompound(meta.toNBT()))); return new Builder(material, amount, new ItemMetaImpl.Builder(meta.tagHandler().copy()));
} }
static final class Builder implements ItemStack.Builder { static final class Builder implements ItemStack.Builder {
final Material material; final Material material;
int amount; int amount;
ItemMeta.Builder metaBuilder; ItemMetaImpl.Builder metaBuilder;
Builder(Material material, int amount, ItemMeta.Builder metaBuilder) { Builder(Material material, int amount, ItemMetaImpl.Builder metaBuilder) {
this.material = material; this.material = material;
this.amount = amount; this.amount = amount;
this.metaBuilder = metaBuilder; this.metaBuilder = metaBuilder;
@ -125,7 +125,7 @@ record ItemStackImpl(Material material, int amount, ItemMeta meta) implements It
@Override @Override
public ItemStack.@NotNull Builder meta(@NotNull UnaryOperator<ItemMeta.Builder> consumer) { public ItemStack.@NotNull Builder meta(@NotNull UnaryOperator<ItemMeta.Builder> consumer) {
this.metaBuilder = consumer.apply(metaBuilder); this.metaBuilder = (ItemMetaImpl.Builder) consumer.apply(metaBuilder);
return this; return this;
} }
@ -142,7 +142,7 @@ record ItemStackImpl(Material material, int amount, ItemMeta meta) implements It
return ItemStackImpl.create(material, amount, metaBuilder.build()); return ItemStackImpl.create(material, amount, metaBuilder.build());
} }
private ItemStack.@NotNull Builder metaBuilder(@NotNull ItemMeta.Builder builder) { private ItemStack.@NotNull Builder metaBuilder(@NotNull ItemMetaImpl.Builder builder) {
this.metaBuilder = builder; this.metaBuilder = builder;
return this; return this;
} }