Added ItemStack potion nbt

This commit is contained in:
Felix Cravic 2020-05-27 12:33:12 +02:00
parent f5f5a6bb4e
commit 9bf193988f
7 changed files with 105 additions and 15 deletions

View File

@ -154,7 +154,9 @@ public class EntityManager {
}
}
// Add connected clients after the handshake (used to free the networking threads)
/**
* Add connected clients after the handshake (used to free the networking threads)
*/
private void waitingPlayersTick() {
Player waitingPlayer;
while ((waitingPlayer = waitingPlayers.poll()) != null) {

View File

@ -0,0 +1,51 @@
package net.minestom.server.entity.type;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.ObjectEntity;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.utils.Position;
import java.util.function.Consumer;
public class EntityPotion extends ObjectEntity {
private ItemStack potion;
public EntityPotion(Position spawnPosition, ItemStack potion) {
super(EntityType.POTION, spawnPosition);
setPotion(potion);
}
@Override
public Consumer<PacketWriter> getMetadataConsumer() {
return packet -> {
super.getMetadataConsumer().accept(packet);
fillMetadataIndex(packet, 7);
};
}
@Override
protected void fillMetadataIndex(PacketWriter packet, int index) {
super.fillMetadataIndex(packet, index);
if (index == 7) {
packet.writeByte((byte) 7);
packet.writeByte(METADATA_SLOT);
packet.writeItemStack(potion);
}
}
@Override
public int getObjectData() {
return 0;
}
public ItemStack getPotion() {
return potion;
}
public void setPotion(ItemStack potion) {
this.potion = potion;
sendMetadataIndex(15);
}
}

View File

@ -60,8 +60,4 @@ public enum Enchantment {
return id;
}
public String toMinecraftNamespaceId() {
return "minecraft:" + name().toLowerCase();
}
}

View File

@ -3,12 +3,15 @@ package net.minestom.server.item;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.item.rule.VanillaStackingRule;
import net.minestom.server.potion.PotionType;
import net.minestom.server.utils.validate.Check;
import java.util.*;
public class ItemStack implements DataContainer {
private static final StackingRule DEFAULT_STACKING_RULE = new VanillaStackingRule(127);
public static ItemStack getAirItem() {
return new ItemStack((short) 0, (byte) 0);
}
@ -16,11 +19,7 @@ public class ItemStack implements DataContainer {
private static StackingRule defaultStackingRule;
private short materialId;
{
if (defaultStackingRule == null)
defaultStackingRule = new VanillaStackingRule(127);
}
private Set<PotionType> potionTypes;
private byte amount;
private short damage;
@ -31,6 +30,11 @@ public class ItemStack implements DataContainer {
private Map<Enchantment, Short> enchantmentMap;
{
if (defaultStackingRule == null)
defaultStackingRule = DEFAULT_STACKING_RULE;
}
private int hideFlag;
private StackingRule stackingRule;
@ -43,6 +47,7 @@ public class ItemStack implements DataContainer {
this.lore = new ArrayList<>();
this.enchantmentMap = new HashMap<>();
this.potionTypes = new HashSet<>();
this.stackingRule = defaultStackingRule;
}
@ -145,6 +150,14 @@ public class ItemStack implements DataContainer {
return this.enchantmentMap.getOrDefault(enchantment, (short) 0);
}
public Set<PotionType> getPotionTypes() {
return Collections.unmodifiableSet(potionTypes);
}
public void addPotionType(PotionType potionType) {
this.potionTypes.add(potionType);
}
public int getHideFlag() {
return hideFlag;
}
@ -187,7 +200,7 @@ public class ItemStack implements DataContainer {
}
public boolean hasNbtTag() {
return hasDisplayName() || hasLore() || isUnbreakable() || !getEnchantmentMap().isEmpty();
return hasDisplayName() || hasLore() || isUnbreakable() || !getEnchantmentMap().isEmpty() || !potionTypes.isEmpty();
}
public ItemStack clone() {

View File

@ -60,7 +60,7 @@ public class ConnectionManager {
}
/**
* Shouldn't be override if not already defined
* Shouldn't be override if already defined
*
* @param uuidProvider the new player connection uuid provider
*/

View File

@ -7,10 +7,13 @@ import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.potion.PotionType;
import net.minestom.server.utils.buffer.BufferWrapper;
import net.minestom.server.utils.item.NbtReaderUtils;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
public class Utils {
@ -171,7 +174,7 @@ public class Utils {
packet.writeByte((byte) 0x08); // Type id (string)
packet.writeShortSizedString("id");
packet.writeShortSizedString(enchantment.toMinecraftNamespaceId());
packet.writeShortSizedString("minecraft:" + enchantment.name().toLowerCase());
}
@ -181,6 +184,20 @@ public class Utils {
}
// End enchantment
// Start potion
{
Set<PotionType> potionTypes = itemStack.getPotionTypes();
if (!potionTypes.isEmpty()) {
for (PotionType potionType : potionTypes) {
packet.writeByte((byte) 0x08); // type id (string)
packet.writeShortSizedString("Potion");
packet.writeShortSizedString("minecraft:" + potionType.name().toLowerCase());
}
}
}
// End potion
// Start hide flags
/*{
int hideFlag = itemStack.getHideFlag();

View File

@ -1,10 +1,11 @@
package net.minestom.server.utils;
package net.minestom.server.utils.item;
import net.kyori.text.Component;
import net.minestom.server.chat.Chat;
import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.potion.PotionType;
import java.util.ArrayList;
@ -14,7 +15,7 @@ public class NbtReaderUtils {
byte typeId = reader.readByte();
//System.out.println("DEBUG TYPE: " + typeId);
System.out.println("DEBUG TYPE: " + typeId);
switch (typeId) {
case 0x00: // TAG_End
// End of item NBT
@ -63,7 +64,17 @@ public class NbtReaderUtils {
break;
case 0x08: // TAG_String
String stringName = reader.readShortSizedString();
if (stringName.equals("Potion")) {
String potionId = reader.readShortSizedString();
potionId = potionId.replace("minecraft:", "").toUpperCase();
PotionType potionType = PotionType.valueOf(potionId);
item.addPotionType(potionType);
readItemStackNBT(reader, item);
}
break;
case 0x09: // TAG_List