Skip to content

Commit 8fcfe67

Browse files
feat: new methods on world
1 parent 4fb2e70 commit 8fcfe67

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

Diff for: CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Kube Utils Changelog
22

3+
## [21.1.1]
4+
5+
### Added
6+
7+
- `findFirstBlockTagWithinRadius` to find a block within a radius that has a specific block tag.
8+
- `findAnyEntitiesWithinRadius` to find any entities within a radius based on an entity type.
9+
- `findLivingEntitiesWithinRadius` to find living entities within a radius based on an entity type.
10+
11+
### Deprecated
12+
13+
- `findEntitiesWithinRadius` in favour of `findLivingEntitiesWithinRadius`
14+
315
## [21.1.0]
416

517
### Changed

Diff for: docs/modules/level.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ kuLevel.spawnStructure("minecraft:structures/village_1", BlockPos.ZERO);
3131
This method fails softly meaning if the structure does not exist, the method will simply do nothing.
3232

3333
### `findEntitiesWithinRadius(entityId: EntityType, start: BlockPos, range: Number)`
34+
### `Deprecated: Use findLivingEntitiesWithinRadius`
3435

35-
Find entities within a radius based on an entity type. The entity type would typically look like `minecraft:skeleton`. You can typically find modded ones from the mods Java code.
36+
Find living entities within a radius based on an entity type. The entity type would typically look like `minecraft:skeleton`. You can typically find modded ones from the mods Java code.
3637

3738
The search radius is from and expanded cube from the start block position. This means that a range of 1 would be a 3x3 cube with the start position in the middle.
3839

