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));
}
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.
}
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);
}
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);
}
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);
}
Aggregations