use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class LanternLoadingTicketIO method load.
static Multimap<String, LanternLoadingTicket> load(Path worldFolder, LanternChunkManager chunkManager, LanternChunkTicketManager service) throws IOException {
final Multimap<String, LanternLoadingTicket> tickets = HashMultimap.create();
final Path file = worldFolder.resolve(TICKETS_FILE);
if (!Files.exists(file)) {
return tickets;
}
final DataContainer dataContainer = NbtStreamUtils.read(Files.newInputStream(file), true);
final Set<String> callbacks = service.getCallbacks().keySet();
final List<DataView> ticketHolders = dataContainer.getViewList(HOLDER_LIST).get();
for (DataView ticketHolder : ticketHolders) {
final String holderName = ticketHolder.getString(HOLDER_NAME).get();
if (!Sponge.getPluginManager().isLoaded(holderName)) {
Lantern.getLogger().warn("Found chunk loading data for plugin {} which is currently not available or active" + " - it will be removed from the world save", holderName);
continue;
}
if (!callbacks.contains(holderName)) {
Lantern.getLogger().warn("The plugin {} has registered persistent chunk loading data but doesn't seem" + " to want to be called back with it - it will be removed from the world save", holderName);
continue;
}
final int maxNumChunks = chunkManager.getMaxChunksForPluginTicket(holderName);
final List<DataView> ticketEntries = ticketHolder.getViewList(TICKETS).get();
for (DataView ticketEntry : ticketEntries) {
final int type = ticketEntry.getInt(TICKET_TYPE).get();
UUID playerUUID = null;
if (ticketEntry.contains(PLAYER_UUID)) {
playerUUID = UUID.fromString(ticketEntry.getString(PLAYER_UUID).get());
}
int numChunks = maxNumChunks;
if (ticketEntry.contains(CHUNK_NUMBER)) {
numChunks = ticketEntry.getInt(CHUNK_NUMBER).get();
} else if (ticketEntry.contains(CHUNK_LIST_DEPTH)) {
numChunks = ticketEntry.getInt(CHUNK_LIST_DEPTH).get();
}
final LanternLoadingTicket ticket;
if (type == TYPE_NORMAL) {
if (playerUUID != null) {
ticket = new LanternPlayerLoadingTicket(holderName, chunkManager, playerUUID, maxNumChunks, numChunks);
} else {
ticket = new LanternLoadingTicket(holderName, chunkManager, maxNumChunks, numChunks);
}
} else if (type == TYPE_ENTITY) {
final LanternEntityLoadingTicket ticket0;
if (playerUUID != null) {
ticket0 = new LanternPlayerEntityLoadingTicket(holderName, chunkManager, playerUUID, maxNumChunks, numChunks);
} else {
ticket0 = new LanternEntityLoadingTicket(holderName, chunkManager, maxNumChunks, numChunks);
}
final int chunkX = ticketEntry.getInt(CHUNK_X).get();
final int chunkZ = ticketEntry.getInt(CHUNK_Z).get();
final long uuidMost = ticketEntry.getLong(ENTITY_UUID_MOST).get();
final long uuidLeast = ticketEntry.getLong(ENTITY_UUID_LEAST).get();
final Vector2i chunkCoords = new Vector2i(chunkX, chunkZ);
final UUID uuid = new UUID(uuidMost, uuidLeast);
ticket0.setEntityReference(new EntityReference(chunkCoords, uuid));
ticket = ticket0;
} else {
Lantern.getLogger().warn("Unknown ticket entry type {} for {}, skipping...", type, holderName);
continue;
}
if (ticketEntry.contains(MOD_DATA)) {
ticket.extraData = ticketEntry.getView(MOD_DATA).get().copy();
}
tickets.put(holderName, ticket);
chunkManager.attach(ticket);
}
}
return tickets;
}
use of org.spongepowered.api.data.DataContainer in project Nucleus by NucleusPowered.
the class CreateWorldCommand method onImport.
private void onImport(Path world, String name) {
// Get the file
Path level = world.resolve("level.dat");
Path levelSponge = world.resolve("level_sponge.dat");
if (Files.exists(level)) {
DataContainer dc;
boolean gz = false;
try {
try (InputStream is = Files.newInputStream(level, StandardOpenOption.READ)) {
// Open it, get the Dimension ID
dc = DataFormats.NBT.readFrom(is);
} catch (EOFException ex) {
try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(level, StandardOpenOption.READ))) {
dc = DataFormats.NBT.readFrom(gzip);
gz = true;
}
}
Files.copy(level, world.resolve("level.dat.nbak"), StandardCopyOption.REPLACE_EXISTING);
dc.set(this.levelName, name);
try (OutputStream os = getOutput(gz, level)) {
DataFormats.NBT.writeTo(os, dc);
os.flush();
}
} catch (IOException e) {
if (Nucleus.getNucleus().isDebugMode()) {
e.printStackTrace();
}
Nucleus.getNucleus().getLogger().warn("Could not read the level.dat. Ignoring.");
}
}
if (Files.exists(levelSponge)) {
DataContainer dc;
boolean gz = false;
try {
try (InputStream is = Files.newInputStream(levelSponge, StandardOpenOption.READ)) {
// Open it, get the Dimension ID
dc = DataFormats.NBT.readFrom(is);
} catch (EOFException ex) {
try (GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(levelSponge, StandardOpenOption.READ))) {
dc = DataFormats.NBT.readFrom(gzip);
gz = true;
}
}
} catch (IOException e) {
if (Nucleus.getNucleus().isDebugMode()) {
e.printStackTrace();
}
Nucleus.getNucleus().getLogger().warn("Could not read the level_sponge.dat. Ignoring.");
return;
}
// For each world, get the dim ID.
Set<Integer> si = Sponge.getServer().getAllWorldProperties().stream().map(x -> x.getAdditionalProperties().getInt(this.toId).orElse(0)).collect(Collectors.toSet());
if (!dc.getInt(this.toId).map(si::contains).orElse(false)) {
for (int i = 2; i < Integer.MAX_VALUE; i++) {
if (!si.contains(i)) {
dc.set(this.toId, i);
break;
}
}
}
UUID uuid = UUID.randomUUID();
dc.set(this.uuidLeast, uuid.getLeastSignificantBits());
dc.set(this.uuidMost, uuid.getMostSignificantBits());
try {
Files.copy(levelSponge, world.resolve("level_sponge.dat.nbak"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
if (Nucleus.getNucleus().isDebugMode()) {
e.printStackTrace();
}
Nucleus.getNucleus().getLogger().warn("Could not backup the level_sponge.dat. Ignoring.");
return;
}
try (OutputStream os = getOutput(gz, levelSponge)) {
DataFormats.NBT.writeTo(os, dc);
os.flush();
} catch (IOException e) {
e.printStackTrace();
Nucleus.getNucleus().getLogger().warn("Could not save the level_sponge.dat. Ignoring.");
}
}
}
use of org.spongepowered.api.data.DataContainer in project Nucleus by NucleusPowered.
the class NucleusItemStackSnapshotSerialiser method serialize.
@Override
public void serialize(TypeToken<?> type, NucleusItemStackSnapshot obj, ConfigurationNode value) throws ObjectMappingException {
DataContainer view = obj.getSnapshot().toContainer();
Map<DataQuery, Object> dataQueryObjectMap = view.getValues(true);
for (Map.Entry<DataQuery, Object> entry : dataQueryObjectMap.entrySet()) {
if (entry.getValue().getClass().isArray()) {
// Convert to a list with type, make it the key.
if (entry.getValue().getClass().getComponentType().isPrimitive()) {
// Create the list of the primitive type.
DataQuery old = entry.getKey();
Tuple<DataQuery, List<?>> dqo = TypeHelper.getList(old, entry.getValue());
view.remove(old);
view.set(dqo.getFirst(), dqo.getSecond());
} else {
// create a list type
view.set(entry.getKey(), Lists.newArrayList((Object[]) entry.getValue()));
}
}
}
value.setValue(DataTranslators.CONFIGURATION_NODE.translate(view));
}
use of org.spongepowered.api.data.DataContainer in project Nucleus by NucleusPowered.
the class NucleusItemStackSnapshotSerialiser method deserialize.
@Override
public NucleusItemStackSnapshot deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
// Process enchantments, temporary fix before Sponge gets a more general fix in.
boolean emptyEnchant = false;
ConfigurationNode ench = value.getNode("UnsafeData", "ench");
if (!ench.isVirtual()) {
List<? extends ConfigurationNode> enchantments = ench.getChildrenList();
if (enchantments.isEmpty()) {
// Remove empty enchantment list.
value.getNode("UnsafeData").removeChild("ench");
} else {
enchantments.forEach(x -> {
try {
short id = Short.parseShort(x.getNode("id").getString());
short lvl = Short.parseShort(x.getNode("lvl").getString());
x.getNode("id").setValue(id);
x.getNode("lvl").setValue(lvl);
} catch (NumberFormatException e) {
x.setValue(null);
}
});
}
}
ConfigurationNode data = value.getNode("Data");
if (!data.isVirtual() && data.hasListChildren()) {
List<? extends ConfigurationNode> n = data.getChildrenList().stream().filter(x -> !x.getNode("DataClass").getString("").endsWith("SpongeEnchantmentData") || (!x.getNode("ManipulatorData", "ItemEnchantments").isVirtual() && x.getNode("ManipulatorData", "ItemEnchantments").hasListChildren())).collect(Collectors.toList());
emptyEnchant = n.size() != data.getChildrenList().size();
if (emptyEnchant) {
if (n.isEmpty()) {
value.removeChild("Data");
} else {
value.getNode("Data").setValue(n);
}
}
}
DataContainer dataContainer = DataTranslators.CONFIGURATION_NODE.translate(value);
Set<DataQuery> ldq = dataContainer.getKeys(true);
for (DataQuery dataQuery : ldq) {
String el = dataQuery.asString(".");
if (el.contains("$Array$")) {
try {
Tuple<DataQuery, Object> r = TypeHelper.getArray(dataQuery, dataContainer);
dataContainer.set(r.getFirst(), r.getSecond());
} catch (Exception e) {
e.printStackTrace();
}
dataContainer.remove(dataQuery);
}
}
ItemStack snapshot;
try {
snapshot = ItemStack.builder().fromContainer(dataContainer).build();
} catch (Exception e) {
return NucleusItemStackSnapshot.NONE;
}
if (emptyEnchant) {
snapshot.offer(Keys.ITEM_ENCHANTMENTS, Lists.newArrayList());
return new NucleusItemStackSnapshot(snapshot.createSnapshot());
}
if (snapshot.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
// Reset the data.
snapshot.offer(Keys.ITEM_ENCHANTMENTS, snapshot.get(Keys.ITEM_ENCHANTMENTS).get());
return new NucleusItemStackSnapshot(snapshot.createSnapshot());
}
return new NucleusItemStackSnapshot(snapshot.createSnapshot());
}
use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.
the class MixinGameProfile method profile$toContainer.
public DataContainer profile$toContainer() {
final DataContainer container = DataContainer.createNew();
container.set(DataQueries.CONTENT_VERSION, this.profile$getContentVersion());
container.set(DataQueries.USER_UUID, this.profile$getUniqueId().toString());
if (this.profile$getName().isPresent()) {
container.set(DataQueries.USER_NAME, this.profile$getName().get());
}
return container;
}
Aggregations