mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
WIP ItemFlag
This commit is contained in:
parent
888c570235
commit
d05a2feff3
@ -214,8 +214,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
this.items[slot] = itemStack;
|
||||
}
|
||||
|
||||
// Refresh inventory items
|
||||
update();
|
||||
// Refresh slot
|
||||
refreshSlot(slot);
|
||||
//update(); in case of problems
|
||||
|
||||
// Sync equipment
|
||||
if (equipmentSlot != null) {
|
||||
|
11
src/main/java/net/minestom/server/item/ItemFlag.java
Normal file
11
src/main/java/net/minestom/server/item/ItemFlag.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
public enum ItemFlag {
|
||||
|
||||
HIDE_ATTRIBUTES,
|
||||
HIDE_DESTROYS,
|
||||
HIDE_ENCHANTS,
|
||||
HIDE_PLACED_ON,
|
||||
HIDE_POTION_EFFECTS,
|
||||
HIDE_UNBREAKABLE
|
||||
}
|
@ -4,10 +4,7 @@ import net.minestom.server.data.Data;
|
||||
import net.minestom.server.data.DataContainer;
|
||||
import net.minestom.server.item.rule.VanillaStackingRule;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class ItemStack implements DataContainer {
|
||||
|
||||
@ -31,7 +28,9 @@ public class ItemStack implements DataContainer {
|
||||
private boolean unbreakable;
|
||||
private ArrayList<String> lore;
|
||||
|
||||
private Map<Enchantment, Integer> enchantmentMap;
|
||||
private Map<Enchantment, Short> enchantmentMap;
|
||||
|
||||
private int hideFlag;
|
||||
|
||||
private StackingRule stackingRule;
|
||||
private Data data;
|
||||
@ -71,6 +70,7 @@ public class ItemStack implements DataContainer {
|
||||
itemStack.isUnbreakable() == unbreakable &&
|
||||
itemStack.getDamage() == damage &&
|
||||
itemStack.enchantmentMap.equals(enchantmentMap) &&
|
||||
itemStack.hideFlag == hideFlag &&
|
||||
itemStack.getStackingRule() == stackingRule &&
|
||||
itemStack.getData() == data;
|
||||
}
|
||||
@ -123,11 +123,11 @@ public class ItemStack implements DataContainer {
|
||||
return lore != null && !lore.isEmpty();
|
||||
}
|
||||
|
||||
public Map<Enchantment, Integer> getEnchantmentMap() {
|
||||
public Map<Enchantment, Short> getEnchantmentMap() {
|
||||
return Collections.unmodifiableMap(enchantmentMap);
|
||||
}
|
||||
|
||||
public void setEnchantment(Enchantment enchantment, int level) {
|
||||
public void setEnchantment(Enchantment enchantment, short level) {
|
||||
if (level < 1) {
|
||||
removeEnchantment(enchantment);
|
||||
return;
|
||||
@ -141,7 +141,40 @@ public class ItemStack implements DataContainer {
|
||||
}
|
||||
|
||||
public int getEnchantmentLevel(Enchantment enchantment) {
|
||||
return this.enchantmentMap.getOrDefault(enchantment, 0);
|
||||
return this.enchantmentMap.getOrDefault(enchantment, (short) 0);
|
||||
}
|
||||
|
||||
public int getHideFlag() {
|
||||
return hideFlag;
|
||||
}
|
||||
|
||||
public void addItemFlags(ItemFlag... hideFlags) {
|
||||
for (ItemFlag f : hideFlags) {
|
||||
this.hideFlag |= getBitModifier(f);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItemFlags(ItemFlag... hideFlags) {
|
||||
for (ItemFlag f : hideFlags) {
|
||||
this.hideFlag &= ~getBitModifier(f);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<ItemFlag> getItemFlags() {
|
||||
Set<ItemFlag> currentFlags = EnumSet.noneOf(ItemFlag.class);
|
||||
|
||||
for (ItemFlag f : ItemFlag.values()) {
|
||||
if (hasItemFlag(f)) {
|
||||
currentFlags.add(f);
|
||||
}
|
||||
}
|
||||
|
||||
return currentFlags;
|
||||
}
|
||||
|
||||
public boolean hasItemFlag(ItemFlag flag) {
|
||||
int bitModifier = getBitModifier(flag);
|
||||
return (this.hideFlag & bitModifier) == bitModifier;
|
||||
}
|
||||
|
||||
public boolean isUnbreakable() {
|
||||
@ -202,4 +235,8 @@ public class ItemStack implements DataContainer {
|
||||
|
||||
ItemStack.defaultStackingRule = defaultStackingRule;
|
||||
}
|
||||
|
||||
private byte getBitModifier(ItemFlag hideFlag) {
|
||||
return (byte) (1 << hideFlag.ordinal());
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ public class NbtReaderUtils {
|
||||
|
||||
if (listName.equals("StoredEnchantments")) {
|
||||
reader.readByte(); // Should be a compound (0x0A)
|
||||
int size = reader.readInteger();
|
||||
int size = reader.readInteger(); // Enchants count
|
||||
|
||||
for (int ench = 0; ench < size; ench++) {
|
||||
reader.readByte(); // Type id (short)
|
||||
|
@ -83,7 +83,7 @@ public class Utils {
|
||||
packet.writeBoolean(false);
|
||||
} else {
|
||||
packet.writeBoolean(true);
|
||||
Utils.writeVarInt(packet, itemStack.getMaterialId());
|
||||
packet.writeVarInt(itemStack.getMaterialId());
|
||||
packet.writeByte(itemStack.getAmount());
|
||||
|
||||
if (!itemStack.hasNbtTag()) {
|
||||
@ -101,10 +101,13 @@ public class Utils {
|
||||
packet.writeInt(1);
|
||||
}
|
||||
|
||||
// Damage
|
||||
packet.writeByte((byte) 0x02);
|
||||
packet.writeShortSizedString("Damage");
|
||||
packet.writeShort(itemStack.getDamage());
|
||||
// Start damage
|
||||
{
|
||||
packet.writeByte((byte) 0x02);
|
||||
packet.writeShortSizedString("Damage");
|
||||
packet.writeShort(itemStack.getDamage());
|
||||
}
|
||||
// End damage
|
||||
|
||||
// Display
|
||||
boolean hasDisplayName = itemStack.hasDisplayName();
|
||||
@ -137,22 +140,23 @@ public class Utils {
|
||||
// End display
|
||||
|
||||
// Start enchantment
|
||||
// FIXME: something is broken, enchants are basically ignored...
|
||||
{
|
||||
Map<Enchantment, Integer> enchantmentMap = itemStack.getEnchantmentMap();
|
||||
Map<Enchantment, Short> enchantmentMap = itemStack.getEnchantmentMap();
|
||||
if (!enchantmentMap.isEmpty()) {
|
||||
packet.writeByte((byte) 0x09); // list
|
||||
packet.writeByte((byte) 0x09); // Type id (list)
|
||||
packet.writeShortSizedString("StoredEnchantments");
|
||||
|
||||
packet.writeByte((byte) 0x0A); // Compound
|
||||
packet.writeInt(enchantmentMap.size()); // Map size
|
||||
|
||||
for (Map.Entry<Enchantment, Integer> entry : enchantmentMap.entrySet()) {
|
||||
for (Map.Entry<Enchantment, Short> entry : enchantmentMap.entrySet()) {
|
||||
Enchantment enchantment = entry.getKey();
|
||||
int level = entry.getValue();
|
||||
short level = entry.getValue();
|
||||
|
||||
packet.writeByte((byte) 0x02); // Type id (short)
|
||||
packet.writeShortSizedString("lvl");
|
||||
packet.writeShort((short) level);
|
||||
packet.writeShort(level);
|
||||
|
||||
packet.writeByte((byte) 0x08); // Type id (string)
|
||||
packet.writeShortSizedString("id");
|
||||
@ -166,6 +170,16 @@ public class Utils {
|
||||
}
|
||||
// End enchantment
|
||||
|
||||
// Start hide flags
|
||||
/*{
|
||||
int hideFlag = itemStack.getHideFlag();
|
||||
if (hideFlag != 0) {
|
||||
packet.writeByte((byte) 3); // Type id (int)
|
||||
packet.writeShortSizedString("HideFlags");
|
||||
packet.writeInt(hideFlag);
|
||||
}
|
||||
}*/
|
||||
|
||||
packet.writeByte((byte) 0); // End nbt
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user