Skip to content

Commit

Permalink
Fix hiding in block
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Jan 7, 2025
1 parent f0817da commit 0e57b32
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 165 deletions.
152 changes: 73 additions & 79 deletions src/main/java/com/wenxin2/marioverse/entities/PiranhaPlantEntity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.wenxin2.marioverse.entities;

import com.wenxin2.marioverse.entities.ai.goals.NearestAttackableTagGoal;
import com.wenxin2.marioverse.entities.ai.goals.PiranhaPlantHideInBlockGoal;
import com.wenxin2.marioverse.init.DamageSourceRegistry;
import com.wenxin2.marioverse.init.SoundRegistry;
import com.wenxin2.marioverse.init.TagRegistry;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
Expand All @@ -26,6 +26,7 @@
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.AirBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
Expand Down Expand Up @@ -72,6 +73,11 @@ protected SoundEvent getDeathSound() {
return SoundRegistry.GOOMBA_STOMP.get();
}

@Override
public int getAmbientSoundInterval() {
return 120;
}

@Override
protected void playStepSound(BlockPos pos, BlockState state) {
this.playSound(SoundRegistry.GOOMBA_STEP.get(), 1.0F, 1.0F);
Expand All @@ -85,7 +91,6 @@ protected void defineSynchedData(SynchedEntityData.Builder builder) {

@Override
protected void registerGoals() {
this.goalSelector.addGoal(0, new PiranhaPlantHideInBlockGoal(this, 40, 40));
this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 0.6D, true));
this.targetSelector.addGoal(0, new NearestAttackableTagGoal(this, TagRegistry.PIRANHA_PLANT_CAN_ATTACK, true));
this.targetSelector.addGoal(1, new HurtByTargetGoal(this).setAlertOthers());
Expand Down Expand Up @@ -119,7 +124,7 @@ protected <E extends GeoAnimatable> PlayState walkAnimController(final Animation
protected <E extends GeoAnimatable> PlayState hideAnimController(final AnimationState<E> event) {
if (this.isHiding()) {
event.setAndContinue(HIDE_ANIM);
} else if (this.level().getBlockState(this.blockPosition().below()).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE))
} else if (this.level().getBlockState(this.blockPosition()).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE))
event.setAndContinue(EMERGE_ANIM);
return PlayState.CONTINUE;
}
Expand All @@ -145,85 +150,26 @@ public AnimatableInstanceCache getAnimatableInstanceCache() {
}

@Override
public boolean isPushable() {
return false;
}

// @Override
// public boolean canBeCollidedWith() {
// return !this.isHiding();
// }

@Override
public boolean isInWall() {
if (isHiding()) {
return false;
} else return super.isInWall();
}

