Search in sources :

Example 76 with DataView

use of org.spongepowered.api.data.DataView in project modules-extra by CubeEngine.

the class DuctData method from.

@Override
public Optional<DuctData> from(DataContainer container) {
    Optional<DataView> filters = container.getView(FILTERS.getQuery());
    if (filters.isPresent()) {
        Map<Direction, List<ItemStack>> map = new HashMap<>();
        for (DataQuery key : filters.get().getKeys(false)) {
            Direction dir = Direction.valueOf(key.toString());
            List<ItemStack> list = filters.get().getSerializableList(key, ItemStack.class).orElse(new ArrayList<>());
            map.put(dir, list);
        }
        this.setFilters((map));
        return Optional.of(this);
    }
    Optional<Integer> uses = container.getInt(USES.getQuery());
    if (uses.isPresent()) {
        this.setUses(uses.get());
        return Optional.of(this);
    }
    return Optional.empty();
}
Also used : DataView(org.spongepowered.api.data.DataView) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) DataQuery(org.spongepowered.api.data.DataQuery) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Direction(org.spongepowered.api.util.Direction)

Example 77 with DataView

use of org.spongepowered.api.data.DataView in project AdamantineShield by Karanum.

the class RollbackManager method performAddition.

// TODO: Set proper causes for block changes caused by rollback/undo
private void performAddition(LookupLine line) {
    World w = Sponge.getServer().getWorld(line.getWorld()).orElse(null);
    if (w == null)
        return;
    if (line.getTarget() instanceof ItemType) {
        Optional<TileEntity> te = w.getTileEntity(line.getPos());
        if (te.isPresent() && te.get() instanceof TileEntityCarrier) {
            TileEntityCarrier c = (TileEntityCarrier) te.get();
            Inventory i = c.getInventory();
            ItemType type = (ItemType) line.getTarget();
            ItemStack stack = ItemStack.builder().fromContainer(line.getDataAsView()).itemType(type).quantity(line.getCount()).build();
            Inventory slot = i.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(line.getSlot())));
            slot.set(stack);
        }
    } else if (line.getTarget() instanceof BlockType) {
        BlockState block = null;
        if (line.getDataAsView() == null) {
            block = BlockState.builder().blockType((BlockType) line.getTarget()).build();
            w.setBlock(line.getPos(), block);
        } else {
            DataView blockData = line.getDataAsView();
            DataView blockState = blockData.getView(DataQuery.of("BlockState")).orElse(null);
            block = BlockState.builder().build(blockState).orElse(null);
            if (block != null)
                w.setBlock(line.getPos(), block);
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) DataView(org.spongepowered.api.data.DataView) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ItemType(org.spongepowered.api.item.ItemType) TileEntityCarrier(org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier) World(org.spongepowered.api.world.World) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 78 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class MemoryDataView method remove.

@Override
public DataView remove(DataQuery path) {
    checkNotNull(path, "path");
    final List<String> parts = path.getParts();
    if (parts.size() > 1) {
        final String subKey = parts.get(0);
        final DataQuery subQuery = of(subKey);
        final Optional<DataView> subViewOptional = getUnsafeView(subQuery);
        if (!subViewOptional.isPresent()) {
            return this;
        }
        final DataView subView = subViewOptional.get();
        subView.remove(path.popFirst());
    } else {
        this.map.remove(parts.get(0));
    }
    return this;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery)

Example 79 with DataView

use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.

the class ScoreboardIO method read.

public static Scoreboard read(Path worldFolder) throws IOException {
    DataView dataView = IOHelper.<DataView>read(worldFolder.resolve(SCOREBOARD_DATA), file -> {
        try {
            return NbtStreamUtils.read(Files.newInputStream(file), true);
        } catch (IOException e) {
            throw new IOException("Unable to access " + file.getFileName() + "!", e);
        }
    }).orElse(null);
    final Scoreboard.Builder scoreboardBuilder = Scoreboard.builder();
    if (dataView == null) {
        return scoreboardBuilder.build();
    }
    final Map<String, Objective> objectives = new HashMap<>();
    dataView = dataView.getView(DATA).orElseThrow(() -> new IllegalStateException("Unable to find the data compound."));
    dataView.getViewList(OBJECTIVES).ifPresent(list -> list.forEach(entry -> {
        final String name = entry.getString(NAME).get();
        final Text displayName = LanternTexts.fromLegacy(entry.getString(DISPLAY_NAME).get());
        final Criterion criterion = Sponge.getRegistry().getType(Criterion.class, entry.getString(CRITERION_NAME).get()).orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a criterion with id: {}, default to dummy.", entry.getString(CRITERION_NAME).get());
            return Criteria.DUMMY;
        });
        final ObjectiveDisplayMode objectiveDisplayMode = Sponge.getRegistry().getType(ObjectiveDisplayMode.class, entry.getString(DISPLAY_MODE).get()).orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a display mode with id: {}, default to integer.", entry.getString(CRITERION_NAME).get());
            return ObjectiveDisplayModes.INTEGER;
        });
        objectives.put(name, Objective.builder().name(name).displayName(displayName).criterion(criterion).objectiveDisplayMode(objectiveDisplayMode).build());
    }));
    dataView.getViewList(SCORES).ifPresent(list -> list.forEach(entry -> {
        // We have to keep all the entries to remain compatible with vanilla mc.
        if (entry.getInt(INVALID).orElse(0) > 0) {
            return;
        }
        final Text name = LanternTexts.fromLegacy(entry.getString(NAME).get());
        final int value = entry.getInt(SCORE).get();
        // TODO
        final boolean locked = entry.getInt(LOCKED).orElse(0) > 0;
        final String objectiveName = entry.getString(OBJECTIVE).get();
        Score score = null;
        Objective objective = objectives.get(objectiveName);
        if (objective != null) {
            score = addToObjective(objective, null, name, value);
        }
        final List<String> extraObjectives = entry.getStringList(EXTRA_OBJECTIVES).orElse(null);
        if (extraObjectives != null) {
            for (String extraObjective : extraObjectives) {
                objective = objectives.get(extraObjective);
                if (objective != null) {
                    score = addToObjective(objective, score, name, value);
                }
            }
        }
    }));
    final List<Team> teams = new ArrayList<>();
    dataView.getViewList(TEAMS).ifPresent(list -> list.forEach(entry -> {
        final Team.Builder builder = Team.builder().allowFriendlyFire(entry.getInt(ALLOW_FRIENDLY_FIRE).orElse(0) > 0).canSeeFriendlyInvisibles(entry.getInt(CAN_SEE_FRIENDLY_INVISIBLES).orElse(0) > 0).name(entry.getString(NAME).get()).displayName(LanternTexts.fromLegacy(entry.getString(DISPLAY_NAME).get())).prefix(LanternTexts.fromLegacy(entry.getString(PREFIX).get())).suffix(LanternTexts.fromLegacy(entry.getString(SUFFIX).get())).members(entry.getStringList(MEMBERS).get().stream().map(LanternTexts::fromLegacy).collect(Collectors.toSet()));
        entry.getString(NAME_TAG_VISIBILITY).ifPresent(value -> builder.nameTagVisibility(Sponge.getRegistry().getAllOf(Visibility.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a name tag visibility with id: {}, default to always.", value);
            return Visibilities.ALWAYS;
        })));
        entry.getString(DEATH_MESSAGE_VISIBILITY).ifPresent(value -> builder.deathTextVisibility(Sponge.getRegistry().getAllOf(Visibility.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a death message visibility with id: {}, default to always.", value);
            return Visibilities.ALWAYS;
        })));
        entry.getString(COLLISION_RULE).ifPresent(value -> builder.collisionRule(Sponge.getRegistry().getAllOf(CollisionRule.class).stream().filter(visibility -> visibility.getName().equals(value)).findFirst().orElseGet(() -> {
            Lantern.getLogger().warn("Unable to find a collision rule with id: {}, default to never.", value);
            return CollisionRules.NEVER;
        })));
        entry.getString(TEAM_COLOR).ifPresent(color -> {
            TextColor textColor = Sponge.getRegistry().getType(TextColor.class, color).orElseGet(() -> {
                Lantern.getLogger().warn("Unable to find a team color with id: {}, default to none.", color);
                return TextColors.NONE;
            });
            if (textColor != TextColors.NONE && textColor != TextColors.RESET) {
                builder.color(textColor);
            }
        });
        teams.add(builder.build());
    }));
    final Scoreboard scoreboard = scoreboardBuilder.objectives(new ArrayList<>(objectives.values())).teams(teams).build();
    dataView.getView(DISPLAY_SLOTS).ifPresent(displaySlots -> {
        for (DataQuery key : displaySlots.getKeys(false)) {
            final Matcher matcher = DISPLAY_SLOT_PATTERN.matcher(key.getParts().get(0));
            if (matcher.matches()) {
                final int internalId = Integer.parseInt(matcher.group(1));
                Lantern.getRegistry().getRegistryModule(DisplaySlotRegistryModule.class).get().getByInternalId(internalId).ifPresent(slot -> {
                    final Objective objective = objectives.get(displaySlots.getString(key).get());
                    if (objective != null) {
                        scoreboard.updateDisplaySlot(objective, slot);
                    }
                });
            }
        }
    });
    return scoreboard;
}
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) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) LanternScoreboard(org.lanternpowered.server.scoreboard.LanternScoreboard) Criterion(org.spongepowered.api.scoreboard.critieria.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) Team(org.spongepowered.api.scoreboard.Team) LanternTeam(org.lanternpowered.server.scoreboard.LanternTeam) DataQuery(org.spongepowered.api.data.DataQuery) TextColor(org.spongepowered.api.text.format.TextColor) ObjectiveDisplayMode(org.spongepowered.api.scoreboard.objective.displaymode.ObjectiveDisplayMode) CollisionRule(org.spongepowered.api.scoreboard.CollisionRule) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) 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) DisplaySlotRegistryModule(org.lanternpowered.server.game.registry.type.scoreboard.DisplaySlotRegistryModule) Visibility(org.spongepowered.api.scoreboard.Visibility)

Example 80 with DataView

use of org.spongepowered.api.data.DataView 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)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6