Skip to content

Commit

Permalink
Wip directional pipe hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Jan 12, 2025
1 parent 47b1267 commit ac32ace
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@
import com.wenxin2.marioverse.inventory.WarpPipeMenu;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
Expand All @@ -45,9 +40,7 @@
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.DirectionalBlock;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand Down
101 changes: 89 additions & 12 deletions src/main/java/com/wenxin2/marioverse/entities/PiranhaPlantEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wenxin2.marioverse.entities;

import com.wenxin2.marioverse.blocks.ClearWarpPipeBlock;
import com.wenxin2.marioverse.entities.ai.goals.NearestAttackableTagGoal;
import com.wenxin2.marioverse.init.ConfigRegistry;
import com.wenxin2.marioverse.init.DamageSourceRegistry;
Expand All @@ -8,6 +9,7 @@
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
Expand Down Expand Up @@ -35,6 +37,7 @@
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.FlowerPotBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -225,7 +228,7 @@ public boolean canBeLeashed() {
@NotNull
@Override
protected Vec3 getLeashOffset() {
return new Vec3(0.0, this.getEyeHeight() - 0.75D, this.getBbWidth() * 0.7F);
return new Vec3(0.0, this.getEyeHeight() / 1.75, this.getBbWidth() / 2);
}

private float currentScale = 1.0F;
Expand All @@ -240,8 +243,14 @@ public void hideInBlock() {
BlockPos pos = this.blockPosition();
BlockPos posAbove = pos.above();
BlockPos posBelow = pos.below();
BlockState state = world.getBlockState(pos);
BlockState stateAbove = world.getBlockState(posAbove);
BlockState stateBelow = world.getBlockState(posBelow);
double speed = 0.02;

if (scaleCooldown > 0)
scaleCooldown--;

if (!isLerping && scaleCooldown == 0) {
if (this.isHiding())
targetScale = 0.3F;
Expand All @@ -266,39 +275,107 @@ public void hideInBlock() {

}

if (scaleCooldown > 0)
scaleCooldown--;

if (this.isHiding() && !world.getBlockState(pos).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !world.getBlockState(posBelow).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)) {
if (this.isHiding() && !state.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !stateBelow.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !state.hasProperty(BlockStateProperties.FACING)
&& !stateBelow.hasProperty(BlockStateProperties.FACING)) {
this.stopHiding();
return;
}

Direction[] prioritizedDirections = new Direction[]{Direction.UP, Direction.DOWN, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST};

if (this.isHiding()) {
// Handle emerging from hiding
for (Direction direction : prioritizedDirections) {
BlockPos offsetPos = pos.relative(direction.getOpposite());
BlockState offsetState = world.getBlockState(offsetPos);

// Check if the block has a FACING property or proceed without it
if (state.hasProperty(BlockStateProperties.FACING)
&& state.getValue(BlockStateProperties.FACING) == direction
&& state.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE) && !offsetState.isSolid()) {

// Check if it's safe to emerge
if (world.getGameTime() % ConfigRegistry.PIRANHA_PLANT_HIDE_DURATION.get() == 0L) {
double deltaX = offsetPos.getX() - this.getX();
double deltaY = offsetPos.getY() - this.getY();
double deltaZ = offsetPos.getZ() - this.getZ();
double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);

this.setNoGravity(false);
this.noPhysics = false;
if (distance > 0) {
this.setDeltaMovement((deltaX / distance) * speed, (deltaY / distance) * speed, (deltaZ / distance) * speed);
this.move(MoverType.SELF, this.getDeltaMovement());
}
this.stopHiding();
return;
}
}

if (!state.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !offsetState.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)) {
this.stopHiding();
return;
}
}
} else {
// Handle hiding logic
for (Direction direction : prioritizedDirections) {
BlockPos offsetPos = pos.relative(direction.getOpposite());
BlockState offsetState = world.getBlockState(offsetPos);

// Check if the block has a FACING property or proceed without it
if (offsetState.hasProperty(BlockStateProperties.FACING)
&& offsetState.getValue(BlockStateProperties.FACING) == direction
&& offsetState.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)) {

// Check if it's safe to hide
if (world.getGameTime() % ConfigRegistry.PIRANHA_PLANT_HIDE_DURATION.get() == 0L && !isLerping && scaleCooldown == 0) {
double deltaX = offsetPos.getX() - this.getX();
double deltaY = offsetPos.getY() - this.getY();
double deltaZ = offsetPos.getZ() - this.getZ();
// double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
double distance = Math.abs(deltaY);;

if (distance > 0) {
this.setDeltaMovement(0, (deltaY / distance) * speed, 0);
// this.setDeltaMovement((deltaX / distance) * speed, (deltaY / distance) * speed, (deltaZ / distance) * speed);
}
this.setNoGravity(true);
this.noPhysics = true;
this.tryToHide();
}
}
}
}

if (this.isHiding() && !state.hasProperty(BlockStateProperties.FACING)) {
if (world.getGameTime() % ConfigRegistry.PIRANHA_PLANT_HIDE_DURATION.get() == 0L
&& world.getBlockState(pos).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !world.getBlockState(posAbove).isSolid()) {
&& state.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !stateAbove.isSolid()) {
double deltaYAbove = posAbove.getY() - this.getY();
double distanceAbove = Math.abs(deltaYAbove);

this.setNoGravity(false);
this.noPhysics = false;
if (distanceAbove > 0) {
this.setDeltaMovement(0, 0.3, 0);
if (state.getBlock() instanceof ClearWarpPipeBlock)
this.setDeltaMovement(0, 0.8, 0);
else this.setDeltaMovement(0, 0.3, 0);
this.move(MoverType.SELF, this.getDeltaMovement());
}
this.stopHiding();
}
} else if (world.getGameTime() % ConfigRegistry.PIRANHA_PLANT_HIDE_DURATION.get() == 0L
&& world.getBlockState(posBelow).is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE) && !isLerping && scaleCooldown == 0) {
&& stateBelow.is(TagRegistry.PIRANHA_PLANTS_CAN_HIDE)
&& !stateBelow.hasProperty(BlockStateProperties.FACING) && !isLerping && scaleCooldown == 0) {
double deltaYBelow = posBelow.getY() - this.getY();
double distanceBelow = Math.abs(deltaYBelow);

if (distanceBelow > 0)
this.setDeltaMovement(0, (deltaYBelow / distanceBelow) * speed, 0);
// this.setDeltaMovement(Vec3.ZERO);
// this.moveTo(posBelow, this.getYRot(), this.getXRot());

this.setNoGravity(true);
this.noPhysics = true;
Expand Down

0 comments on commit ac32ace

Please sign in to comment.