forked from jzitnik/twodcraft
68 lines
2.6 KiB
Java
68 lines
2.6 KiB
Java
package cz.jzitnik.game.handlers.place;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Set;
|
|
|
|
import cz.jzitnik.game.annotations.BlockDropPercentage;
|
|
import cz.jzitnik.game.annotations.BlockRegistry;
|
|
import cz.jzitnik.game.annotations.PlaceOnSolid;
|
|
import cz.jzitnik.game.annotations.ResetDataOnMine;
|
|
import org.reflections.Reflections;
|
|
|
|
public class PlaceHandler {
|
|
private final HashMap<String, CustomPlaceHandler> 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<Class<?>> 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
Reflections reflections2 = new Reflections("cz.jzitnik.game.entities.items.registry.blocks");
|
|
Set<Class<?>> blocks = reflections2
|
|
.getTypesAnnotatedWith(BlockRegistry.class);
|
|
|
|
for (Class<?> clazz : blocks) {
|
|
if (clazz.isAnnotationPresent(PlaceOnSolid.class) || clazz.isAnnotationPresent(ResetDataOnMine.class) || clazz.isAnnotationPresent(BlockDropPercentage.class)) {
|
|
try {
|
|
var annotation = clazz.getAnnotation(BlockRegistry.class);
|
|
var id = annotation.value();
|
|
placeHandlerList.put(id, new CustomAnnotationHandler(clazz));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|