Skip to content

Commit

Permalink
Sync subclasses on an individual basis
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Oct 23, 2016
1 parent 696bd8a commit 404df5f
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/main/java/slimeknights/mantle/config/AbstractConfigFile.java
Expand Up @@ -31,6 +31,7 @@
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;

Expand Down Expand Up @@ -135,35 +136,53 @@ public byte[] getPacketData() {
}

public boolean sync(AbstractConfigFile other) {
if(other.getClass() != this.getClass()) {
return sync(other, this);
}

public boolean sync(Object other, Object that) {
if(other.getClass() != that.getClass()) {
return false;
}

List<Field> fieldsToProcess = new ArrayList<>();
getAllFields(fieldsToProcess, this.getClass());
getAllFields(fieldsToProcess, that.getClass());

for(Field field : fieldsToProcess) {
try {
// don't sync transient fields
if(Modifier.isTransient(field.getModifiers())) {
continue;
}
if(!field.isAccessible()) {
field.setAccessible(true);
}
Object original = field.get(this);
Object remote = field.get(other);
syncField(other, field);
}

return needsSaving();
}

private void syncField(Object other, Field field) {
try {
// don't sync transient fields
if(Modifier.isTransient(field.getModifiers())) {
return;
}
if(Modifier.isStatic(field.getModifiers())) {
return;
}
if(!field.isAccessible()) {
field.setAccessible(true);
}

Object original = field.get(this);
Object remote = field.get(other);

// is this a subclass that contains entries itself?
if(field.getClass().isAnnotationPresent(ConfigSerializable.class)) {
sync(remote, original);
}
else {
if(!original.equals(remote)) {
field.set(this, remote);
setNeedsSaving();
}
} catch(IllegalAccessException e) {
e.printStackTrace();
}
} catch(IllegalAccessException e) {
e.printStackTrace();
}

return needsSaving();
}

public void setNeedsSaving() {
Expand Down

0 comments on commit 404df5f

Please sign in to comment.