Added EnchantedBookMeta

This commit is contained in:
Felix Cravic 2020-08-01 00:21:03 +02:00
parent 3b24be4925
commit af14d2d324
5 changed files with 102 additions and 62 deletions

View File

@ -51,7 +51,7 @@ public class PlayerInit {
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
instanceContainer.setChunkGenerator(noiseTestGenerator);
netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.NETHER);
netherTest.enableAutoChunkLoad(true);

View File

@ -56,7 +56,7 @@ public class SimpleCommand implements CommandProcessor {
ChickenCreature chickenCreature = new ChickenCreature(new Position(-10, 43, -10));
chickenCreature.setInstance(instance);
chickenCreature.setPathTo(player.getPosition().clone().add(30, 0, 30));
chickenCreature.setPathTo(player.getPosition());
return true;
}

View File

@ -5,6 +5,7 @@ import net.minestom.server.chat.ColoredText;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.item.attribute.ItemAttribute;
import net.minestom.server.item.metadata.EnchantedBookMeta;
import net.minestom.server.item.metadata.ItemMeta;
import net.minestom.server.item.metadata.MapMeta;
import net.minestom.server.item.metadata.PotionMeta;
@ -36,7 +37,6 @@ public class ItemStack implements DataContainer {
this.lore = new ArrayList<>();
this.enchantmentMap = new HashMap<>();
this.storedEnchantmentMap = new HashMap<>();
this.attributes = new ArrayList<>();
this.itemMeta = findMeta();
@ -47,7 +47,6 @@ public class ItemStack implements DataContainer {
private ArrayList<ColoredText> lore;
private Map<Enchantment, Short> enchantmentMap;
private Map<Enchantment, Short> storedEnchantmentMap;
private List<ItemAttribute> attributes;
private int hideFlag;
@ -142,7 +141,6 @@ public class ItemStack implements DataContainer {
itemStack.isUnbreakable() == unbreakable &&
itemStack.getDamage() == damage &&
itemStack.enchantmentMap.equals(enchantmentMap) &&
itemStack.storedEnchantmentMap.equals(storedEnchantmentMap) &&
itemStack.attributes.equals(attributes) &&
itemStack.hideFlag == hideFlag &&
sameMeta &&
@ -302,50 +300,6 @@ public class ItemStack implements DataContainer {
return this.enchantmentMap.getOrDefault(enchantment, (short) 0);
}
/**
* Get the stored enchantment map
* Stored enchantments are used on enchanted book
*
* @return an unmodifiable map containing the item stored enchantments
*/
public Map<Enchantment, Short> getStoredEnchantmentMap() {
return Collections.unmodifiableMap(storedEnchantmentMap);
}
/**
* Set a stored enchantment level
*
* @param enchantment the enchantment type
* @param level the enchantment level
*/
public void setStoredEnchantment(Enchantment enchantment, short level) {
if (level < 1) {
removeStoredEnchantment(enchantment);
return;
}
this.storedEnchantmentMap.put(enchantment, level);
}
/**
* Remove a stored enchantment
*
* @param enchantment the enchantment type
*/
public void removeStoredEnchantment(Enchantment enchantment) {
this.storedEnchantmentMap.remove(enchantment);
}
/**
* Get a stored enchantment level
*
* @param enchantment the enchantment type
* @return the stored enchantment level, 0 if not present
*/
public int getStoredEnchantmentLevel(Enchantment enchantment) {
return this.storedEnchantmentMap.getOrDefault(enchantment, (short) 0);
}
/**
* Get the item attributes
*
@ -493,7 +447,7 @@ public class ItemStack implements DataContainer {
*/
public boolean hasNbtTag() {
return hasDisplayName() || hasLore() || damage != 0 || isUnbreakable() ||
!enchantmentMap.isEmpty() || !storedEnchantmentMap.isEmpty() ||
!enchantmentMap.isEmpty() ||
!attributes.isEmpty() ||
hideFlag != 0 || customModelData != 0 || (itemMeta != null && itemMeta.hasNbt());
}
@ -511,7 +465,6 @@ public class ItemStack implements DataContainer {
itemStack.setStackingRule(getStackingRule());
itemStack.enchantmentMap = new HashMap<>(enchantmentMap);
itemStack.storedEnchantmentMap = new HashMap<>(storedEnchantmentMap);
itemStack.attributes = new ArrayList<>(attributes);
itemStack.hideFlag = hideFlag;
@ -627,6 +580,9 @@ public class ItemStack implements DataContainer {
if (material == Material.FILLED_MAP)
return new MapMeta();
if (material == Material.ENCHANTED_BOOK)
return new EnchantedBookMeta();
return null;
}

View File

@ -0,0 +1,91 @@
package net.minestom.server.item.metadata;
import net.minestom.server.item.Enchantment;
import net.minestom.server.utils.NBTUtils;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class EnchantedBookMeta implements ItemMeta {
private Map<Enchantment, Short> storedEnchantmentMap = new HashMap<>();
/**
* Get the stored enchantment map
* Stored enchantments are used on enchanted book
*
* @return an unmodifiable map containing the item stored enchantments
*/
public Map<Enchantment, Short> getStoredEnchantmentMap() {
return Collections.unmodifiableMap(storedEnchantmentMap);
}
/**
* Set a stored enchantment level
*
* @param enchantment the enchantment type
* @param level the enchantment level
*/
public void setStoredEnchantment(Enchantment enchantment, short level) {
if (level < 1) {
removeStoredEnchantment(enchantment);
return;
}
this.storedEnchantmentMap.put(enchantment, level);
}
/**
* Remove a stored enchantment
*
* @param enchantment the enchantment type
*/
public void removeStoredEnchantment(Enchantment enchantment) {
this.storedEnchantmentMap.remove(enchantment);
}
/**
* Get a stored enchantment level
*
* @param enchantment the enchantment type
* @return the stored enchantment level, 0 if not present
*/
public int getStoredEnchantmentLevel(Enchantment enchantment) {
return this.storedEnchantmentMap.getOrDefault(enchantment, (short) 0);
}
@Override
public boolean hasNbt() {
return !storedEnchantmentMap.isEmpty();
}
@Override
public boolean isSimilar(ItemMeta itemMeta) {
return itemMeta instanceof EnchantedBookMeta &&
((EnchantedBookMeta) itemMeta).storedEnchantmentMap.equals(storedEnchantmentMap);
}
@Override
public void read(NBTCompound compound) {
if (compound.containsKey("StoredEnchantments")) {
NBTUtils.loadEnchantments(compound.getList("StoredEnchantments"), this::setStoredEnchantment);
}
}
@Override
public void write(NBTCompound compound) {
if (!storedEnchantmentMap.isEmpty()) {
NBTUtils.writeEnchant(compound, "StoredEnchantments", storedEnchantmentMap);
}
}
@Override
public ItemMeta clone() {
EnchantedBookMeta enchantedBookMeta = new EnchantedBookMeta();
enchantedBookMeta.storedEnchantmentMap.putAll(storedEnchantmentMap);
return enchantedBookMeta;
}
}

View File

@ -68,7 +68,7 @@ public class NBTUtils {
}
}
private static void writeEnchant(NBTCompound nbt, String listName, Map<Enchantment, Short> enchantmentMap) {
public static void writeEnchant(NBTCompound nbt, String listName, Map<Enchantment, Short> enchantmentMap) {
NBTList<NBTCompound> enchantList = new NBTList<>(NBTTypes.TAG_Compound);
for (Map.Entry<Enchantment, Short> entry : enchantmentMap.entrySet()) {
final Enchantment enchantment = entry.getKey();
@ -132,9 +132,7 @@ public class NBTUtils {
if (nbt.containsKey("Enchantments")) {
loadEnchantments(nbt.getList("Enchantments"), item::setEnchantment);
}
if (nbt.containsKey("StoredEnchantments")) {
loadEnchantments(nbt.getList("StoredEnchantments"), item::setStoredEnchantment);
}
if (nbt.containsKey("AttributeModifiers")) {
NBTList<NBTCompound> attributes = nbt.getList("AttributeModifiers");
for (NBTCompound attributeNBT : attributes) {
@ -175,7 +173,7 @@ public class NBTUtils {
itemMeta.read(nbt);
}
private static void loadEnchantments(NBTList<NBTCompound> enchantments, EnchantmentSetter setter) {
public static void loadEnchantments(NBTList<NBTCompound> enchantments, EnchantmentSetter setter) {
for (NBTCompound enchantment : enchantments) {
final short level = enchantment.getShort("lvl");
final String id = enchantment.getString("id");
@ -263,11 +261,6 @@ public class NBTUtils {
if (!enchantmentMap.isEmpty()) {
writeEnchant(itemNBT, "Enchantments", enchantmentMap);
}
final Map<Enchantment, Short> storedEnchantmentMap = itemStack.getStoredEnchantmentMap();
if (!storedEnchantmentMap.isEmpty()) {
writeEnchant(itemNBT, "StoredEnchantments", storedEnchantmentMap);
}
}
// End enchantment
@ -323,7 +316,7 @@ public class NBTUtils {
}
@FunctionalInterface
private interface EnchantmentSetter {
public interface EnchantmentSetter {
void applyEnchantment(Enchantment name, short level);
}
}