@Override
protected boolean wouldNotSuffocateAtTargetPose(Pose pose) {
AABB aabb = this.getDimensions(pose).makeBoundingBox(this.position());
return this.level().noBlockCollision(this, aabb) || this.isHiding();
public void readAdditionalSaveData(CompoundTag tag) {
super.readAdditionalSaveData(tag);
this.entityData.set(DATA_ID_HIDE_FLAGS, tag.getByte("HideFlags"));
}

@NotNull
@Override
protected AABB makeBoundingBox() {
AABB originalBoundingBox = super.makeBoundingBox();

if (isHiding()) {
double height = originalBoundingBox.getYsize() * 0.5;
return new AABB(originalBoundingBox.minX, originalBoundingBox.minY, originalBoundingBox.minZ,
originalBoundingBox.maxX, originalBoundingBox.maxY * 0.5, originalBoundingBox.maxZ);
} else return super.makeBoundingBox();
}

public boolean isHiding() {
return this.getHideFlag(8)/* && this.getPersistentData().getInt("marioverse:piranha_plant_hide_cooldown") > 0*/;
public void addAdditionalSaveData(CompoundTag tag) {
super.addAdditionalSaveData(tag);
tag.putByte("HideFlags", this.entityData.get(DATA_ID_HIDE_FLAGS));
}

@Override
public void tick() {

super.tick();
this.checkForCollisions();
double targetY;

// int hideCooldown = this.getPersistentData().getInt("marioverse:piranha_plant_hide_cooldown");
// if (hideCooldown > 0) {
// --hideCooldown;
// }
this.hideInBlock();

if (this.isInWaterOrBubble())
this.ejectPassengers();

if (this.isHiding()) {
this.noPhysics = true;
this.setNoGravity(true);
} else {
this.noPhysics = false;
this.setNoGravity(false);
}

// if (!this.isHiding()
// && this.level().getBlockState(this.blockPosition().below()).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)) {
// targetY = this.blockPosition().getY() - 1;
// this.tryToHide();
// this.getPersistentData().putInt("marioverse:piranha_plant_hide_cooldown", 40);
// } else {
// targetY = this.blockPosition().getY();
// }
//
// double currentY = this.getY();
// double speed = 0.1;
//
// if (Math.abs(targetY - currentY) > speed) {
// double direction = targetY > currentY ? speed : -speed;
// this.setDeltaMovement(0, direction, 0);
// } else {
// this.setDeltaMovement(0, 0, 0);
// this.setPos(this.getX(), targetY, this.getZ());
// }
}

@Override
Expand All @@ -242,21 +188,40 @@ public static boolean checkPiranhaPlantSpawnRules(EntityType<? extends Monster>
}

@Override
public int getAmbientSoundInterval() {
return 120;
}

protected void handleAirSupply(int airSupplyAmount) {
if (this.isAlive() && this.isInWaterOrBubble()) {
this.setAirSupply(airSupplyAmount);
}
public boolean isPushable() {
return false;
}

@Override
public boolean isPushedByFluid() {
return false;
}

@Override
public boolean isInWall() {
if (isHiding()) {
return false;
} else return super.isInWall();
}

@Override
protected boolean wouldNotSuffocateAtTargetPose(Pose pose) {
AABB aabb = this.getDimensions(pose).makeBoundingBox(this.position());
return this.level().noBlockCollision(this, aabb) || this.isHiding();
}

@NotNull
@Override
protected AABB makeBoundingBox() {
AABB originalBoundingBox = super.makeBoundingBox();

if (isHiding()) {
double height = originalBoundingBox.getYsize() * 0.5;
return new AABB(originalBoundingBox.minX, originalBoundingBox.minY, originalBoundingBox.minZ,
originalBoundingBox.maxX, originalBoundingBox.maxY * 0.5, originalBoundingBox.maxZ);
} else return super.makeBoundingBox();
}

@Override
public boolean canBeLeashed() {
return true;
Expand All @@ -268,12 +233,31 @@ protected Vec3 getLeashOffset() {
return new Vec3(0.0, this.getEyeHeight() - 0.5D, this.getBbWidth() * 0.4F);
}

public void hideInBlock() {
BlockPos pos = this.blockPosition();

if (this.isHiding()) {
this.setNoGravity(true);
if (this.level().getGameTime() % 262L == 0L
&& this.level().getBlockState(pos).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& this.level().getBlockState(pos.above()).getBlock() instanceof AirBlock) {
this.setPos(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5);
this.stopHiding();
}
} else if (this.level().getGameTime() % 262L == 0L
&& this.level().getBlockState(pos.below()).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)) {
this.setNoGravity(false);
this.tryToHide();
this.setPos(pos.getX() + 0.5, pos.getY() - 1, pos.getZ() + 0.5);
}
}

public void checkForCollisions() {
List<Entity> nearbyEntities = this.level().getEntities(this,
this.getBoundingBox().inflate(0.15D), entity -> !entity.isSpectator()
&& entity instanceof LivingEntity && !(entity instanceof PiranhaPlantEntity));

if (!nearbyEntities.isEmpty()) {
if (!nearbyEntities.isEmpty() && this.isHiding()) {
for (Entity collidingEntity : nearbyEntities) {
if (collidingEntity instanceof PiranhaPlantEntity
|| !(collidingEntity.getType().is(TagRegistry.PIRANHA_PLANT_CAN_ATTACK)))
Expand All @@ -286,6 +270,16 @@ public void checkForCollisions() {
}
}

protected void handleAirSupply(int airSupplyAmount) {
if (this.isAlive() && this.isInWaterOrBubble()) {
this.setAirSupply(airSupplyAmount);
}
}

public boolean isHiding() {
return this.getHideFlag(8);
}

public void hide(boolean isHiding) {
this.setHideFlag(8, isHiding);
}
Expand Down

This file was deleted.

0 comments on commit 0e57b32

Please sign in to comment.