feat: Added some new annotations

This commit is contained in:
2025-03-08 12:13:00 +01:00
parent 13e2a74b37
commit e9f753da3d
6 changed files with 198 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PlaceOnSolid {
}

View File

@ -0,0 +1,11 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResetDataOnMine {
}

View File

@ -2,11 +2,15 @@ package cz.jzitnik.game.entities.items.registry.blocks;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.BlockRegistry;
import cz.jzitnik.game.annotations.PlaceOnSolid;
import cz.jzitnik.game.annotations.ResetDataOnMine;
import cz.jzitnik.game.annotations.Sapling;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.logic.services.saplings.SaplingData;
@Sapling
@PlaceOnSolid
@ResetDataOnMine
@BlockRegistry("oak_sapling")
public class OakSaplingBlock extends Block {
public OakSaplingBlock() {

View File

@ -0,0 +1,66 @@
package cz.jzitnik.game.handlers.place;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.PlaceOnSolid;
import cz.jzitnik.game.annotations.ResetDataOnMine;
import cz.jzitnik.game.entities.Block;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class CustomAnnotationHandler implements CustomPlaceHandler {
private final Class<?> clazz;
private final DefaultPlaceHandler defaultPlaceHandler = new DefaultPlaceHandler();
public CustomAnnotationHandler(Class<?> clazz) {
this.clazz = clazz;
}
@Override
public boolean place(Game game, int x, int y) {
if (clazz.isAnnotationPresent(PlaceOnSolid.class)) {
return placeOnSolid(game, x, y);
}
return defaultPlaceHandler.place(game, x, y);
}
@Override
public boolean mine(Game game, int x, int y) {
if (clazz.isAnnotationPresent(ResetDataOnMine.class)) {
resetDataOnMine(game, x, y);
}
return defaultPlaceHandler.mine(game, x, y);
}
private boolean placeOnSolid(Game game, int x, int y) {
var blocksBottom = game.getWorld()[y + 1][x];
if (blocksBottom.stream().allMatch(Block::isGhost)) {
return false;
}
return defaultPlaceHandler.place(game, x, y);
}
private void resetDataOnMine(Game game, int x, int y) {
var blocks = game.getWorld()[y][x].stream().filter(i -> !i.getBlockId().equals("air")).toList();
for (Block block : blocks) {
if (block.getData() == null) {
continue;
}
try {
Constructor<?> constructor = block.getData().getClass().getDeclaredConstructor();
Object object = constructor.newInstance();
block.setData(object);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException _) {
}
}
}
}

View File

@ -3,6 +3,9 @@ package cz.jzitnik.game.handlers.place;
import java.util.HashMap;
import java.util.Set;
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 {
@ -43,5 +46,21 @@ public class PlaceHandler {
}
}
}
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)) {
try {
var annotation = clazz.getAnnotation(BlockRegistry.class);
var id = annotation.value();
placeHandlerList.put(id, new CustomAnnotationHandler(clazz));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}