use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.
the class JsonDataFormat method readView.
private static void readView(JsonReader reader, DataView view) throws IOException {
reader.beginObject();
while (reader.hasNext()) {
DataQuery key = of(reader.nextName());
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
// Check this early so we don't need to copy the view
readView(reader, view.createView(key));
} else {
view.set(key, read(reader));
}
}
reader.endObject();
}
use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.
the class NbtTranslator method containerToCompound.
private static void containerToCompound(final DataView container, final NBTTagCompound compound) {
// We don't need to get deep values since all nested DataViews will be found
// from the instance of checks.
checkNotNull(container);
checkNotNull(compound);
for (Map.Entry<DataQuery, Object> entry : container.getValues(false).entrySet()) {
Object value = entry.getValue();
String key = entry.getKey().asString('.');
if (value instanceof DataView) {
NBTTagCompound inner = new NBTTagCompound();
containerToCompound(container.getView(entry.getKey()).get(), inner);
compound.setTag(key, inner);
} else if (value instanceof Boolean) {
compound.setTag(key + BOOLEAN_IDENTIFER, new NBTTagByte(((Boolean) value) ? (byte) 1 : 0));
} else {
compound.setTag(key, getBaseFromObject(value));
}
}
}
use of org.spongepowered.api.data.DataQuery 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.DataQuery in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testToString.
@Test
public void testToString() {
DataContainer container = DataContainer.createNew();
DataQuery testQuery = of("foo", "bar", "baz");
List<Integer> intList = ImmutableList.of(1, 2, 3, 4);
container.set(testQuery, intList);
assertTrue(container.getIntegerList(testQuery).isPresent());
assertTrue(container.getIntegerList(testQuery).get().equals(intList));
List<Double> doubleList = ImmutableList.of(1.0D, 2.0D, 3.0D, 4.0D);
container.set(testQuery, doubleList);
assertTrue(container.getDoubleList(testQuery).isPresent());
assertTrue(container.getDoubleList(testQuery).get().equals(doubleList));
List<Short> shortList = ImmutableList.of((short) 1, (short) 2, (short) 3, (short) 4);
container.set(testQuery, shortList);
assertTrue(container.getShortList(testQuery).isPresent());
assertTrue(container.getShortList(testQuery).get().equals(shortList));
List<Byte> byteList = ImmutableList.of((byte) 1, (byte) 2, (byte) 3, (byte) 4);
container.set(testQuery, byteList);
List<SimpleData> list = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
String number = Integer.toString(i);
list.add(new SimpleData(i, 0.1 * i, "i", Lists.asList(number, new String[] { " foo", "bar" })));
}
container.set(of("SimpleData"), list);
container.toString();
}
use of org.spongepowered.api.data.DataQuery in project SpongeCommon by SpongePowered.
the class MemoryDataTest method testString.
@Test
public void testString() {
DataContainer container = DataContainer.createNew();
DataQuery testQuery = of("foo", "bar");
container.set(testQuery, "foo");
Optional<String> stringOptional = container.getString(testQuery);
assertTrue(stringOptional.isPresent());
assertTrue(stringOptional.get().equals("foo"));
}
Aggregations