mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 09:01:54 +01:00
WIP meta support
This commit is contained in:
parent
0ab66113c8
commit
cbfeb4e15b
@ -1,7 +1,6 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.item.metadata.ItemMeta;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@ -15,16 +14,12 @@ public class Item {
|
||||
|
||||
private final Material material;
|
||||
private final int amount;
|
||||
//private final ItemMeta meta;
|
||||
private final Component displayName;
|
||||
private final List<Component> lore;
|
||||
private final ItemMeta meta;
|
||||
|
||||
protected Item(@NotNull Material material, int amount,
|
||||
@Nullable Component displayName, @Nullable List<Component> lore) {
|
||||
protected Item(@NotNull Material material, int amount, ItemMeta meta) {
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
this.displayName = displayName;
|
||||
this.lore = lore;
|
||||
this.meta = meta;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
@ -34,10 +29,8 @@ public class Item {
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
public @NotNull ItemBuilder builder() {
|
||||
return new ItemBuilder(material)
|
||||
.amount(amount)
|
||||
.displayName(displayName)
|
||||
.lore(lore);
|
||||
return new ItemBuilder(material, meta.builder())
|
||||
.amount(amount);
|
||||
}
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
@ -62,13 +55,14 @@ public class Item {
|
||||
return withAmount(intUnaryOperator.applyAsInt(amount));
|
||||
}
|
||||
|
||||
public <T extends ItemMeta> @NotNull Item withMeta(Class<T> metaType, Consumer<T> metaConsumer) {
|
||||
@Contract(value = "_, _ -> new", pure = true)
|
||||
public <T extends ItemMetaBuilder> @NotNull Item withMeta(Class<T> metaType, Consumer<T> metaConsumer) {
|
||||
return builder().meta(metaType, metaConsumer).build();
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @Nullable Component getDisplayName() {
|
||||
return displayName;
|
||||
return meta.getDisplayName();
|
||||
}
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
@ -78,12 +72,12 @@ public class Item {
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
public @NotNull Item withDisplayName(@NotNull UnaryOperator<@Nullable Component> componentUnaryOperator) {
|
||||
return withDisplayName(componentUnaryOperator.apply(displayName));
|
||||
return withDisplayName(componentUnaryOperator.apply(getDisplayName()));
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @Nullable List<@NotNull Component> getLore() {
|
||||
return lore;
|
||||
return meta.getLore();
|
||||
}
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
@ -93,6 +87,6 @@ public class Item {
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
public @NotNull Item withLore(@NotNull UnaryOperator<@Nullable List<@NotNull Component>> loreUnaryOperator) {
|
||||
return withLore(loreUnaryOperator.apply(lore));
|
||||
return withLore(loreUnaryOperator.apply(getLore()));
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.item.metadata.ItemMeta;
|
||||
import net.minestom.server.item.meta.CompassMeta;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@ -15,12 +13,17 @@ public class ItemBuilder {
|
||||
|
||||
private final Material material;
|
||||
private int amount;
|
||||
private Component displayName;
|
||||
private List<Component> lore;
|
||||
protected ItemMetaBuilder metaBuilder;
|
||||
|
||||
protected ItemBuilder(@NotNull Material material) {
|
||||
protected ItemBuilder(@NotNull Material material, @NotNull ItemMetaBuilder metaBuilder) {
|
||||
this.material = material;
|
||||
this.amount = 0;
|
||||
this.metaBuilder=metaBuilder;
|
||||
}
|
||||
|
||||
protected ItemBuilder(@NotNull Material material) {
|
||||
// TODO: meta depends on material
|
||||
this(material, new CompassMeta.Builder());
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> this")
|
||||
@ -30,31 +33,32 @@ public class ItemBuilder {
|
||||
}
|
||||
|
||||
@Contract(value = "_, _ -> this")
|
||||
public <T extends ItemMeta> @NotNull ItemBuilder meta(Class<T> metaType, Consumer<T> metaConsumer) {
|
||||
// TODO
|
||||
public <T extends ItemMetaBuilder> @NotNull ItemBuilder meta(Class<T> metaType, Consumer<T> itemMetaConsumer) {
|
||||
itemMetaConsumer.accept((T) metaBuilder);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> this")
|
||||
public @NotNull ItemBuilder displayName(@Nullable Component displayName) {
|
||||
this.displayName = displayName;
|
||||
this.metaBuilder.displayName(displayName);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> this")
|
||||
public @NotNull ItemBuilder lore(List<@NotNull Component> lore) {
|
||||
this.lore = Collections.unmodifiableList(lore);
|
||||
this.metaBuilder.lore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> this")
|
||||
public @NotNull ItemBuilder lore(Component... lore) {
|
||||
return lore(Arrays.asList(lore));
|
||||
this.metaBuilder.lore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
public @NotNull Item build() {
|
||||
return new Item(material, amount, displayName, lore);
|
||||
return new Item(material, amount, metaBuilder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
35
src/main/java/net/minestom/server/item/ItemMeta.java
Normal file
35
src/main/java/net/minestom/server/item/ItemMeta.java
Normal file
@ -0,0 +1,35 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ItemMeta implements Cloneable {
|
||||
|
||||
private final ItemMetaBuilder builder;
|
||||
private final Component displayName;
|
||||
private final List<Component> lore;
|
||||
|
||||
protected ItemMeta(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
this.builder = metaBuilder.clone();
|
||||
this.displayName = metaBuilder.displayName;
|
||||
this.lore = metaBuilder.lore;
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @Nullable Component getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public @Nullable List<@NotNull Component> getLore() {
|
||||
return lore;
|
||||
}
|
||||
|
||||
protected @NotNull ItemMetaBuilder builder() {
|
||||
return builder.clone();
|
||||
}
|
||||
}
|
@ -1,5 +1,44 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
public class ItemMetaBuilder {
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ItemMetaBuilder implements Cloneable {
|
||||
|
||||
protected Component displayName;
|
||||
protected List<Component> lore;
|
||||
|
||||
public void displayName(@Nullable Component displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public void lore(List<@NotNull Component> lore) {
|
||||
this.lore = Collections.unmodifiableList(lore);
|
||||
}
|
||||
|
||||
public void lore(Component... lore) {
|
||||
lore(Arrays.asList(lore));
|
||||
}
|
||||
|
||||
public abstract @NotNull ItemMeta build();
|
||||
|
||||
protected abstract void deepClone(@NotNull ItemMetaBuilder metaBuilder);
|
||||
|
||||
@Override
|
||||
protected ItemMetaBuilder clone() {
|
||||
try {
|
||||
var builder = (ItemMetaBuilder) super.clone();
|
||||
deepClone(builder);
|
||||
return builder;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
throw new UnsupportedOperationException("Weird thing happened");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
68
src/main/java/net/minestom/server/item/meta/CompassMeta.java
Normal file
68
src/main/java/net/minestom/server/item/meta/CompassMeta.java
Normal file
@ -0,0 +1,68 @@
|
||||
package net.minestom.server.item.meta;
|
||||
|
||||
import net.minestom.server.item.ItemMeta;
|
||||
import net.minestom.server.item.ItemMetaBuilder;
|
||||
import net.minestom.server.utils.Position;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CompassMeta extends ItemMeta {
|
||||
|
||||
private final boolean lodestoneTracked;
|
||||
private final String lodestoneDimension;
|
||||
private final Position lodestonePosition;
|
||||
|
||||
protected CompassMeta(ItemMetaBuilder metaBuilder,
|
||||
boolean lodestoneTracked, String lodestoneDimension, Position lodestonePosition) {
|
||||
super(metaBuilder);
|
||||
this.lodestoneTracked = lodestoneTracked;
|
||||
this.lodestoneDimension = lodestoneDimension;
|
||||
this.lodestonePosition = lodestonePosition;
|
||||
}
|
||||
|
||||
public boolean isLodestoneTracked() {
|
||||
return lodestoneTracked;
|
||||
}
|
||||
|
||||
public String getLodestoneDimension() {
|
||||
return lodestoneDimension;
|
||||
}
|
||||
|
||||
public Position getLodestonePosition() {
|
||||
return lodestonePosition;
|
||||
}
|
||||
|
||||
public static class Builder extends ItemMetaBuilder {
|
||||
|
||||
private boolean lodestoneTracked;
|
||||
private String lodestoneDimension;
|
||||
private Position lodestonePosition;
|
||||
|
||||
public Builder lodestoneTracked(boolean lodestoneTracked) {
|
||||
this.lodestoneTracked = lodestoneTracked;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder lodestoneDimension(String lodestoneDimension) {
|
||||
this.lodestoneDimension = lodestoneDimension;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder lodestonePosition(Position lodestonePosition) {
|
||||
this.lodestonePosition = lodestonePosition;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CompassMeta build() {
|
||||
return new CompassMeta(this, lodestoneTracked, lodestoneDimension, lodestonePosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deepClone(@NotNull ItemMetaBuilder metaBuilder) {
|
||||
var compassBuilder = (CompassMeta.Builder) metaBuilder;
|
||||
compassBuilder.lodestoneTracked = lodestoneTracked;
|
||||
compassBuilder.lodestoneDimension = lodestoneDimension;
|
||||
compassBuilder.lodestonePosition = lodestonePosition;
|
||||
}
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ import net.minestom.server.inventory.PlayerInventory;
|
||||
import net.minestom.server.item.Item;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.item.metadata.WritableBookMeta;
|
||||
import net.minestom.server.item.meta.CompassMeta;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
import net.minestom.server.ping.ResponseDataConsumer;
|
||||
import net.minestom.server.utils.Position;
|
||||
@ -66,24 +66,20 @@ public class PlayerInit {
|
||||
//inventory.setItemStack(3, new ItemStack(Material.DIAMOND, (byte) 34));
|
||||
|
||||
{
|
||||
Item item = Item.builder(Material.WRITABLE_BOOK)
|
||||
Item item = Item.builder(Material.COMPASS)
|
||||
.amount(5)
|
||||
.meta(WritableBookMeta.class, writableBookMeta -> {
|
||||
writableBookMeta.setTitle("Title");
|
||||
.meta(CompassMeta.Builder.class, builder -> {
|
||||
builder.lodestonePosition(new Position(0, 0, 0));
|
||||
})
|
||||
.displayName(Component.text("displayName"))
|
||||
.build();
|
||||
|
||||
item = item.with(itemBuilder -> itemBuilder
|
||||
.amount(10)
|
||||
.meta(WritableBookMeta.class, writableBookMeta -> {
|
||||
writableBookMeta.setTitle("Title2");
|
||||
.meta(CompassMeta.Builder.class, builder -> {
|
||||
builder.lodestonePosition(new Position(5, 0, 0));
|
||||
})
|
||||
.lore(Component.text("Lore")));
|
||||
|
||||
item = item.withMeta(WritableBookMeta.class, writableBookMeta -> {
|
||||
writableBookMeta.setTitle("Title3");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user