feat: Implemented shears

This commit is contained in:
Jakub Žitník 2025-03-09 14:25:27 +01:00
parent 0f6d02a92e
commit a736795bd7
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
16 changed files with 199 additions and 10 deletions

View File

@ -40,7 +40,7 @@ public class SpriteLoader {
ITEM_STICK, ITEM_LEATHER,
// Block Items
ITEM_DIRT, ITEM_OAK_LOG, ITEM_OAK_LEAF, ITEM_OAK_PLANKS, ITEM_COBBLESTONE, ITEM_STONE, ITEM_OAK_DOOR, ITEM_WOOL, ITEM_OBSIDIAN,
ITEM_DIRT, ITEM_GRASS, ITEM_OAK_LOG, ITEM_OAK_LEAF, ITEM_OAK_PLANKS, ITEM_COBBLESTONE, ITEM_STONE, ITEM_OAK_DOOR, ITEM_WOOL, ITEM_OBSIDIAN,
// Ore Items
ITEM_COAL_ORE, ITEM_IRON_ORE, ITEM_GOLD_ORE, ITEM_DIAMOND_ORE,
@ -63,6 +63,8 @@ public class SpriteLoader {
DIAMOND_SWORD, DIAMOND_PICKAXE, DIAMOND_AXE, DIAMOND_SHOVEL, DIAMOND_HOE,
SHEARS,
BUCKET, WATER_BUCKET, LAVA_BUCKET, MILK_BUCKET,
// Food
ITEM_PORKCHOP, ITEM_COOKED_PORKCHOP, ITEM_MUTTON, ITEM_COOKED_MUTTON, ITEM_BEEF, ITEM_STEAK, ITEM_APPLE,
@ -134,6 +136,7 @@ public class SpriteLoader {
// Block Items
SPRITES_MAP.put(SPRITES.ITEM_DIRT, new SimpleSprite("items/dirt.ans"));
SPRITES_MAP.put(SPRITES.ITEM_GRASS, new SimpleSprite("items/grass.ans"));
SPRITES_MAP.put(SPRITES.ITEM_OAK_LOG, new SimpleSprite("items/oak_log.ans"));
SPRITES_MAP.put(SPRITES.ITEM_OAK_LEAF, new SimpleSprite("items/oak_leaves.ans"));
SPRITES_MAP.put(SPRITES.ITEM_OAK_PLANKS, new SimpleSprite("items/oak_planks.ans"));
@ -196,6 +199,8 @@ public class SpriteLoader {
SPRITES_MAP.put(SPRITES.DIAMOND_SHOVEL, new SimpleSprite("items/diamond_shovel.ans"));
SPRITES_MAP.put(SPRITES.DIAMOND_HOE, new SimpleSprite("items/diamond_hoe.ans"));
SPRITES_MAP.put(SPRITES.SHEARS, new SimpleSprite("items/shears.ans"));
SPRITES_MAP.put(SPRITES.BUCKET, new SimpleSprite("items/bucket.ans"));
SPRITES_MAP.put(SPRITES.WATER_BUCKET, new SimpleSprite("items/water_bucket.ans"));
SPRITES_MAP.put(SPRITES.LAVA_BUCKET, new SimpleSprite("items/lava_bucket.ans"));

View File

@ -0,0 +1,12 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(CustomDrops.class)
public @interface CustomDrop {
String tool();
String drops();
int percentage() default 100;
}

View File

@ -0,0 +1,12 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomDrops {
CustomDrop[] value();
}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"grass_bush", "_", "_",
"dirt", "_", "_",
"_", "_", "_"
},
result = "grass",
amount = 1
)
public class GrassBlockRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"_", "iron_ingot", "_",
"iron_ingot", "_", "_",
"_", "_", "_"
},
result = "shears",
amount = 1
)
public class ShearsRecipe{}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"iron_ingot", "_", "_",
"_", "iron_ingot", "_",
"_", "_", "_"
},
result = "shears",
amount = 1
)
public class ShearsRecipe2{}

View File

