mirror of
				https://github.com/IntellectualSites/PlotSquared.git
				synced 2025-11-04 11:13:45 +01:00 
			
		
		
		
	Use generics instead of the raw class
This commit is contained in:
		@@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.config;
 | 
				
			|||||||
import com.github.intellectualsites.plotsquared.configuration.MemorySection;
 | 
					import com.github.intellectualsites.plotsquared.configuration.MemorySection;
 | 
				
			||||||
import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration;
 | 
					import com.github.intellectualsites.plotsquared.configuration.file.YamlConfiguration;
 | 
				
			||||||
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
 | 
					import com.github.intellectualsites.plotsquared.plot.PlotSquared;
 | 
				
			||||||
 | 
					import com.github.intellectualsites.plotsquared.plot.config.Settings.Enabled_Components;
 | 
				
			||||||
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
 | 
					import com.github.intellectualsites.plotsquared.plot.util.StringMan;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
@@ -30,7 +31,7 @@ public class Config {
 | 
				
			|||||||
     * @param <T>
 | 
					     * @param <T>
 | 
				
			||||||
     * @return
 | 
					     * @return
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static <T> T get(String key, Class root) {
 | 
					    public static <T> T get(String key, Class<?> root) {
 | 
				
			||||||
        String[] split = key.split("\\.");
 | 
					        String[] split = key.split("\\.");
 | 
				
			||||||
        Object instance = getInstance(split, root);
 | 
					        Object instance = getInstance(split, root);
 | 
				
			||||||
        if (instance != null) {
 | 
					        if (instance != null) {
 | 
				
			||||||
@@ -55,7 +56,7 @@ public class Config {
 | 
				
			|||||||
     * @param value value
 | 
					     * @param value value
 | 
				
			||||||
     * @param root
 | 
					     * @param root
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static void set(String key, Object value, Class root) {
 | 
					    public static void set(String key, Object value, Class<? extends Config> root) {
 | 
				
			||||||
        String[] split = key.split("\\.");
 | 
					        String[] split = key.split("\\.");
 | 
				
			||||||
        Object instance = getInstance(split, root);
 | 
					        Object instance = getInstance(split, root);
 | 
				
			||||||
        if (instance != null) {
 | 
					        if (instance != null) {
 | 
				
			||||||
@@ -81,7 +82,7 @@ public class Config {
 | 
				
			|||||||
        PlotSquared.debug("Failed to set config option: " + key + ": " + value + " | " + instance);
 | 
					        PlotSquared.debug("Failed to set config option: " + key + ": " + value + " | " + instance);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static boolean load(File file, Class root) {
 | 
					    public static boolean load(File file, Class<? extends Config> root) {
 | 
				
			||||||
        if (!file.exists()) {
 | 
					        if (!file.exists()) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -102,7 +103,7 @@ public class Config {
 | 
				
			|||||||
     * @param file
 | 
					     * @param file
 | 
				
			||||||
     * @param root
 | 
					     * @param root
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static void save(File file, Class root) {
 | 
					    public static void save(File file, Class<? extends Config> root) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            if (!file.exists()) {
 | 
					            if (!file.exists()) {
 | 
				
			||||||
                file.getParentFile().mkdirs();
 | 
					                file.getParentFile().mkdirs();
 | 
				
			||||||
@@ -123,7 +124,7 @@ public class Config {
 | 
				
			|||||||
     * @param clazz
 | 
					     * @param clazz
 | 
				
			||||||
     * @return
 | 
					     * @return
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static Map<String, Object> getFields(Class clazz) {
 | 
					    public static Map<String, Object> getFields(Class<Enabled_Components> clazz) {
 | 
				
			||||||
        HashMap<String, Object> map = new HashMap<>();
 | 
					        HashMap<String, Object> map = new HashMap<>();
 | 
				
			||||||
        for (Field field : clazz.getFields()) {
 | 
					        for (Field field : clazz.getFields()) {
 | 
				
			||||||
            if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
 | 
					            if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
 | 
				
			||||||
@@ -242,7 +243,7 @@ public class Config {
 | 
				
			|||||||
     * @param root
 | 
					     * @param root
 | 
				
			||||||
     * @return
 | 
					     * @return
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private static Field getField(String[] split, Class root) {
 | 
					    private static Field getField(String[] split, Class<?> root) {
 | 
				
			||||||
        Object instance = getInstance(split, root);
 | 
					        Object instance = getInstance(split, root);
 | 
				
			||||||
        if (instance == null) {
 | 
					        if (instance == null) {
 | 
				
			||||||
            return null;
 | 
					            return null;
 | 
				
			||||||
@@ -278,7 +279,7 @@ public class Config {
 | 
				
			|||||||
     * @param root
 | 
					     * @param root
 | 
				
			||||||
     * @return The instance or null
 | 
					     * @return The instance or null
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private static Object getInstance(String[] split, Class root) {
 | 
					    private static Object getInstance(String[] split, Class<?> root) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            Class<?> clazz = root == null ? MethodHandles.lookup().lookupClass() : root;
 | 
					            Class<?> clazz = root == null ? MethodHandles.lookup().lookupClass() : root;
 | 
				
			||||||
            Object instance = clazz.newInstance();
 | 
					            Object instance = clazz.newInstance();
 | 
				
			||||||
@@ -286,9 +287,9 @@ public class Config {
 | 
				
			|||||||
                if (split.length == 1) {
 | 
					                if (split.length == 1) {
 | 
				
			||||||
                    return instance;
 | 
					                    return instance;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                Class found = null;
 | 
					                Class<?> found = null;
 | 
				
			||||||
                Class<?>[] classes = clazz.getDeclaredClasses();
 | 
					                Class<?>[] classes = clazz.getDeclaredClasses();
 | 
				
			||||||
                for (Class current : classes) {
 | 
					                for (Class<?> current : classes) {
 | 
				
			||||||
                    if (current.getSimpleName().equalsIgnoreCase(toFieldName(split[0]))) {
 | 
					                    if (current.getSimpleName().equalsIgnoreCase(toFieldName(split[0]))) {
 | 
				
			||||||
                        found = current;
 | 
					                        found = current;
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
@@ -361,8 +362,6 @@ public class Config {
 | 
				
			|||||||
     * Set some field to be accessible.
 | 
					     * Set some field to be accessible.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param field
 | 
					     * @param field
 | 
				
			||||||
     * @throws NoSuchFieldException
 | 
					 | 
				
			||||||
     * @throws IllegalAccessException
 | 
					 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private static void setAccessible(Field field) {
 | 
					    private static void setAccessible(Field field) {
 | 
				
			||||||
        field.setAccessible(true);
 | 
					        field.setAccessible(true);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user