use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class LanternByteBuffer method getDataView.
@Nullable
@Override
public DataView getDataView(int index) {
final int oldIndex = this.buf.readerIndex();
this.buf.readerIndex(index);
final DataView data = readDataView();
this.buf.readerIndex(oldIndex);
return data;
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class LanternTextHelper method parseHoverAction.
@SuppressWarnings("deprecation")
public static HoverAction<?> parseHoverAction(String action, String value) throws JsonParseException {
final DataView dataView;
switch(action) {
case "show_text":
return TextActions.showText(TextSerializers.LEGACY_FORMATTING_CODE.deserializeUnchecked(value));
case "show_item":
try {
dataView = JsonDataFormat.readContainer(value, false);
} catch (IOException e) {
throw new JsonParseException("Failed to parse the item data container", e);
}
final ItemStack itemStack = ItemStackStore.INSTANCE.deserialize(dataView);
return TextActions.showItem(itemStack.createSnapshot());
case "show_entity":
try {
dataView = JsonDataFormat.readContainer(value, false);
} catch (IOException e) {
throw new JsonParseException("Failed to parse the entity data container", e);
}
final UUID uuid = UUID.fromString(dataView.getString(SHOW_ENTITY_ID).get());
final String name = dataView.getString(SHOW_ENTITY_NAME).get();
EntityType entityType = null;
if (dataView.contains(SHOW_ENTITY_TYPE)) {
entityType = Sponge.getRegistry().getType(EntityType.class, dataView.getString(SHOW_ENTITY_TYPE).get()).orElse(null);
}
return TextActions.showEntity(uuid, name, entityType);
default:
throw new IllegalArgumentException("Unknown hover action type: " + action);
}
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class LanternBlockSnapshot method toContainer.
@Override
public DataContainer toContainer() {
final DataContainer container = DataContainer.createNew().set(DataQueries.BLOCK_STATE, this.state);
if (this.location != null) {
container.set(Queries.WORLD_ID, this.location.world.getUniqueId());
final DataView positionView = container.createView(DataQueries.SNAPSHOT_WORLD_POSITION);
positionView.set(Queries.POSITION_X, this.location.position.getX());
positionView.set(Queries.POSITION_Y, this.location.position.getY());
positionView.set(Queries.POSITION_Z, this.location.position.getZ());
}
this.notifier.ifPresent(notifier -> container.set(Queries.NOTIFIER_ID, notifier));
this.creator.ifPresent(creator -> container.set(Queries.CREATOR_ID, creator));
return container;
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class FlatGeneratorSettingsParser method toString.
public static String toString(FlatGeneratorSettings settings) {
// All the parts
final List<Object> parts = new ArrayList<>();
// The current version
parts.add(3);
// All the layers
final List<String> layers = new ArrayList<>();
settings.getLayers().forEach(layer -> {
final StringBuilder builder = new StringBuilder();
final int depth = layer.getDepth();
// Only append the depth if needed
if (depth > 1) {
builder.append(depth).append('*');
}
final BlockState block = layer.getBlockState();
// Append the block id
builder.append(block.getType().getId());
final int data = BlockRegistryModule.get().getStateData(block);
// Only append the data if needed
if (data > 0) {
builder.append(':').append(data);
}
layers.add(builder.toString());
});
// Add the layers part
parts.add(Joiner.on(',').join(layers));
// Add the biome id part
parts.add(BiomeRegistryModule.get().getInternalId(settings.getBiomeType()));
final List<String> extraDataValues = new ArrayList<>();
settings.getExtraData().getValues(false).entrySet().forEach(e -> {
final Object value = e.getValue();
if (value instanceof DataView) {
final List<String> values = new ArrayList<>();
((DataView) value).getValues(false).entrySet().forEach(e1 -> {
final Object value1 = e1.getValue();
// Only integer numbers are currently supported
if (value instanceof Number) {
values.add(e1.getKey().getParts().get(0) + '=' + ((Number) value1).intValue());
}
});
final StringBuilder builder = new StringBuilder();
builder.append(e.getKey().getParts().get(0));
if (values.size() > 0) {
builder.append('(');
builder.append(Joiner.on(' ').join(values));
builder.append(')');
}
extraDataValues.add(builder.toString());
}
});
if (!extraDataValues.isEmpty()) {
parts.add(Joiner.on(',').join(extraDataValues));
}
return Joiner.on(';').join(parts);
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class LanternLoadingTicketIO method save.
static void save(Path worldFolder, Set<LanternLoadingTicket> tickets) throws IOException {
final Path file = worldFolder.resolve(TICKETS_FILE);
if (!Files.exists(file)) {
Files.createFile(file);
}
final Multimap<String, LanternLoadingTicket> sortedByPlugin = HashMultimap.create();
for (LanternLoadingTicket ticket : tickets) {
sortedByPlugin.put(ticket.getPlugin(), ticket);
}
final List<DataView> ticketHolders = new ArrayList<>();
for (Entry<String, Collection<LanternLoadingTicket>> entry : sortedByPlugin.asMap().entrySet()) {
final Collection<LanternLoadingTicket> tickets0 = entry.getValue();
final List<DataView> ticketEntries = new ArrayList<>();
for (LanternLoadingTicket ticket0 : tickets0) {
final DataContainer ticketData = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
ticketData.set(TICKET_TYPE, ticket0 instanceof EntityLoadingTicket ? TYPE_ENTITY : TYPE_NORMAL);
final int numChunks = ticket0.getNumChunks();
// Store the list depth for backwards compatible or something,
// the current forge version doesn't use it either
ticketData.set(CHUNK_LIST_DEPTH, (byte) Math.min(numChunks, 127));
// Storing the chunks number, this number is added by us
ticketData.set(CHUNK_NUMBER, numChunks);
if (ticket0 instanceof PlayerLoadingTicket) {
final PlayerLoadingTicket ticket1 = (PlayerLoadingTicket) ticket0;
// This is a bit strange, since it already added,
// but if forge uses it...
ticketData.set(MOD_ID, entry.getKey());
ticketData.set(PLAYER_UUID, ticket1.getPlayerUniqueId().toString());
}
if (ticket0.extraData != null) {
ticketData.set(MOD_DATA, ticket0.extraData);
}
if (ticket0 instanceof EntityChunkLoadingTicket) {
final EntityChunkLoadingTicket ticket1 = (EntityChunkLoadingTicket) ticket0;
ticket1.getOrCreateEntityReference().ifPresent(ref -> {
final Vector2i position = ref.getChunkCoords();
final UUID uniqueId = ref.getUniqueId();
ticketData.set(CHUNK_X, position.getX());
ticketData.set(CHUNK_Z, position.getY());
ticketData.set(ENTITY_UUID_MOST, uniqueId.getMostSignificantBits());
ticketData.set(ENTITY_UUID_LEAST, uniqueId.getLeastSignificantBits());
});
}
ticketEntries.add(ticketData);
}
ticketHolders.add(DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(HOLDER_NAME, entry.getKey()).set(TICKETS, ticketEntries));
}
final DataContainer dataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(HOLDER_LIST, ticketHolders);
NbtStreamUtils.write(dataContainer, Files.newOutputStream(file), true);
}
Aggregations