Last active
February 20, 2023 18:33
-
-
Save Yuhtin/fddb63577d9ba605e6dce136a3510b24 to your computer and use it in GitHub Desktop.
Load every class from config using fieldnames
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
/** | |
* @author <a href="https://github.com/Yuhtin">Yuhtin</a> | |
*/ | |
import org.bukkit.configuration.ConfigurationSection; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
public class ConfigLoader { | |
private final File file; | |
private final String sectionName; | |
public ConfigLoader(File file, String sectionName) { | |
this.file = file; | |
this.sectionName = sectionName; | |
} | |
public <T> List<T> load(Class<T> clazz) { | |
List<T> list = new ArrayList<>(); | |
FileConfiguration config = YamlConfiguration.loadConfiguration(file); | |
if (config.isConfigurationSection(sectionName)) { | |
ConfigurationSection section = config.getConfigurationSection(sectionName); | |
for (String key : section.getKeys(false)) { | |
Map<String, Object> map = section.getConfigurationSection(key).getValues(false); | |
try { | |
T instance = clazz.newInstance(); | |
for (Map.Entry<String, Object> entry : map.entrySet()) { | |
String fieldName = entry.getKey(); | |
Object fieldValue = entry.getValue(); | |
setField(instance, clazz, fieldName, fieldValue); | |
} | |
list.add(instance); | |
} catch (InstantiationException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return list; | |
} | |
private <T> void setField(T instance, Class<T> clazz, String fieldName, Object fieldValue) { | |
try { | |
clazz.getDeclaredField(fieldName).set(instance, fieldValue); | |
} catch (NoSuchFieldException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
import org.junit.Test; | |
import java.io.File; | |
import java.util.List; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
public class ConfigLoaderTest { | |
@Test | |
public void testLoadRodFromConfig() { | |
File configFile = new File("rods.yml"); | |
ConfigLoader configLoader = new ConfigLoader(configFile, "rod"); | |
List<Rod> rodList = configLoader.load(Rod.class); | |
assertNotNull(rodList); | |
assertEquals(5, rodList.size()); | |
assertEquals(1, rodList.get(0).getId()); | |
assertEquals("de Bambú", rodList.get(0).getName()); | |
assertEquals(10.0, rodList.get(0).getMaxWeight(), 0.001); | |
assertEquals(0, rodList.get(0).getPrice(), 0.001); | |
assertEquals(5, rodList.get(4).getId()); | |
assertEquals("Telescópica", rodList.get(4).getName()); | |
assertEquals(250.0, rodList.get(4).getMaxWeight(), 0.001); | |
assertEquals(72500000, rodList.get(4).getPrice(), 0.001); | |
} | |
} |
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
import lombok.Builder; | |
import lombok.Data; | |
/** | |
* @author <a href="https://github.com/Yuhtin">Yuhtin</a> | |
*/ | |
@Builder | |
@Data | |
public class Rod { | |
private final int id; | |
private final String name; | |
private final double maxWeight; | |
private final double price; | |
} |
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
rod: | |
1: | |
name: "de Bambú" | |
maxWeight: 10.0 | |
price: 0 | |
2: | |
name: "de Grafite" | |
maxWeight: 25.0 | |
price: 500 | |
3: | |
name: "de Fibra de Vidro" | |
maxWeight: 50.0 | |
price: 75000 | |
4: | |
name: "de Fibra de Carbono" | |
maxWeight: 100.0 | |
price: 2500000 | |
5: | |
name: "Telescópica" | |
maxWeight: 250.0 | |
price: 72500000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment