-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
164 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ plugins { | |
} | ||
|
||
group 'pl.mikigal' | ||
version '1.1.4' | ||
version '1.1.5' | ||
|
||
publishing { | ||
repositories { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/pl/mikigal/config/serializer/universal/UniversalArraySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package pl.mikigal.config.serializer.universal; | ||
|
||
import org.bukkit.configuration.ConfigurationSection; | ||
import pl.mikigal.config.BukkitConfiguration; | ||
import pl.mikigal.config.exception.InvalidConfigFileException; | ||
import pl.mikigal.config.exception.MissingSerializerException; | ||
import pl.mikigal.config.serializer.Serializer; | ||
import pl.mikigal.config.serializer.Serializers; | ||
import pl.mikigal.config.util.ConversionUtils; | ||
import pl.mikigal.config.util.TypeUtils; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class UniversalArraySerializer extends Serializer<Object[]> { | ||
|
||
@Override | ||
protected void saveObject(String path, Object[] object, BukkitConfiguration configuration) { | ||
if (object.length == 0) { | ||
throw new IllegalStateException("Can't set empty array to config"); | ||
} | ||
|
||
Class<?> generic = TypeUtils.getArrayGeneric(object); | ||
Serializer<?> serializer = Serializers.of(generic); | ||
if (serializer == null && !TypeUtils.isSimpleType(generic)) { | ||
throw new MissingSerializerException(generic); | ||
} | ||
|
||
configuration.set(path + ".type", generic.getName()); | ||
|
||
int index = 0; | ||
for (Object element : object) { | ||
if (serializer == null) { | ||
configuration.set(path + "." + index, element); | ||
} | ||
else { | ||
serializer.serialize(path + "." + index, element, configuration); | ||
} | ||
|
||
index++; | ||
} | ||
} | ||
|
||
@Override | ||
public Object[] deserialize(String path, BukkitConfiguration configuration) { | ||
ConfigurationSection section = configuration.getConfigurationSection(path); | ||
Set<String> keys = section.getKeys(false); | ||
keys.stream() | ||
.filter(key -> !key.equals("type")) | ||
.forEach(key -> { | ||
if (ConversionUtils.asInt(key) == Integer.MIN_VALUE) { | ||
throw new InvalidConfigFileException("Invalid index: " + key + " in " + path + " (should be integer)"); | ||
} | ||
}); | ||
|
||
String type = section.getString("type"); | ||
Objects.requireNonNull(type, "Serializer type is not defined for " + path); | ||
|
||
try { | ||
Serializer<?> serializer = Serializers.of(type); | ||
Class<?> typeClass = Class.forName(type); | ||
|
||
if (serializer == null && !TypeUtils.isSimpleType(typeClass)) { | ||
throw new MissingSerializerException(typeClass); | ||
} | ||
|
||
int length = Collections.max(keys | ||
.stream() | ||
.filter(key -> !key.equals("type")) | ||
.map(Integer::parseInt) | ||
.collect(Collectors.toList())) + 1; | ||
|
||
Object[] array = (Object[]) Array.newInstance(typeClass, length); | ||
for (String key : keys) { | ||
if (key.equals("type")) { | ||
continue; | ||
} | ||
|
||
int index = Integer.parseInt(key); | ||
if (serializer == null) { | ||
array[index] = configuration.get(path + "." + index); | ||
continue; | ||
} | ||
|
||
array[index] = serializer.deserialize(path + "." + index, configuration); | ||
} | ||
|
||
return array; | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters