use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class InventorySnapshotSerializer method deserialize.
public static InventorySnapshot deserialize(List<DataView> itemDataViews) {
final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
final Int2ObjectMap<ItemStackSnapshot> itemsByIndex = new Int2ObjectOpenHashMap<>();
for (DataView itemDataView : itemDataViews) {
final int slot = itemDataView.getByte(SLOT).get() & 0xff;
final ItemStackSnapshot itemStackSnapshot = itemStackSerializer.deserialize(itemDataView).createSnapshot();
itemsByIndex.put(slot, itemStackSnapshot);
}
return InventorySnapshot.ofRawMap(itemsByIndex);
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class UserStore method serializeSlot.
private static void serializeSlot(int index, Slot slot, ObjectSerializer<LanternItemStack> itemStackSerializer, List<DataView> views) {
final Optional<ItemStack> optItemStack = slot.peek();
if (!optItemStack.isPresent()) {
return;
}
final ItemStack itemStack = optItemStack.get();
final DataView itemView = itemStackSerializer.serialize((LanternItemStack) itemStack);
itemView.set(SLOT, (byte) index);
views.add(itemView);
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class UserStore method serializeEnderChest.
private static List<DataView> serializeEnderChest(GridInventory enderChestInventory) {
final List<DataView> itemViews = new ArrayList<>();
final Iterable<Slot> slots = enderChestInventory.slots();
for (Slot slot : slots) {
final Optional<ItemStack> optItemStack = slot.peek();
if (!optItemStack.isPresent()) {
continue;
}
final DataView itemView = ItemStackStore.INSTANCE.serialize((LanternItemStack) optItemStack.get());
itemView.set(SLOT, (byte) enderChestInventory.getProperty(slot, SlotIndex.class, null).get().getValue().intValue());
itemViews.add(itemView);
}
return itemViews;
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class UserStore method deserializeValues.
@Override
public void deserializeValues(T player, SimpleValueContainer valueContainer, DataView dataView) {
// Try to convert old bukkit values first
dataView.getLong(BUKKIT_FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
dataView.getLong(BUKKIT_LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
// Deserialize sponge data
dataView.getView(DataQueries.EXTENDED_SPONGE_DATA).ifPresent(view -> {
view.getLong(FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
view.getLong(LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
});
dataView.getView(ABILITIES).ifPresent(view -> {
view.getInt(FLYING).ifPresent(v -> valueContainer.set(Keys.IS_FLYING, v > 0));
view.getDouble(FLYING_SPEED).ifPresent(v -> valueContainer.set(Keys.FLYING_SPEED, v));
view.getInt(CAN_FLY).ifPresent(v -> valueContainer.set(Keys.CAN_FLY, v > 0));
});
final Map<UUID, RespawnLocation> respawnLocations = new HashMap<>();
// Overworld respawn location is saved in the root container
final Optional<Double> optSpawnX = dataView.getDouble(RESPAWN_LOCATIONS_X);
final Optional<Double> optSpawnY = dataView.getDouble(RESPAWN_LOCATIONS_Y);
final Optional<Double> optSpawnZ = dataView.getDouble(RESPAWN_LOCATIONS_Z);
if (optSpawnX.isPresent() && optSpawnY.isPresent() && optSpawnZ.isPresent()) {
UUID uniqueId = Lantern.getWorldManager().getWorldProperties(0).get().getUniqueId();
respawnLocations.put(uniqueId, deserializeRespawnLocation(dataView, uniqueId, optSpawnX.get(), optSpawnY.get(), optSpawnZ.get()));
}
dataView.getViewList(RESPAWN_LOCATIONS).ifPresent(v -> v.forEach(view -> {
int dimensionId = view.getInt(RESPAWN_LOCATIONS_DIMENSION).get();
Lantern.getWorldManager().getWorldProperties(dimensionId).ifPresent(props -> {
UUID uniqueId = props.getUniqueId();
double x = view.getDouble(RESPAWN_LOCATIONS_X).get();
double y = view.getDouble(RESPAWN_LOCATIONS_Y).get();
double z = view.getDouble(RESPAWN_LOCATIONS_Z).get();
respawnLocations.put(uniqueId, deserializeRespawnLocation(view, uniqueId, x, y, z));
});
}));
valueContainer.set(Keys.RESPAWN_LOCATIONS, respawnLocations);
dataView.getInt(SCORE).ifPresent(v -> valueContainer.set(LanternKeys.SCORE, v));
final GameMode gameMode = dataView.getInt(GAME_MODE).flatMap(v -> GameModeRegistryModule.get().getByInternalId(v)).orElse(GameModes.NOT_SET);
valueContainer.set(Keys.GAME_MODE, gameMode);
player.getInventory().getHotbar().setSelectedSlotIndex(dataView.getInt(SELECTED_ITEM_SLOT).orElse(0));
// Deserialize the player inventory
dataView.getViewList(INVENTORY).ifPresent(views -> deserializePlayerInventory(player.getInventory(), views));
// Deserialize the ender chest inventory
dataView.getViewList(ENDER_CHEST_INVENTORY).ifPresent(views -> deserializeEnderChest(player.getEnderChestInventory(), views));
dataView.getView(RECIPE_BOOK).ifPresent(view -> {
view.getInt(RECIPE_BOOK_FILTER_ACTIVE).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_FILTER_ACTIVE, v > 0));
view.getInt(RECIPE_BOOK_GUI_OPEN).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_GUI_OPEN, v > 0));
});
dataView.getString(OPEN_ADVANCEMENT_TREE).ifPresent(id -> valueContainer.set(LanternKeys.OPEN_ADVANCEMENT_TREE, AdvancementTreeRegistryModule.get().getById(id)));
super.deserializeValues(player, valueContainer, dataView);
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class FireworkChargeItemTypeObjectSerializer method serializeExplosion.
static DataView serializeExplosion(FireworkEffect effect) {
final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
dataView.set(FLICKER, (byte) (effect.flickers() ? 1 : 0));
dataView.set(TRAIL, (byte) (effect.hasTrail() ? 1 : 0));
dataView.set(TYPE, (byte) ((LanternFireworkShape) effect.getShape()).getInternalId());
dataView.set(COLORS, effect.getColors().stream().mapToInt(Color::getRgb).toArray());
dataView.set(FADE_COLORS, effect.getFadeColors().stream().mapToInt(Color::getRgb).toArray());
return dataView;
}
Aggregations