Skip to content

Commit 7c3d680

Browse files
committed
move safe spawn pos function and use it for bunny dismount logic
1 parent 89b5e44 commit 7c3d680

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/main/java/drzhark/mocreatures/MoCTools.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ public static BlockPos getActualTopSolidOrLiquidBlock(World world, BlockPos pos)
140140
return blockPos;
141141
}
142142

143+
/**
144+
* Finds a safe location to put entities when they are dismounted from a player's head, or being spawned into the world through other means.
145+
*/
146+
public static BlockPos getSafeMobPos(EntityLivingBase entity, BlockPos near, Boolean addHeight) {
147+
int radius = 6;
148+
int maxTries = 24;
149+
BlockPos testing;
150+
for (int i = 0; i < maxTries; i++) {
151+
int x = near.getX() + entity.getEntityWorld().rand.nextInt(radius * 2) - radius;
152+
int z = near.getZ() + entity.getEntityWorld().rand.nextInt(radius * 2) - radius;
153+
int y = entity.getEntityWorld().getHeight(x, z) + (addHeight ? 16 : 0);
154+
testing = new BlockPos(x, y, z);
155+
while (entity.getEntityWorld().isAirBlock(testing) && testing.getY() > 0) {
156+
testing = testing.down(1);
157+
}
158+
IBlockState iblockstate = entity.getEntityWorld().getBlockState(testing);
159+
if (iblockstate.canEntitySpawn(entity)) {
160+
return testing.up(1);
161+
}
162+
}
163+
return null;
164+
}
165+
143166
/**
144167
* spawns tiny slimes
145168
*/
@@ -1228,7 +1251,15 @@ public static void dismountPassengerFromEntity(Entity passenger, Entity entity,
12281251
if (force || (passenger instanceof EntityLivingBase && entity.isSneaking())) {
12291252
System.out.println("Forcing dismount from " + entity + " for passenger " + passenger);
12301253
passenger.dismountRidingEntity();
1231-
passenger.setPositionAndUpdate(entity.posX, entity.posY + 1D, entity.posZ);
1254+
double dist = (-1.5D);
1255+
double newPosX = entity.posX + (dist * Math.sin(((EntityLivingBase) entity).renderYawOffset / 57.29578F));
1256+
double newPosZ = entity.posZ - (dist * Math.cos(((EntityLivingBase) entity).renderYawOffset / 57.29578F));
1257+
BlockPos safeSpawnPos = getSafeMobPos((EntityLivingBase) passenger, new BlockPos(newPosX, entity.posY, newPosZ), false);
1258+
if (safeSpawnPos != null) {
1259+
passenger.setPositionAndUpdate(safeSpawnPos.getX(), safeSpawnPos.getY() + 1D, safeSpawnPos.getZ());
1260+
} else {
1261+
passenger.setPositionAndUpdate(entity.posX, entity.posY + 1D, entity.posZ);
1262+
}
12321263
MoCTools.playCustomSound(passenger, SoundEvents.ENTITY_CHICKEN_EGG);
12331264
if (entity instanceof EntityPlayer) {
12341265
NBTTagCompound tag = entity.getEntityData();

src/main/java/drzhark/mocreatures/event/MoCEventHooks.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void onPopulateVillage(PopulateChunkEvent.Post event) {
7070
if (event.getRand().nextInt(100) < MoCreatures.proxy.kittyVillageChance) {
7171
BlockPos pos = new BlockPos(event.getChunkX() * 16, 100, event.getChunkZ() * 16);
7272
MoCEntityKitty kitty = new MoCEntityKitty(world);
73-
BlockPos spawnPos = getSafeSpawnPos(kitty, pos.add(8, 0, 8));
73+
BlockPos spawnPos = MoCTools.getSafeMobPos(kitty, pos.add(8, 0, 8), true);
7474
if (spawnPos == null || !WorldEntitySpawner.canCreatureTypeSpawnAtLocation(EntityLiving.SpawnPlacementType.ON_GROUND, world, spawnPos))
7575
return;
7676
kitty.onInitialSpawn(world.getDifficultyForLocation(new BlockPos(kitty)), null);
@@ -135,24 +135,4 @@ public void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) {
135135
}
136136
}
137137
}
138-
139-
private BlockPos getSafeSpawnPos(EntityLivingBase entity, BlockPos near) {
140-
int radius = 6;
141-
int maxTries = 24;
142-
BlockPos testing;
143-
for (int i = 0; i < maxTries; i++) {
144-
int x = near.getX() + entity.getEntityWorld().rand.nextInt(radius * 2) - radius;
145-
int z = near.getZ() + entity.getEntityWorld().rand.nextInt(radius * 2) - radius;
146-
int y = entity.getEntityWorld().getHeight(x, z) + 16;
147-
testing = new BlockPos(x, y, z);
148-
while (entity.getEntityWorld().isAirBlock(testing) && testing.getY() > 0) {
149-
testing = testing.down(1);
150-
}
151-
IBlockState iblockstate = entity.getEntityWorld().getBlockState(testing);
152-
if (iblockstate.canEntitySpawn(entity)) {
153-
return testing.up(1);
154-
}
155-
}
156-
return null;
157-
}
158138
}

0 commit comments

Comments
 (0)