Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kinggoesgaming/7ec0ab5793d68ac877a9 to your computer and use it in GitHub Desktop.
Save kinggoesgaming/7ec0ab5793d68ac877a9 to your computer and use it in GitHub Desktop.
/*
* This file is part of FoundationsCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2016 - 2016 Hunar Roop (KingGoesGaming) Kahlon <http://www.kinggoesgaming.com>
* Copyright (c) Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.kinggoesgaming.foundations.core.services;
import com.kinggoesgaming.foundations.api.configuration.Configuration;
import com.kinggoesgaming.foundations.api.services.ConfigurationService;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* The {@link com.kinggoesgaming.foundations.core.FoundationsCore} implementation of {@link ConfigurationService}.
*/
public class FoundationsConfigurationService implements ConfigurationService {
private Map<String, Map<String, Configuration>> configMap = new HashMap<>();
@Override
public Optional<Configuration> get(String plugin, String key) {
if (configMap.containsKey(plugin)) {
Map<String, Configuration> localMap = configMap.get(plugin);
if (localMap.containsKey(key)) {
return Optional.of(localMap.get(key));
}
}
return Optional.empty();
}
@Override
public void register(String plugin, String key, Class<? extends Configuration> configClazz) {
Configuration config = null;
try {
for (Constructor<?> constructor : configClazz.getConstructors()) {
if (constructor.getParameterCount() == 0) {
constructor.setAccessible(true);
config = (Configuration) constructor.newInstance();
}
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
if (config != null) {
Map<String, Configuration> localMap;
if (configMap.containsKey(plugin)) {
localMap = configMap.get(plugin);
} else {
localMap = new HashMap<>();
}
localMap.put(key, config);
configMap.put(plugin, localMap);
}
}
@Override
public void register(String plugin, String key, Class<? extends Configuration> configClazz, Object[] parameters) {
Configuration config = null;
try {
for (Constructor<?> constructor : configClazz.getConstructors()) {
if (constructor.getParameterCount() == parameters.length) {
constructor.setAccessible(true);
config = (Configuration) constructor.newInstance(parameters);
}
}
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
if (config != null) {
Map<String, Configuration> localMap;
if (configMap.containsKey(plugin)) {
localMap = configMap.get(plugin);
} else {
localMap = new HashMap<>();
}
localMap.put(key, config);
configMap.put(plugin, localMap);
}
}
@Override
public void register(String plugin, String key, Configuration config) {
Map<String, Configuration> localMap;
if (configMap.containsKey(plugin)) {
localMap = configMap.get(plugin);
} else {
localMap = new HashMap<>();
}
localMap.put(key, config);
configMap.put(plugin, localMap);
}
@Override
public void saveAll() {
configMap.forEach((plugin, map) -> map.forEach((key, config) -> {
try {
config.save();
} catch (IOException e) {
e.printStackTrace();
}
}));
}
@Override
public void saveAll(String plugin) {
if (configMap.containsKey(plugin)) {
configMap.get(plugin).forEach((key, config) -> {
try {
config.save();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
@Override
public void unregister(String plugin, String key) {
if (configMap.containsKey(plugin)) {
Map<String, Configuration> localMap = configMap.get(plugin);
if (localMap.containsKey(key)) {
try {
localMap.get(key).save();
} catch (IOException e) {
e.printStackTrace();
}
localMap.remove(key);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment