Fixed enchantments (StoredEnchantments+Enchantments support)

This commit is contained in:
Felix Cravic 2020-05-28 23:54:26 +02:00
parent 619c680f1b
commit d416efad5b
3 changed files with 43 additions and 6 deletions

View File

@ -29,6 +29,7 @@ public class ItemStack implements DataContainer {
private ArrayList<String> lore;
private Map<Enchantment, Short> enchantmentMap;
private Map<Enchantment, Short> storedEnchantmentMap;
private List<ItemAttribute> attributes;
private Set<PotionType> potionTypes;
@ -49,6 +50,7 @@ public class ItemStack implements DataContainer {
this.lore = new ArrayList<>();
this.enchantmentMap = new HashMap<>();
this.storedEnchantmentMap = new HashMap<>();
this.attributes = new ArrayList<>();
this.potionTypes = new HashSet<>();
@ -153,6 +155,32 @@ public class ItemStack implements DataContainer {
return this.enchantmentMap.getOrDefault(enchantment, (short) 0);
}
/**
* Stored enchantments are used on enchanted book
*
* @return an unmodifiable map containing the stored enchantments
*/
public Map<Enchantment, Short> getStoredEnchantmentMap() {
return Collections.unmodifiableMap(storedEnchantmentMap);
}
public void setStoredEnchantment(Enchantment enchantment, short level) {
if (level < 1) {
removeStoredEnchantment(enchantment);
return;
}
this.storedEnchantmentMap.put(enchantment, level);
}
public void removeStoredEnchantment(Enchantment enchantment) {
this.storedEnchantmentMap.remove(enchantment);
}
public int getStoredEnchantmentLevel(Enchantment enchantment) {
return this.storedEnchantmentMap.getOrDefault(enchantment, (short) 0);
}
public List<ItemAttribute> getAttributes() {
return Collections.unmodifiableList(attributes);
}
@ -212,7 +240,8 @@ public class ItemStack implements DataContainer {
public boolean hasNbtTag() {
return hasDisplayName() || hasLore() || isUnbreakable() ||
!getEnchantmentMap().isEmpty() || !attributes.isEmpty() || !potionTypes.isEmpty();
!enchantmentMap.isEmpty() || !storedEnchantmentMap.isEmpty() ||
!attributes.isEmpty() || !potionTypes.isEmpty();
}
public ItemStack clone() {

View File

@ -158,7 +158,7 @@ public class Utils {
Map<Enchantment, Short> enchantmentMap = itemStack.getEnchantmentMap();
if (!enchantmentMap.isEmpty()) {
packet.writeByte((byte) 0x09); // Type id (list)
packet.writeShortSizedString("StoredEnchantments");
packet.writeShortSizedString("Enchantments");
packet.writeByte((byte) 0x0A); // Compound
packet.writeInt(enchantmentMap.size()); // Map size

View File

@ -85,7 +85,9 @@ public class NbtReaderUtils {
String listName = reader.readShortSizedString();
if (listName.equals("StoredEnchantments")) {
final boolean isEnchantment = listName.equals("Enchantments");
final boolean isStoredEnchantment = listName.equals("StoredEnchantments");
if (isEnchantment || isStoredEnchantment) {
reader.readByte(); // Should be a compound (0x0A)
int size = reader.readInteger(); // Enchantments count
@ -100,16 +102,22 @@ public class NbtReaderUtils {
// Convert id
id = id.replace("minecraft:", "").toUpperCase();
Enchantment enchantment = Enchantment.valueOf(id);
item.setEnchantment(enchantment, lvl);
if (isEnchantment) {
item.setEnchantment(enchantment, lvl);
} else if (isStoredEnchantment) {
item.setStoredEnchantment(enchantment, lvl);
}
}
reader.readByte(); // Compound end
readItemStackNBT(reader, item);
} else if (listName.equals("AttributeModifiers")) {
}
if (listName.equals("AttributeModifiers")) {
reader.readByte(); // Should be a compound (0x0A);
int size = reader.readInteger(); // Attributes count
for (int i = 0; i < size; i++) {