More meta utils method, fix openBook

This commit is contained in:
TheMode 2021-04-10 18:55:26 +02:00
parent 0619b1b14b
commit 0be4c6d8c7
5 changed files with 95 additions and 62 deletions

View File

@ -42,6 +42,7 @@ import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.PlayerInventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.item.meta.WrittenBookMeta;
import net.minestom.server.listener.PlayerDiggingListener;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState;
@ -67,6 +68,7 @@ import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.instance.InstanceUtils;
import net.minestom.server.utils.inventory.PlayerInventoryUtils;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
@ -1100,15 +1102,14 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
@Override
public void openBook(@NotNull Book book) {
// make the book
ItemStack writtenBook = ItemStack.of(Material.WRITTEN_BOOK);
// TODO: WRITTEN_BOOK meta
//writtenBook.setItemMeta(WrittenBookMeta.fromAdventure(book, this));
ItemStack writtenBook = ItemStack.builder(Material.WRITTEN_BOOK)
.meta(WrittenBookMeta.fromAdventure(book, this))
.build();
// Set book in offhand
SetSlotPacket setBookPacket = new SetSlotPacket();
setBookPacket.windowId = 0;
setBookPacket.slot = 45;
setBookPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT;
setBookPacket.itemStack = writtenBook;
playerConnection.sendPacket(setBookPacket);
@ -1120,7 +1121,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Restore the item in offhand
SetSlotPacket restoreItemPacket = new SetSlotPacket();
restoreItemPacket.windowId = 0;
restoreItemPacket.slot = 45;
restoreItemPacket.slot = PlayerInventoryUtils.OFFHAND_SLOT;
restoreItemPacket.itemStack = getItemInOffHand();
playerConnection.sendPacket(restoreItemPacket);
}

View File

