Search in sources :

Example 1 with DataHolder

use of org.spongepowered.api.data.DataHolder in project SpongeCommon by SpongePowered.

the class SpongeUser method writeToNbt.

public void writeToNbt(NBTTagCompound compound) {
    this.loadInventory();
    compound.setTag(NbtDataUtil.Minecraft.INVENTORY, this.inventory.writeToNBT(new NBTTagList()));
    compound.setInteger(NbtDataUtil.Minecraft.SELECTED_ITEM_SLOT, this.inventory.currentItem);
    compound.setTag(NbtDataUtil.ENTITY_POSITION, NbtDataUtil.newDoubleNBTList(this.posX, this.posY, this.posZ));
    compound.setInteger(NbtDataUtil.ENTITY_DIMENSION, this.dimension);
    compound.setTag(NbtDataUtil.ENTITY_ROTATION, NbtDataUtil.newFloatNBTList(this.rotationYaw, this.rotationPitch));
    final NBTTagCompound forgeCompound = compound.getCompoundTag(NbtDataUtil.FORGE_DATA);
    final NBTTagCompound spongeCompound = forgeCompound.getCompoundTag(NbtDataUtil.SPONGE_DATA);
    spongeCompound.removeTag(NbtDataUtil.USER_SPAWN_LIST);
    final NBTTagList spawnList = new NBTTagList();
    for (Entry<UUID, RespawnLocation> entry : this.spawnLocations.entrySet()) {
        final RespawnLocation respawn = entry.getValue();
        final NBTTagCompound spawnCompound = new NBTTagCompound();
        spawnCompound.setUniqueId(NbtDataUtil.UUID, entry.getKey());
        spawnCompound.setDouble(NbtDataUtil.USER_SPAWN_X, respawn.getPosition().getX());
        spawnCompound.setDouble(NbtDataUtil.USER_SPAWN_Y, respawn.getPosition().getY());
        spawnCompound.setDouble(NbtDataUtil.USER_SPAWN_Z, respawn.getPosition().getZ());
        // No way to know
        spawnCompound.setBoolean(NbtDataUtil.USER_SPAWN_FORCED, false);
        spawnList.appendTag(spawnCompound);
    }
    if (!spawnList.hasNoTags()) {
        spongeCompound.setTag(NbtDataUtil.USER_SPAWN_LIST, spawnList);
        forgeCompound.setTag(NbtDataUtil.SPONGE_DATA, spongeCompound);
        compound.setTag(NbtDataUtil.FORGE_DATA, forgeCompound);
    }
    CustomDataNbtUtil.writeCustomData(spongeCompound, ((DataHolder) this));
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) RespawnLocation(org.spongepowered.api.util.RespawnLocation) DataHolder(org.spongepowered.api.data.DataHolder) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID)

Example 2 with DataHolder

use of org.spongepowered.api.data.DataHolder in project SpongeCommon by SpongePowered.

the class SpongeUser method readFromNbt.

public void readFromNbt(NBTTagCompound compound) {
    this.reset();
    this.nbt = compound;
    NBTTagList position = compound.getTagList(NbtDataUtil.ENTITY_POSITION, NbtDataUtil.TAG_DOUBLE);
    this.posX = position.getDoubleAt(0);
    this.posY = position.getDoubleAt(1);
    this.posZ = position.getDoubleAt(2);
    this.dimension = 0;
    if (compound.hasKey(NbtDataUtil.ENTITY_DIMENSION)) {
        this.dimension = compound.getInteger(NbtDataUtil.ENTITY_DIMENSION);
    }
    NBTTagList rotation = compound.getTagList(NbtDataUtil.ENTITY_ROTATION, NbtDataUtil.TAG_FLOAT);
    this.rotationYaw = rotation.getFloatAt(0);
    this.rotationPitch = rotation.getFloatAt(1);
    // See EntityPlayer#readEntityFromNBT
    final NBTTagCompound spongeCompound = compound.getCompoundTag(NbtDataUtil.FORGE_DATA).getCompoundTag(NbtDataUtil.SPONGE_DATA);
    CustomDataNbtUtil.readCustomData(spongeCompound, ((DataHolder) this));
    if (!spongeCompound.hasNoTags()) {
        final NBTTagList spawnList = spongeCompound.getTagList(NbtDataUtil.USER_SPAWN_LIST, NbtDataUtil.TAG_COMPOUND);
        for (int i = 0; i < spawnList.tagCount(); i++) {
            final NBTTagCompound spawnCompound = spawnList.getCompoundTagAt(i);
            final UUID uuid = spawnCompound.getUniqueId(NbtDataUtil.UUID);
            if (uuid.getLeastSignificantBits() != 0 && uuid.getMostSignificantBits() != 0) {
                final double xPos = spawnCompound.getDouble(NbtDataUtil.USER_SPAWN_X);
                final double yPos = spawnCompound.getDouble(NbtDataUtil.USER_SPAWN_Y);
                final double zPos = spawnCompound.getDouble(NbtDataUtil.USER_SPAWN_Z);
                final boolean forced = spawnCompound.getBoolean(NbtDataUtil.USER_SPAWN_FORCED);
                this.spawnLocations.put(uuid, new RespawnLocation.Builder().forceSpawn(forced).position(new Vector3d(xPos, yPos, zPos)).world(uuid).build());
            }
        }
    }
// TODO Read: any other data that should be available through data manipulators.
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) RespawnLocation(org.spongepowered.api.util.RespawnLocation) Vector3d(com.flowpowered.math.vector.Vector3d) DataHolder(org.spongepowered.api.data.DataHolder) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) UUID(java.util.UUID)

