<commit_before>package ninja.egg82.plugin.utils;

import java.util.ArrayList;
import java.util.Arrays;

import org.bukkit.event.Event;

import ninja.egg82.patterns.ServiceLocator;
import ninja.egg82.plugin.commands.EventCommand;
import ninja.egg82.plugin.commands.PluginCommand;
import ninja.egg82.plugin.commands.TickCommand;
import ninja.egg82.plugin.handlers.CommandHandler;
import ninja.egg82.plugin.handlers.EventListener;
import ninja.egg82.plugin.handlers.PermissionsManager;
import ninja.egg82.plugin.handlers.TickHandler;
import ninja.egg82.utils.ReflectUtil;

public class SpigotReflectUtil {
	//vars
	private static CommandHandler commandHandler = null;
	private static EventListener eventListener = null;
	private static PermissionsManager permissionsManager = null;
	private static TickHandler tickHandler = null;
	
	//constructor
	public SpigotReflectUtil() {
		
	}
	
	//public
	public static int addServicesFromPackage(String packageName) {
		ArrayList<Class<?>> services = ReflectUtil.getClasses(Object.class, packageName);
		for (Class<?> service : services) {
			ServiceLocator.provideService(service);
		}
		return services.size();
	}
	
	public static int addCommandsFromPackage(String packageName) {
		int numCommands = 0;
		
		if (commandHandler == null) {
			commandHandler = (CommandHandler) ServiceLocator.getService(CommandHandler.class);
		}
		
		ArrayList<Class<? extends PluginCommand>> enums = ReflectUtil.getClasses(PluginCommand.class, packageName);
		for (Class<? extends PluginCommand> c : enums) {
			String name = c.getSimpleName();
			String pkg = c.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			numCommands++;
			commandHandler.setCommand(name.substring(0, name.length() - 7).toLowerCase(), c);
		}
		
		return numCommands;
	}
	