@ -94,13 +94,10 @@ public abstract class ItemMetaBuilder {
@Contract("_ -> this")
public @NotNull ItemMetaBuilder enchantments(@NotNull Map<Enchantment, Short> enchantments) {
this.enchantmentMap = enchantments;
if (!enchantmentMap.isEmpty()) {
handleMap(enchantmentMap, "Enchantments", nbt, () -> {
NBTUtils.writeEnchant(nbt, "Enchantments", enchantmentMap);
} else {
this.nbt.removeTag("Enchantments");
}
return nbt.get("Enchantments");
});
return this;
}
@ -114,6 +111,7 @@ public abstract class ItemMetaBuilder {
@Contract("-> this")
public @NotNull ItemMetaBuilder clearEnchantment() {
this.enchantmentMap.clear();
enchantments(enchantmentMap);
return this;
}
@ -121,10 +119,8 @@ public abstract class ItemMetaBuilder {
public @NotNull ItemMetaBuilder attributes(@NotNull List<@NotNull ItemAttribute> attributes) {
this.attributes = attributes;
if (!attributes.isEmpty()) {
handleCollection(attributes, "AttributeModifiers", nbt, () -> {
NBTList<NBTCompound> attributesNBT = new NBTList<>(NBTTypes.TAG_Compound);
for (ItemAttribute itemAttribute : attributes) {
final UUID uuid = itemAttribute.getUuid();
attributesNBT.add(
@ -137,10 +133,8 @@ public abstract class ItemMetaBuilder {
.setString("Name", itemAttribute.getInternalName())
);
}
this.nbt.set("AttributeModifiers", attributesNBT);
} else {
this.nbt.removeTag("AttributeModifiers");
}
return attributesNBT;
});
return this;
}
@ -194,6 +188,39 @@ public abstract class ItemMetaBuilder {
}
}
protected void handleNullable(@Nullable Object value,
@NotNull String key,
@NotNull NBTCompound nbtCompound,
@NotNull Supplier<@NotNull NBT> supplier) {
if (value != null) {
nbtCompound.set(key, supplier.get());
} else {
nbtCompound.removeTag(key);
}
}
protected void handleCollection(@NotNull Collection<?> objects,
@NotNull String key,
@NotNull NBTCompound nbtCompound,
@NotNull Supplier<@NotNull NBT> supplier) {
if (!objects.isEmpty()) {
nbtCompound.set(key, supplier.get());
} else {
nbtCompound.removeTag(key);
}
}
protected void handleMap(@NotNull Map<?, ?> objects,
@NotNull String key,
@NotNull NBTCompound nbtCompound,
@NotNull Supplier<@NotNull NBT> supplier) {
if (!objects.isEmpty()) {
nbtCompound.set(key, supplier.get());
} else {
nbtCompound.removeTag(key);
}
}
@Contract(value = "_, _ -> new", pure = true)
public static @NotNull ItemMetaBuilder fromNBT(@NotNull ItemMetaBuilder src, @NotNull NBTCompound nbtCompound) {
ItemMetaBuilder dest = src.getSupplier().get();

View File

@ -35,13 +35,13 @@ public class EnchantedBookMeta extends ItemMeta implements ItemMetaBuilder.Provi
private Map<Enchantment, Short> enchantments = new HashMap<>();
public Builder enchantments(Map<Enchantment, Short> enchantments) {
public @NotNull Builder enchantments(Map<Enchantment, Short> enchantments) {
this.enchantments = enchantments;
NBTUtils.writeEnchant(nbt, "StoredEnchantments", enchantments);
return this;
}
public Builder enchantment(Enchantment enchantment, short level) {
public @NotNull Builder enchantment(Enchantment enchantment, short level) {
this.enchantments.put(enchantment, level);
enchantments(enchantments);
return this;

View File

@ -6,6 +6,7 @@ import net.minestom.server.adventure.AdventureSerializer;
import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemMetaBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTString;
@ -14,6 +15,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WritableBookMeta.Builder> {
@ -23,19 +25,19 @@ public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provid
private final List<Component> pages;
protected WritableBookMeta(@NotNull ItemMetaBuilder metaBuilder,
String author, String title,
List<Component> pages) {
@Nullable String author, @Nullable String title,
@NotNull List<@NotNull Component> pages) {
super(metaBuilder);
this.author = author;
this.title = title;
this.pages = new ArrayList<>(pages);
}
public String getAuthor() {
public @Nullable String getAuthor() {
return author;
}
public String getTitle() {
public @Nullable String getTitle() {
return title;
}
@ -49,27 +51,30 @@ public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provid
private String title;
private List<Component> pages = new ArrayList<>();
public Builder author(String author) {
public Builder author(@Nullable String author) {
this.author = author;
this.nbt.setString("author", author);
handleNullable(author, "author", nbt,
() -> new NBTString(Objects.requireNonNull(author)));
return this;
}
public Builder title(String title) {
public Builder title(@Nullable String title) {
this.title = title;
this.nbt.setString("title", author);
handleNullable(title, "title", nbt,
() -> new NBTString(Objects.requireNonNull(title)));
return this;
}
public Builder pages(List<Component> pages) {
public Builder pages(@NotNull List<@NotNull Component> pages) {
this.pages = pages;
this.nbt.setString("title", author);
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (Component page : pages) {
list.add(new NBTString(AdventureSerializer.serialize(page)));
}
this.nbt.set("pages", list);
handleCollection(pages, "pages", nbt, () -> {
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (Component page : pages) {
list.add(new NBTString(AdventureSerializer.serialize(page)));
}
return list;
});
return this;
}

View File

@ -8,15 +8,10 @@ import net.minestom.server.adventure.Localizable;
import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemMetaBuilder;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.function.Supplier;
public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provider<WrittenBookMeta.Builder> {
@ -28,8 +23,9 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
private final List<Component> pages;
protected WrittenBookMeta(@NotNull ItemMetaBuilder metaBuilder, boolean resolved,
WrittenBookGeneration generation, String author, String title,
List<Component> pages) {
@Nullable WrittenBookGeneration generation,
@Nullable String author, @Nullable String title,
@NotNull List<@NotNull Component> pages) {
super(metaBuilder);
this.resolved = resolved;
this.generation = generation;
@ -42,15 +38,15 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
return resolved;
}
public WrittenBookGeneration getGeneration() {
public @Nullable WrittenBookGeneration getGeneration() {
return generation;
}
public String getAuthor() {
public @Nullable String getAuthor() {
return author;
}
public String getTitle() {
public @Nullable String getTitle() {
return title;
}
@ -94,33 +90,37 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
return this;
}
public Builder generation(WrittenBookGeneration generation) {
public Builder generation(@Nullable WrittenBookGeneration generation) {
this.generation = generation;
this.nbt.setInt("generation", generation.ordinal());
handleNullable(generation, "generation", nbt,
() -> new NBTInt(Objects.requireNonNull(generation).ordinal()));
return this;
}
public Builder author(String author) {
public Builder author(@Nullable String author) {
this.author = author;
this.nbt.setString("author", author);
handleNullable(author, "author", nbt,
() -> new NBTString(Objects.requireNonNull(author)));
return this;
}
public Builder title(String title) {
public Builder title(@Nullable String title) {
this.title = title;
this.nbt.setString("title", author);
handleNullable(title, "title", nbt,
() -> new NBTString(Objects.requireNonNull(title)));
return this;
}
public Builder pages(List<Component> pages) {
public Builder pages(@NotNull List<@NotNull Component> pages) {
this.pages = pages;
this.nbt.setString("title", author);
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (Component page : pages) {
list.add(new NBTString(AdventureSerializer.serialize(page)));
}
this.nbt.set("pages", list);
handleCollection(pages, "pages", nbt, () -> {
NBTList<NBTString> list = new NBTList<>(NBTTypes.TAG_String);
for (Component page : pages) {
list.add(new NBTString(AdventureSerializer.serialize(page)));
}
return list;
});
return this;
}