chore: Water placement
This commit is contained in:
@ -13,6 +13,7 @@ import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLOutput;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
@ -64,7 +65,7 @@ public class Main {
|
||||
|
||||
terminal.trackMouse(Terminal.MouseTracking.Off);
|
||||
terminal.close();
|
||||
} catch (IOException | InterruptedException _) {
|
||||
} catch (IOException | InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
|
12
src/main/java/cz/jzitnik/game/annotations/PickupHandler.java
Normal file
12
src/main/java/cz/jzitnik/game/annotations/PickupHandler.java
Normal file
@ -0,0 +1,12 @@
|
||||
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;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface PickupHandler {
|
||||
String value();
|
||||
}
|
@ -3,10 +3,15 @@ package cz.jzitnik.game.crafting.recipes;
|
||||
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
|
||||
|
||||
@CraftingRecipeRegistry(
|
||||
recipe = {
|
||||
/*recipe = {
|
||||
"iron_ingot", "_", "iron_ingot",
|
||||
"_", "iron_ingot", "_",
|
||||
"_", "_", "_"
|
||||
},*/
|
||||
recipe = {
|
||||
"dirt", "_", "_",
|
||||
"_", "_", "_",
|
||||
"_", "_", "_"
|
||||
},
|
||||
result = "bucket",
|
||||
amount = 1
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cz.jzitnik.game.entities;
|
||||
|
||||
import cz.jzitnik.game.handlers.pickup.PickupHandlerProvider;
|
||||
import cz.jzitnik.game.handlers.place.PlaceHandler;
|
||||
import cz.jzitnik.game.mobs.EntityHurtAnimation;
|
||||
import cz.jzitnik.game.mobs.EntityKill;
|
||||
@ -8,4 +9,5 @@ public class Dependencies {
|
||||
public PlaceHandler placeHandler = new PlaceHandler();
|
||||
public EntityHurtAnimation entityHurtAnimation = new EntityHurtAnimation();
|
||||
public EntityKill entityKill = new EntityKill();
|
||||
public PickupHandlerProvider pickupHandlerProvider = new PickupHandlerProvider();
|
||||
}
|
||||
|
@ -82,8 +82,6 @@ public class ItemBlockSupplier {
|
||||
Item dropItem = getItem(dropKey);
|
||||
if (dropKey.equals(block.getBlockId())) {
|
||||
dropItem.setBlock(Optional.of(block));
|
||||
} else {
|
||||
dropItem.setBlock(Optional.of(getBlock(dropKey)));
|
||||
}
|
||||
block.setDrops(List.of(dropItem));
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.logic.services.water.WaterData;
|
||||
import cz.jzitnik.game.sprites.Water;
|
||||
|
||||
@BlockRegistry("water")
|
||||
@BlockRegistry(value = "water", drops = "water_item")
|
||||
public class WaterBlock extends Block {
|
||||
public WaterBlock() {
|
||||
super("water", SpriteLoader.SPRITES.WATER);
|
||||
|
@ -0,0 +1,7 @@
|
||||
package cz.jzitnik.game.handlers.pickup;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
|
||||
public interface CustomPickupHandler {
|
||||
boolean handle(Game game, int x, int y);
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package cz.jzitnik.game.handlers.pickup;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
import cz.jzitnik.game.annotations.PickupHandler;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
public class PickupHandlerProvider {
|
||||
public final HashMap<String, CustomPickupHandler> pickupList = new HashMap<>();
|
||||
|
||||
public CustomPickupHandler get(String itemId) {
|
||||
return pickupList.get(itemId);
|
||||
}
|
||||
|
||||
public PickupHandlerProvider() {
|
||||
registerHandlers();
|
||||
}
|
||||
|
||||
private void registerHandlers() {
|
||||
Reflections reflections = new Reflections("cz.jzitnik.game.handlers.pickup.handlers");
|
||||
Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(PickupHandler.class);
|
||||
|
||||
for (Class<?> clazz : handlerClasses) {
|
||||
if (CustomPickupHandler.class.isAssignableFrom(clazz)) {
|
||||
try {
|
||||
CustomPickupHandler handlerInstance = (CustomPickupHandler) clazz.getDeclaredConstructor().newInstance();
|
||||
PickupHandler annotation = clazz.getAnnotation(PickupHandler.class);
|
||||
String key = annotation.value();
|
||||
if (!key.contains(",")) {
|
||||
pickupList.put(key, handlerInstance);
|
||||
} else {
|
||||
String[] keys = key.split(",");
|
||||
for (String keyx : keys) {
|
||||
pickupList.put(keyx, handlerInstance);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cz.jzitnik.game.handlers.pickup.handlers;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.annotations.PickupHandler;
|
||||
import cz.jzitnik.game.handlers.pickup.CustomPickupHandler;
|
||||
|
||||
@PickupHandler("bucket,water_bucket")
|
||||
public class WaterPickupHandler implements CustomPickupHandler {
|
||||
|
||||
@Override
|
||||
public boolean handle(Game game, int x, int y) {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cz.jzitnik.game.handlers.place.handlers;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.annotations.PlaceHandler;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.entities.items.Item;
|
||||
import cz.jzitnik.game.handlers.place.CustomPlaceHandler;
|
||||
|
||||
@PlaceHandler("bucket")
|
||||
public class BucketPlaceHandler implements CustomPlaceHandler {
|
||||
@Override
|
||||
public boolean place(Game game, int x, int y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mine(Game game, int x, int y) {
|
||||
var world = game.getWorld();
|
||||
var blocks = world[y][x];
|
||||
var flowingBlocks = blocks.stream().filter(Block::isFlowing).toList();
|
||||
|
||||
if (flowingBlocks.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var flowingBlock = flowingBlocks.getFirst();
|
||||
var items = flowingBlock.getDrops();
|
||||
for (Item item: items) {
|
||||
game.getInventory().addItem(item);
|
||||
}
|
||||
blocks.remove(flowingBlock);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package cz.jzitnik.tui;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import cz.jzitnik.game.ui.Window;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.jline.terminal.MouseEvent;
|
||||
|
Reference in New Issue
Block a user