Search in sources :

Example 41 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class LivingEntityStore method save.

@Override
public void save(T entity, CompoundTag tag) {
    super.save(entity, tag);
    tag.putShort("Air", entity.getRemainingAir());
    if (entity.getCustomName() != null && !entity.getCustomName().isEmpty()) {
        tag.putString("CustomName", entity.getCustomName());
        tag.putBool("CustomNameVisible", entity.isCustomNameVisible());
    }
    tag.putFloat("HealF", entity.getHealth());
    tag.putShort("Health", (int) entity.getHealth());
    tag.putShort("AttackTime", entity.getNoDamageTicks());
    tag.putBool("FallFlying", entity.isFallFlying());
    AttributeManager am = entity.getAttributeManager();
    Map<String, Property> properties = am.getAllProperties();
    if (!properties.isEmpty()) {
        List<CompoundTag> attributes = new ArrayList<>();
        for (Entry<String, Property> property : properties.entrySet()) {
            CompoundTag attribute = new CompoundTag();
            attribute.putString("Name", property.getKey());
            Property p = property.getValue();
            attribute.putDouble("Base", p.getValue());
            if (p.getModifiers() != null && !p.getModifiers().isEmpty()) {
                List<CompoundTag> modifiers = new ArrayList<>();
                for (Modifier modifier : p.getModifiers()) {
                    CompoundTag modifierTag = new CompoundTag();
                    modifierTag.putDouble("Amount", modifier.getAmount());
                    modifierTag.putString("Name", modifier.getName());
                    modifierTag.putInt("Operation", modifier.getOperation());
                    modifierTag.putLong("UUIDLeast", modifier.getUuid().getLeastSignificantBits());
                    modifierTag.putLong("UUIDMost", modifier.getUuid().getMostSignificantBits());
                    modifiers.add(modifierTag);
                }
                attribute.putCompoundList("Modifiers", modifiers);
            }
            attributes.add(attribute);
        }
        tag.putCompoundList("Attributes", attributes);
    }
    List<CompoundTag> effects = new LinkedList<>();
    for (PotionEffect effect : entity.getActivePotionEffects()) {
        CompoundTag effectTag = new CompoundTag();
        effectTag.putByte("Id", effect.getType().getId());
        effectTag.putByte("Amplifier", effect.getAmplifier());
        effectTag.putInt("Duration", effect.getDuration());
        effectTag.putBool("Ambient", effect.isAmbient());
        effectTag.putBool("ShowParticles", true);
        effects.add(effectTag);
    }
    tag.putCompoundList("ActiveEffects", effects);
    EntityEquipment equip = entity.getEquipment();
    if (equip != null) {
        tag.putCompoundList("Equipment", Arrays.asList(NbtSerialization.writeItem(equip.getItemInHand(), -1), NbtSerialization.writeItem(equip.getBoots(), -1), NbtSerialization.writeItem(equip.getLeggings(), -1), NbtSerialization.writeItem(equip.getChestplate(), -1), NbtSerialization.writeItem(equip.getHelmet(), -1)));
        tag.putList("DropChances", TagType.FLOAT, Arrays.asList(equip.getItemInHandDropChance(), equip.getBootsDropChance(), equip.getLeggingsDropChance(), equip.getChestplateDropChance(), equip.getHelmetDropChance()));
    }
    tag.putBool("CanPickUpLoot", entity.getCanPickupItems());
}
Also used : AttributeManager(net.glowstone.entity.AttributeManager) PotionEffect(org.bukkit.potion.PotionEffect) EntityEquipment(org.bukkit.inventory.EntityEquipment) Property(net.glowstone.entity.AttributeManager.Property) Modifier(net.glowstone.entity.AttributeManager.Modifier) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 42 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class LivingEntityStore method load.

