package cz.jzitnik.game.handlers.place; import java.util.HashMap; import java.util.Set; import org.reflections.Reflections; public class PlaceHandler { private final HashMap placeHandlerList = new HashMap<>(); private final CustomPlaceHandler defaultPlaceHandler = new DefaultPlaceHandler(); public boolean contains(String itemId) { return placeHandlerList.containsKey(itemId); } public CustomPlaceHandler get(String itemId) { if (!contains(itemId)) { return defaultPlaceHandler; } return placeHandlerList.get(itemId); } public PlaceHandler() { registerHandlers(); } private void registerHandlers() { Reflections reflections = new Reflections("cz.jzitnik.game.handlers.place.handlers"); Set> handlerClasses = reflections.getTypesAnnotatedWith(cz.jzitnik.game.annotations.PlaceHandler.class); for (Class clazz : handlerClasses) { if (CustomPlaceHandler.class.isAssignableFrom(clazz)) { try { CustomPlaceHandler handlerInstance = (CustomPlaceHandler) clazz.getDeclaredConstructor().newInstance(); cz.jzitnik.game.annotations.PlaceHandler annotation = clazz.getAnnotation(cz.jzitnik.game.annotations.PlaceHandler.class); placeHandlerList.put(annotation.value(), handlerInstance); } catch (Exception e) { e.printStackTrace(); } } } } }