@ -3,10 +3,12 @@ package cz.jzitnik.game.entities.items.registry.blocks;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.BlockDropPercentage;
import cz.jzitnik.game.annotations.BlockRegistry;
import cz.jzitnik.game.annotations.CustomDrop;
import cz.jzitnik.game.annotations.PlaceOnSolid;
import cz.jzitnik.game.entities.Block;
@PlaceOnSolid
@CustomDrop(tool = "shears", drops = "grass_bush")
@BlockDropPercentage(13)
@BlockRegistry(value = "grass_bush", drops = "wheat_seeds")
public class GrassBushBlock extends Block {

View File

@ -0,0 +1,13 @@
package cz.jzitnik.game.entities.items.registry.items;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.ItemRegistry;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.entities.items.ItemType;
@ItemRegistry("grass_bush")
public class GrassBushItem extends Item {
public GrassBushItem() {
super("grass_bush", "Grass bush", ItemType.BLOCK, SpriteLoader.SPRITES.GRASS_BUSH);
}
}

View File

@ -0,0 +1,13 @@
package cz.jzitnik.game.entities.items.registry.items;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.ItemRegistry;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.entities.items.ItemType;
@ItemRegistry("grass")
public class GrassItem extends Item {
public GrassItem() {
super("grass", "Grass", ItemType.BLOCK, SpriteLoader.SPRITES.ITEM_GRASS);
}
}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.entities.items.registry.items;
import cz.jzitnik.game.SpriteLoader;
import cz.jzitnik.game.annotations.ItemRegistry;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.entities.items.ItemType;
@ItemRegistry("shears")
public class ShearsItem extends Item {
public ShearsItem() {
super("shears", "Shears", ItemType.SHEARS, SpriteLoader.SPRITES.SHEARS);
setDurability(238);
}
}

View File

@ -34,6 +34,8 @@ public class Generation {
// Spawn player at a valid starting point
world[terrainHeight[256] - 1][256].add(steveBlock2);
world[terrainHeight[256] - 2][256].add(steveBlock);
game.getInventory().addItem(ItemBlockSupplier.getItem("shears"));
}
private static void initializeWorld(List<Block>[][] world) {

View File

@ -47,7 +47,6 @@ public class EventHandlerProvider {
Set<Method> mineHandlers = reflections.getMethodsAnnotatedWith(MineEventHandler.class);
for (Method method : mineHandlers) {
System.out.println(method);
if (method.getParameterCount() == 4 &&
method.getParameterTypes()[0] == ScreenRenderer.class &&
method.getParameterTypes()[1] == Game.class &&

View File

@ -1,19 +1,22 @@
package cz.jzitnik.game.handlers.place;
import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.BlockDropPercentage;
import cz.jzitnik.game.annotations.PlaceOnSolid;
import cz.jzitnik.game.annotations.ResetDataOnMine;
import cz.jzitnik.game.annotations.*;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
public class CustomAnnotationHandler implements CustomPlaceHandler {
private final Class<?> clazz;
private final DefaultPlaceHandler defaultPlaceHandler = new DefaultPlaceHandler();
private record BlockDrop(String drops, int percentage) {}
public CustomAnnotationHandler(Class<?> clazz) {
this.clazz = clazz;
}
@ -33,15 +36,37 @@ public class CustomAnnotationHandler implements CustomPlaceHandler {
resetDataOnMine(game, x, y);
}
boolean drop = true;
boolean dropDefault = true;
if (clazz.isAnnotationPresent(BlockDropPercentage.class)) {
drop = blockDropPercentage(game, x, y);
if (clazz.isAnnotationPresent(CustomDrops.class) || clazz.isAnnotationPresent(CustomDrop.class)) {
var annotations = clazz.isAnnotationPresent(CustomDrops.class) ?
clazz.getAnnotation(CustomDrops.class).value() :
new CustomDrop[] { clazz.getAnnotation(CustomDrop.class) };
var hashmap = new HashMap<String, BlockDrop>();
for (CustomDrop customDrop : annotations) {
hashmap.put(customDrop.tool(), new BlockDrop(customDrop.drops(), customDrop.percentage()));
}
if (game.getInventory().getItemInHand().isPresent() && hashmap.containsKey(game.getInventory().getItemInHand().get().getId())) {
BlockDrop blockDrop = hashmap.get(game.getInventory().getItemInHand().get().getId());
Random random = new Random();
int num = random.nextInt(100);
if (num < blockDrop.percentage) {
dropDefault = false;
game.getInventory().addItem(ItemBlockSupplier.getItem(blockDrop.drops()));
}
}
}
if (dropDefault && clazz.isAnnotationPresent(BlockDropPercentage.class)) {
dropDefault = blockDropPercentage(game, x, y);
}
defaultPlaceHandler.mine(game, x, y);
return drop;
return dropDefault;
}
private boolean blockDropPercentage(Game game, int x, int y) {

View File

@ -59,7 +59,7 @@ public class PlaceHandler {
for (Class<?> clazz : blocks) {
var annotation = clazz.getAnnotation(BlockRegistry.class);
var id = annotation.value();
if (clazz.isAnnotationPresent(PlaceOnSolid.class) || clazz.isAnnotationPresent(ResetDataOnMine.class) || clazz.isAnnotationPresent(BlockDropPercentage.class)) {
if (clazz.isAnnotationPresent(PlaceOnSolid.class) || clazz.isAnnotationPresent(ResetDataOnMine.class) || clazz.isAnnotationPresent(BlockDropPercentage.class) || clazz.isAnnotationPresent(CustomDrops.class) || clazz.isAnnotationPresent(CustomDrop.class)) {
try {
placeHandlerList.put(id, new CustomAnnotationHandler(clazz));
} catch (Exception e) {

View File

@ -0,0 +1,25 @@
                                                 
                                             
                                         
                                     
                                 
                            
                           
                          
                           
                           
                          
                         
                        
                          
                         
                         
                       
                       
                          
                           
                                 
                                   
                                        
                                            
                                                 

View File

@ -0,0 +1,25 @@