Skip to content

Commit

Permalink
Omg meow
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Feb 15, 2025
1 parent 2cb2d2d commit 04db130
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.fluid.Fluids;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtHelper;
Expand All @@ -23,10 +24,12 @@
import net.minecraft.server.network.EntityTrackerEntry;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.property.Properties;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class LevitatingBlockEntity extends Entity {
protected static final TrackedData<BlockPos> BLOCK_POS = DataTracker.registerData(LevitatingBlockEntity.class, TrackedDataHandlerRegistry.BLOCK_POS);
Expand All @@ -36,6 +39,7 @@ public class LevitatingBlockEntity extends Entity {
private BlockState blockState = Blocks.STONE.getDefaultState();

public BlockEntity cachedBlockEntity;
public boolean shouldRevertNow = false;

public LevitatingBlockEntity(EntityType<?> type, World world) {
super(type, world);
Expand Down Expand Up @@ -110,10 +114,12 @@ public void tick() {
}
}

this.tickCollisions();

if (!this.getWorld().isClient) {
BlockPos blockPos = this.getBlockPos();
BlockPos blockPos = BlockPos.ofFloored(this.getPos().add(0, 0.5, 0));

if (this.getWeight() >= 1 && isOnGround() && getWorld().getBlockState(blockPos).isReplaceable()) {
if (this.getWeight() >= 1 && (isOnGround() || shouldRevertNow) && getWorld().getBlockState(blockPos).isReplaceable()) {
var isWater = getWorld().getFluidState(blockPos).isOf(Fluids.WATER);
var isWaterLoggable = this.blockState.contains(Properties.WATERLOGGED);

Expand Down Expand Up @@ -159,6 +165,29 @@ public void tick() {
}
}

public void tickCollisions() {
var velocity = this.getVelocity();
var currentPos = this.getPos();
var nextPos = currentPos.add(velocity);

var hit = getEntityCollision(currentPos, nextPos);
if (hit != null) {
hit.getEntity().setVelocity(hit.getEntity().getVelocity().add(velocity));
this.setVelocity(velocity.multiply(0.5));
}
}

protected boolean canHit(Entity entity) {
return entity.canBeHitByProjectile();
}

@Nullable
protected EntityHitResult getEntityCollision(Vec3d currentPosition, Vec3d nextPosition) {
return ProjectileUtil.getEntityCollision(
this.getWorld(), this, currentPosition, nextPosition, this.getBoundingBox().stretch(this.getVelocity()).expand(1.0), this::canHit
);
}

@Override
public Packet<ClientPlayPacketListener> createSpawnPacket(EntityTrackerEntry entityTrackerEntry) {
return new EntitySpawnS2CPacket(this, entityTrackerEntry, Block.getRawIdFromState(blockState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public Fragment change(SpellContext ctx, EntityFragment target, NumberFragment n
ctx.useMana(this, (float) (60 * (1 - weight)));

levitatingBlock.setWeight((float) weight);
levitatingBlock.shouldRevertNow = weight >= 1;

ModEntityComponents.GRACE.get(entity).triggerGrace("weight", 20);
} else {
if (!(entity instanceof LivingEntity)) {
Expand Down

0 comments on commit 04db130

Please sign in to comment.