Skip to content

Commit

Permalink
clean sitting up a bit, and fix a possible crash
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlikePaladin committed Dec 28, 2023
1 parent 205c317 commit 31c39a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
}

List<ChairEntity> active = world.getEntitiesByClass(ChairEntity.class, new Box(pos), Entity::hasPassengers);
if (active == null)
return ActionResult.FAIL;

List<Entity> hasPassenger = new ArrayList<>();
active.forEach(chairEntity -> hasPassenger.add(chairEntity.getFirstPassenger()));
if (!active.isEmpty() && hasPassenger.stream().anyMatch(Entity::isPlayer)) {
Expand Down Expand Up @@ -161,10 +164,10 @@ public ActionResult sitEntity(World world, BlockPos pos, BlockState state, Entit
public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
super.onEntityCollision(state, world, pos, entity);
List<ChairEntity> active = world.getEntitiesByClass(ChairEntity.class, new Box(pos), Entity::hasPassengers);
if (!active.isEmpty())
if (active == null || !active.isEmpty())
return;

if (entity instanceof PlayerEntity || entity instanceof IronGolemEntity || entity instanceof AbstractMinecartEntity || entity.hasVehicle() || !(entity instanceof LivingEntity)) {
if (entity instanceof PlayerEntity || entity instanceof IronGolemEntity || entity instanceof AbstractMinecartEntity || entity.hasVehicle() || !(entity instanceof LivingEntity) || entity instanceof ChairEntity) {
return;
}
if (!PaladinFurnitureMod.getPFMConfig().doMobsSitOnChairs())
Expand Down
36 changes: 14 additions & 22 deletions common/src/main/java/com/unlikepaladin/pfm/entity/ChairEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.unlikepaladin.pfm.blocks.ToiletState;
import com.unlikepaladin.pfm.client.PaladinFurnitureModClient;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Dismounting;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
Expand All @@ -16,8 +15,6 @@
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -85,35 +82,30 @@ public boolean isPushedByFluids() {
@Override
public Vec3d updatePassengerForDismount(LivingEntity passenger) {
Direction direction = this.getMovementDirection();
if (direction.getAxis() == Direction.Axis.Y) {
return super.updatePassengerForDismount(passenger);
} else {
int[][] is = Dismounting.getDismountOffsets(direction);
BlockPos blockPos = this.getBlockPos();
BlockPos.Mutable mutable = new BlockPos.Mutable();
UnmodifiableIterator var6 = passenger.getPoses().iterator();
if (this.world.getBlockState(this.getBlockPos()).getBlock() instanceof AbstractSittableBlock) {
direction = this.world.getBlockState(this.getBlockPos()).get(AbstractSittableBlock.FACING).getOpposite();
}
if (direction.getAxis() != Direction.Axis.Y) {
int[][] dismountingOffsets = Dismounting.getDismountOffsets(direction);
BlockPos chairPos = this.getBlockPos();
BlockPos.Mutable dismountPos = new BlockPos.Mutable();

while(var6.hasNext()) {
EntityPose entityPose = (EntityPose)var6.next();
for (EntityPose entityPose : passenger.getPoses()) {
Box box = passenger.getBoundingBox(entityPose);
int[][] var9 = is;
int var10 = is.length;

for(int var11 = 0; var11 < var10; ++var11) {
int[] js = var9[var11];
mutable.set(blockPos.getX() + js[0], blockPos.getY() + 0.3, blockPos.getZ() + js[1]);
double d = this.world.getDismountHeight(mutable);
if (Dismounting.canDismountInBlock(d)) {
Vec3d vec3d = Vec3d.ofCenter(mutable, d);
for (int[] dismountingOffset : dismountingOffsets) {
dismountPos.set(chairPos.getX() + dismountingOffset[0], chairPos.getY() + 0.3, chairPos.getZ() + dismountingOffset[1]);
double dismountHeight = this.world.getDismountHeight(dismountPos);
if (Dismounting.canDismountInBlock(dismountHeight)) {
Vec3d vec3d = Vec3d.ofCenter(dismountPos, dismountHeight);
if (Dismounting.canPlaceEntityAt(this.world, passenger, box.offset(vec3d))) {
passenger.setPose(entityPose);
return vec3d;
}
}
}
}
return super.updatePassengerForDismount(passenger);
}
return super.updatePassengerForDismount(passenger);
}

public static DefaultAttributeContainer.Builder createMobAttributes(){
Expand Down

0 comments on commit 31c39a3

Please sign in to comment.