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();
}
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);
}
}
}
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;
}
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;
}
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;
});
}
Aggregations