use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SchematicTranslator method translate.
@Override
public Schematic translate(DataView view) throws InvalidDataException {
int version = view.getInt(DataQueries.Schematic.VERSION).get();
// TODO version conversions
if (version != VERSION) {
throw new InvalidDataException(String.format("Unknown schematic version %d (current version is %d)", version, VERSION));
}
DataView metadata = view.getView(DataQueries.Schematic.METADATA).orElse(null);
if (metadata != null) {
Optional<DataView> dot_data = metadata.getView(DataQuery.of("."));
if (dot_data.isPresent()) {
DataView data = dot_data.get();
for (DataQuery key : data.getKeys(false)) {
if (!metadata.contains(key)) {
metadata.set(key, data.get(key).get());
}
}
}
}
// TODO error handling for these optionals
int width = view.getShort(DataQueries.Schematic.WIDTH).get();
int height = view.getShort(DataQueries.Schematic.HEIGHT).get();
int length = view.getShort(DataQueries.Schematic.LENGTH).get();
if (width > MAX_SIZE || height > MAX_SIZE || length > MAX_SIZE) {
throw new InvalidDataException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, MAX_SIZE));
}
int[] offset = (int[]) view.get(DataQueries.Schematic.OFFSET).orElse(null);
if (offset == null) {
offset = new int[3];
}
if (offset.length != 3) {
throw new InvalidDataException("Schematic offset was not of length 3");
}
BlockPalette palette;
Optional<DataView> paletteData = view.getView(DataQueries.Schematic.PALETTE);
int palette_max = view.getInt(DataQueries.Schematic.PALETTE_MAX).orElse(0xFFFF);
if (paletteData.isPresent()) {
// If we had a default palette_max we don't want to allocate all
// that space for nothing so we use a sensible default instead
palette = new BimapPalette(palette_max != 0xFFFF ? palette_max : 64);
DataView paletteMap = paletteData.get();
Set<DataQuery> paletteKeys = paletteMap.getKeys(false);
for (DataQuery key : paletteKeys) {
BlockState state = Sponge.getRegistry().getType(BlockState.class, key.getParts().get(0)).get();
((BimapPalette) palette).assign(state, paletteMap.getInt(key).get());
}
} else {
palette = GlobalPalette.instance;
}
MutableBlockVolume buffer = new ArrayMutableBlockBuffer(palette, new Vector3i(-offset[0], -offset[1], -offset[2]), new Vector3i(width, height, length));
byte[] blockdata = (byte[]) view.get(DataQueries.Schematic.BLOCK_DATA).get();
int index = 0;
int i = 0;
int value = 0;
int varint_length = 0;
while (i < blockdata.length) {
value = 0;
varint_length = 0;
while (true) {
value |= (blockdata[i] & 127) << (varint_length++ * 7);
if (varint_length > 5) {
throw new RuntimeException("VarInt too big (probably corrupted data)");
}
if ((blockdata[i] & 128) != 128) {
i++;
break;
}
i++;
}
// index = (y * length + z) * width + x
int y = index / (width * length);
int z = (index % (width * length)) / width;
int x = (index % (width * length)) % width;
BlockState state = palette.get(value).get();
buffer.setBlock(x - offset[0], y - offset[1], z - offset[2], state);
index++;
}
Map<Vector3i, TileEntityArchetype> tiles = Maps.newHashMap();
List<DataView> tiledata = view.getViewList(DataQueries.Schematic.TILEENTITY_DATA).orElse(null);
if (tiledata != null) {
for (DataView tile : tiledata) {
int[] pos = (int[]) tile.get(DataQueries.Schematic.TILEENTITY_POS).get();
if (offset.length != 3) {
throw new InvalidDataException("Schematic tileentity pos was not of length 3");
}
TileEntityType type = TileEntityTypeRegistryModule.getInstance().getForClass(TileEntity.REGISTRY.getObject(new ResourceLocation(tile.getString(DataQuery.of("id")).get())));
TileEntityArchetype archetype = new SpongeTileEntityArchetypeBuilder().state(buffer.getBlock(pos[0] - offset[0], pos[1] - offset[1], pos[2] - offset[2])).tileData(tile).tile(type).build();
tiles.put(new Vector3i(pos[0] - offset[0], pos[1] - offset[1], pos[2] - offset[2]), archetype);
}
}
Schematic schematic = new SpongeSchematic(buffer, tiles, metadata);
return schematic;
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class CustomDataNbtUtil method apply.
public static DataTransactionResult apply(DataView view, DataManipulator<?, ?> manipulator) {
if (!view.contains(DataQueries.Compatibility.Forge.ROOT)) {
view.set(DataQueries.Compatibility.Forge.ROOT, DataContainer.createNew());
}
final DataView forgeCompound = view.getView(DataQueries.Compatibility.Forge.ROOT).orElseThrow(DataUtil.dataNotFound());
if (!forgeCompound.contains(DataQueries.General.SPONGE_ROOT)) {
forgeCompound.set(DataQueries.General.SPONGE_ROOT, DataContainer.createNew());
}
final DataView spongeTag = forgeCompound.getView(DataQueries.General.SPONGE_ROOT).orElseThrow(DataUtil.dataNotFound());
boolean isReplacing;
// Validate that the custom manipulator isn't already existing in the compound
final List<DataView> customData;
if (spongeTag.contains(DataQueries.General.CUSTOM_MANIPULATOR_LIST)) {
customData = spongeTag.getViewList(DataQueries.General.CUSTOM_MANIPULATOR_LIST).orElseThrow(DataUtil.dataNotFound());
for (DataView dataView : customData) {
final String dataId = dataView.getString(DataQueries.DATA_ID).orElseThrow(DataUtil.dataNotFound());
if (DataUtil.getRegistrationFor(manipulator).getId().equals(dataId)) {
final DataView existingData = dataView.getView(DataQueries.INTERNAL_DATA).orElseThrow(DataUtil.dataNotFound());
DataManipulator<?, ?> existing = deserialize(dataId, existingData);
isReplacing = existing != null;
final DataContainer container = manipulator.toContainer();
dataView.set(DataQueries.INTERNAL_DATA, container);
if (isReplacing) {
return DataTransactionResult.successReplaceResult(manipulator.getValues(), existing.getValues());
}
return DataTransactionResult.successReplaceResult(manipulator.getValues(), ImmutableList.of());
}
}
} else {
customData = new ArrayList<>();
}
final DataContainer container = DataContainer.createNew();
container.set(DataQueries.DATA_ID, DataUtil.getRegistrationFor(manipulator).getId());
container.set(DataQueries.INTERNAL_DATA, manipulator.toContainer());
customData.add(container);
spongeTag.set(DataQueries.General.CUSTOM_MANIPULATOR_LIST, customData);
return DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS).success(manipulator.getValues()).build();
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SpongeEntitySnapshot method toContainer.
@Override
public DataContainer toContainer() {
final List<DataView> dataList = DataUtil.getSerializedImmutableManipulatorList(this.manipulators);
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(Queries.WORLD_ID, this.worldUuid.toString()).set(DataQueries.ENTITY_TYPE, this.entityType.getId()).createView(DataQueries.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, this.position.getX()).set(Queries.POSITION_Y, this.position.getY()).set(Queries.POSITION_Z, this.position.getZ()).getContainer().createView(DataQueries.ENTITY_ROTATION).set(Queries.POSITION_X, this.rotation.getX()).set(Queries.POSITION_Y, this.rotation.getY()).set(Queries.POSITION_Z, this.rotation.getZ()).getContainer().createView(DataQueries.ENTITY_SCALE).set(Queries.POSITION_X, this.scale.getX()).set(Queries.POSITION_Y, this.scale.getY()).set(Queries.POSITION_Z, this.scale.getZ()).getContainer().set(DataQueries.DATA_MANIPULATORS, dataList);
if (this.entityUuid != null) {
container.set(DataQueries.ENTITY_ID, this.entityUuid.toString());
}
if (this.compound != null) {
container.set(DataQueries.UNSAFE_NBT, NbtTranslator.getInstance().translateFrom(this.compound));
}
return container;
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SpongeItemStackBuilder method fromContainer.
@Override
public ItemStack.Builder fromContainer(DataView container) {
checkNotNull(container);
if (!container.contains(DataQueries.ITEM_TYPE) || !container.contains(DataQueries.ITEM_COUNT) || !container.contains(DataQueries.ITEM_DAMAGE_VALUE)) {
return this;
}
reset();
final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
quantity(count);
final String itemTypeId = getData(container, DataQueries.ITEM_TYPE, String.class);
final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemTypeId).get();
itemType(itemType);
this.damageValue = getData(container, DataQueries.ITEM_DAMAGE_VALUE, Integer.class);
if (container.contains(DataQueries.UNSAFE_NBT)) {
final NBTTagCompound compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
if (compound.hasKey(NbtDataUtil.SPONGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
compound.removeTag(NbtDataUtil.SPONGE_DATA);
}
this.compound = compound;
}
if (container.contains(DataQueries.DATA_MANIPULATORS)) {
final List<DataView> views = container.getViewList(DataQueries.DATA_MANIPULATORS).get();
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
this.itemDataSet = new HashSet<>();
manipulators.forEach(this.itemDataSet::add);
}
return this;
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MixinItemStack method resyncCustomToTag.
private void resyncCustomToTag() {
if (!this.manipulators.isEmpty()) {
final NBTTagList newList = new NBTTagList();
final List<DataView> manipulatorViews = DataUtil.getSerializedManipulatorList(this.getCustomManipulators());
for (DataView dataView : manipulatorViews) {
newList.appendTag(NbtTranslator.getInstance().translateData(dataView));
}
final NBTTagCompound spongeCompound = getOrCreateSubCompound(NbtDataUtil.SPONGE_DATA);
spongeCompound.setTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, newList);
} else if (!this.failedData.isEmpty()) {
final NBTTagList newList = new NBTTagList();
for (DataView failedDatum : this.failedData) {
newList.appendTag(NbtTranslator.getInstance().translateData(failedDatum));
}
final NBTTagCompound spongeCompound = getOrCreateSubCompound(NbtDataUtil.SPONGE_DATA);
spongeCompound.setTag(NbtDataUtil.FAILED_CUSTOM_DATA, newList);
} else {
if (hasTagCompound()) {
this.getTagCompound().removeTag(NbtDataUtil.SPONGE_DATA);
}
if (this.getTagCompound().hasNoTags()) {
this.setTagCompound(null);
}
}
}
Aggregations