Skip to content

Commit 68d228c

Browse files
committed
extract common utilities
1 parent fdcebd0 commit 68d228c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.steve.ai.util;
2+
3+
import com.steve.ai.entity.SteveEntity;
4+
import net.minecraft.core.registries.BuiltInRegistries;
5+
import net.minecraft.resources.ResourceLocation;
6+
import net.minecraft.world.entity.player.Player;
7+
import net.minecraft.world.level.block.Block;
8+
import net.minecraft.world.level.block.Blocks;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Common utility methods used across multiple action classes
14+
*/
15+
public class ActionUtils {
16+
17+
/**
18+
* Find the nearest player to a Steve entity
19+
*
20+
* @param steve The Steve entity
21+
* @return The nearest player, or null if no players found
22+
*/
23+
public static Player findNearestPlayer(SteveEntity steve) {
24+
List<? extends Player> players = steve.level().players();
25+
26+
if (players.isEmpty()) {
27+
return null;
28+
}
29+
30+
Player nearest = null;
31+
double nearestDistance = Double.MAX_VALUE;
32+
33+
for (Player player : players) {
34+
if (!player.isAlive() || player.isRemoved() || player.isSpectator()) {
35+
continue;
36+
}
37+
38+
double distance = steve.distanceTo(player);
39+
if (distance < nearestDistance) {
40+
nearest = player;
41+
nearestDistance = distance;
42+
}
43+
}
44+
45+
return nearest;
46+
}
47+
48+
/**
49+
* Parse a block name string into a Block instance
50+
* Handles common resource names and aliases
51+
*
52+
* @param blockName The block name (e.g., "iron_ore", "diamond", "minecraft:stone")
53+
* @return The Block instance, or Blocks.AIR if not found
54+
*/
55+
public static Block parseBlock(String blockName) {
56+
blockName = blockName.toLowerCase().replace(" ", "_");
57+
58+
// Add minecraft namespace if not present
59+
if (!blockName.contains(":")) {
60+
blockName = "minecraft:" + blockName;
61+
}
62+
63+
ResourceLocation resourceLocation = new ResourceLocation(blockName);
64+
Block block = BuiltInRegistries.BLOCK.get(resourceLocation);
65+
return block != null ? block : Blocks.AIR;
66+
}
67+
}

0 commit comments

Comments
 (0)