Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.EntityCollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.satisfy.farm_and_charm.core.registry.TagRegistry;
Expand All @@ -28,6 +29,15 @@
import java.util.List;

public class CattlegridBlock extends Block {
private static final VoxelShape FLOOR_COLLISION = Shapes.block();
private static final VoxelShape MOB_WALLS = Shapes.or(
Block.box(0.0, 16.0, 0.0, 16.0, 28.0, 2.0),
Block.box(0.0, 16.0, 14.0, 16.0, 28.0, 16.0),
Block.box(0.0, 16.0, 0.0, 2.0, 28.0, 16.0),
Block.box(14.0, 16.0, 0.0, 16.0, 28.0, 16.0)
);
private static final VoxelShape MOB_COLLISION = Shapes.or(FLOOR_COLLISION, MOB_WALLS);

public CattlegridBlock(Properties properties) {
super(properties);
}
Expand All @@ -54,8 +64,7 @@ public void stepOn(Level level, BlockPos pos, BlockState state, Entity entity) {
return;
}

if (entity instanceof Mob mob) {
mob.makeStuckInBlock(state, new Vec3(0.0, 1.0, 0.0));
if (entity instanceof Mob) {
return;
}

Expand All @@ -71,7 +80,16 @@ protected boolean isPathfindable(BlockState state, PathComputationType type) {

@Override
public @NotNull VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return Shapes.block();
if (!(context instanceof EntityCollisionContext entityContext)) {
return MOB_COLLISION;
}

Entity entity = entityContext.getEntity();
if (!(entity instanceof Mob) || entity.getType().is(TagRegistry.CAN_WALK_OVER_CATTLEGRID)) {
return FLOOR_COLLISION;
}

return MOB_COLLISION;
}

@Override
Expand All @@ -86,4 +104,4 @@ public void appendHoverText(ItemStack itemStack, Item.TooltipContext tooltipCont
tooltip.add(Component.translatable("tooltip.farm_and_charm.tooltip_information.hold", shiftKey).withStyle(Style.EMPTY.withColor(TextColor.fromRgb(earthyColor))));
}
}
}
}