use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class ScoreboardIO method write.
public static void write(Path folder, Scoreboard scoreboard) throws IOException {
final List<DataView> objectives = scoreboard.getObjectives().stream().map(objective -> DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(NAME, objective.getName()).set(DISPLAY_NAME, ((LanternObjective) objective).getLegacyDisplayName()).set(CRITERION_NAME, objective.getCriterion().getId()).set(DISPLAY_MODE, objective.getDisplayMode().getId())).collect(Collectors.toList());
final List<DataView> scores = new ArrayList<>();
for (Score score : scoreboard.getScores()) {
final Iterator<Objective> it = score.getObjectives().iterator();
final DataView baseView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(NAME, ((LanternScore) score).getLegacyName()).set(SCORE, score.getScore());
// TODO: Locked state
final DataView mainView = baseView.copy().set(OBJECTIVE, it.next().getName());
final List<String> extraObjectives = new ArrayList<>();
while (it.hasNext()) {
final String extraObjectiveName = it.next().getName();
scores.add(baseView.copy().set(OBJECTIVE, extraObjectiveName).set(INVALID, (byte) 1));
extraObjectives.add(extraObjectiveName);
}
if (!extraObjectives.isEmpty()) {
mainView.set(EXTRA_OBJECTIVES, extraObjectives);
}
}
final List<DataView> teams = new ArrayList<>();
for (Team team : scoreboard.getTeams()) {
final DataView container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(ALLOW_FRIENDLY_FIRE, (byte) (team.allowFriendlyFire() ? 1 : 0)).set(CAN_SEE_FRIENDLY_INVISIBLES, (byte) (team.canSeeFriendlyInvisibles() ? 1 : 0)).set(NAME_TAG_VISIBILITY, team.getNameTagVisibility().getName()).set(NAME, team.getName()).set(DISPLAY_NAME, ((LanternTeam) team).getLegacyDisplayName()).set(DEATH_MESSAGE_VISIBILITY, team.getDeathMessageVisibility().getName()).set(COLLISION_RULE, team.getCollisionRule().getName()).set(PREFIX, ((LanternTeam) team).getLegacyPrefix()).set(SUFFIX, ((LanternTeam) team).getLegacySuffix());
final TextColor teamColor = team.getColor();
if (teamColor != TextColors.NONE) {
container.set(TEAM_COLOR, teamColor.getId());
}
final Set<Text> members = team.getMembers();
container.set(MEMBERS, members.stream().map(LanternTexts::toLegacy).collect(Collectors.toList()));
teams.add(container);
}
final DataContainer rootDataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final DataView dataView = rootDataContainer.createView(DATA).set(OBJECTIVES, objectives).set(SCORES, scores).set(TEAMS, teams);
final DataView displaySlots = dataView.createView(DISPLAY_SLOTS);
((LanternScoreboard) scoreboard).getObjectivesInSlot().entrySet().forEach(entry -> displaySlots.set(DataQuery.of("slot_" + ((LanternDisplaySlot) entry.getKey()).getInternalId()), entry.getValue().getName()));
IOHelper.write(folder.resolve(SCOREBOARD_DATA), file -> {
NbtStreamUtils.write(rootDataContainer, Files.newOutputStream(file), true);
return true;
});
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class UserIO method save.
public static void save(Path dataFolder, AbstractUser player) throws IOException {
final String fileName = player.getUniqueId().toString() + ".dat";
final DataContainer dataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final ObjectStore<AbstractUser> objectStore = ObjectStoreRegistry.get().get(AbstractUser.class).get();
objectStore.serialize(player, dataContainer);
final Optional<DataView> optSpongeData = dataContainer.getView(DataQueries.EXTENDED_SPONGE_DATA);
dataContainer.remove(DataQueries.EXTENDED_SPONGE_DATA);
Path dataFolder0 = dataFolder.resolve(PLAYER_DATA_FOLDER);
if (!Files.exists(dataFolder0)) {
Files.createDirectories(dataFolder0);
}
Path dataFile = dataFolder0.resolve(fileName);
NbtStreamUtils.write(dataContainer, Files.newOutputStream(dataFile), true);
dataFolder0 = dataFolder.resolve(SPONGE_PLAYER_DATA_FOLDER);
if (!Files.exists(dataFolder0)) {
Files.createDirectories(dataFolder0);
}
dataFile = dataFolder0.resolve(fileName);
if (optSpongeData.isPresent()) {
final DataView spongeData = optSpongeData.get();
spongeData.set(NAME, player.getName());
NbtStreamUtils.write(spongeData, Files.newOutputStream(dataFile), true);
} else {
Files.deleteIfExists(dataFile);
}
final Path statisticsFile = dataFolder.resolve(STATISTICS_FOLDER).resolve(player.getUniqueId().toString() + ".json");
player.getStatisticMap().save(statisticsFile);
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class DataHelper method toContainer.
public static DataContainer toContainer(IValueContainer valueContainer) {
final DataContainer dataContainer = DataContainer.createNew();
final ValueCollection valueCollection = valueContainer.getValueCollection();
final LanternDataManager dataManager = Lantern.getGame().getDataManager();
for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
if (!(registration instanceof Element)) {
continue;
}
final Key<?> key = registration.getKey();
final DataQuery dataQuery = key.getQuery();
final TypeToken<?> typeToken = key.getElementToken();
final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
// The value's shouldn't be null inside a data manipulator,
// since it doesn't support removal of values
dataContainer.set(dataQuery, typeSerializer.serialize(typeToken, context, checkNotNull(((Element) registration).get(), "element")));
}
return dataContainer;
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class IDataHolder method toContainer.
@Override
default DataContainer toContainer() {
final DataContainer dataContainer = DataContainer.createNew();
DataHelper.serializeRawData(dataContainer, this);
return dataContainer;
}
use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.
the class LanternEntity method toContainer.
@Override
public DataContainer toContainer() {
final DataContainer dataContainer = DataContainer.createNew().set(DataQueries.ENTITY_TYPE, getType()).set(DataQueries.POSITION, getPosition()).set(DataQueries.ROTATION, getRotation());
DataHelper.serializeRawData(dataContainer, this);
return dataContainer;
}
Aggregations