// these tags that apply to living entities only are documented as global:
// - short "Air"
// - string "CustomName"
// - bool "CustomNameVisible"
// todo: the following tags
// - float "AbsorptionAmount"
// - short "HurtTime"
// - int "HurtByTimestamp"
// - short "DeathTime"
// - bool "PersistenceRequired"
// - bool "Leashed"
// - compound "Leash"
// on ActiveEffects, bool "ShowParticles"
@Override
public void load(T entity, CompoundTag compound) {
    super.load(entity, compound);
    if (compound.isShort("Air")) {
        entity.setRemainingAir(compound.getShort("Air"));
    }
    if (compound.isString("CustomName")) {
        entity.setCustomName(compound.getString("CustomName"));
    }
    if (compound.isByte("CustomNameVisible")) {
        entity.setCustomNameVisible(compound.getBool("CustomNameVisible"));
    }
    if (compound.isFloat("HealF")) {
        entity.setHealth(compound.getFloat("HealF"));
    } else if (compound.isShort("Health")) {
        entity.setHealth(compound.getShort("Health"));
    }
    if (compound.isShort("AttackTime")) {
        entity.setNoDamageTicks(compound.getShort("AttackTime"));
    }
    if (compound.isByte("FallFlying")) {
        entity.setFallFlying(compound.getBool("FallFlying"));
    }
    if (compound.isList("ActiveEffects", TagType.COMPOUND)) {
        for (CompoundTag effect : compound.getCompoundList("ActiveEffects")) {
            // should really always have every field, but be forgiving if possible
            if (!effect.isByte("Id") || !effect.isInt("Duration")) {
                continue;
            }
            PotionEffectType type = PotionEffectType.getById(effect.getByte("Id"));
            int duration = effect.getInt("Duration");
            if (type == null || duration < 0) {
                continue;
            }
            int amplifier = 0;
            boolean ambient = false;
            if (compound.isByte("Amplifier")) {
                amplifier = compound.getByte("Amplifier");
            }
            if (compound.isByte("Ambient")) {
                ambient = compound.getBool("Ambient");
            }
            // bool "ShowParticles"
            entity.addPotionEffect(new PotionEffect(type, duration, amplifier, ambient), true);
        }
    }
    EntityEquipment equip = entity.getEquipment();
    if (equip != null) {
        if (compound.isList("Equipment", TagType.COMPOUND)) {
            List<CompoundTag> list = compound.getCompoundList("Equipment");
            if (list.size() == 5) {
                equip.setItemInHand(NbtSerialization.readItem(list.get(0)));
                equip.setBoots(NbtSerialization.readItem(list.get(1)));
                equip.setLeggings(NbtSerialization.readItem(list.get(2)));
                equip.setChestplate(NbtSerialization.readItem(list.get(3)));
                equip.setHelmet(NbtSerialization.readItem(list.get(4)));
            }
        }
        if (compound.isList("DropChances", TagType.FLOAT)) {
            List<Float> list = compound.getList("DropChances", TagType.FLOAT);
            if (list.size() == 5) {
                equip.setItemInHandDropChance(list.get(0));
                equip.setBootsDropChance(list.get(1));
                equip.setLeggingsDropChance(list.get(2));
                equip.setChestplateDropChance(list.get(3));
                equip.setHelmetDropChance(list.get(4));
            }
        }
    }
    if (compound.isByte("CanPickUpLoot")) {
        entity.setCanPickupItems(compound.getBool("CanPickUpLoot"));
    }
    if (compound.isList("Attributes", TagType.COMPOUND)) {
        List<CompoundTag> attributes = compound.getCompoundList("Attributes");
        AttributeManager am = entity.getAttributeManager();
        for (CompoundTag tag : attributes) {
            if (tag.isString("Name") && tag.isDouble("Base")) {
                List<Modifier> modifiers = null;
                if (tag.isList("Modifiers", TagType.COMPOUND)) {
                    modifiers = new ArrayList<>();
                    List<CompoundTag> modifierTags = tag.getCompoundList("Modifiers");
                    for (CompoundTag modifierTag : modifierTags) {
                        if (modifierTag.isDouble("Amount") && modifierTag.isString("Name") && modifierTag.isInt("Operation") && modifierTag.isLong("UUIDLeast") && modifierTag.isLong("UUIDMost")) {
                            modifiers.add(new Modifier(modifierTag.getString("Name"), new UUID(modifierTag.getLong("UUIDLeast"), modifierTag.getLong("UUIDMost")), modifierTag.getDouble("Amount"), (byte) modifierTag.getInt("Operation")));
                        }
                    }
                }
                am.setProperty(tag.getString("Name"), tag.getDouble("Base"), modifiers);
            }
        }
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) AttributeManager(net.glowstone.entity.AttributeManager) EntityEquipment(org.bukkit.inventory.EntityEquipment) Modifier(net.glowstone.entity.AttributeManager.Modifier) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 43 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class PlayerStore method load.

// todo: the following tags
// - int "Score"
// - int "foodTickTimer"
// in "abilities":
// - bool "invulnerable"
// - bool "mayBuild"
// - bool "instabuild"
// in "bukkit":
// - bool "keepLevel"
// - int "expToDrop"
// - int "newExp"
// - int "newLevel"
// - int "newTotalExp"
// Bukkit walk and fly speed units are twice Minecraft's
@Override
public void load(GlowPlayer entity, CompoundTag tag) {
    super.load(entity, tag);
    // experience
    if (tag.isInt("XpLevel")) {
        entity.setLevel(tag.getInt("XpLevel"));
    }
    if (tag.isFloat("XpP")) {
        entity.setExp(tag.getFloat("XpP"));
    }
    if (tag.isInt("XpTotal")) {
        entity.setTotalExperience(tag.getInt("XpTotal"));
    }
    // food
    if (tag.isInt("foodLevel")) {
        entity.setFoodLevel(tag.getInt("foodLevel"));
    }
    if (tag.isFloat("foodSaturationLevel")) {
        entity.setSaturation(tag.getFloat("foodSaturationLevel"));
    }
    if (tag.isFloat("foodExhaustionLevel")) {
        entity.setExhaustion(tag.getFloat("foodExhaustionLevel"));
    }
    // spawn location
    if (tag.isInt("SpawnX") && tag.isInt("SpawnY") && tag.isInt("SpawnZ")) {
        int x = tag.getInt("SpawnX");
        int y = tag.getInt("SpawnY");
        int z = tag.getInt("SpawnZ");
        boolean forced = false;
        if (tag.isByte("SpawnForced")) {
            forced = tag.getBool("SpawnForced");
        }
        entity.setBedSpawnLocation(new Location(entity.getWorld(), x, y, z), forced);
    }
    // abilities
    if (tag.isCompound("abilities")) {
        CompoundTag abilities = tag.getCompound("abilities");
        if (abilities.isFloat("walkSpeed")) {
            entity.setWalkSpeed(abilities.getFloat("walkSpeed") * 2f);
        }
        if (abilities.isFloat("flySpeed")) {
            entity.setFlySpeed(abilities.getFloat("flySpeed") * 2f);
        }
        if (abilities.isByte("mayfly")) {
            entity.setAllowFlight(abilities.getBool("mayfly"));
        }
        if (abilities.isByte("flying")) {
            entity.setFlying(abilities.getBool("flying"));
        }
    }
// bukkit
// cannot read firstPlayed, lastPlayed, or lastKnownName
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 44 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class NbtSerialization method writeItem.

/**
     * Write an item stack to an NBT tag. Null stacks produce an empty tag,
     * and if slot is negative it is omitted from the result.
     *
     * @param stack The stack to write, or null.
     * @param slot  The slot, or negative to omit.
     * @return The resulting tag.
     */
public static CompoundTag writeItem(ItemStack stack, int slot) {
    CompoundTag tag = new CompoundTag();
    if (stack == null || stack.getType() == Material.AIR) {
        return tag;
    }
    tag.putString("id", ItemIds.getName(stack.getType()));
    tag.putShort("Damage", stack.getDurability());
    tag.putByte("Count", stack.getAmount());
    tag.putByte("Slot", slot);
    CompoundTag meta = GlowItemFactory.instance().writeNbt(stack.getItemMeta());
    if (meta != null) {
        tag.putCompound("tag", meta);
    }
    return tag;
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 45 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class NbtStructureDataService method writeStructuresData.

@Override
public void writeStructuresData(Map<Integer, GlowStructure> structures) {
    for (GlowStructure structure : structures.values()) {
        if (structure.isDirty()) {
            CompoundTag root = new CompoundTag();
            CompoundTag data = new CompoundTag();
            CompoundTag features = new CompoundTag();
            CompoundTag feature = new CompoundTag();
            StructureStore<GlowStructure> store = StructureStorage.saveStructure(structure, feature);
            File structureFile = new File(structureDir, store.getId() + ".dat");
            if (structureFile.exists()) {
                try (NBTInputStream in = new NBTInputStream(new FileInputStream(structureFile))) {
                    data = new CompoundTag();
                    data = in.readCompound();
                    if (data.isCompound("data")) {
                        data = data.getCompound("data");
                        if (data.isCompound("Features")) {
                            features = data.getCompound("Features");
                        }
                    }
                } catch (IOException e) {
                    server.getLogger().log(Level.SEVERE, "Failed to read structure data from " + structureFile, e);
                }
            }
            String key = "[" + structure.getChunkX() + "," + structure.getChunkZ() + "]";
            features.putCompound(key, feature);
            data.putCompound("Features", features);
            root.putCompound("data", data);
            try (NBTOutputStream nbtOut = new NBTOutputStream(new FileOutputStream(structureFile))) {
                nbtOut.writeTag(root);
            } catch (IOException e) {
                server.getLogger().log(Level.SEVERE, "Failed to write structure data to " + structureFile, e);
            }
            structure.setDirty(false);
        }
    }
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) FileOutputStream(java.io.FileOutputStream) NBTInputStream(net.glowstone.util.nbt.NBTInputStream) IOException(java.io.IOException) File(java.io.File) NBTOutputStream(net.glowstone.util.nbt.NBTOutputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag) FileInputStream(java.io.FileInputStream)

Aggregations

CompoundTag (net.glowstone.util.nbt.CompoundTag)58 ArrayList (java.util.ArrayList)9 NBTInputStream (net.glowstone.util.nbt.NBTInputStream)7 Location (org.bukkit.Location)7 IOException (java.io.IOException)6 NBTOutputStream (net.glowstone.util.nbt.NBTOutputStream)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)3 BlockEntity (net.glowstone.block.entity.BlockEntity)3 GlowEntity (net.glowstone.entity.GlowEntity)3 ByteBuf (io.netty.buffer.ByteBuf)2 FileOutputStream (java.io.FileOutputStream)2 UUID (java.util.UUID)2 ChunkSection (net.glowstone.chunk.ChunkSection)2 AttributeManager (net.glowstone.entity.AttributeManager)2 Modifier (net.glowstone.entity.AttributeManager.Modifier)2 GlowStructure (net.glowstone.generator.structures.GlowStructure)2 StructureBoundingBox (net.glowstone.generator.structures.util.StructureBoundingBox)2 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)2 Material (org.bukkit.Material)2