Skip to content

Commit

Permalink
Fixed some more modded items, added wait option for deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaeding committed Sep 13, 2019
1 parent cb239d9 commit 63a2e9e
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import rocks.milspecsg.msdatasync.service.data.ApiInventorySerializer;

import java.util.*;
import java.util.stream.Collectors;

public class ApiSpongeInventorySerializer extends ApiInventorySerializer<Snapshot, Key, User, Inventory, ItemStackSnapshot> {

Expand Down Expand Up @@ -74,10 +75,15 @@ public boolean deserializeInventory(Snapshot snapshot, Inventory inventory, Item
for (Iterator<Inventory> slots = inventory.slots().iterator(); slots.hasNext(); ) {
Inventory slot = slots.next();
if (stacks.hasNext()) {
DataContainer dc = DataContainer.createNew(DataView.SafetyMode.ALL_DATA_CLONED);
deserialize(stacks.next().properties).forEach(dc::set);
ItemStack is = ItemStack.builder().fromContainer(dc).build();
slot.set(is);
SerializedItemStack stack = stacks.next();
try {
DataContainer dc = DataContainer.createNew(DataView.SafetyMode.ALL_DATA_CLONED);
deserialize(stack.properties).forEach(dc::set);
ItemStack is = ItemStack.builder().fromContainer(dc).build();
slot.set(is);
} catch (Exception e) {
e.printStackTrace();
}
} else if (!(inventory instanceof PlayerInventory)) {
if (slots.hasNext()) {
slot.set(fallbackItemStackSnapshot.createStack());
Expand All @@ -86,8 +92,6 @@ public boolean deserializeInventory(Snapshot snapshot, Inventory inventory, Item
}
}
}


} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down Expand Up @@ -132,36 +136,52 @@ private static Map<String, Object> serialize(Map<DataQuery, Object> values) {

private static Map<DataQuery, Object> deserialize(Map<String, Object> values) {
Map<DataQuery, Object> result = new HashMap<>();
values.forEach((s, o) -> {
for (Map.Entry<String, Object> e : values.entrySet()) {
String s = e.getKey();
Object o = e.getValue();
if (o == null) {
continue;
}
s = s.replace('-', '.');
DataQuery dq = DataQuery.of(SEPARATOR, s);
if (o instanceof Map) {
Map<String, Object> m = (Map<String, Object>) o;
Map<DataQuery, Object> r1 = new HashMap<>();
m.forEach((s1, m1) -> {
for (Map.Entry<String, Object> mapEntry : m.entrySet()) {
String s1 = mapEntry.getKey();
Object m1 = mapEntry.getValue();
if (m1 == null) {
continue;
} else if (m1 instanceof List) {
m1 = ((List<?>) m1).stream().filter(Objects::nonNull).collect(Collectors.toList());
}
Object value = m1;
s1 = s1.replace(PERIOD_REPLACEMENT, '.');
try {
Map<DataQuery, Object> v = deserialize((Map<String, Object>) m1);
DataContainer dc = DataContainer.createNew(DataView.SafetyMode.ALL_DATA_CLONED);
v.forEach(dc::set);
if (s1.equals("ench")) {
value = ImmutableList.of(dc);
} else {
value = dc;
if (m1 instanceof Map) {
try {
Map<DataQuery, Object> v = deserialize((Map<String, Object>) m1);
DataContainer dc = DataContainer.createNew(DataView.SafetyMode.ALL_DATA_CLONED);
v.forEach(dc::set);
if (s1.equals("ench")) {
value = ImmutableList.of(dc);
} else {
value = dc;
}
} catch (Exception ignored) {
}
} catch (ClassCastException ignored) {
}
r1.put(DataQuery.of(SEPARATOR, s1), value);
});
}
result.put(dq, r1);
} else if (!s.equals("ItemType") && o instanceof String) {
String n = o.toString().replace(PERIOD_REPLACEMENT, '.');
result.put(dq, n);
} else if (o instanceof List) {
result.put(dq, ((List<?>) o).stream().filter(Objects::nonNull).collect(Collectors.toList()));
} else {
result.put(dq, o);
}
});
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,32 @@ public CompletableFuture<Optional<Snapshot>> deserialize(User user, Object plugi

@Override
public CompletableFuture<Optional<Snapshot>> deserialize(User user, Object plugin) {
return memberRepository.getLatestSnapshot(user.getUniqueId()).thenApplyAsync(optionalSnapshot -> {
CompletableFuture<Void> waitForSnapshot;
if (configurationService.getConfigBoolean(ConfigKeys.SERIALIZE_WAIT_FOR_SNAPSHOT_ON_JOIN)) {
waitForSnapshot = CompletableFuture.runAsync(() -> {
// while (true){
// if (!memberRepository.getNext().map(member -> {
// System.out.println(member.userUUID);
// return member.userUUID.equals(user.getUniqueId());
// }).orElse(false))
// break;
// }
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
} else {
waitForSnapshot = CompletableFuture.completedFuture(null);
}
return waitForSnapshot.thenApplyAsync(v -> memberRepository.getLatestSnapshot(user.getUniqueId()).thenApplyAsync(optionalSnapshot -> {
if (!optionalSnapshot.isPresent()) {
System.err.println("[MSDataSync] Could not find snapshot for user " + user.getName() + "! Check your DB configuration!");
return Optional.empty();
return Optional.<Snapshot>empty();
}

return deserialize(user, plugin, optionalSnapshot.get()).join();
});

}).join());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rocks.milspecsg.msdatasync.service.implementation.member;

import com.google.inject.Inject;
import com.mongodb.WriteResult;
import org.bson.types.ObjectId;
import org.mongodb.morphia.query.Query;
Expand All @@ -11,13 +12,19 @@
import rocks.milspecsg.msdatasync.model.core.Member;
import rocks.milspecsg.msdatasync.model.core.Snapshot;
import rocks.milspecsg.msdatasync.service.member.ApiMemberRepository;
import rocks.milspecsg.msrepository.db.mongodb.MongoContext;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public abstract class ApiSpongeMemberRepository extends ApiMemberRepository<Member, Snapshot, Key, User> {

@Inject
public ApiSpongeMemberRepository(MongoContext mongoContext) {
super(mongoContext);
}

@Override
public Optional<User> getUser(UUID userUUID) {
return Sponge.getServiceManager().provide(UserStorageService.class).flatMap(u -> u.get(userUUID));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package rocks.milspecsg.msdatasync.service.implementation.snapshot;

import com.google.inject.Inject;
import org.spongepowered.api.data.key.Key;
import rocks.milspecsg.msdatasync.model.core.Snapshot;
import rocks.milspecsg.msdatasync.service.snapshot.ApiSnapshotRepository;
import rocks.milspecsg.msrepository.db.mongodb.MongoContext;

public abstract class ApiSpongeSnapshotRepository extends ApiSnapshotRepository<Snapshot, Key> {

@Inject
public ApiSpongeSnapshotRepository(MongoContext mongoContext) {
super(mongoContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rocks.milspecsg.msdatasync.api.member;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.mongodb.morphia.query.Query;
import rocks.milspecsg.msdatasync.model.core.Member;
Expand Down Expand Up @@ -91,6 +92,9 @@ default String getDefaultIdentifierPluralLower() {

CompletableFuture<Optional<U>> getUser(ObjectId id);

//TODO: move this method declaration to ApiRepository in repo MSRepository
// Optional<Member> getNext();

Query<M> asQuery(UUID userUUID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.mapping.DefaultCreator;
import rocks.milspecsg.msdatasync.model.core.Member;
Expand All @@ -19,6 +20,7 @@ public class ApiMongoContext extends MongoContext {
public ApiMongoContext(ConfigurationService configurationService) {
this.configurationService = configurationService;
configurationService.addConfigLoadedListener(this::loadConfig);
addConnectionOpenedListener(this::connectionOpened);
}

private void loadConfig(Object plugin) {
Expand All @@ -34,6 +36,13 @@ private void loadConfig(Object plugin) {
init(hostname, port, dbName, username, password, useAuth);
}

private void connectionOpened(Datastore datastore) {
// if enabled, database must be a replica set
if (configurationService.getConfigBoolean(ConfigKeys.SERIALIZE_WAIT_FOR_SNAPSHOT_ON_JOIN)) {
boolean isReplicaSet = datastore.getMongo().getReplicaSetStatus() != null;
}
}

@Override
protected void initMorphiaMaps(Morphia morphia) {
morphia.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

public abstract class ConfigKeys extends rocks.milspecsg.msrepository.api.config.ConfigKeys {

public static final int ENABLED_SERIALIZERS_LIST = 1;
public static final int SERIALIZE_ENABLED_SERIALIZERS_LIST = 1;
public static final int SERIALIZE_ON_JOIN_LEAVE = 10;
public static final int SERIALIZE_WAIT_FOR_SNAPSHOT_ON_JOIN = 11;
public static final int MONGODB_HOSTNAME = 20;
public static final int MONGODB_PORT = 21;
public static final int MONGODB_DBNAME = 22;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void loadConfig(Object plugin) {

verifyExternalSerializers();

List<String> enabledSerializers = new ArrayList<>(configurationService.getConfigList(ConfigKeys.ENABLED_SERIALIZERS_LIST, new TypeToken<List<String>>() {
List<String> enabledSerializers = new ArrayList<>(configurationService.getConfigList(ConfigKeys.SERIALIZE_ENABLED_SERIALIZERS_LIST, new TypeToken<List<String>>() {
}));
serializers = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import rocks.milspecsg.msdatasync.api.snapshot.SnapshotRepository;
import rocks.milspecsg.msdatasync.model.core.Member;
import rocks.milspecsg.msdatasync.model.core.Snapshot;
import rocks.milspecsg.msrepository.db.mongodb.MongoContext;
import rocks.milspecsg.msrepository.model.Dbo;
import rocks.milspecsg.msrepository.service.ApiRepository;

Expand All @@ -19,6 +20,11 @@ public abstract class ApiMemberRepository<M extends Member, S extends Snapshot,
@Inject
protected SnapshotRepository<S, K> snapshotRepository;

@Inject
public ApiMemberRepository(MongoContext mongoContext) {
super(mongoContext);
}

@Override
public CompletableFuture<Optional<M>> getOneOrGenerate(UUID userUUID) {
return CompletableFuture.supplyAsync(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import rocks.milspecsg.msdatasync.api.keys.DataKeyService;
import rocks.milspecsg.msdatasync.api.snapshot.SnapshotRepository;
import rocks.milspecsg.msdatasync.model.core.Snapshot;
import rocks.milspecsg.msrepository.db.mongodb.MongoContext;
import rocks.milspecsg.msrepository.service.ApiRepository;

import java.util.HashMap;
Expand All @@ -14,6 +15,11 @@ public abstract class ApiSnapshotRepository<S extends Snapshot, K> extends ApiRe
@Inject
DataKeyService<K> dataKeyService;

@Inject
public ApiSnapshotRepository(MongoContext mongoContext) {
super(mongoContext);
}

@Override
public boolean setSnapshotValue(S snapshot, K key, Optional<?> optionalValue) {
if (!optionalValue.isPresent()) {
Expand Down
2 changes: 1 addition & 1 deletion msrepository
8 changes: 5 additions & 3 deletions sponge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ dependencies {

implementation 'com.google.inject:guice:4.0'
compile 'com.google.guava:guava:28.1-jre'
implementation 'org.mongodb:mongodb-driver-sync:3.10.1'
implementation 'org.mongodb:mongodb-driver-sync:3.11.0'
implementation 'org.mongodb:mongo-java-driver:3.11.0'

implementation 'org.mongodb.morphia:morphia:1.3.2'

compileOnly('org.spongepowered:spongeapi:7.2.0-SNAPSHOT') {
Expand All @@ -44,9 +46,9 @@ shadowJar {

dependencies {
include dependency('org.mongodb:bson:3.10.1')
include dependency('org.mongodb:mongodb-driver-sync:3.10.1')
include dependency('org.mongodb:mongodb-driver-sync:3.11.0')
include dependency('org.mongodb.morphia:morphia:1.3.2')
include dependency('org.mongodb:mongo-java-driver:3.4.0')
include dependency('org.mongodb:mongo-java-driver:3.11.0')
include project(':msrepository:msr-api:api-config')
include project(':msrepository:msr-api:api-repository')
include project(':msrepository:msr-sponge:sponge-config')
Expand Down
2 changes: 1 addition & 1 deletion sponge/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
modGroup=rocks.milspecsg
modVersion=0.5.2
modVersion=0.6.0
modBaseName=MSDataSync
28 changes: 20 additions & 8 deletions sponge/src/main/java/rocks/milspecsg/msdatasync/MSDataSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import rocks.milspecsg.msdatasync.api.data.UserSerializer;
import rocks.milspecsg.msdatasync.api.tasks.SerializationTaskService;
import rocks.milspecsg.msdatasync.commands.SyncCommandManager;
import rocks.milspecsg.msdatasync.listeners.PlayerListener;
Expand Down Expand Up @@ -90,12 +91,17 @@ public void reload(GameReloadEvent event) {
@Listener
public void stop(GameStoppingEvent event) {
Sponge.getServer().getConsole().sendMessage(Text.of(MSDataSyncPluginInfo.pluginPrefix, TextColors.YELLOW, "Stopping..."));
logger.info("Saving all players on server");
UserSerializer<Snapshot, User> userSerializer = injector.getInstance(com.google.inject.Key.get(new TypeLiteral<UserSerializer<Snapshot, User>>() {
}));

Sponge.getServer().getOnlinePlayers().forEach(player -> userSerializer.serialize(player, "Server Stop"));

removeListeners();
logger.debug("Unregistered listeners");
logger.info("Unregistered listeners");

stopTasks();
logger.debug("Stopped tasks");
logger.info("Stopped tasks");

Sponge.getServer().getConsole().sendMessage(Text.of(MSDataSyncPluginInfo.pluginPrefix, TextColors.YELLOW, "Done"));
}
Expand Down Expand Up @@ -156,14 +162,20 @@ protected void configure() {

bind(SnapshotOptimizationService.class);

bind(new TypeLiteral<ApiSpongeMemberRepository>() {})
.to(new TypeLiteral<MSMemberRepository>() {});
bind(new TypeLiteral<ApiSpongeMemberRepository>() {
})
.to(new TypeLiteral<MSMemberRepository>() {
});

bind(new TypeLiteral<ApiSpongeSnapshotRepository>() {})
.to(new TypeLiteral<MSSnapshotRepository>() {});
bind(new TypeLiteral<ApiSpongeSnapshotRepository>() {
})
.to(new TypeLiteral<MSSnapshotRepository>() {
});

bind(new TypeLiteral<ApiSerializationTaskService<Member, Snapshot, User>>() {})
.to(new TypeLiteral<ApiSpongeSerializationTaskService<Member, Snapshot>>() {});
bind(new TypeLiteral<ApiSerializationTaskService<Member, Snapshot, User>>() {
})
.to(new TypeLiteral<ApiSpongeSerializationTaskService<Member, Snapshot>>() {
});

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public final class MSDataSyncPluginInfo implements SpongePluginInfo {
public static final String id = "msdatasync";
public static final String name = "MSDataSync";
public static final String version = "0.5.2";
public static final String version = "0.6.0";
public static final String description = "A plugin to synchronize player inventories with a database";
public static final String url = "https://milspecsg.rocks";
public static final String authors = "Cableguy20";
Expand Down
Loading

0 comments on commit 63a2e9e

Please sign in to comment.