Search in sources :

Example 96 with DataContainer

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;
    });
}
Also used : CollisionRule(org.spongepowered.api.scoreboard.CollisionRule) Visibility(org.spongepowered.api.scoreboard.Visibility) HashMap(java.util.HashMap) LanternTexts(org.lanternpowered.server.text.LanternTexts) DataQuery(org.spongepowered.api.data.DataQuery) LanternObjective(org.lanternpowered.server.scoreboard.LanternObjective) ArrayList(java.util.ArrayList) ObjectiveDisplayMode(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode) NbtStreamUtils(org.lanternpowered.server.data.persistence.nbt.NbtStreamUtils) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Objective(org.spongepowered.api.scoreboard.objective.Objective) TextColors(org.spongepowered.api.text.format.TextColors) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) DisplaySlotRegistryModule(org.lanternpowered.server.game.registry.type.scoreboard.DisplaySlotRegistryModule) Iterator(java.util.Iterator) Files(java.nio.file.Files) Criterion(org.spongepowered.api.scoreboard.critieria.Criterion) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) Visibilities(org.spongepowered.api.scoreboard.Visibilities) Set(java.util.Set) LanternDisplaySlot(org.lanternpowered.server.scoreboard.LanternDisplaySlot) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) Team(org.spongepowered.api.scoreboard.Team) IOException(java.io.IOException) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam) Collectors(java.util.stream.Collectors) Score(org.spongepowered.api.scoreboard.Score) Criteria(org.spongepowered.api.scoreboard.critieria.Criteria) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) List(java.util.List) DataView(org.spongepowered.api.data.DataView) CollisionRules(org.spongepowered.api.scoreboard.CollisionRules) Lantern(org.lanternpowered.server.game.Lantern) ObjectiveDisplayModes(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayModes) Pattern(java.util.regex.Pattern) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) DataView(org.spongepowered.api.data.DataView) LanternObjective(org.lanternpowered.server.scoreboard.LanternObjective) Objective(org.spongepowered.api.scoreboard.objective.Objective) Score(org.spongepowered.api.scoreboard.Score) LanternScore(org.lanternpowered.server.scoreboard.LanternScore) DataContainer(org.spongepowered.api.data.DataContainer) LanternDisplaySlot(org.lanternpowered.server.scoreboard.LanternDisplaySlot) LanternTexts(org.lanternpowered.server.text.LanternTexts) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) Team(org.spongepowered.api.scoreboard.Team) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam) TextColor(org.spongepowered.api.text.format.TextColor) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam)

Example 97 with DataContainer

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);
}
Also used : Path(java.nio.file.Path) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) AbstractUser(org.lanternpowered.server.entity.living.player.AbstractUser)

Example 98 with DataContainer

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;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer)

Example 99 with 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;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 100 with 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;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)110 DataView (org.spongepowered.api.data.DataView)30 DataQuery (org.spongepowered.api.data.DataQuery)28 Test (org.junit.Test)24 List (java.util.List)15 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 Nullable (javax.annotation.Nullable)11 Collection (java.util.Collection)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CatalogType (org.spongepowered.api.CatalogType)9 Sponge (org.spongepowered.api.Sponge)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 DataSerializable (org.spongepowered.api.data.DataSerializable)7