Skip to content

Commit

Permalink
Synchronizing Achievements
Browse files Browse the repository at this point in the history
- Closes #9
  • Loading branch information
BrainStone committed Apr 27, 2017
1 parent 9ed3f82 commit f46c404
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/world/jnc/invsync/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void load() {
Values.Synchronize.enableExperience = synchronize.getNode("enableExperience").getBoolean(true);
Values.Synchronize.enableHealth = synchronize.getNode("enableHealth").getBoolean(true);
Values.Synchronize.enableHunger = synchronize.getNode("enableHunger").getBoolean(true);
Values.Synchronize.enableAchievements = synchronize.getNode("enableAchievements").getBoolean(true);

ConfigurationNode storage = rootNode.getNode("storage");
Values.Storage.storageEngine = storage.getNode("storageEngine").getString(validStorageEngines[0]);
Expand Down Expand Up @@ -132,6 +133,8 @@ public static class Synchronize {
private static boolean enableHealth;
@Getter
private static boolean enableHunger;
@Getter
private static boolean enableAchievements;
}

@UtilityClass
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/world/jnc/invsync/util/InventorySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.persistence.DataFormats;
import org.spongepowered.api.data.value.mutable.MutableBoundedValue;
import org.spongepowered.api.data.value.mutable.SetValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.statistic.achievement.Achievement;

import com.google.common.collect.Sets;

import lombok.Cleanup;
import lombok.experimental.UtilityClass;
Expand All @@ -42,6 +46,7 @@ public class InventorySerializer {
private static final DataQuery HEALTH = DataQuery.of("health");
private static final DataQuery FOOD_LEVEL = DataQuery.of("foodLevel");
private static final DataQuery SATURATION = DataQuery.of("saturation");
private static final DataQuery ACHIEVEMENTS = DataQuery.of("achievements");
private static final DataQuery SLOT = DataQuery.of("slot");
private static final DataQuery STACK = DataQuery.of("stack");

Expand All @@ -50,6 +55,7 @@ public class InventorySerializer {
private static final Key<MutableBoundedValue<Double>> KEY_HEALTH = Keys.HEALTH;
private static final Key<MutableBoundedValue<Integer>> KEY_FOOD_LEVEL = Keys.FOOD_LEVEL;
private static final Key<MutableBoundedValue<Double>> KEY_SATURATION = Keys.SATURATION;
private static final Key<SetValue<Achievement>> KEY_ACHIEVEMENTS = Keys.ACHIEVEMENTS;

public static byte[] serializePlayer(Player player) throws IOException {
DataContainer container = new MemoryDataContainer();
Expand All @@ -73,6 +79,10 @@ public static byte[] serializePlayer(Player player) throws IOException {
container.set(FOOD_LEVEL, player.get(KEY_FOOD_LEVEL).get());
container.set(SATURATION, player.get(KEY_SATURATION).get());
}
if (Config.Values.Synchronize.getEnableAchievements()) {
container.set(ACHIEVEMENTS, player.get(KEY_ACHIEVEMENTS).get());
}

if (Config.Values.Global.getDebug()) {
@Cleanup
ByteArrayOutputStream debug = new ByteArrayOutputStream();
Expand Down Expand Up @@ -112,6 +122,7 @@ public static void deserializePlayer(Player player, byte[] data) throws IOExcept
Optional<Double> health = container.getDouble(HEALTH);
Optional<Integer> foodLevel = container.getInt(FOOD_LEVEL);
Optional<Double> saturation = container.getDouble(SATURATION);
Optional<List<Achievement>> achievements = container.getCatalogTypeList(ACHIEVEMENTS, Achievement.class);

if (inventory.isPresent() && Config.Values.Synchronize.getEnableInventory()) {
deserializeInventory(inventory.get(), player.getInventory());
Expand All @@ -132,6 +143,10 @@ public static void deserializePlayer(Player player, byte[] data) throws IOExcept
player.offer(KEY_FOOD_LEVEL, foodLevel.get());
player.offer(KEY_SATURATION, saturation.get());
}
if (achievements.isPresent() && Config.Values.Synchronize.getEnableAchievements()) {
player.offer(KEY_ACHIEVEMENTS, Sets.newHashSet(achievements.get()));
}

if (Config.Values.Global.getDebug()) {
@Cleanup
ByteArrayOutputStream debug = new ByteArrayOutputStream();
Expand Down

0 comments on commit f46c404

Please sign in to comment.