Example 3 with DataHolder

use of org.spongepowered.api.data.DataHolder in project LanternServer by LanternPowered.

the class CompositeValueStoreHelper method offerFast.

protected static <H extends ValueContainer<?>> boolean offerFast(ICompositeValueStore<?, H> store, Iterable<H> valueContainers, MergeFunction function) {
    // Leave this, the compiler complains
    final CompositeValueStore store1 = store;
    if (store1 instanceof DataHolder) {
        final Set<Key<?>> keys = new HashSet<>();
        for (H valueContainer : valueContainers) {
            keys.addAll(valueContainer.getKeys());
        }
        final boolean hasListeners = hasListeners(store, keys);
        if (hasListeners) {
            return offer(store, valueContainers, function, () -> true).isSuccessful();
        }
    }
    return store.offerFastNoEvents(valueContainers, function);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) LanternKey(org.lanternpowered.server.data.key.LanternKey) Key(org.spongepowered.api.data.key.Key) HashSet(java.util.HashSet)

Example 4 with DataHolder

use of org.spongepowered.api.data.DataHolder in project SpongeCommon by SpongePowered.

the class HasDataFilterDelegate method write.

@Override
public void write(ClassWriter cw, MethodVisitor mv, Method method, Parameter param, int localParam) {
    if (!DataHolder.class.isAssignableFrom(param.getType())) {
        throw new IllegalStateException("Annotated type for data filter is not a DataHolder");
    }
    mv.visitVarInsn(ALOAD, localParam);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(DataHolder.class));
    mv.visitLdcInsn(Type.getType(this.anno.value()));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(DataHolder.class), "get", "(Ljava/lang/Class;)Ljava/util/Optional;", true);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "isPresent", "()Z", false);
    Label success = new Label();
    if (this.anno.inverse()) {
        mv.visitJumpInsn(IFEQ, success);
    } else {
        mv.visitJumpInsn(IFNE, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) Label(org.objectweb.asm.Label)

Example 5 with DataHolder

use of org.spongepowered.api.data.DataHolder in project SpongeCommon by SpongePowered.

the class SupportsDataFilterDelegate method write.

@Override
public void write(ClassWriter cw, MethodVisitor mv, Method method, Parameter param, int localParam) {
    if (!DataHolder.class.isAssignableFrom(param.getType())) {
        throw new IllegalStateException("Annotated type for data filter is not a DataHolder");
    }
    mv.visitVarInsn(ALOAD, localParam);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(DataHolder.class));
    mv.visitLdcInsn(Type.getType(this.anno.value()));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(DataHolder.class), "supports", "(Ljava/lang/Class;)Z", true);
    Label success = new Label();
    if (this.anno.inverse()) {
        mv.visitJumpInsn(IFEQ, success);
    } else {
        mv.visitJumpInsn(IFNE, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) Label(org.objectweb.asm.Label)

Aggregations

DataHolder (org.spongepowered.api.data.DataHolder)9 Label (org.objectweb.asm.Label)4 LanternKey (org.lanternpowered.server.data.key.LanternKey)3 UUID (java.util.UUID)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 Key (org.spongepowered.api.data.key.Key)2 ChangeDataHolderEvent (org.spongepowered.api.event.data.ChangeDataHolderEvent)2 RespawnLocation (org.spongepowered.api.util.RespawnLocation)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RegisteredListener (org.lanternpowered.server.event.RegisteredListener)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)1 CompositeValueStore (org.spongepowered.api.data.value.mutable.CompositeValueStore)1 Cause (org.spongepowered.api.event.cause.Cause)1