	@SuppressWarnings("unchecked")
	public static int addEventsFromPackage(String packageName) {
		int numEvents = 0;
		
		if (eventListener == null) {
			eventListener = (EventListener) ServiceLocator.getService(EventListener.class);
		}
		
		ArrayList<Class<? extends EventCommand>> enums = ReflectUtil.getClasses(EventCommand.class, packageName);
		Class<? extends Event> c2 = null;
		for (Class<? extends EventCommand> c : enums) {
			String name = c.getSimpleName();
			String pkg = c.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			String eventName = name.substring(0, name.length() - 7);
			
			c2 = null;
			c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.block." + eventName);
			if (c2 == null) {
				c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.enchantment." + eventName);
				if (c2 == null) {
					c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.entity." + eventName);
					if (c2 == null) {
						c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.hanging." + eventName);
						if (c2 == null) {
							c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.inventory." + eventName);
							if (c2 == null) {
								c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.player." + eventName);
								if (c2 == null) {
									c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.server." + eventName);
									if (c2 == null) {
										c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.vehicle." + eventName);
										if (c2 == null) {
											c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.weather." + eventName);
											if (c2 == null) {
												c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.world." + eventName);
												if (c2 == null) {
													continue;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
			numEvents++;
			eventListener.setEvent(c2, c);
		}
		
		return numEvents;
	}
	
	public static int addPermissionsFromClass(Class<?> clazz) {
		int numPermissions = 0;
		
		if (permissionsManager == null) {
			permissionsManager = (PermissionsManager) ServiceLocator.getService(PermissionsManager.class);
		}
		
		Object[] enums = ReflectUtil.getStaticFields(clazz);
		String[] permissions = Arrays.copyOf(enums, enums.length, String[].class);
		for (String p : permissions) {
			numPermissions++;
			permissionsManager.addPermission(p);
		}
		
		return numPermissions;
	}
	
	public static int addTicksFromPackage(String packageName) {
		int numTicks = 0;
		
		if (tickHandler == null) {
			tickHandler = (TickHandler) ServiceLocator.getService(TickHandler.class);
		}
		
		ArrayList<Class<? extends TickCommand>> enums = ReflectUtil.getClasses(TickCommand.class, packageName);
		for (Class<? extends TickCommand> t : enums) {
			String pkg = t.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			numTicks++;
			tickHandler.addTickCommand(t);
		}
		
		return numTicks;
	}
	
	//private
	
}<commit_msg>Added clearAll() to SpigotReflectUtil<commit_after>package ninja.egg82.plugin.utils;

import java.util.ArrayList;
import java.util.Arrays;

import org.bukkit.event.Event;

import ninja.egg82.patterns.ServiceLocator;
import ninja.egg82.plugin.commands.EventCommand;
import ninja.egg82.plugin.commands.PluginCommand;
import ninja.egg82.plugin.commands.TickCommand;
import ninja.egg82.plugin.handlers.CommandHandler;
import ninja.egg82.plugin.handlers.EventListener;
import ninja.egg82.plugin.handlers.PermissionsManager;
import ninja.egg82.plugin.handlers.TickHandler;
import ninja.egg82.utils.ReflectUtil;

public class SpigotReflectUtil {
	//vars
	private static CommandHandler commandHandler = null;
	private static EventListener eventListener = null;
	private static PermissionsManager permissionsManager = null;
	private static TickHandler tickHandler = null;
	
	//constructor
	public SpigotReflectUtil() {
		
	}
	
	//public
	public static int addServicesFromPackage(String packageName) {
		ArrayList<Class<?>> services = ReflectUtil.getClasses(Object.class, packageName);
		for (Class<?> service : services) {
			ServiceLocator.provideService(service);
		}
		return services.size();
	}
	
	public static int addCommandsFromPackage(String packageName) {
		int numCommands = 0;
		
		if (commandHandler == null) {
			commandHandler = (CommandHandler) ServiceLocator.getService(CommandHandler.class);
		}
		
		ArrayList<Class<? extends PluginCommand>> enums = ReflectUtil.getClasses(PluginCommand.class, packageName);
		for (Class<? extends PluginCommand> c : enums) {
			String name = c.getSimpleName();
			String pkg = c.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			numCommands++;
			commandHandler.setCommand(name.substring(0, name.length() - 7).toLowerCase(), c);
		}
		
		return numCommands;
	}
	
	@SuppressWarnings("unchecked")
	public static int addEventsFromPackage(String packageName) {
		int numEvents = 0;
		
		if (eventListener == null) {
			eventListener = (EventListener) ServiceLocator.getService(EventListener.class);
		}
		
		ArrayList<Class<? extends EventCommand>> enums = ReflectUtil.getClasses(EventCommand.class, packageName);
		Class<? extends Event> c2 = null;
		for (Class<? extends EventCommand> c : enums) {
			String name = c.getSimpleName();
			String pkg = c.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			String eventName = name.substring(0, name.length() - 7);
			
			c2 = null;
			c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.block." + eventName);
			if (c2 == null) {
				c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.enchantment." + eventName);
				if (c2 == null) {
					c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.entity." + eventName);
					if (c2 == null) {
						c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.hanging." + eventName);
						if (c2 == null) {
							c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.inventory." + eventName);
							if (c2 == null) {
								c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.player." + eventName);
								if (c2 == null) {
									c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.server." + eventName);
									if (c2 == null) {
										c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.vehicle." + eventName);
										if (c2 == null) {
											c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.weather." + eventName);
											if (c2 == null) {
												c2 = (Class<? extends Event>) ReflectUtil.getClassFromName("org.bukkit.event.world." + eventName);
												if (c2 == null) {
													continue;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
			numEvents++;
			eventListener.setEvent(c2, c);
		}
		
		return numEvents;
	}
	
	public static int addPermissionsFromClass(Class<?> clazz) {
		int numPermissions = 0;
		
		if (permissionsManager == null) {
			permissionsManager = (PermissionsManager) ServiceLocator.getService(PermissionsManager.class);
		}
		
		Object[] enums = ReflectUtil.getStaticFields(clazz);
		String[] permissions = Arrays.copyOf(enums, enums.length, String[].class);
		for (String p : permissions) {
			numPermissions++;
			permissionsManager.addPermission(p);
		}
		
		return numPermissions;
	}
	
	public static int addTicksFromPackage(String packageName) {
		int numTicks = 0;
		
		if (tickHandler == null) {
			tickHandler = (TickHandler) ServiceLocator.getService(TickHandler.class);
		}
		
		ArrayList<Class<? extends TickCommand>> enums = ReflectUtil.getClasses(TickCommand.class, packageName);
		for (Class<? extends TickCommand> t : enums) {
			String pkg = t.getName();
			pkg = pkg.substring(0, pkg.lastIndexOf('.'));
			
			if (!pkg.equalsIgnoreCase(packageName)) {
				continue;
			}
			
			numTicks++;
			tickHandler.addTickCommand(t);
		}
		
		return numTicks;
	}
	
	public static void clearAll() {
		commandHandler.clear();
		eventListener.clear();
		permissionsManager.clear();
		tickHandler.clear();
	}
	
	//private
	
}
{'fraction_non_alphanumeric': 0.12859848484848485, 'fraction_numerical': 0.010227272727272727, 'mean_word_length': 3.6198600174978126, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3102594', 'n_tokens_mistral': 4084, 'n_tokens_neox': 3274, 'n_words': 675}