Skip to content

Commit

Permalink
Merge pull request #25 from Rebirth-of-the-Night/fix/ground-spawn
Browse files Browse the repository at this point in the history
Add beneath spawn type, ground spawns top to bottom
  • Loading branch information
SekretOne authored Apr 14, 2021
2 parents 8bc58a9 + 2877e51 commit b5b0459
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
8 changes: 7 additions & 1 deletion docs/invasion/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,19 @@ This defines how the spawner will try to spawn a mob.

key | type | optional | default| description
:---|:---|:---|:---|:---
type | string | yes | config | defines the spawn type, value values are `ground` and `air`
type | string | yes | config | defines the spawn type, value values are `ground`, `air`, and `beneath`
light | int[min, max] | yes | config | defines the light range to spawn the mob in
rangeXZ | int[min, max] | yes | config | defines how far away from the player to try and spawn the mob
rangeY | int | yes | config | defines how far +/- Y to try and spawn the mob
stepRadius | int | yes | config | defines the step radius for the spawn sampler
sampleDistance | int | yes | config | defines the sample distance for the spawn sampler

Spawn Type | Description
---------- | -----------
ground | Spawns on solid ground, preferring higher elevations
air | Spawns in air, preferring the air
beneath | Spawns on solid ground, preferring lower elevations

```json
{
"spawn": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.codetaylor.mc.onslaught.modules.onslaught.invasion.sampler.predicate;

import java.util.function.Predicate;
import net.minecraft.entity.EntityLiving;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;

/** Responsible for testing a pillar of blocks for a valid ground spawn location. */
public class SpawnPredicateBeneath implements Predicate<EntityLiving> {

private final BlockPos.MutableBlockPos blockPos;
private final int lightMin;
private final int lightMax;
private final int verticalRange;

public SpawnPredicateBeneath(int lightMin, int lightMax, int verticalRange) {

super();
this.blockPos = new BlockPos.MutableBlockPos();
this.lightMin = lightMin;
this.lightMax = lightMax;
this.verticalRange = verticalRange;
}

@Override
public boolean test(EntityLiving entity) {

double verticalMin = Math.max(0, -this.verticalRange + entity.posY);
double verticalMax = Math.min(255, this.verticalRange + entity.posY);

for (double y = verticalMin; y <= verticalMax; y++) {
entity.setPosition(entity.posX, y, entity.posZ);

if (this.testEntityPosition(entity)) {
return true;
}
}

return false;
}

private boolean testEntityPosition(EntityLiving entity) {

this.blockPos.setPos(entity.posX, entity.posY, entity.posZ);

if (!entity.world.isBlockLoaded(this.blockPos)) {
return false;
}

// Ensure the light level isn't above the max
int light = entity.world.getLightFromNeighbors(this.blockPos);

if (light < this.lightMin || light > this.lightMax) {
return false;
}

// Ensure we're testing an air block
if (!entity.world.isAirBlock(this.blockPos)) {
return false;
}

// Ensure the entity isn't colliding with blocks or fluids
if (!entity.isNotColliding()) {
return false;
}

// Ensure the block below the entity has a solid top
this.blockPos.move(EnumFacing.DOWN);

if (this.blockPos.getY() < 0 || this.blockPos.getY() > 255) {
return false;
}

return entity.world.isSideSolid(this.blockPos, EnumFacing.UP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public Predicate<EntityLiving> create(InvasionPlayerData.InvasionData.SpawnData
return new SpawnPredicateAir(light[0], light[1], spawnData.rangeY);
}

case beneath:
{
int[] light = Util.evaluateRangeArray(spawnData.light);
return new SpawnPredicateBeneath(light[0], light[1], spawnData.rangeY);
}

default:
throw new IllegalArgumentException("Unknown spawn type: " + spawnData.type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean test(EntityLiving entity) {
double verticalMin = Math.max(0, -this.verticalRange + entity.posY);
double verticalMax = Math.min(255, this.verticalRange + entity.posY);

for (double y = verticalMin; y <= verticalMax; y++) {
for (double y = verticalMax; y >= verticalMin; y--) {
entity.setPosition(entity.posX, y, entity.posZ);

if (this.testEntityPosition(entity)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum EnumSpawnType {
*/

ground,
air
air,
beneath
}

public static class Group {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"type": "air",
"light": [
0,
14
15
],
"rangeXZ": [
4,
Expand Down

0 comments on commit b5b0459

Please sign in to comment.