@@ -44,6 +45,35 @@ const entities = kuLevel.findEntitiesWithinRadius("minecraft:skeleton", BlockPos
4445
console.log(entities)
4546
```
4647

48+
### `findLivingEntitiesWithinRadius(entityId: EntityType, start: BlockPos, range: Number)`
49+
50+
Find living entities within a radius based on an entity type. The entity type would typically look like `minecraft:skeleton`. You can typically find modded ones from the mods Java code.
51+
52+
The search radius is from and expanded cube from the start block position. This means that a range of 1 would be a 3x3 cube with the start position in the middle.
53+
54+
`@return net.minecraft.world.entity.LivingEntity[]`
55+
56+
```javascript
57+
const kuLevel = new Ku.Level(...);
58+
const entities = kuLevel.findLivingEntitiesWithinRadius("minecraft:skeleton", BlockPos.ZERO, 4);
59+
console.log(entities)
60+
```
61+
62+
### `findAnyEntitiesWithinRadius(entityId: EntityType, start: BlockPos, range: Number)`
63+
64+
Find any entities within a radius based on an entity type. The entity type would typically look like `minecraft:skeleton`. You can typically find modded ones from the mods Java code.
65+
66+
The search radius is from and expanded cube from the start block position. This means that a range of 1 would be a 3x3 cube with the start position in the middle.
67+
68+
`@return net.minecraft.world.entity.Entity[]`
69+
70+
```javascript
71+
const kuLevel = new Ku.Level(...);
72+
const entities = kuLevel.findAnyEntitiesWithinRadius("minecraft:display", BlockPos.ZERO, 4);
73+
console.log(entities)
74+
```
75+
76+
4777
### `findBlockWithinRadius(blockstate: BlockState, start: BlockPos, range: Number, absolute: boolean)`
4878

4979
Find blocks within a radius that match a given state. Depending on if the `absolute` flag is set to true will depend on if we compare the default state of the block or the absolute state of the block meaning the states **must** match.
@@ -66,6 +96,12 @@ Very much the same as `findBlockWithinRadius` but instead will find the first bl
6696

6797
`@return BlockPos | null`
6898

99+
### `findFirstBlockTagWithinRadius(tag: TagKey<Block>, start: BlockPos, range: number)`
100+
101+
Very much the same as `findBlockWithinRadius` but instead of a block state we use a block tag to find the block.
102+
103+
`@return BlockPos | null`
104+
69105
### `getRandomLocation(start: BlockPos, min: Number, max: Number)`
70106

71107
Generate a random BlockPos within a min and max range.

Diff for: gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ minecraft_version=1.21.1
77
neoforge_version=55
88
neoforge_full_version=21.1.55
99
neoforge_loader_version=4
10-
mod_version=0
10+
mod_version=1
1111
neo_version_range=[21.1.0-beta,)
1212

1313
maven_group=pro.mikey.mods
@@ -18,4 +18,4 @@ mod_author=ErrorMikey/Mikey
1818
curseforge_id=678815
1919
modrinth_id=JHdL51WR
2020

21-
kubejs_version=2101.7.0-build.131
21+
kubejs_version=2101.7.2-build.216

Diff for: src/main/java/pro/mikey/kubeutils/kubejs/modules/LevelKu.java

+64-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.core.registries.Registries;
88
import net.minecraft.resources.ResourceLocation;
99
import net.minecraft.server.level.ServerLevel;
10+
import net.minecraft.tags.TagKey;
1011
import net.minecraft.util.Mth;
1112
import net.minecraft.world.entity.Entity;
1213
import net.minecraft.world.entity.LivingEntity;
@@ -23,6 +24,7 @@
2324
import java.util.*;
2425
import java.util.function.Predicate;
2526

27+
@SuppressWarnings("unused")
2628
public class LevelKu {
2729
private static final ResourceLocation UNKNOWN = KubeUtils.id("unknown");
2830
private final ServerLevel level;
@@ -56,13 +58,48 @@ public void spawnStructure(String structureFile, BlockPos spawnLocation) {
5658
* @param range the range to select entities within
5759
*
5860
* @return the list of entities found.
61+
* @deprecated Use {@link #findLivingEntitiesWithinRadius(ResourceLocation, BlockPos, int)}
5962
*/
63+
@Deprecated
6064
public List<LivingEntity> findEntitiesWithinRadius(ResourceLocation entityId, BlockPos start, int range) {
65+
return _findEntityInRange(LivingEntity.class, entityId, start, range)
66+
.stream()
67+
.map(e -> (LivingEntity) e)
68+
.toList();
69+
}
70+
71+
/**
72+
* Find living entities within a radius based on an entity type
73+
*
74+
* @param entityId the entity resource location (id)
75+
* @param start the starting position to build the bounding box from
76+
* @param range the range to select entities within
77+
*
78+
* @return the list of entities found.
79+
*/
80+
public List<LivingEntity> findLivingEntitiesWithinRadius(ResourceLocation entityId, BlockPos start, int range) {
81+
return findEntitiesWithinRadius(entityId, start, range);
82+
}
83+
84+
/**
85+
* Find any entities within a radius based on an entity type
86+
*
87+
* @param entityId the entity resource location (id)
88+
* @param start the starting position to build the bounding box from
89+
* @param range the range to select entities within
90+
*
91+
* @return the list of entities found.
92+
*/
93+
public List<Entity> findAnyEntitiesWithinRadius(ResourceLocation entityId, BlockPos start, int range) {
94+
return _findEntityInRange(Entity.class, entityId, start, range);
95+
}
96+
97+
private List<Entity> _findEntityInRange(Class<? extends Entity> entityClass, ResourceLocation entityId, BlockPos start, int range) {
6198
AABB boundingBox = new AABB(start).inflate(range);
6299

63-
List<LivingEntity> entities = new ArrayList<>();
100+
List<Entity> entities = new ArrayList<>();
64101
for (Entity current : level.getEntities().getAll()) {
65-
if (!(current instanceof LivingEntity)) {
102+
if (!entityClass.isInstance(current)) {
66103
continue;
67104
}
68105

@@ -74,7 +111,7 @@ public List<LivingEntity> findEntitiesWithinRadius(ResourceLocation entityId, Bl
74111
}
75112

76113
if (boundingBox.contains(current.position())) {
77-
entities.add((LivingEntity) current);
114+
entities.add(current);
78115
}
79116
}
80117

@@ -131,6 +168,30 @@ public BlockPos findSingleBlockWithinRadius(BlockState block, BlockPos start, in
131168
return null;
132169
}
133170

171+
/**
172+
* The same as {@link #findSingleBlockWithinRadius(BlockState, BlockPos, int, boolean)} but instead of a block state
173+
* we use a block tag to find the block.
174+
*
175+
* @param blockTag the block tag we're looking for
176+
* @param start the starting position
177+
* @param range the radius to build from the starting position
178+
*
179+
* @return the position of the block found or null when nothing is found.
180+
*/
181+
@Nullable
182+
public BlockPos findFirstBlockTagWithinRadius(TagKey<Block> blockTag, BlockPos start, int range) {
183+
Iterator<BlockPos> iterator = BlockPos.betweenClosedStream(new BoundingBox(start).inflatedBy(range)).iterator();
184+
185+
while (iterator.hasNext()) {
186+
var current = iterator.next();
187+
if (level.getBlockState(current).is(blockTag)) {
188+
return current.immutable();
189+
}
190+
}
191+
192+
return null;
193+
}
194+
134195
/**
135196
* Generate a random location as a {@link BlockPos} at within two given bounds.
136197
*

0 commit comments

Comments
 (0)